surrealdb / surrealdb.go

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

Bug: error unmarshalling to time.time #181

Open MaxThom opened 6 hours ago

MaxThom commented 6 hours ago

Describe the bug

SurrealDb: v1.5.4 SDK: v0.3.0

While upgrading to the new version of the sdk, I keep getting this error when umarshalling my Device struct error listing devices from database: cbor: cannot unmarshal array into Go struct field connection.RPCResponse[[]github.com/surrealdb/surrealdb%2ego.QueryResult[[]github.com/maxthom/mir/pkgs/mir_models.Device]].result of type time.Time

I have two time.Time field in those structs:

Device struct {
    ApiVersion string     `json:"apiVersion" yaml:"apiVersion"`
    ApiName    string     `json:"apiName" yaml:"apiName"`
    Status     Status     `json:"status" yaml:"status"`
}

type Status struct {
    Online         bool      `json:"online" yaml:"online"`
    LastHearthbeat time.Time `json:"lastHearthbeat" yaml:"lastHearthbeat"`
    Schema         Schema    `json:"schema" yaml:"schema"`
}

type Schema struct {
    CompressedSchema []byte    `json:"compressedSchema" yaml:"compressedSchema"`
    PackageNames     []string  `json:"packageNames" yaml:"packageNames"`
    LastSchemaFetch  time.Time `json:"lastSchemaFetch" yaml:"lastSchemaFetch"`
}

Something I have notice, before, when creating the DeviceObject using Create, the timestamps where set to the zero value of time, and with the new sdk, they are set to NULL. It works unmarshalling when null, but not when set or zero value Before:

status: {
        lastHearthbeat: d'0001-01-01T00:00:00Z',
        online: false,
        schema: {
            compressedSchema: NULL,
            lastSchemaFetch: d'0001-01-01T00:00:00Z',
            packageNames: NULL
        }
    }

After:

status: {
        lastHearthbeat: NULL,
        online: false,
        schema: {
            compressedSchema: NULL,
            lastSchemaFetch: NULL,
            packageNames: NULL
        }
    }

I'm not exactly sure thats the issue. Do i need to upgrade to v2.0 of surreal? Do i need to use models.CustomDateTime instead of time.Time?

Steps to reproduce

  1. Create a document with go time.Time set to a value
  2. Retrieve and unmarshal the document

Expected behaviour

Unmarshal properly

SurrealDB version

1.5.4

Contact Details

maxthomassin@hotmail.com

Is there an existing issue for this?

Code of Conduct

MaxThom commented 5 hours ago

I tried replacing my time.Time with models.CustomDateTime. Unmarshalling works, but the value look truncated. This get written 2024-11-08 20:12:54.317842571 +0000 UTC and this get read 1970-01-01 00:00:00.317842571 +0000 UTC just the ns are there... this is in the db d'1970-01-01T00:00:00.354Z'

MaxThom commented 5 hours ago

Moreover, except the create, I am using I am using the Query function. This is my hearbeat update query UPDATE devices MERGE {status: {lastHearthbeat: $BEAT,online: $ON,},} WHERE spec.deviceId = "0xf86tlm"; Where BEAT is a time.Time