Cannot pass [] datastore.PropertyList to the GetMulti function (datastore: src has an invalid type)

I wrote the following function, as the passing map will be dynamic, I use datastore.PropertyList. A single insert works with PropertyList, but Multiplean error message appears in the insert: "datastore: src has an invalid type"

Edited and added full source

Where did I go wrong?

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!")
}
}
+4
source share
1 answer

As the error shows: the "datastore: src has invalid type"value you pass as src( &propArray) is of an invalid type.

*[]datastore.PropertyList src Client.PutMulti(). :

src , dst GetMulti.

dst Client.GetMulti():

dst []S, []*S, []I []P S, - I P , P *P PropertyLoadSaver. []I, dst Get: PropertyLoadSaver.

, , ( &):

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

. , ( index). , id - , . , index 0. , , .

+3

Source: https://habr.com/ru/post/1618244/


All Articles