-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: behavior when getting removed callback #6910
Conversation
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.
Obtaining key values based on order does not solve this problem, because the deleted element may be at the front or at the back.
Failed case
- err := createCallback.Register("plugin_1_fn1", c1)
+ err := createCallback.Before("*").Register("plugin_1_fn1", c1)
I don't know the history of this part of the code, and I don't understand why the deleted callback is not removed when compiling
.
previously, the callbacks system allowed a new callback to overwrite a previously deleted one, which is no longer supported now~ |
maybe we can remove the callback from |
Thank you for your review. You are right, I overlooked that part. func TestRemoveNotWorking(t *testing.T) {
db, _ := gorm.Open(nil, nil)
rawCallback := db.Callback().Raw()
rawCallback.Before("*").Register("cb1", func(*gorm.DB) { panic("Remove does not work") })
rawCallback.Remove("cb1")
db.Exec("SELECT 1") // panic
}
// In this case, it works correctly.
func TestRemoveWorking(t *testing.T) {
db, _ := gorm.Open(nil, nil)
rawCallback := db.Callback().Raw()
rawCallback.Before("*").Register("cb1", func(*gorm.DB) { panic("Remove does not work") })
rawCallback.Before("*").Remove("cb1")
db.Exec("SELECT 1") // not panic
} I think that adopting the method of removing callbacks that have been marked for removal is not only a solution for the |
Sorry, I forgot to close this. |
What did this pull request do?
Resolved the issue where
Get()
retrieves a callback that has been removed byRemove()
.This was due to the search continuing even when a callback with
v.removed: true
was found, eventually reaching another callback of the same name withv.removed: false
.User Case Description
If a callback named
cb1
is registered, it is expected that callingGet("cb1")
afterRemove("cb1")
should returnnil
. However, a callback function registered ascb1
is being returned instead.