From dc4d31a9fd5ca85c876a98ab2ec4e28619d6b030 Mon Sep 17 00:00:00 2001 From: Yavolte Date: Fri, 15 Aug 2025 09:37:07 +0800 Subject: [PATCH] fix model afterdelete --- go.mod | 2 ++ go.sum | 4 +++ hook.go | 2 -- model.go | 5 +++- plugins/identity/engine.go | 51 ++++++++++++++++++++++++++++++++++ plugins/identity/identified.go | 13 ++++++--- plugins/validate/types.go | 8 +++--- query.go | 16 +++++------ types.go | 5 ++++ types/rule.go | 2 +- types/schema.go | 2 +- 11 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 plugins/identity/engine.go diff --git a/go.mod b/go.mod index e50f9f3..c9a628f 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,10 @@ go 1.22.9 require ( git.nobla.cn/golang/kos v0.1.32 + github.com/bwmarrin/snowflake v0.3.0 github.com/cespare/xxhash/v2 v2.3.0 github.com/go-playground/validator/v10 v10.23.0 + github.com/google/uuid v1.6.0 github.com/longbridgeapp/sqlparser v0.3.2 github.com/rs/xid v1.6.0 github.com/uole/sqlparser v0.0.1 diff --git a/go.sum b/go.sum index a9683b0..2da4ad1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.nobla.cn/golang/kos v0.1.32 h1:sFVCA7vKc8dPUd0cxzwExOSPX2mmMh2IuwL6cYS1pBc= git.nobla.cn/golang/kos v0.1.32/go.mod h1:35Z070+5oB39WcVrh5DDlnVeftL/Ccmscw2MZFe9fUg= +github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= +github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -16,6 +18,8 @@ github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M= github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= diff --git a/hook.go b/hook.go index 4b776b8..9ef94e5 100644 --- a/hook.go +++ b/hook.go @@ -78,7 +78,6 @@ func (hook *hookManager) afterCreate(ctx context.Context, tx *gorm.DB, model any cb(ctx, tx, model, diff) } } - return } func (hook *hookManager) beforeUpdate(ctx context.Context, tx *gorm.DB, model any) (err error) { @@ -106,7 +105,6 @@ func (hook *hookManager) afterUpdate(ctx context.Context, tx *gorm.DB, model any cb(ctx, tx, model, diff) } } - return } func (hook *hookManager) beforeSave(ctx context.Context, tx *gorm.DB, model any) (err error) { diff --git a/model.go b/model.go index c1f9965..5889188 100644 --- a/model.go +++ b/model.go @@ -695,9 +695,12 @@ func (m *Model) Delete(w http.ResponseWriter, r *http.Request) { if errTx != nil { return } - m.getHook().afterDelete(childCtx, tx, model) return }); err == nil { + if deleter, ok := model.(afterDeleted); ok { + deleter.AfterDeleted(childCtx, dbSess) + } + m.getHook().afterDelete(childCtx, dbSess, model) m.response.Success(w, types.DeleteResponse{ ID: idStr, Status: "deleted", diff --git a/plugins/identity/engine.go b/plugins/identity/engine.go new file mode 100644 index 0000000..7ffd511 --- /dev/null +++ b/plugins/identity/engine.go @@ -0,0 +1,51 @@ +package identity + +import ( + "github.com/bwmarrin/snowflake" + "github.com/google/uuid" + "github.com/rs/xid" +) + +type Engine interface { + NextID() string +} + +type ( + XidEngine struct { + } + + UUIDEngine struct { + } + + SnowflakeEngine struct { + e *snowflake.Node + } +) + +func (e *XidEngine) NextID() string { + return xid.New().String() +} + +func (e *UUIDEngine) NextID() string { + return uuid.New().String() +} + +func (e *SnowflakeEngine) NextID() string { + return e.e.Generate().String() +} + +func NewXidEngine() *XidEngine { + return &XidEngine{} +} + +func NewUUIDEngine() *UUIDEngine { + return &UUIDEngine{} +} + +func NewSnowflakeEngine(nodeID int64) *SnowflakeEngine { + node, err := snowflake.NewNode(nodeID) + if err != nil { + panic(err) + } + return &SnowflakeEngine{e: node} +} diff --git a/plugins/identity/identified.go b/plugins/identity/identified.go index 6e0e597..a8667ac 100644 --- a/plugins/identity/identified.go +++ b/plugins/identity/identified.go @@ -3,12 +3,12 @@ package identity import ( "reflect" - "github.com/rs/xid" "gorm.io/gorm" "gorm.io/gorm/schema" ) type Identify struct { + e Engine } func (identity *Identify) Name() string { @@ -21,7 +21,7 @@ func (identity *Identify) Initialize(db *gorm.DB) (err error) { } func (identity *Identify) NextID() string { - return xid.New().String() + return identity.e.NextID() } func (identity *Identify) Grant(db *gorm.DB) { @@ -56,6 +56,11 @@ func (identity *Identify) Grant(db *gorm.DB) { } } -func New() *Identify { - return &Identify{} +func New(e Engine) *Identify { + if e == nil { + e = NewXidEngine() + } + return &Identify{ + e: e, + } } diff --git a/plugins/validate/types.go b/plugins/validate/types.go index c357a09..7c4bc8d 100644 --- a/plugins/validate/types.go +++ b/plugins/validate/types.go @@ -1,11 +1,12 @@ package validate import ( - "git.nobla.cn/golang/rest/types" - "gorm.io/gorm" "reflect" "regexp" "strconv" + + "git.nobla.cn/golang/rest/types" + "gorm.io/gorm" ) const ( @@ -14,7 +15,7 @@ const ( var ( scopeCtxKey = &validateScope{} - telephoneRegex = regexp.MustCompile("^\\d{5,20}$") + telephoneRegex = regexp.MustCompile(`^\d{5,20}$`) ) type ( @@ -62,7 +63,6 @@ func formatError(rule types.Rule, scm *types.Schema, tag string) string { switch tag { case "db_unique": s = scm.Label + "值已经存在." - break case "required": s = scm.Label + "值不能为空." case "max": diff --git a/query.go b/query.go index 6171f62..0d351bf 100644 --- a/query.go +++ b/query.go @@ -17,7 +17,7 @@ type ( db *gorm.DB condition string fields []string - params []interface{} + params []any table string joins []join orderBy []string @@ -28,9 +28,9 @@ type ( } condition struct { - Field string `json:"field"` - Value interface{} `json:"value"` - Expr string `json:"expr"` + Field string `json:"field"` + Value any `json:"value"` + Expr string `json:"expr"` } join struct { @@ -60,16 +60,16 @@ func (query *Query) compile() (*gorm.DB, error) { if query.table != "" { db = db.Table(query.table) } - if query.joins != nil && len(query.joins) > 0 { + if len(query.joins) > 0 { for _, joinEntity := range query.joins { cs, ps := query.buildConditions("OR", false, joinEntity.Conditions...) db = db.Joins(joinEntity.Direction+" JOIN "+joinEntity.Table+" ON "+cs, ps...) } } - if query.orderBy != nil && len(query.orderBy) > 0 { + if len(query.orderBy) > 0 { db = db.Order(strings.Join(query.orderBy, ",")) } - if query.groupBy != nil && len(query.groupBy) > 0 { + if len(query.groupBy) > 0 { db = db.Group(strings.Join(query.groupBy, ",")) } if query.offset > 0 { @@ -366,7 +366,7 @@ func (query *Query) ResetSelect() *Query { return query } -func (query *Query) Count(v interface{}) (i int64) { +func (query *Query) Count(v any) (i int64) { var ( db *gorm.DB err error diff --git a/types.go b/types.go index 6a21837..0959ba0 100644 --- a/types.go +++ b/types.go @@ -62,6 +62,11 @@ type ( AfterSaved(ctx context.Context, tx *gorm.DB) } + //删除后的回调 + afterDeleted interface { + AfterDeleted(ctx context.Context, tx *gorm.DB) + } + sqlCountResponse struct { Count int64 `json:"count"` } diff --git a/types/rule.go b/types/rule.go index db253b8..33eeefa 100644 --- a/types/rule.go +++ b/types/rule.go @@ -20,7 +20,7 @@ func (n Scenarios) Value() (driver.Value, error) { } // Scan implements the Scanner interface. -func (n *Rule) Scan(value interface{}) error { +func (n *Rule) Scan(value any) error { if value == nil { return nil } diff --git a/types/schema.go b/types/schema.go index fb20a0c..9299237 100644 --- a/types/schema.go +++ b/types/schema.go @@ -63,7 +63,7 @@ func (n Scenarios) Has(str string) bool { } // Scan implements the Scanner interface. -func (n *Scenarios) Scan(value interface{}) error { +func (n *Scenarios) Scan(value any) error { if value == nil { return nil }