-
Notifications
You must be signed in to change notification settings - Fork 16
/
apijson.go
executable file
·94 lines (71 loc) · 2 KB
/
apijson.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package apijson
import (
"context"
"github.com/glennliao/apijson-go/action"
"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/query"
)
type Plugin interface {
Install(ctx context.Context, a *ApiJson)
}
type ApiJson struct {
config *config.Config
Debug bool // 是否开启debug模式, 显示每步骤
ctx context.Context
actionHooks []action.Hook
actionHookMap map[string][]*action.Hook
}
var DefaultApiJson = New()
func New() *ApiJson {
a := &ApiJson{}
a.config = config.New()
a.ctx = context.Background()
return a
}
// Load load for defaultApiJson, 简化使用
func Load(apps ...func(ctx context.Context, a *ApiJson)) *ApiJson {
for _, app := range apps {
DefaultApiJson.Use(app)
}
DefaultApiJson.Load()
return DefaultApiJson
}
func (a *ApiJson) Use(p ...func(ctx context.Context, a *ApiJson)) *ApiJson {
for _, plugin := range p {
plugin(a.ctx, a)
}
return a
}
func (a *ApiJson) Load() {
a.config.ReLoad()
}
func (a *ApiJson) Config() *config.Config {
return a.config
}
func (a *ApiJson) NewQuery(ctx context.Context, req model.Map) *query.Query {
q := query.New(ctx, a.Config().QueryConfig(), req)
q.DbMeta = a.config.DbMeta
q.DbFieldStyle = a.config.DbFieldStyle
q.JsonFieldStyle = a.config.JsonFieldStyle
q.NoAccessVerify = a.config.Access.NoVerify
q.AccessCondition = a.config.Access.ConditionFunc
return q
}
func (a *ApiJson) NewAction(ctx context.Context, method string, req model.Map) *action.Action {
act := action.New(ctx, a.Config().ActionConfig(), method, req)
act.NoAccessVerify = a.config.Access.NoVerify
act.DbFieldStyle = a.config.DbFieldStyle
act.JsonFieldStyle = a.config.JsonFieldStyle
act.NewQuery = a.NewQuery
act.HooksMap = a.actionHookMap
return act
}
func (a *ApiJson) RegActionHook(hook action.Hook) {
if a.actionHookMap == nil {
a.actionHookMap = make(map[string][]*action.Hook)
}
for _, item := range hook.For {
a.actionHookMap[item] = append(a.actionHookMap[item], &hook)
}
}