-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.go
More file actions
68 lines (59 loc) · 2.13 KB
/
callback.go
File metadata and controls
68 lines (59 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package mongox
import (
"context"
validator2 "github.com/go-playground/validator/v10"
"github.com/matiniiuu/mongox/hook/validator"
"github.com/matiniiuu/mongox/hook/model"
"github.com/matiniiuu/mongox/callback"
"github.com/matiniiuu/mongox/hook/field"
"github.com/matiniiuu/mongox/operation"
)
func RegisterPlugin(name string, cb callback.CbFn, opType operation.OpType) {
callback.Callbacks.Register(opType, name, cb)
}
func RemovePlugin(name string, opType operation.OpType) {
callback.Callbacks.Remove(opType, name)
}
type PluginConfig struct {
EnableDefaultFieldHook bool
EnableModelHook bool
EnableValidationHook bool
// use to replace to the default validate instance
Validate *validator2.Validate
}
func InitPlugin(config *PluginConfig) {
if config.EnableDefaultFieldHook {
opTypes := []operation.OpType{operation.OpTypeBeforeInsert, operation.OpTypeBeforeUpdate, operation.OpTypeBeforeUpsert}
for _, opType := range opTypes {
typ := opType
RegisterPlugin("mongox:default_field", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
return field.Execute(ctx, opCtx, typ, opts...)
}, typ)
}
}
if config.EnableModelHook {
opTypes := []operation.OpType{
operation.OpTypeBeforeInsert, operation.OpTypeAfterInsert,
operation.OpTypeBeforeDelete, operation.OpTypeAfterDelete,
operation.OpTypeBeforeUpdate, operation.OpTypeAfterUpdate,
operation.OpTypeBeforeUpsert, operation.OpTypeAfterUpsert,
operation.OpTypeBeforeFind, operation.OpTypeAfterFind,
}
for _, opType := range opTypes {
typ := opType
RegisterPlugin("mongox:model", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
return model.Execute(ctx, opCtx, typ, opts...)
}, typ)
}
}
if config.EnableValidationHook {
validator.SetValidate(config.Validate)
opTypes := []operation.OpType{operation.OpTypeBeforeInsert, operation.OpTypeBeforeUpsert}
for _, opType := range opTypes {
typ := opType
RegisterPlugin("mongox:validation", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
return validator.Execute(ctx, opCtx, typ, opts...)
}, typ)
}
}
}