Skip to content

Commit 2947052

Browse files
committed
change: actionExecutor
1 parent f403a04 commit 2947052

File tree

16 files changed

+181
-108
lines changed

16 files changed

+181
-108
lines changed

@doc/action.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# 非开放请求
22

3+
structures 不能省略层级, 可为空(则代表所有内容都开放)

action/action.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type Action struct {
3838
// jsonFieldStyle 数据库返回的字段
3939
JsonFieldStyle config.FieldStyle
4040

41-
Functions *config.Functions
4241
actionConfig *config.ActionConfig
4342
}
4443

action/hook.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ type Hook struct {
2020
AfterExecutorDo func(ctx context.Context, n *Node, method string) error
2121
}
2222

23-
// todo hook可获取到executor的返回值
24-
2523
var hooksMap = map[string][]Hook{}
2624

2725
func RegHook(h Hook) {

action/node.go

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Node struct {
2424

2525
Data []model.Map // 需写入数据库的数据
2626
Where []model.Map // 条件
27+
Ret model.Map // 节点返回值
2728
RowKey string // 主键
2829

2930
structure *config.Structure
@@ -56,25 +57,27 @@ func (n *Node) parseReq(method string) {
5657
n.Where = append(n.Where, model.Map{})
5758

5859
for key, val := range item {
60+
5961
if key == consts.Role {
6062
n.Role = util.String(val)
61-
} else {
62-
key = n.action.DbFieldStyle(n.ctx, n.tableName, key)
63+
continue
64+
}
6365

64-
if method == http.MethodDelete {
66+
key = n.action.DbFieldStyle(n.ctx, n.tableName, key)
67+
68+
switch method {
69+
case http.MethodPost:
70+
n.Data[i][key] = val
71+
case http.MethodDelete:
72+
n.Where[i][key] = val
73+
case http.MethodPut:
74+
if key == n.RowKey || key == n.RowKey+"{}" {
6575
n.Where[i][key] = val
6676
} else {
67-
if key == n.RowKey || key == n.RowKey+"{}" {
68-
if method == http.MethodPut {
69-
n.Where[i][key] = val
70-
} else {
71-
n.Data[i][key] = val
72-
}
73-
} else {
74-
n.Data[i][key] = val
75-
}
77+
n.Data[i][key] = val
7678
}
7779
}
80+
7881
}
7982
}
8083

@@ -103,10 +106,9 @@ func (n *Node) parse(ctx context.Context, method string) error {
103106
if err != nil {
104107
return err
105108
}
106-
109+
var accessRoles []string
107110
if n.action.NoAccessVerify == false {
108111
// 1. 检查权限, 无权限就不用做参数检查了
109-
var accessRoles []string
110112

111113
switch method {
112114
case http.MethodPost:
@@ -129,6 +131,8 @@ func (n *Node) parse(ctx context.Context, method string) error {
129131
return err
130132
}
131133

134+
n.whereUpdate(ctx, method, accessRoles)
135+
132136
return nil
133137
}
134138

@@ -169,12 +173,18 @@ func (n *Node) checkAccess(ctx context.Context, method string, accessRoles []str
169173
return gerror.Newf("node not access: %s with %s", n.Key, n.Role)
170174
}
171175

176+
return nil
177+
}
178+
179+
// ? todo 整合到哪
180+
func (n *Node) whereUpdate(ctx context.Context, method string, accessRoles []string) error {
181+
172182
for i, item := range n.req {
173183

174184
condition := config.NewConditionRet()
175185

176186
conditionReq := config.ConditionReq{
177-
AccessName: n.tableName,
187+
AccessName: n.Key,
178188
TableAccessRoleList: accessRoles,
179189
Method: method,
180190
NodeRole: n.Role,
@@ -252,7 +262,7 @@ func (n *Node) reqUpdate() error {
252262
}
253263
}
254264
k := key[0 : len(key)-2]
255-
val, err := n.action.Functions.Call(n.ctx, functionName, param)
265+
val, err := n.action.actionConfig.CallFunc(n.ctx, functionName, param)
256266
if err != nil {
257267
return err
258268
}
@@ -296,15 +306,13 @@ func (n *Node) reqUpdateBeforeDo() error {
296306
return nil
297307
}
298308

299-
func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.Map, err error) {
309+
func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error) {
300310

301311
err = EmitHook(ctx, BeforeExecutorDo, n, method)
302312
if err != nil {
303313
return nil, err
304314
}
305315

306-
var count int64
307-
308316
switch method {
309317
case http.MethodPost:
310318

@@ -334,20 +342,17 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
334342
}
335343
}
336344

337-
var id int64
338-
339-
id, count, err = executor.GetActionExecutor(n.executor).Insert(ctx, n.tableName, n.Data)
345+
ret, err := executor.GetActionExecutor(n.executor).Do(ctx, executor.ActionExecutorReq{
346+
Method: method,
347+
Table: n.tableName,
348+
Data: n.Data,
349+
Where: nil,
350+
})
340351

341352
if err != nil {
342353
return nil, err
343354
}
344355

345-
ret = model.Map{
346-
"code": 200,
347-
"count": count,
348-
"id": id,
349-
}
350-
351356
if len(n.Data) > 0 { //多条插入时返回值已经应该无意义了
352357

353358
jsonStyle := n.action.JsonFieldStyle
@@ -363,31 +368,25 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
363368
}
364369

365370
case http.MethodPut:
366-
count, err = executor.GetActionExecutor(n.executor).Update(ctx, n.tableName, n.Data[dataIndex], n.Where[dataIndex])
367-
if err != nil {
368-
return nil, err
369-
}
370-
371-
ret = model.Map{
372-
"code": 200,
373-
"count": count,
374-
}
375371
case http.MethodDelete:
376-
count, err = executor.GetActionExecutor(n.executor).Delete(ctx, n.tableName, n.Where[dataIndex])
377-
if err != nil {
378-
return nil, err
379-
}
380372

381-
ret = model.Map{
382-
"code": 200,
383-
"count": count,
384-
}
373+
default:
374+
return nil, gerror.New("undefined method:" + method)
385375
}
386376

387-
if ret == nil {
388-
return nil, gerror.New("undefined method:" + method)
377+
ret, err = executor.GetActionExecutor(n.executor).Do(ctx, executor.ActionExecutorReq{
378+
Method: method,
379+
Table: n.tableName,
380+
Data: n.Data,
381+
Where: n.Where,
382+
})
383+
384+
if err != nil {
385+
return nil, err
389386
}
390387

388+
n.Ret = ret
389+
391390
err = EmitHook(ctx, AfterExecutorDo, n, method)
392391
if err != nil {
393392
return nil, err
@@ -403,22 +402,11 @@ func (n *Node) execute(ctx context.Context, method string) (model.Map, error) {
403402
return nil, err
404403
}
405404

406-
if method == http.MethodPost { // 新增时可以合并新增
407-
ret, err := n.do(ctx, method, 0)
408-
if err != nil {
409-
return nil, err
410-
}
411-
return ret, nil
412-
} else {
413-
for i, _ := range n.req {
414-
_, err := n.do(ctx, method, i)
415-
if err != nil {
416-
return nil, err
417-
}
418-
}
405+
ret, err := n.do(ctx, method)
406+
if err != nil {
407+
return nil, err
419408
}
420409

421-
return model.Map{
422-
"code": 200,
423-
}, nil
410+
n.Ret = ret
411+
return n.Ret, nil
424412
}

apijson.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func (a *ApiJson) NewQuery(ctx context.Context, req model.Map) *query.Query {
6060
q := query.New(ctx, a.Config().QueryConfig(), req)
6161

6262
q.DbMeta = a.config.DbMeta
63-
q.Functions = a.config.Functions
6463
q.DbFieldStyle = a.config.DbFieldStyle
6564
q.JsonFieldStyle = a.config.JsonFieldStyle
6665

@@ -76,7 +75,6 @@ func (a *ApiJson) NewAction(ctx context.Context, method string, req model.Map) *
7675
act.NoAccessVerify = a.config.Access.NoVerify
7776
act.DbFieldStyle = a.config.DbFieldStyle
7877
act.JsonFieldStyle = a.config.JsonFieldStyle
79-
act.Functions = a.config.Functions
8078

8179
return act
8280
}

config/access.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func (c *ConditionRet) AddRaw(k string, v any) {
3737
}
3838

3939
func (c *ConditionRet) Where() map[string]any {
40-
c.condition[consts.Raw] = c.rawCondition
40+
if len(c.rawCondition) > 0 {
41+
c.condition[consts.Raw] = c.rawCondition
42+
}
4143
return c.condition
4244
}
4345

config/action_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type ActionConfig struct {
99
requestConfig *RequestConfig
1010
access *Access
11-
functions *Functions
11+
functions *functions
1212
rowKeyGenFuncMap map[string]RowKeyGenFuncHandler
1313
defaultRoleFunc DefaultRole
1414
}

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func RegDbMetaProvider(name string, provider DbMetaProvider) {
2929
type Config struct {
3030
Access *Access
3131

32-
Functions *Functions
32+
Functions *functions
3333

3434
MaxTreeWidth int
3535
MaxTreeDeep int
@@ -70,7 +70,7 @@ func New() *Config {
7070
a.DbFieldStyle = CaseSnake
7171
a.JsonFieldStyle = CaseCamel
7272

73-
a.Functions = &Functions{}
73+
a.Functions = &functions{}
7474
a.Functions.funcMap = make(map[string]Func)
7575

7676
return a

config/executor/action.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import (
77
)
88

99
type ActionExecutor interface {
10-
Insert(ctx context.Context, table string, data any) (id int64, count int64, err error)
11-
Update(ctx context.Context, table string, data model.Map, where model.Map) (count int64, err error)
12-
Delete(ctx context.Context, table string, where model.Map) (count int64, err error)
10+
Do(ctx context.Context, req ActionExecutorReq) (ret model.Map, err error)
11+
}
12+
13+
type ActionExecutorReq struct {
14+
Method string
15+
Table string
16+
Data []model.Map
17+
Where []model.Map
1318
}
1419

15-
// todo 调整executor
1620
var actionExecutorMap = map[string]ActionExecutor{}
1721

1822
func RegActionExecutor(name string, e ActionExecutor) {

config/functions.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ type ParamItem struct {
1313
Desc string
1414
}
1515

16-
// todo Func会和下方functions混淆,不利于代码补全
1716
type Func struct {
1817
ParamList []ParamItem
1918
Handler func(ctx context.Context, param model.Map) (res any, err error)
2019
}
2120

22-
type Functions struct {
21+
type functions struct {
2322
funcMap map[string]Func
2423
}
2524

26-
func (f *Functions) Bind(name string, _func Func) {
25+
func (f *functions) Bind(name string, _func Func) {
2726
if _, exists := f.funcMap[name]; exists {
2827
panic(fmt.Errorf(" function %s has exists", name))
2928
}
3029
f.funcMap[name] = _func
3130
}
3231

33-
func (f *Functions) Call(ctx context.Context, name string, param g.Map) (any, error) {
32+
func (f *functions) Call(ctx context.Context, name string, param g.Map) (any, error) {
3433
return f.funcMap[name].Handler(ctx, param)
3534
}
3635

0 commit comments

Comments
 (0)