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): add ToStructLenient method to decode to struct fields with no error return in case of un-matched row's column with struct's exported fields #5153
Merged
hengfengli
merged 3 commits into
googleapis:main
from
rahul2393:decode-struct-unequal-fields
Nov 22, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -288,6 +288,10 @@ func errToStructArgType(p interface{}) error { | |||||||||||
// 2. Otherwise, if the name of a field matches the name of a column (ignoring case), | ||||||||||||
// decode the column into the field. | ||||||||||||
// | ||||||||||||
// 3. The number of columns in the row must match the number of exported fields in the struct. | ||||||||||||
// There must be exactly one match for each column in the row. The method will return an error | ||||||||||||
// if a column in the row cannot be assigned to a field in the struct. | ||||||||||||
// | ||||||||||||
// The fields of the destination struct can be of any type that is acceptable | ||||||||||||
// to spanner.Row.Column. | ||||||||||||
// | ||||||||||||
|
@@ -311,5 +315,48 @@ func (r *Row) ToStruct(p interface{}) error { | |||||||||||
&sppb.StructType{Fields: r.fields}, | ||||||||||||
&proto3.ListValue{Values: r.vals}, | ||||||||||||
p, | ||||||||||||
false, | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
// ToStructLenient fetches the columns in a row into the fields of a struct. | ||||||||||||
// The rules for mapping a row's columns into a struct's exported fields | ||||||||||||
// are: | ||||||||||||
// | ||||||||||||
// 1. If a field has a `spanner: "column_name"` tag, then decode column | ||||||||||||
// 'column_name' into the field. A special case is the `spanner: "-"` | ||||||||||||
// tag, which instructs ToStruct to ignore the field during decoding. | ||||||||||||
// | ||||||||||||
// 2. Otherwise, if the name of a field matches the name of a column (ignoring case), | ||||||||||||
// decode the column into the field. | ||||||||||||
// | ||||||||||||
// 3. The number of columns in the row and exported fields in the struct do not need to match. | ||||||||||||
// Any field in the struct that cannot not be assigned a value from the row is assigned its default value. | ||||||||||||
// Any column in the row that does not have a corresponding field in the struct is ignored. | ||||||||||||
// | ||||||||||||
// The fields of the destination struct can be of any type that is acceptable | ||||||||||||
// to spanner.Row.Column. | ||||||||||||
// | ||||||||||||
// Slice and pointer fields will be set to nil if the source column is NULL, and a | ||||||||||||
// non-nil value if the column is not NULL. To decode NULL values of other types, use | ||||||||||||
// one of the spanner.NullXXX types as the type of the destination field. | ||||||||||||
// | ||||||||||||
// If ToStructLenient returns an error, the contents of p are undefined. Some fields may | ||||||||||||
// have been successfully populated, while others were not; you should not use any of | ||||||||||||
// the fields. | ||||||||||||
func (r *Row) ToStructLenient(p interface{}) error { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This warning is still true for this method. Although this method is less likely to return an error, it can still fail if for example the type of one of the fields is not compatible with the datatype of the corresponding column. |
||||||||||||
// Check if p is a pointer to a struct | ||||||||||||
if t := reflect.TypeOf(p); t == nil || t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct { | ||||||||||||
return errToStructArgType(p) | ||||||||||||
} | ||||||||||||
if len(r.vals) != len(r.fields) { | ||||||||||||
return errFieldsMismatchVals(r) | ||||||||||||
} | ||||||||||||
// Call decodeStruct directly to decode the row as a typed proto.ListValue. | ||||||||||||
return decodeStruct( | ||||||||||||
&sppb.StructType{Fields: r.fields}, | ||||||||||||
&proto3.ListValue{Values: r.vals}, | ||||||||||||
p, | ||||||||||||
true, | ||||||||||||
) | ||||||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.