首页 > 解决方案 > 使用“反射”将数据附加到指向已定义结构的接口

问题描述

我正在尝试创建一个函数,它从 Mongo 集合中获取所有文档并将它们查询到声明的结构。为了实现这一点,我为类型接口的函数设置了参数,以便它可以使用两个结构。这是我的代码:

在包实体中:

type Project struct {
    Title string
    Position string
    ....
}

type Projects struct {
    Projects []Project
}

在当前包中:

var docs entities.Projects
var doc entities.Project

//doc represents a document from Mongo Collection 
//docs represents an array of documents, each element is a document
//collection has type *mongo.Collection and points to the desired collection on MongoDB.
createQuery(&doc, &docs, collection)

func createQuery(doc interface{}, docs interface{}, c *mongo.Collection) {
    documents := reflect.ValueOf(docs).Elem()
    document := reflect.ValueOf(doc)

    cur, err := c.Find(context.Background(), bson.D{{}})

    if err != nil {
        log.Fatal(err)
    }
    for cur.Next(context.Background()) {
        err = cur.Decode(document.Interface())
        if err != nil {
            log.Fatal(err)
        }
        //Error is thrown here
        documents.Set(reflect.Append(documents, document))
        fmt.Println(doc)
    }
    if err := cur.Err(); err != nil {
        log.Fatal(err)
    }

    if err != nil {
        fmt.Printf("oh shit this is the error %s \n", err)
    }
    cur.Close(context.Background())

    fmt.Printf("documents: %+v\n", documents.Interface())
    fmt.Printf("document: %+v\n", document.CanSet())
}


---ERROR OUTPUT---
panic: reflect: call of reflect.Append on struct Value

我能够使用文档变量将数据设置为 doc,尽管在执行 document.CanSet() 时为 false(因此它甚至可能不起作用)。当我尝试将文档附加到文档界面时,程序中断的地方。

标签: mongodbgostructreflectionappend

解决方案


问题中的代码将结构传递docs给需要切片的函数。传入切片字段的地址docs而不是docs其本身。

createQuery函数可以从切片本身确定切片元素类型。无需传入示例值。

var docs entities.Projects
createQuery(&docs.Projects, collection)
for _, doc := range docs.Projects {
   fmt.Println(doc.Title)
}

调用cur.Decode需要一个指向未初始化值的指针。使用 reflect.New 创建该值。

func createQuery(docs interface{}, c *mongo.Collection) {
    docsv := reflect.ValueOf(docs).Elem()
    doct := docsv.Type().Elem()

    cur, err := c.Find(context.Background(), bson.D{{}})
    if err != nil {
        log.Fatal(err)
    }

    for cur.Next(context.Background()) {
        docpv := reflect.New(doct)
        err = cur.Decode(docpv.Interface())
        if err != nil {
            log.Fatal(err)
        }
        docsv.Set(reflect.Append(docsv, docpv.Elem()))
    }
    if err := cur.Err(); err != nil {
        log.Fatal(err)
    }
    cur.Close(context.Background())
}

顺便entities.Projects说一句,如果该结构类型只有一个字段,则不需要该结构类型。改用[]Project

var docs []entities.Project
createQuery(&docs, collection)
for _, doc := range docs {
   fmt.Println(doc.Title)
}

推荐阅读