surrealdb / surrealdb.go

SurrealDB SDK for Golang
https://surrealdb.com
Apache License 2.0
231 stars 62 forks source link

example code gives unmarshalling error #60

Closed thecodekitchen closed 1 year ago

thecodekitchen commented 1 year ago

I started a local in-memory database according to the installation instructions, ran go mod init, ran go get github.com/surrealdb/surrealdb.go, created a main.go file in the project root, copied and pasted the example code into it, and ran it with 'go run main.go'. I received the following error:

panic: json: cannot unmarshal object into Go value of type []main.User

goroutine 1 [running]:
main.main()
    /home/atd/Projects/surreal_go/main.go:46 +0xa0b
exit status 2

What's going wrong here?

lukasbowen commented 1 year ago

I am running into the same issue following the example.

type User struct {
    ID   string `json:"id,omitempty"`
    Name string `json:"name"`
}

user:= User{
    Name: "Test",
}

data, err := handler.db.Create("user", user)
if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}

// Unmarshal data
createdUser:= make([]User, 1)
err = surrealdb.Unmarshal(data, &createdUser)
lukasbowen commented 1 year ago

Although from testing it the following will work. I am not sure if the document version using make([]User, 1) is attended or this updated example is the intended solution.

type User struct {
    ID   string `json:"id,omitempty"`
    Name string `json:"name"`
}

user:= User{
    Name: "Test",
}

data, err := handler.db.Create("user", user)
if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}

// Unmarshal data
createdUser:= User{}
err = surrealdb.Unmarshal(data, &createdUser)
ElecTwix commented 1 year ago

Hi @lukasbowen and @thecodekitchen. I'm sorry for your bad experience with the driver. the problem you are facing is caused by RPC data response to change with beta 8 to 9. The driver has been updated but there is no release at the moment. You can use go get ithub.com/surrealdb/surrealdb.go@0316f40

If it is not working please let me know I can look into out.

also if docs are not enough you can look into unit tests and tests the lib yourself.

ElecTwix commented 1 year ago

@phughk I think we need to make a release. for beta v9 fix.

phughk commented 1 year ago

Thanks ElecTwix! Will do that now