我正在尝试构建一个相当通用的服务来在 arangodb
中创建图表。
在我的 ReadVertex()
函数中,我希望能够读取一个顶点的任意字段的属性并将其读入一个变量,最好是一个字符串,而不必创建特定的结构使用针对特定属性进行硬编码的 json 标记。
我可以毫无问题地插入任意对象作为顶点的属性,并且我已经尝试将它们读入字符串,但它总是空白。
一个我想完成的例子:如果一个顶点有一个属性:"Prop1": {"Nested1": true, "Nested2": false}
,和一个属性"Prop2": "Just a string"
,在我的 ReadVertex()
函数中,我希望能够按照以下方式做一些事情
// query the database and get a cursor containing prop1 and prop2
var prop1 someType
cursor.ReadDocument(ctx, &prop1)
fmt.Println(prop1.String()) // should print {"Prop1": {"Nested1": true, "Nested2": false}}
var prop2 someType
cursor.ReadDocument(ctx, &prop2)
fmt.Println(prop2.String()) // should print {"Prop2": "Just a string"}
我希望 prop1 和 prop2 的类型相同。
最佳答案
事实证明,答案比我想象的要简单得多。如果 prop1
和 prop2
是 interface{}
类型,它会以映射的形式读取数据,然后只要map 都是字符串(在我的例子中是这样),要将其转换为 json 字符串,我可以使用 json.Marshal()
并将返回的字节数组转换为字符串。
关于go - 如何使用 cursor.ReadDocument() 从 arangodb 读取任意对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56045516/