-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix(datastore): Ignore field mismatch errors #8694
Conversation
datastore/datastore.go
Outdated
// IgnoreFieldMismatch allows ignoring ErrFieldMismatch error while | ||
// reading or querying data. | ||
// WARNING: Ignoring ErrFieldMismatch can cause data loss | ||
func (c *Client) IgnoreFieldMismatch() { |
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.
Consider expressing this as a client specific ClientIOption. Here is an example from storage:
google-cloud-go/storage/option.go
Lines 63 to 75 in 532a37c
func WithXMLReads() option.ClientOption { | |
return &withReadAPI{useJSON: false} | |
} | |
type withReadAPI struct { | |
internaloption.EmbeddableAdapter | |
useJSON bool | |
} | |
func (w *withReadAPI) ApplyStorageOpt(c *storageConfig) { | |
c.useJSONforReads = w.useJSON | |
c.readAPIWasSet = true | |
} |
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.
Updated the PR
// WithIgnoreFieldMismatch allows ignoring ErrFieldMismatch error while | ||
// reading or querying data. | ||
// WARNING: Ignoring ErrFieldMismatch can cause data loss | ||
func WithIgnoreFieldMismatch() option.ClientOption { |
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.
nit: Is it better to call this type/field IgnoreFieldMismatch
without the "with"?
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.
General naming convention for options is WIthXXXX
Some examples
https://github.com/googleapis/google-cloud-go/blob/main/vertexai/genai/option.go#L22-L26
https://github.com/googleapis/google-cloud-go/blob/main/storage/transfermanager/option.go#L27-L62
@bhshkh do we still want this PR? |
Yes |
ErrFieldMismatch is returned when a field is to be loaded into a different type than the one it was stored from, or when a field is missing or unexported in the destination struct.
This PR allows user to suppress such errors. Some scenarios in which this would be useful are listed in #913
Points to note
If entity written to Datastore is
{X: 1, Y:2}
and it is loaded intotype NewStruct struct{X int}
, then{X:1}
is returned to the user. Now, if user modifies thisX = 5
and writes it back to Datastore, there will be no Y field left for this entity in Datastore.Fixes: #913