From 90e0445c431c3a1f14bbc5c5bf2f1f08efc6a7b1 Mon Sep 17 00:00:00 2001 From: demoManito <1430482733@qq.com> Date: Thu, 29 May 2025 14:37:39 +0800 Subject: [PATCH] fix(schema): Check the callback function parameter type --- schema/schema.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index db236797..60b7e552 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -313,8 +313,12 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam for _, cbName := range callbackTypes { if methodValue := callBackToMethodValue(modelValue, cbName); methodValue.IsValid() { switch methodValue.Type().String() { - case "func(*gorm.DB) error": // TODO hack - reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true) + case "func(*gorm.DB) error": + if inVarPkg := methodValue.Type().In(0).Elem().PkgPath(); inVarPkg != "gorm.io/gorm" { + logger.Default.Warn(context.Background(), "The hook function enters the parameter type incorrectly, the function parameter type is `%v`", inVarPkg) + } else { + reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true) + } default: logger.Default.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName) }