forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
executable file
·429 lines (330 loc) · 8.31 KB
/
node.go
File metadata and controls
executable file
·429 lines (330 loc) · 8.31 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package action
import (
"context"
"net/http"
"strings"
"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/util"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/samber/lo"
)
type Node struct {
req []model.Map
ctx context.Context
Action *Action
Key string
IsList bool
tableName string
Role string
Data []model.Map // 需写入数据库的数据
Where []model.Map // 条件
Ret model.Map // 节点返回值
RowKey string // 主键
structure *config.Structure
executor string
keyNode map[string]*Node
// access *config.Access
}
func newNode(key string, req []model.Map, structure *config.Structure, executor string) Node {
n := Node{
Key: key, req: req, structure: structure, executor: executor,
}
n.Data = []model.Map{}
n.Where = []model.Map{}
for _ = range n.req {
n.Data = append(n.Data, model.Map{})
n.Where = append(n.Where, model.Map{})
}
if strings.HasSuffix(key, consts.ListKeySuffix) {
n.Key = util.RemoveSuffix(key, consts.ListKeySuffix)
n.IsList = true
}
return n
}
// parse req data to data/where
func (n *Node) parseReq(method string) {
for i, item := range n.req {
for key, val := range item {
if key == consts.Role {
n.Role = util.String(val)
continue
}
key = n.Action.DbFieldStyle(n.ctx, n.tableName, key)
switch method {
case http.MethodPost:
n.Data[i][key] = val
case http.MethodDelete:
n.Where[i][key] = val
case http.MethodPut:
if key == n.RowKey || key == n.RowKey+consts.OpIn {
n.Where[i][key] = val
} else {
n.Data[i][key] = val
}
}
}
}
}
// parse node
func (n *Node) parse(ctx context.Context, method string) error {
key := n.Key
if strings.HasSuffix(key, consts.ListKeySuffix) {
key = util.RemoveSuffix(key, consts.ListKeySuffix)
}
access, err := n.Action.ActionConfig.GetAccessConfig(key, true)
if err != nil {
return err
}
n.tableName = access.Name
n.RowKey = access.RowKey
// 0. 角色替换
err = n.roleUpdate()
if err != nil {
return err
}
var accessRoles []string
if n.Action.NoAccessVerify == false {
// 1. 检查权限, 无权限就不用做参数检查了
switch method {
case http.MethodPost:
accessRoles = access.Post
case http.MethodPut:
accessRoles = access.Put
case http.MethodDelete:
accessRoles = access.Delete
}
err = n.checkAccess(ctx, method, accessRoles)
if err != nil {
return err
}
}
// 2. 检查参数
err = n.checkReq()
if err != nil {
return err
}
// 3. get where by accessCondition
err = n.whereUpdate(ctx, method, accessRoles)
n.parseReq(method)
return err
}
// update node role
func (n *Node) roleUpdate() error {
if val, exists := n.structure.Insert[consts.Role]; exists {
if n.Role == "" {
n.Role = util.String(val)
}
}
if val, exists := n.structure.Update[consts.Role]; exists {
n.Role = util.String(val)
}
return nil
}
func (n *Node) checkAccess(ctx context.Context, method string, accessRoles []string) error {
role, err := n.Action.ActionConfig.DefaultRoleFunc()(ctx, config.RoleReq{
AccessName: n.tableName,
Method: method,
NodeRole: n.Role,
})
if err != nil {
return err
}
if role == consts.DENY {
return consts.NewDenyErr(n.Key, n.Role)
}
n.Role = role
if !lo.Contains(accessRoles, role) {
return consts.NewNoAccessErr(n.Key, n.Role)
}
return nil
}
func (n *Node) whereUpdate(ctx context.Context, method string, accessRoles []string) error {
for i, item := range n.req {
condition := config.NewConditionRet()
req := model.Map{}
for k, v := range item {
k := n.Action.DbFieldStyle(ctx, n.RowKey, k)
req[k] = v
}
conditionReq := config.ConditionReq{
AccessName: n.Key,
TableAccessRoleList: accessRoles,
Method: method,
NodeRole: n.Role,
NodeReq: req,
}
err := n.Action.ActionConfig.ConditionFunc(ctx, conditionReq, condition)
if err != nil {
return err
}
if method == http.MethodPost {
for k, v := range condition.AllWhere() {
n.Data[i][k] = v
}
} else {
for k, v := range condition.AllWhere() {
n.Where[i][k] = v
}
}
}
return nil
}
func (n *Node) checkReq() error {
for _, item := range n.req {
// must
for _, key := range n.structure.Must {
if _, exists := item[key]; !exists {
return consts.NewStructureKeyNoFoundErr(n.Key + "." + key)
}
}
// refuse
if len(n.structure.Refuse) > 0 && n.structure.Refuse[0] == "!" {
if len(n.structure.Must) == 0 {
return consts.NewValidStructureErr("REFUSE为!时必须指定MUST:" + n.Key)
}
for key, _ := range item {
if !lo.Contains(n.structure.Must, key) {
return consts.NewValidStructureErr("不能包含:" + n.Key + "." + key)
}
}
} else {
for _, key := range n.structure.Refuse {
if _, exists := item[key]; exists {
return consts.NewValidStructureErr("不能包含:" + n.Key + "." + key)
}
}
}
}
return nil
}
// reqUpdate 处理 Update/Insert等
func (n *Node) reqUpdate() error {
for i, _ := range n.req {
for key, updateVal := range n.structure.Update {
if strings.HasSuffix(key, consts.FunctionsKeySuffix) {
k := key[0 : len(key)-2]
// call functions
{
actionConfig := n.Action.ActionConfig
functionName, paramKeys := util.ParseFunctionsStr(updateVal.(string))
_func := actionConfig.Func(functionName)
param := model.Map{}
for paramI, item := range _func.ParamList {
if item.Name == consts.FunctionOriReqParam {
param[item.Name] = n.Data[i]
} else {
param[item.Name] = n.Data[i][paramKeys[paramI]]
}
}
val, err := actionConfig.CallFunc(n.ctx, functionName, param)
if err != nil {
return err
}
if val != nil {
n.Data[i][k] = val
}
}
} else {
n.Data[i][key] = updateVal
}
}
for key, updateVal := range n.structure.Insert {
if _, exists := n.Data[i][key]; !exists {
n.Data[i][key] = updateVal
}
}
}
return nil
}
// reqUpdate 处理 Update/Insert等 (事务内)
func (n *Node) reqUpdateBeforeDo() error {
for i, _ := range n.req {
for k, v := range n.Data[i] {
// 处理 ref
if strings.HasSuffix(k, consts.RefKeySuffix) {
refNodeKey, refCol := util.ParseRefCol(v.(string))
if strings.HasSuffix(refNodeKey, consts.ListKeySuffix) { // 双列表
n.Data[i][k] = n.keyNode[refNodeKey].Data[i][n.Action.DbFieldStyle(n.ctx, n.tableName, refCol)]
} else {
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][n.Action.DbFieldStyle(n.ctx, n.tableName, refCol)]
}
}
}
}
return nil
}
func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error) {
var rowKeyVal model.Map
var rowKey string
access, err := n.Action.ActionConfig.GetAccessConfig(n.Key, true)
if err != nil {
return nil, err
}
switch method {
case http.MethodPost:
if access.RowKeyGen != "" {
for i, _ := range n.Data {
rowKeyVal, err = n.Action.ActionConfig.RowKeyGen(ctx, access.RowKeyGen, n.Key, n.tableName, n.Data[i])
if err != nil {
return nil, gerror.Wrap(err, "RowKeyGen")
}
for k, v := range rowKeyVal {
if k == consts.RowKey {
n.Data[i][access.RowKey] = v
} else {
n.Data[i][k] = v
}
}
}
}
rowKey = access.RowKey
case http.MethodPut:
case http.MethodDelete:
default:
return nil, consts.NewMethodNotSupportErr(method)
}
executor, err := GetActionExecutor(n.executor)
if err != nil {
return nil, err
}
ret, err = executor.Do(ctx, ActionExecutorReq{
Method: method,
Table: n.tableName,
Data: n.Data,
Where: n.Where,
Access: access,
Config: n.Action.ActionConfig,
NewQuery: n.Action.NewQuery,
})
if err != nil {
return nil, err
}
if len(n.Data) == 1 {
jsonStyle := n.Action.JsonFieldStyle
if rowKeyVal != nil {
for k, v := range rowKeyVal {
if k == consts.RowKey {
ret[jsonStyle(ctx, n.tableName, rowKey)] = v
} else {
ret[jsonStyle(ctx, n.tableName, k)] = v
}
}
}
}
n.Ret = ret
return
}
func (n *Node) execute(ctx context.Context, method string) (model.Map, error) {
err := n.reqUpdateBeforeDo()
if err != nil {
return nil, err
}
ret, err := n.do(ctx, method)
if err != nil {
return nil, err
}
n.Ret = ret
return n.Ret, nil
}