forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
executable file
·65 lines (50 loc) · 1.59 KB
/
executor.go
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
package action
import (
"context"
"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/query"
"github.com/samber/lo"
)
type ActionExecutorReq struct {
Method string
Table string
Data []model.Map
Where []model.Map
Access *config.AccessConfig
Config *config.ActionConfig
NewQuery func(ctx context.Context, req model.Map) *query.Query
}
var actionExecutorMap = map[string]ActionExecutor{}
func RegExecutor(name string, e ActionExecutor) {
actionExecutorMap[name] = e
}
func GetActionExecutor(name string) (ActionExecutor, error) {
if name == "" {
name = "default"
}
if v, exists := actionExecutorMap[name]; exists {
return v, nil
}
return nil, consts.NewSysErr("action executor not found: " + name)
}
func ActionExecutorList() []string {
return lo.Keys(actionExecutorMap)
}
type ActionExecutor interface {
Do(ctx context.Context, req ActionExecutorReq) (ret model.Map, err error)
}
// TransactionHandler 事务处理函数
type TransactionHandler func(ctx context.Context, action func(ctx context.Context) error) error
type TransactionResolver func(ctx context.Context, req *Action) TransactionHandler
var noTransactionHandler = func(ctx context.Context, action func(ctx context.Context) error) error {
return action(ctx)
}
var transactionResolver TransactionResolver
func RegTransactionResolver(r TransactionResolver) {
transactionResolver = r
}
func GetTransactionHandler(ctx context.Context, req *Action) TransactionHandler {
return transactionResolver(ctx, req)
}