Skip to content

Commit 75a7ae6

Browse files
重构 callback
1 parent 6e354b9 commit 75a7ae6

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

hook/field/field.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,22 @@ import (
2121
"github.com/chenmingyong0423/go-mongox/operation"
2222
)
2323

24+
func getPayload(opCtx *operation.OpContext, opType operation.OpType) any {
25+
switch opType {
26+
case operation.OpTypeBeforeInsert, operation.OpTypeAfterInsert:
27+
return opCtx.Doc
28+
case operation.OpTypeBeforeUpdate, operation.OpTypeAfterUpdate:
29+
return opCtx.Updates
30+
case operation.OpTypeBeforeDelete, operation.OpTypeAfterDelete:
31+
return opCtx.Filter
32+
case operation.OpTypeBeforeUpsert, operation.OpTypeAfterUpsert:
33+
return opCtx.Replacement
34+
}
35+
return nil
36+
}
37+
2438
func Execute(ctx context.Context, opCtx *operation.OpContext, opType operation.OpType, opts ...any) error {
25-
payLoad := opCtx.GetPayload(opType)
39+
payLoad := getPayload(opCtx, opType)
2640
if payLoad == nil {
2741
return nil
2842
}

operation/operation_type.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,3 @@ type OpContext struct {
4141
Updates any
4242
Replacement any
4343
}
44-
45-
func (opCtx OpContext) GetPayload(opType OpType) any {
46-
switch opType {
47-
case OpTypeBeforeInsert, OpTypeAfterInsert:
48-
return opCtx.Doc
49-
case OpTypeBeforeUpdate, OpTypeAfterUpdate:
50-
return opCtx.Updates
51-
case OpTypeBeforeDelete, OpTypeAfterDelete:
52-
return opCtx.Filter
53-
case OpTypeBeforeUpsert, OpTypeAfterUpsert:
54-
return opCtx.Replacement
55-
}
56-
return nil
57-
}

0 commit comments

Comments
 (0)