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: Query method fails in case of nested struct #180

Open
2 tasks done
mahbubabbas opened this issue Nov 7, 2024 · 4 comments
Open
2 tasks done

Bug: Query method fails in case of nested struct #180

mahbubabbas opened this issue Nov 7, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@mahbubabbas
Copy link

Describe the bug

type BaseModel struct {
	ID        *models.RecordID       `json:"id,omitempty"`
	CreatedAt *models.CustomDateTime `json:"created_at,omitempty"`
	UpdatedAt *models.CustomDateTime `json:"updated_at,omitempty"`
}

UnitOfMeasureResponse struct {
	BaseModel
	Name             string                  `json:"name"`
	Code             string                  `json:"code"`
	BaseUnit         *UnitOfMeasureResponse  `json:"base_unit,omitempty"`
	ConversionFactor float64                 `json:"conversion_factor,omitempty"`
	IsBase           bool                    `json:"is_base,omitempty"`
	Description      string                  `json:"description,omitempty"`
}

type AttributeResponse struct {
	BaseModel
	Name     string                 `json:"name"`
	Unit     *UnitOfMeasureResponse `json:"unit,omitempty"`
	DataType string                 `json:"data_type,omitempty"`
}

When I try to call the Query method as below:

res, err := surrealdb.Query[surrealdb.Result[[]AttributeResponse]](db.DB, query, nil)
if err != nil {
	return nil, err
}

I am getting the following error:

cbor: cannot unmarshal array into Go struct field 
connection.RPCResponse[[]github.com/surrealdb/surrealdb%2ego.QueryResult[github.com/surrealdb/surrealdb%2ego.Result[[]olive/models.AttributeResponse]]].result of 
type surrealdb.Result[[]olive/models.AttributeResponse] (cannot decode CBOR array to struct without toarray option)

Steps to reproduce

Mentioned above

Expected behaviour

Nested struct should be supported

SurrealDB version

surreal 2.0.4, surrealdb.go 0.3.0, windows 11

Contact Details

[email protected]

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
@EtienneBruines
Copy link
Contributor

EtienneBruines commented Nov 14, 2024

I noticed that this seems to work:

results, err := surrealdb.Query[[]AttributeResponse](db.DB, query, nil)
if results == nil {
   // handle this to prevent panics
}
attributeResponses, err := (*results)[0] // there was only one query, so this should eixst

More specifically, the TResult of the .Query (between the []) should be an array.

That does result in some ugly parsing/validating/error-checking code, so I wouldn't be surprised if this changed again in a future update.

@mahbubabbas
Copy link
Author

@EtienneBruines please verify again it does NOT work

@SOG-web
Copy link

SOG-web commented Nov 27, 2024

@EtienneBruines please verify again it does NOT work

I think it has been changed a little, I will post the updated way here tomorrow

@mahbubabbas
Copy link
Author

Why is the SurrealDB team silent on this? It's a crucial issue. In real life scenario there would be many such cases like mine where nested structure is having more than 2 levels.

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

No branches or pull requests

3 participants