-
Notifications
You must be signed in to change notification settings - Fork 24
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
structs not supported #281
Comments
cloud.google.com/go/spanner supports passing structs as arguments to queries. Fixes googleapis#281
It's not pretty, but a workaround is to use GenericColumnValue and construct it manually, like: // the entries can be replaced with:
spanner.GenericColumnValue{
Type: &spannerpb.Type{
Code: spannerpb.TypeCode_ARRAY,
ArrayElementType: &spannerpb.Type{
Code: spannerpb.TypeCode_STRUCT,
StructType: &spannerpb.StructType{
Fields: []*spannerpb.StructType_Field{
{Name: "id", Type: &spannerpb.Type{Code: spannerpb.TypeCode_INT64}},
{Name: "name", Type: &spannerpb.Type{Code: spannerpb.TypeCode_STRING}},
},
},
},
},
Value: &structpb.Value{
Kind: &structpb.Value_ListValue{
ListValue: &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_ListValue{
ListValue: &structpb.ListValue{
Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "0"}},
{Kind: &structpb.Value_StringValue{StringValue: "Hello"}},
},
},
},
},
{
Kind: &structpb.Value_ListValue{
ListValue: &structpb.ListValue{
Values: []*structpb.Value{
{Kind: &structpb.Value_StringValue{StringValue: "1"}},
{Kind: &structpb.Value_StringValue{StringValue: "World"}},
},
},
},
},
},
},
},
},
} Of course, the readability can be significantly improved via some helpers, e.g. // the entries can be replaced with:
spanner.GenericColumnValue{
Type: arrayOf(
structOf(
fieldOf("id", int64Type()),
fieldOf("name", stringType()),
),
)
...
}
func arrayOf(typ *spannerpb.Type) *spannerpb.Type {
return &spannerpb.Type{
Code: spannerpb.TypeCode_ARRAY,
ArrayElementType: typ,
}
}
func structOf(fields ...*spannerpb.StructType_Field) *spannerpb.Type {
return &spannerpb.Type{
Code: spannerpb.TypeCode_Struct,
Fields: fields,
}
}
func fieldOf(name string, typ *spannerpb.Type) *spannerpb.StructType_Field {
return &spannerpb.StructType_Field{
Name: name,
Type: typ,
}
}
func int64Type() *spannerpb.Type { return &spannerpb.Type{Kind: spannerpb.TypeCoode_INT64} }
func stringType() *spannerpb.Type { return &spannerpb.Type{Kind: spannerpb.TypeCoode_STRING} } |
#282 seems to break gorm custom datatypes(https://gorm.io/docs/data_types.html). My custom datatype valuer and scanner interfaces just seem to be getting ignored. Downgrading to v1.6.0 seems to work again. Not sure if I'm missing something but here's my current implementation
|
When a type had implemented custom Value func to convert the type, then the conversion method was not being called for structs. Fixes googleapis#281
@newtonmunene99 ah didn't think about that interaction. Sent a patch #289 |
When a type had implemented custom Value func to convert the type, then the conversion method was not being called for structs. Fixes #281
https://pkg.go.dev/cloud.google.com/go/spanner supports structs https://pkg.go.dev/cloud.google.com/go/spanner#hdr-Structs, however this feature is not exposed directly in go-sql-spanner.
For example the following code fails due to the check in
go-sql-spanner/driver.go
Line 748 in 94bc417
If I change
changeIsValidType
to always return true, then the code above passes without problems.There's probably a workaround for this, but I haven't, yet discovered it. Nevertheless, it would be nice to support
struct
andarray of struct
directly in go-sql-spanner.The text was updated successfully, but these errors were encountered: