fix: serializer use default valueOf in assignInterfacesToValue #5168
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.
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 se 8000 rializer'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.