forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.go
More file actions
157 lines (125 loc) · 2.93 KB
/
Copy pathaction.go
File metadata and controls
157 lines (125 loc) · 2.93 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package action
import (
"context"
"github.com/glennliao/apijson-go/config/db"
"github.com/glennliao/apijson-go/consts"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"strings"
)
// Action 非get查询的request表中的请求
type Action struct {
ctx context.Context
tagRequest *db.Request
method string
req g.Map
err error
children map[string]*Node
keyNode map[string]*Node
}
func New(ctx context.Context, method string, req g.Map) *Action {
request, err := checkTag(req, method)
if err != nil {
panic(err)
}
delete(req, "tag")
delete(req, "version")
a := &Action{
ctx: ctx,
tagRequest: request,
method: method,
req: req,
children: map[string]*Node{},
keyNode: map[string]*Node{},
}
return a
}
func (a *Action) parse() error {
structures := a.tagRequest.Structure
for key, v := range a.req {
structuresKey := key
if strings.HasSuffix(key, consts.ListKeySuffix) {
structuresKey = structuresKey[0 : len(structuresKey)-2]
}
structure, ok := structures[key]
if !ok {
if structure, ok = structures[structuresKey]; !ok { //User[]可读取User或者User[]
return gerror.New("structure错误: 400, 缺少" + key)
}
}
var list []g.Map
_v, ok := v.(g.Map)
if ok { // 将所有node都假设成列表, 如果单个则看成一个元素的批量
list = []g.Map{_v}
} else {
list = gconv.SliceMap(v)
}
node := newNode(key, list, structure, a.tagRequest.Executor[key])
node.ctx = a.ctx
a.keyNode[key] = &node
node.keyNode = a.keyNode
err := node.parse(a.ctx, a.method)
if err != nil {
return err
}
a.children[key] = &node
}
return nil
}
func (a *Action) Result() (g.Map, error) {
err := a.parse()
if err != nil {
return nil, err
}
ret := g.Map{}
for _, k := range a.tagRequest.ExecQueue {
node := a.children[k]
err = EmitHook(a.ctx, BeforeExec, node, a.method)
if err != nil {
return nil, err
}
}
for _, k := range a.tagRequest.ExecQueue {
node := a.children[k]
err = node.reqUpdate()
if err != nil {
return nil, err
}
}
err = g.DB().Transaction(a.ctx, func(ctx context.Context, tx *gdb.TX) error {
for _, k := range a.tagRequest.ExecQueue {
node := a.children[k]
ret[k], err = node.execute(ctx, a.method)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
for _, k := range a.tagRequest.ExecQueue {
node := a.children[k]
err = EmitHook(a.ctx, AfterExec, node, a.method)
if err != nil {
return nil, err
}
}
return ret, err
}
func checkTag(req g.Map, method string) (*db.Request, error) {
_tag, ok := req["tag"]
if !ok {
return nil, gerror.New("tag 缺失")
}
tag := gconv.String(_tag)
version := req["version"]
request, err := db.GetRequest(tag, method, gconv.String(version))
if err != nil {
return nil, err
}
return request, nil
}