我可以使用 Go API 一次性获取 faunaDB 文档的 json 数据吗?
Can I get a faunaDB document's json data in one shot with Go API?
使用 faunaDB 的 Go API,我可以为 post founaDB 定义一个结构,如下所示:
type LabelData struct {
RedirectURL string `fauna:"redirectURL"`
Owner string `fauna:"owner"`
RedirectCount int `fauna:"redirectCount"`
}
然后我可以使用 labelData 一次性调用标签数据结构的创建,如下所示:
labelData := LabelData{
RedirectURL: "",
Owner: "",
RedirectCount: 0,
}
newlabel, _ := client.Query(
f.Create(
f.Collection("label"),
f.Obj{"data": labelData},
),
)
我的问题是,我能否一次性将上面创建的文档的数据获取到 LabelData 类型 valuable?目前,我将其获取到 LabelData 类型的每个元素,如下所示:
func faunaLabelInfo(id string) (owner string, redirectCount int, redirectURL string, err error) {
var data f.Value
ret, err := client.Query(f.Get(f.RefCollection(f.Collection("label"), documentID)))
err = ret.At(f.ObjKey("data")).Get(&data)
err = data.At(f.ObjKey("owner")).Get(&owner)
err = data.At(f.ObjKey("redirectCount")).Get(&redirectCount)
err = data.At(f.ObjKey("redirectURL")).Get(&redirectURL)
我的预期如下,但这是错误的。
var labelData LabelData
ret.At(f.ObjKey("LabelData")).Get(&labelData)
感谢您的建议!
我根据 docker (docker run --rm --name faunadb -p 8443:8443 fauna/faunadb
):
制作了完整的工作示例
package main
import (
"fmt"
f "github.com/fauna/faunadb-go/faunadb"
)
type LabelData struct {
RedirectURL string `fauna:"redirectURL"`
Owner string `fauna:"owner"`
RedirectCount int `fauna:"redirectCount"`
}
var (
data = f.ObjKey("data")
ref = f.ObjKey("ref")
)
func main() {
var labelId f.RefV
config := f.Endpoint("http://localhost:8443")
client := f.NewFaunaClient("secret", config)
labelData := LabelData{
RedirectURL: "",
Owner: "",
RedirectCount: 0,
}
// Create a collection to store labels
_, _ = client.Query(f.CreateCollection(f.Obj{"name": "label"}))
newlabel, err := client.Query(
f.Create(
f.Collection("label"),
f.Obj{"data": labelData},
),
)
if err != nil {
return
}
// Get generated label ID
_ = newlabel.At(ref).Get(&labelId)
fmt.Println(labelId)
// Retrieve label by its ID
value, _ := client.Query(f.Get(labelId))
var newLabelData LabelData
_ = value.At(data).Get(&newLabelData)
fmt.Println(newLabelData.RedirectURL)
fmt.Println(newLabelData.Owner)
fmt.Println(newLabelData.RedirectCount)
}
使用 faunaDB 的 Go API,我可以为 post founaDB 定义一个结构,如下所示:
type LabelData struct {
RedirectURL string `fauna:"redirectURL"`
Owner string `fauna:"owner"`
RedirectCount int `fauna:"redirectCount"`
}
然后我可以使用 labelData 一次性调用标签数据结构的创建,如下所示:
labelData := LabelData{
RedirectURL: "",
Owner: "",
RedirectCount: 0,
}
newlabel, _ := client.Query(
f.Create(
f.Collection("label"),
f.Obj{"data": labelData},
),
)
我的问题是,我能否一次性将上面创建的文档的数据获取到 LabelData 类型 valuable?目前,我将其获取到 LabelData 类型的每个元素,如下所示:
func faunaLabelInfo(id string) (owner string, redirectCount int, redirectURL string, err error) {
var data f.Value
ret, err := client.Query(f.Get(f.RefCollection(f.Collection("label"), documentID)))
err = ret.At(f.ObjKey("data")).Get(&data)
err = data.At(f.ObjKey("owner")).Get(&owner)
err = data.At(f.ObjKey("redirectCount")).Get(&redirectCount)
err = data.At(f.ObjKey("redirectURL")).Get(&redirectURL)
我的预期如下,但这是错误的。
var labelData LabelData
ret.At(f.ObjKey("LabelData")).Get(&labelData)
感谢您的建议!
我根据 docker (docker run --rm --name faunadb -p 8443:8443 fauna/faunadb
):
package main
import (
"fmt"
f "github.com/fauna/faunadb-go/faunadb"
)
type LabelData struct {
RedirectURL string `fauna:"redirectURL"`
Owner string `fauna:"owner"`
RedirectCount int `fauna:"redirectCount"`
}
var (
data = f.ObjKey("data")
ref = f.ObjKey("ref")
)
func main() {
var labelId f.RefV
config := f.Endpoint("http://localhost:8443")
client := f.NewFaunaClient("secret", config)
labelData := LabelData{
RedirectURL: "",
Owner: "",
RedirectCount: 0,
}
// Create a collection to store labels
_, _ = client.Query(f.CreateCollection(f.Obj{"name": "label"}))
newlabel, err := client.Query(
f.Create(
f.Collection("label"),
f.Obj{"data": labelData},
),
)
if err != nil {
return
}
// Get generated label ID
_ = newlabel.At(ref).Get(&labelId)
fmt.Println(labelId)
// Retrieve label by its ID
value, _ := client.Query(f.Get(labelId))
var newLabelData LabelData
_ = value.At(data).Get(&newLabelData)
fmt.Println(newLabelData.RedirectURL)
fmt.Println(newLabelData.Owner)
fmt.Println(newLabelData.RedirectCount)
}