(注意,这个问题不需要 Dgraph;它只是我遇到问题时使用的工具)
假设你有一些结构
type AccountSettings struct {
Uid string `json:"uid"`
DType string `json:"dgraph.type"`
Name string `json:"name"`
Username string `json:"username"`
}
type Settings struct {
Uid string `json:"uid"`
DType string `json:"dgraph.type"`
Account AccountSettings `json:"account"`
}
type Tag struct {
Uid string `json:"uid"`
DType string `json:"dgraph.type"`
Label Label `json:"label"`
}
type User struct {
Uid string `json:"uid"`
DType string `json:"dgraph.type"`
Settings Settings `json:"settings"`
Tags []Tag `json:"tags"`
}
然后创建一个用户并将其添加到您的 Dgraph 数据库中
user = User{
DType: "User",
Settings: Settings{
DType: "Settings",
Account: SettingsAccount{
DType: "SettingsAccount",
Name: "Billy Bob"
Username: "billyboy123"
},
Tags: []Tag{{
DType: "Tag",
Label: "admin"
}, {
DType: "Tag",
Label: "user"
}}
},
}
然后你像查询你的 Dgraph 数据库(假设你有用户节点的 uid)
{
user(func: uid("0x1234")) {
uid
settings {
account {
name
username
}
}
tags {
label
}
}
}
响应将返回为
{
"data": {
"user": [
{
"uid": "0x1234"
"settings": [
{
"account": [
{
"name": "Billy Bob",
"username": "billyboy123"
}
]
}
]
"tags": [
{
label: "admin"
},
{
label: "user"
},
]
}
]
}
}
所以问题是 Dgraph 的 JSON 响应会将结构类型的字段作为数组返回(可能是因为可能有多个节点指向单个其他节点),但是由于它是一个数组,所以你不能立即用User
struct (因为User.Settings
是 type Settings
,而不是[]Settings
)。
为了用User
结构(或任何其他类型的结构)编组 Dgraph JSON 响应,你会怎么做?
请务必注意,settings
JSON 响应中的数组应该只是该数组中的第一个元素,但该tags
数组仍应保留为数组,因为这是User
结构中指定的内容。