gpt4 book ai didi

google-app-engine - 无法将 []datastore.PropertyList 传递给 GetMulti 函数(数据存储 : src has invalid type)

转载 作者:IT王子 更新时间:2023-10-29 00:45:12 26 4
gpt4 key购买 nike

我已经编写了以下函数,因为传递 map 将是动态的,所以我正在使用 datastore.PropertyList。单次插入适用于 PropertyList,但在 Multiple 中插入错误显示为“datastore: src has invalid type”

编辑并添加了完整的源代码

我哪里做错了?

package main

import (
"fmt"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/cloud"
"google.golang.org/cloud/datastore"
"io/ioutil"
)

func main() {
//Single Insert

// var map0 map[string]interface{}
// map0 = make(map[string]interface{})
// map0["Id"] = "600"
// map0["Name"] = "Prasad"
// map0["Age"] = 23

// setOneDataStore(map0)

//Multiple Insert

var allMaps []map[string]interface{}
allMaps = make([]map[string]interface{}, 2)

var map1 map[string]interface{}
map1 = make(map[string]interface{})
map1["Id"] = "700"
map1["Name"] = "Jay"
map1["Age"] = 23

var map2 map[string]interface{}
map2 = make(map[string]interface{})
map2["Id"] = "800"
map2["Name"] = "Peter"
map2["Age"] = 30

allMaps[0] = map1
allMaps[1] = map2

setManyDataStore(allMaps)
}

func getDataStoreClient() (client *datastore.Client, err error) {

keyFile := "JAYWORLD-30y4f7c347pq.json"
projectID := "jay-world"

jsonKey, err := ioutil.ReadFile(keyFile)
if err != nil {
fmt.Println(err.Error())
} else {
conf, err := google.JWTConfigFromJSON(
jsonKey,
datastore.ScopeDatastore,
datastore.ScopeUserEmail,
)
if err != nil {
fmt.Println(err.Error())
} else {
ctx := context.Background()
client, err = datastore.NewClient(ctx, projectID, cloud.WithTokenSource(conf.TokenSource(ctx)))
if err != nil {
fmt.Println(err.Error())
}
}
}

return
}

func setManyDataStore(Objects []map[string]interface{}) {
ctx := context.Background()
client, err := getDataStoreClient() //have connection code in another function
ctx = datastore.WithNamespace(ctx, "CompanyA")

if err == nil {

var keys []*datastore.Key
keys = make([]*datastore.Key, len(Objects))
var propArray []datastore.PropertyList
propArray = make([]datastore.PropertyList, len(Objects))

for index := 0; index < len(Objects); index++ {
keys[index] = datastore.NewKey(ctx, "users", Objects[index]["Id"].(string), 0, nil)

props := datastore.PropertyList{}

for key, value := range Objects[index] {
props = append(props, datastore.Property{Name: key, Value: value})
}
propArray[index] = props
}

if _, err := client.PutMulti(ctx, keys, propArray); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Success!")
}

} else {
fmt.Println("Connection Failed")
}
}

func setOneDataStore(Object map[string]interface{}) {

ctx := context.Background()
client, err := getDataStoreClient() //have connection code in another function
ctx = datastore.WithNamespace(ctx, "CompanyA")

key := datastore.NewKey(ctx, "users", Object["Id"].(string), 0, nil)

props := datastore.PropertyList{}

for key, value := range Object {
props = append(props, datastore.Property{Name: key, Value: value})
}

_, err = client.Put(ctx, key, &props)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Success!")
}
}

最佳答案

如错误所示:"datastore: src has invalid type" 您作为 src (&propArray) 传递的值具有无效类型。

您不能将类型为*[]datastore.PropertyList 的值作为Client.PutMulti()src 参数传递。 .引用文档:

src must satisfy the same conditions as the dst argument to GetMulti.

dst 的条件在 Client.GetMulti() :

dst must be a []S, []*S, []I or []P, for some struct type S, some interface type I, or some non-interface non-pointer type P such that P or *P implements PropertyLoadSaver. If an []I, each element must be a valid dst for Get: it must be a struct pointer or implement PropertyLoadSaver.

所以你不能传递一个指向 slice 的指针,但你可以而且你应该传递一个 slice (删除地址 & 运算符):

if _, err := client.PutMulti(ctx, keys, propArray); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Success!")
}

注意:您似乎正在创建具有特定 ID 的键(即 index)。但是如果key name 和id 都是零值,那就被认为是一个不完整的key。您的第一个 key 将是一个不完整的 key ,因为您的 index0 开头。您可能希望使用与索引不同的 ID。

关于google-app-engine - 无法将 []datastore.PropertyList 传递给 GetMulti 函数(数据存储 : src has invalid type),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com