填充作为指针传递给函数的结构数组

时间:2018-11-12 00:48:31

标签: go google-cloud-platform google-cloud-datastore

我想使用Google Cloud Platform数据存储进行数据分页,并且我在GCP页面(https://cloud.google.com/datastore/docs/concepts/queries)上找到了一个使用游标进行此操作的示例,它的工作原理绝对不错。

Google提供的示例已对变量var tasks []Taskvar task Task进行了硬编码,我想创建一个 reusable 函数,通过该函数可以将指针通过传递给结构数组键入interface{}的参数,并使用此函数填充此结构。例如:

    type MyStruct1 struct {
        F1 string
    }

    type MyStruct2 struct {
        F1 int
    }

    func list(ctx context.Context, cursorStr string, data interface{}) {
    ...
    }

    func main() {
        mystruct1 := make([]MyStruct1, 0)
        list(ctx, "", &mystruct1)

        mystruct2 := make([]MyStruct2, 0)
        list(ctx, "", &mystruct2)
    }

当我需要在此函数中创建一个用于存储记录的变量,然后将其附加到作为指针传递的struct数组上时,我的问题就开始了。

来自Google的示例

func SnippetIterator_Cursor() {
    ctx := context.Background()
    client, _ := datastore.NewClient(ctx, "my-proj")
    cursorStr := ""
    // [START datastore_cursor_paging]
    const pageSize = 5
    query := datastore.NewQuery("Tasks").Limit(pageSize)
    if cursorStr != "" {
        cursor, err := datastore.DecodeCursor(cursorStr)
        if err != nil {
            log.Fatalf("Bad cursor %q: %v", cursorStr, err)
        }
        query = query.Start(cursor)
    }

    // Read the tasks.
    var tasks []Task << THIS IS WHAT I WANT TO BE GENERIC 
    var task Task. << THIS IS WHAT I WANT TO BE GENERIC 
    it := client.Run(ctx, query)
    _, err := it.Next(&task)
    for err == nil {
        tasks = append(tasks, task)
        _, err = it.Next(&task)
    }
    if err != iterator.Done {
        log.Fatalf("Failed fetching results: %v", err)
    }

    // Get the cursor for the next page of results.
    nextCursor, err := it.Cursor()
    // [END datastore_cursor_paging]
    _ = err        // Check the error.
    _ = nextCursor // Use nextCursor.String as the next page's token.
}

基于上述代码的我的通用功能

func list(ctx context.Context, kind string, data interface{}, pageSize int, cursorStr string) string {
    query := datastore.NewQuery(kind).Limit(pageSize)
    if cursorStr != "" {
        cursor, err := datastore.DecodeCursor(cursorStr)
        if err != nil {
            log.Fatalf("Bad cursor %q: %v", cursorStr, err)
        }
        query = query.Start(cursor)
    }
    it := query.Run(ctx)

    // HERE IS WHERE THE PROBLEM STARTS
    var vet []interface{}
    var rec interface{}

    k, err := it.Next(rec)
    if err != nil {
        log.Println(err.Error())
    }
    for err == nil {
        log.Println(k, rec) // PROBLEM: The key comes ok but rec comes nil
        vet = append(vet, rec)
        k, err = it.Next(rec)
    }
    log.Println(vet) // PROBLEM: vet has only nils

    nextCursor, err := it.Cursor()
    if err != nil {
        log.Println(err.Error())
    }

    data = vet
    return nextCursor.String()
}

func TestListFunc() {
    data := make([]Tasks, 0)
    cursor := list(ctx, "Tasks", &data, 10, "")
    x, _ := json.MarshalIndent(data, " ", "   ")
    log.Println(string(x))
}

问题:数据存储迭代器.Next()似乎没有将记录存储在类型为interface{}的变量中

1 个答案:

答案 0 :(得分:1)

使用reflect软件包:

component2

使用指向目标切片的指针调用该函数:

func list(ctx context.Context, kind string, dst interface{}, pageSize int, cursorStr string) string {
    client, _ := datastore.NewClient(ctx, "my-proj")
    query := datastore.NewQuery(kind).Limit(pageSize)
    if cursorStr != "" {
        cursor, err := datastore.DecodeCursor(cursorStr)
        if err != nil {
            log.Fatalf("Bad cursor %q: %v", cursorStr, err)
        }
        query = query.Start(cursor)
    }

    // Get reflect value for the result slice.
    results := reflect.ValueOf(dst).Elem()

    // Allocate new value of the slice element type. 
    // resultp is pointer to that value.
    resultp := reflect.New(results.Type().Elem())

    it := client.Run(ctx, query)
    _, err := it.Next(resultp.Interface())
    for err == nil {
        // Append last value to results
        results.Set(reflect.Append(results, resultp.Elem())

        _, err = it.Next(resultp.Interface())
    }
    if err != iterator.Done {
        log.Fatalf("Failed fetching results: %v", err)
    }

    // Get the cursor for the next page of results.
    nextCursor, err := it.Cursor()
    // [END datastore_cursor_paging]
    _ = err        // Check the error.
    _ = nextCursor // Use nextCursor.String as the next page's token.
}