Open
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
This pull request fixes issue #7709.
It relaxes the parameter type of the Updates method in the generic API gorm.G[T] to any. This change aligns the generic method's behavior with the polymorphic nature of gorm.DB.Updates, enabling it to correctly handle updates from both map[string]interface{} and struct types. Previously, the strict type constraint limited the method's usability in dynamic field update scenarios.
The implementation involves modifying the Updates method signature in the CreateInterface[T] and ChainInterface[T] interfaces. The core logic in chainG[T].Updates is adjusted to first set the model with Model(r) (where r is of type *T) and then apply the updates. This ensures the correct table schema is targeted before executing the update operation with the provided values.
Verification was completed by adding new tests in tests/generics_test.go, which cover both map and struct update paths. This change is non-breaking and maintains the behavior of existing calls while enhancing functionality for dynamic updates.
User Case Description
Example 1: Updating with a map
update specific fields by passing a map[string]interface{}.
Example 2: Updating with a struct
The existing behavior of updating from a struct instance remains fully supported.