mongodb - 使用“反射”将数据附加到指向已定义结构的接口
问题描述
我正在尝试创建一个函数,它从 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(因此它甚至可能不起作用)。当我尝试将文档附加到文档界面时,程序中断的地方。
解决方案
问题中的代码将结构传递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)
}
推荐阅读
- c - MacOS SO_REUSEADDR/SO_REUSEPORT 与 Linux 不一致?
- python - 将从文本文件中获取的所有打印字符串合并到一个列表中
- spring - 休眠中的 UUID 映射
- python - 在“Think Python”中理解海龟圆和弧
- javascript - JavaScript 对象返回空值
- java - java pojo中如何区分JSON null值和默认java null
- mysql - 为什么sql正确和运行它的内在机制呢?
- php - pug-php中的href不起作用
- sql - 使用单个语句更改多个表中的公共列
- python - Django 表单:无法显示验证错误