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
feat(spanner): implement valuer and scanner interfaces #4936
Conversation
@@ -554,6 +815,11 @@ func (n *NullJSON) UnmarshalJSON(payload []byte) error { | |||
return nil | |||
} | |||
|
|||
// GormDataType is used by gorm to determine the default data type for fields with this type. | |||
func (n NullJSON) GormDataType() string { | |||
return "JSON" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about JSON
? Why does it not implement Value and Scan interfaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran into a problem with JSON
, and that is that spanner.NullJson
has an exported field called Value
. Go does not allow a struct to have a method and a field with the same name, so we cannot add the Value()
method to NullJson
. At least not without making a breaking change by renaming the Value
field to something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add this problem to the PR description? or the comment in the code? Just for records that why we don't implement valuer and scanner interfaces for JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I've added it both to the comments for NullJSON and to the PR description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Valuer / Scanner Interfaces
Adds implementations for the
driver.Valuer
andsql.Scanner
interfaces for the Spanner Null* types. This makes it possible to use both thespanner.Null*
types and the underlying native types in the Go sql driver.That is, both the following will then be supported:
AND
It is not possible to implement this directly in the Go sql driver, as these types are defined in the
spanner
package.The interfaces are not implemented for the
spanner.NullJSON
type for two reasons:NullJSON
already has a field calledValue
, which makes it technically impossible to add a method calledValue()
.NullJSON
is of typeinterface{}
, which means that it can be anything. This means that there is no relevant other type thanNullJSON
that a user can use when callingsql.Row#Scan(dest ...interface{})
for a JSON column.Gorm Data Type
Adds default data type mappings for the
spanner.Null*
types. That is;NullInt64
is for example mapped by default to anINT64
column. This allows structs that usespanner.Null*
types for its fields to be used directly in Gorm migrations without the need to annotate them with the data type they should have in the database.This feature is implemented by adding the
func GormDataType() string
to each of thespanner.Null*
types.