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: serializer use default valueOf in assignInterfacesToValue #5168
Conversation
18af4ba
to
b5796c9
Compare
The Valuer, ReflectValuer func write like before mostly due to performance reason, want to cache as much as possible when parsing the struct. |
b5796c9
to
fb92330
Compare
hi @jinzhu , thanks for point out the performance issue. I just revert back the replacement of DefaultValueOf and DefaultReflectValueOf in setupValuerAndSetter. But for the sake of fix the problem, I keep the DefaultValueOf method call with the serializer if statement in Do you have any better idea on how to fix it? Currently I think for the serializer scenario, there must exist two different |
Also another thing to notice, in
But in the replacement of serializer's field.Set method, the type assert for v is a pointer to schema.serializer
Is that a typo ? |
fixed the issue, and merged the PR in 3c00980 Thank you! |
What did this pull request do?
Fix the serializer feature in the scenario of Assign().FirstOrCreate() and provide the corresponding test case.
User Case Description
My team uses Assign().FirstCreate() to implement CreateOrUpdateXXXX function. But when I try to run the following code (the intention was to simply insert the new record into the user_config table). I got an error message " failed to set value {"CommonItemList":[1,2,3,4]} to field UserConfig ".
Then I dig into the FirstOrCreate() function, and compare the implementation with Create function. GORM would do a
field.ValueOf
to get the field and then callfield.Set
to actually set the data (in assignInterfacesToValue function). After assigning, GORM would call Create() to insert the data.Since serializer's ValueOf returned a
schema.serializer
object, not the real FieldType (in this case, *main.UserConfig), it's not possible to assign the value to the field.schema.serializer
serves like a wrapper for the field that requires serialization, it implements driver.Valuer interface.But the corresponding ValueOf method will be called in
ConvertToCreateValues
function, which transforms*main.UserConfig
toschema.serializer
. So the early transformation in assignInterfacesToValue in not needed, we just need the default ValueOf implementation.