Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: UnmarshalRaw panics when unmarshalling object #139

Open
2 tasks done
AAA1exa8 opened this issue May 25, 2024 · 3 comments · May be fixed by #140
Open
2 tasks done

Bug: UnmarshalRaw panics when unmarshalling object #139

AAA1exa8 opened this issue May 25, 2024 · 3 comments · May be fixed by #140
Labels
bug Something isn't working

Comments

@AAA1exa8
Copy link

AAA1exa8 commented May 25, 2024

Describe the bug

When .Query() returns object UnmarshalRaw panics with

goroutine 1 [running]:
github.com/surrealdb/surrealdb%2ego.UnmarshalRaw({0x635ea0?, 0xc00022e1f8?}, {0x630aa0, 0xc000224430})
        ~/go/pkg/mod/github.com/surrealdb/surrealdb.go@v0.2.1/db.go:119 +0x2e5
main.main()
        /paht/to/file/side.go:39 +0x309
exit status 2

Steps to reproduce

package main

import (
	"fmt"

	"github.com/surrealdb/surrealdb.go"
)

type Id struct {
	Id int `json:"id"`
}

func main() {
	db, err := surrealdb.New("ws://localhost:8000/rpc")
	if err != nil {
		fmt.Print(err.Error())
		return
	}
	defer db.Close()
	if _, err = db.Signin(map[string]interface{}{
		"user": "root",
		"pass": "root",
	}); err != nil {
		fmt.Print(err.Error())
		return
	}
	if _, err = db.Use("namespace", "database"); err != nil {
		fmt.Print(err.Error())
		return
	}
	iddb, err := db.Query(`RETURN {
		id: 3
	}`, nil)
	if err != nil {
		fmt.Print(err.Error())
		return
	}
	var id Id
	_, err = surrealdb.UnmarshalRaw(iddb, &id)
	if err != nil {
		fmt.Print(err.Error())
	}
	fmt.Print(id)

}

Expected behaviour

UnmarashalRaw should correctly unmarshall the response

There is workaround by doing

iddb, err := db.Query(`RETURN [{
	id: 3
}]`, nil)
var id []Id
_, err = surrealdb.UnmarshalRaw(iddb, &id)

SurrealDB version

1.5.0 for linux on x86_64

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@AAA1exa8 AAA1exa8 added the bug Something isn't working label May 25, 2024
@AAA1exa8
Copy link
Author

AAA1exa8 commented May 25, 2024

I've just quickly went through the code of the current commit and it seems the RawQuery type still expects array

type RawQuery[I any] struct {
	Status string `json:"status"`
	Time   string `json:"time"`
	Result []I    `json:"result"`
	Detail string `json:"detail"`
}

this is the code I ran with the current commit of main

package main

import (
	"fmt"

	"github.com/surrealdb/surrealdb.go"
	"github.com/surrealdb/surrealdb.go/pkg/conn/gorilla"
	"github.com/surrealdb/surrealdb.go/pkg/marshal"
)

type Id struct {
	Id int `json:"id"`
}

func main() {
	ws := gorilla.Create()
	conn, err := ws.Connect("ws://localhost:8000/rpc")
	if err != nil {
		fmt.Print(err.Error())
		return
	}
	db, err := surrealdb.New("ws://localhost:8000/rpc", conn)
	if err != nil {
		fmt.Print(err.Error())
		return
	}
	defer db.Close()
	if _, err = db.Signin(&surrealdb.Auth{
		Namespace: "namespace",
		Database:  "database",
		Username:  "root",
		Password:  "root",
	}); err != nil {
		fmt.Print(err.Error())
		return
	}
	iddb, err := db.Query(`RETURN {
		id: 3
	}`, nil)
	if err != nil {
		fmt.Print(err.Error())
		return
	}
	var id []marshal.RawQuery[Id]
	err = marshal.UnmarshalRaw(iddb, &id)
	if err != nil {
		fmt.Print(err.Error())
	}
	fmt.Print(id)

}

it returns error but no longer panics

json: cannot unmarshal object into Go struct field RawQuery[main.Id].result of type []main.Id[{OK 12.487µs [] }]

it got fixed for me when I changed the type of result to I instead of []I

It also wokrs as expected when the query returns array. The type parameter just needs to alos be array

iddb, err := db.Query(`RETURN [{
	id: 3
}]`, nil)
var id []marshal.RawQuery[[]Id]
err = marshal.UnmarshalRaw(iddb, &id)

I can open PR with the fix

@ElecTwix
Copy link
Contributor

I can open PR with the fix

Feel free to Open PR, all contributions are welcome.
As far as I remember SmartUnmarshal also uses that maybe it can need a bit of refactoring in there too.

@AAA1exa8
Copy link
Author

its one line change in SmartUnmarshall

AAA1exa8 added a commit to AAA1exa8/surrealdb.go that referenced this issue May 25, 2024
@AAA1exa8 AAA1exa8 linked a pull request May 25, 2024 that will close this issue
AAA1exa8 added a commit to AAA1exa8/surrealdb.go that referenced this issue May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants