surrealdb / surrealdb.go

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

fix: add ability to unmarshall objects (#139) #140

Open AAA1exa8 opened 3 months ago

AAA1exa8 commented 3 months ago

Fixes #139

This PR makes it possible to unmarshall queries returning objects

currently query like this

RETURN [3]

is unmarshalled to this type

var num []marshal.RawQuery[int]

even tought the return type of the query is array

and query like this

RETURN 3

is impossible to unmarshall with UnmarshalRaw.

This PR chanes it that the first query is unmarshalled to

var num []marshal.RawQuery[[]int]

and the second one to

var num []marshal.RawQuery[int]
ElecTwix commented 3 months ago

First of all thanks for your PR @AAA1exa8,

Got an error while testing. to reproduce go test -v

=== RUN   TestSurrealDBSuite
=== RUN   TestSurrealDBSuite/gorilla
=== RUN   TestSurrealDBSuite/gorilla/TestConcurrentOperations
=== RUN   TestSurrealDBSuite/gorilla/TestConcurrentOperations/Concurrent_select_non_existent_rows_100
=== RUN   TestSurrealDBSuite/gorilla/TestConcurrentOperations/Concurrent_create_rows_100
=== RUN   TestSurrealDBSuite/gorilla/TestConcurrentOperations/Concurrent_select_exist_rows_100
=== RUN   TestSurrealDBSuite/gorilla/TestCreate
=== RUN   TestSurrealDBSuite/gorilla/TestCreate/raw_map_works
=== RUN   TestSurrealDBSuite/gorilla/TestCreate/Single_create_works
=== RUN   TestSurrealDBSuite/gorilla/TestCreate/Multiple_creates_works
    db_test.go:425: Creating multiple records is not supported yet
=== RUN   TestSurrealDBSuite/gorilla/TestDelete
=== RUN   TestSurrealDBSuite/gorilla/TestFetch
=== RUN   TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_for_individual_users
    db_test.go:272: TODO(gh-116) Fetch unimplemented
=== RUN   TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_hardcoded_query
    db_test.go:290: 
            Error Trace:    /home/electwix/dev/test/surrealdb.go/db_test.go:290
                                    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112
            Error:          Received unexpected error:
                        json: cannot unmarshal array into Go struct field RawQuery[github.com/surrealdb/surrealdb%2ego_test.testUserWithFriend[github.com/surrealdb/surrealdb%2ego_test.testUserWithFriend[interface {}]]].result of type surrealdb_test.testUserWithFriend[github.com/surrealdb/surrealdb%2ego_test.testUserWithFriend[interface {}]]
            Test:           TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_hardcoded_query
    db_test.go:293: 
            Error Trace:    /home/electwix/dev/test/surrealdb.go/db_test.go:293
                                    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112
            Error:          "[]" should have 1 item(s), but has 0
            Test:           TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_hardcoded_query
=== RUN   TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_thing_and_fetchString
    db_test.go:298: TODO(gh-116) Fetch unimplemented
=== RUN   TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_fetchString
    db_test.go:308: TODO(gh-116) Fetch unimplemented
=== RUN   TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_thing_or_tableName
    db_test.go:324: 
            Error Trace:    /home/electwix/dev/test/surrealdb.go/db_test.go:324
                                    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112
            Error:          Received unexpected error:
                        json: cannot unmarshal array into Go struct field RawQuery[github.com/surrealdb/surrealdb%2ego_test.testUserWithFriend[github.com/surrealdb/surrealdb%2ego_test.testUserWithFriend[interface {}]]].result of type surrealdb_test.testUserWithFriend[github.com/surrealdb/surrealdb%2ego_test.testUserWithFriend[interface {}]]
            Test:           TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_thing_or_tableName
    db_test.go:327: 
            Error Trace:    /home/electwix/dev/test/surrealdb.go/db_test.go:327
                                    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112
            Error:          "[]" should have 1 item(s), but has 0
            Test:           TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_thing_or_tableName
=== RUN   TestSurrealDBSuite/gorilla/TestInsert
=== RUN   TestSurrealDBSuite/gorilla/TestInsert/raw_map_works
=== RUN   TestSurrealDBSuite/gorilla/TestInsert/Single_insert_works
=== RUN   TestSurrealDBSuite/gorilla/TestInsert/Multiple_insert_works
=== RUN   TestSurrealDBSuite/gorilla/TestLiveViaMethod
=== RUN   TestSurrealDBSuite/gorilla/TestLiveViaQuery
=== RUN   TestSurrealDBSuite/gorilla/TestLiveWithOptionsViaMethod
=== RUN   TestSurrealDBSuite/gorilla/TestMerge
=== RUN   TestSurrealDBSuite/gorilla/TestNonRowSelect
=== RUN   TestSurrealDBSuite/gorilla/TestPatch
=== RUN   TestSurrealDBSuite/gorilla/TestSelect
=== RUN   TestSurrealDBSuite/gorilla/TestSelect/Select_many_with_table
=== RUN   TestSurrealDBSuite/gorilla/TestSelect/Select_single_record
=== RUN   TestSurrealDBSuite/gorilla/TestSmartMarshalQuery
=== RUN   TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/create_with_SmartMarshal_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/select_with_SmartMarshal_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/update_with_SmartMarshal_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/delete_with_SmartMarshal_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/check_if_data_deleted_SmartMarshal_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery
=== RUN   TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/raw_create_query
    db_test.go:645: 
            Error Trace:    /home/electwix/dev/test/surrealdb.go/db_test.go:645
                                    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112
            Error:          Received unexpected error:
                        json: cannot unmarshal array into Go struct field RawQuery[github.com/surrealdb/surrealdb%2ego_test.testUser].result of type surrealdb_test.testUser
            Test:           TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/raw_create_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/raw_select_query
    db_test.go:655: 
            Error Trace:    /home/electwix/dev/test/surrealdb.go/db_test.go:655
                                    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112
            Error:          Received unexpected error:
                        json: cannot unmarshal array into Go struct field RawQuery[github.com/surrealdb/surrealdb%2ego_test.testUser].result of type surrealdb_test.testUser
            Test:           TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/raw_select_query
=== RUN   TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/select_query
--- FAIL: TestSurrealDBSuite (0.28s)
    --- FAIL: TestSurrealDBSuite/gorilla (0.28s)
        --- PASS: TestSurrealDBSuite/gorilla/TestConcurrentOperations (0.14s)
            --- PASS: TestSurrealDBSuite/gorilla/TestConcurrentOperations/Concurrent_select_non_existent_rows_100 (0.04s)
            --- PASS: TestSurrealDBSuite/gorilla/TestConcurrentOperations/Concurrent_create_rows_100 (0.05s)
            --- PASS: TestSurrealDBSuite/gorilla/TestConcurrentOperations/Concurrent_select_exist_rows_100 (0.04s)
        --- PASS: TestSurrealDBSuite/gorilla/TestCreate (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestCreate/raw_map_works (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestCreate/Single_create_works (0.00s)
            --- SKIP: TestSurrealDBSuite/gorilla/TestCreate/Multiple_creates_works (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestDelete (0.00s)
        --- FAIL: TestSurrealDBSuite/gorilla/TestFetch (0.00s)
            --- SKIP: TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_for_individual_users (0.00s)
            --- FAIL: TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_hardcoded_query (0.00s)
            --- SKIP: TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_thing_and_fetchString (0.00s)
            --- SKIP: TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_fetchString (0.00s)
            --- FAIL: TestSurrealDBSuite/gorilla/TestFetch/Run_fetch_on_query_using_map[string]interface{}_for_thing_or_tableName (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestInsert (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestInsert/raw_map_works (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestInsert/Single_insert_works (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestInsert/Multiple_insert_works (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestLiveViaMethod (0.04s)
        --- PASS: TestSurrealDBSuite/gorilla/TestLiveViaQuery (0.04s)
        --- PASS: TestSurrealDBSuite/gorilla/TestLiveWithOptionsViaMethod (0.04s)
        --- PASS: TestSurrealDBSuite/gorilla/TestMerge (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestNonRowSelect (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestPatch (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestSelect (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSelect/Select_many_with_table (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSelect/Select_single_record (0.00s)
        --- PASS: TestSurrealDBSuite/gorilla/TestSmartMarshalQuery (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/create_with_SmartMarshal_query (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/select_with_SmartMarshal_query (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/update_with_SmartMarshal_query (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/delete_with_SmartMarshal_query (0.00s)
            --- PASS: TestSurrealDBSuite/gorilla/TestSmartMarshalQuery/check_if_data_deleted_SmartMarshal_query (0.00s)
        --- FAIL: TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery (0.00s)
            --- FAIL: TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/raw_create_query (0.00s)
            --- FAIL: TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/raw_select_query (0.00s)
            --- FAIL: TestSurrealDBSuite/gorilla/TestSmartUnMarshalQuery/select_query (0.00s)
panic: runtime error: index out of range [0] with length 0 [recovered]
    panic: runtime error: index out of range [0] with length 0

goroutine 801 [running]:
testing.tRunner.func1.2({0x87a880, 0xc0000ca108})
    /usr/lib/go/src/testing/testing.go:1631 +0x24a
testing.tRunner.func1()
    /usr/lib/go/src/testing/testing.go:1634 +0x377
panic({0x87a880?, 0xc0000ca108?})
    /usr/lib/go/src/runtime/panic.go:770 +0x132
github.com/surrealdb/surrealdb%2ego_test.(*SurrealDBTestSuite).TestSmartUnMarshalQuery.func3()
    /home/electwix/dev/test/surrealdb.go/db_test.go:663 +0x16c
github.com/stretchr/testify/suite.(*Suite).Run.func2(0xc0001124e0?)
    /home/electwix/go/pkg/mod/github.com/stretchr/testify@v1.8.4/suite/suite.go:112 +0x30
testing.tRunner(0xc0001124e0, 0xc000792ca8)
    /usr/lib/go/src/testing/testing.go:1689 +0xfb
created by testing.(*T).Run in goroutine 796
    /usr/lib/go/src/testing/testing.go:1742 +0x390
exit status 2
FAIL    github.com/surrealdb/surrealdb.go   0.289s
AAA1exa8 commented 3 months ago

sorry about that I amended the commit and now it should work just fine

ElecTwix commented 3 months ago

sorry about that I amended the commit and now it should work just fine

Yes, it is working fine right now.

PR LGTM.

@timpratim, @phughk What do you think?

phughk commented 3 months ago

I am fairly detached from this, but isn't this beating around the eternal problem of "lists are objects" and "a list of 1 object can be considered an object"? Why can't this be a list of 1?

ElecTwix commented 3 months ago

I am fairly detached from this, but isn't this beating around the eternal problem of "lists are objects" and "a list of 1 object can be considered an object"? Why can't this be a list of 1?

Yeah, but we don't have objects in golang so in that sense we expected an array but didn't get it.

Normally when we do SELECT * FROM user it returns an array, but OP executes RETURN { id: 3 } in that case it returns a primate value and generic of marshal.RawQuery[K] was getting K type was considered array by default, because of that he cannot declare primate value such as int etc. he just removed array by default we can still put array need to give explicitly generic field a array of something.