googleapis / google-cloud-go

Google Cloud Client Libraries for Go.
https://cloud.google.com/go/docs/reference
Apache License 2.0
3.78k stars 1.3k forks source link

spanner: add support of encode/decode the custom type array #6852

Open git-hulk opened 2 years ago

git-hulk commented 2 years ago

Is your feature request related to a problem? Please describe. We are using the custom type like NullString but the array custom type []CustomType since it's NOT in the allow types: https://github.com/googleapis/google-cloud-go/blob/7934007d92dd119539f128974571dd24259afad9/spanner/value.go#L3865

Does it make sense to support encoding the custom type array like spanner.NullString.

Describe the solution you'd like

Can encode []CustomType if CustomType implemented the spanner.Encoder.

git-hulk commented 2 years ago

@rahul2393 I can try to handle this if you have no time to do it recently.

harshachinta commented 2 years ago

@git-hulk []CustomType is already supported if it implements spanner.Encoder interface. You can follow the below linked sample code to use the functionality. Sample code

git-hulk commented 2 years ago

@harshachinta sure, many thanks for your help, will try it soon.

git-hulk commented 2 years ago

@harshachinta sure, many thanks for your help, will try it soon.

@harshachinta I got what you mean after looking at the sample code, but it's a bit trivial to implement a struct for all custom types, it'd be better can support the custom type slice directly.

harshachinta commented 2 years ago

@harshachinta I got what you mean after looking at the sample code, but it's a bit trivial to implement a struct for all custom types, it'd be better can support the custom type slice directly.

@git-hulk Do you mean you don't want to implement spanner.encoder interface? Instead treat []CustomType as array of struct? Can you provide a sample code of what you are trying to do and what is the error you are facing? Also provide what is the output you are expecting for that case. It would help us better understand the usecase.

git-hulk commented 2 years ago

Hi @harshachinta

@git-hulk Do you mean you don't want to implement spanner.encoder interface? Instead treat []CustomType as array of struct?

What I want is []CustomType can also be encoded if the CustomType has implemented the spanner.encoder like spanner.NullString.