Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2947052

Browse files
committed
change: actionExecutor
1 parentf403a04 commit2947052

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
AfterExecutorDofunc(ctx context.Context,n*Node,methodstring)error
2121
}
2222

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

2725
funcRegHook(hHook) {

‎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
RowKeystring// 主键
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
forkey,val:=rangeitem {
60+
5961
ifkey==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-
ifmethod==http.MethodDelete {
66+
key=n.action.DbFieldStyle(n.ctx,n.tableName,key)
67+
68+
switchmethod {
69+
casehttp.MethodPost:
70+
n.Data[i][key]=val
71+
casehttp.MethodDelete:
72+
n.Where[i][key]=val
73+
casehttp.MethodPut:
74+
ifkey==n.RowKey||key==n.RowKey+"{}" {
6575
n.Where[i][key]=val
6676
}else {
67-
ifkey==n.RowKey||key==n.RowKey+"{}" {
68-
ifmethod==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
iferr!=nil {
104107
returnerr
105108
}
106-
109+
varaccessRoles []string
107110
ifn.action.NoAccessVerify==false {
108111
// 1. 检查权限, 无权限就不用做参数检查了
109-
varaccessRoles []string
110112

111113
switchmethod {
112114
casehttp.MethodPost:
@@ -129,6 +131,8 @@ func (n *Node) parse(ctx context.Context, method string) error {
129131
returnerr
130132
}
131133

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

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

176+
returnnil
177+
}
178+
179+
// ? todo 整合到哪
180+
func (n*Node)whereUpdate(ctx context.Context,methodstring,accessRoles []string)error {
181+
172182
fori,item:=rangen.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
iferr!=nil {
257267
returnerr
258268
}
@@ -296,15 +306,13 @@ func (n *Node) reqUpdateBeforeDo() error {
296306
returnnil
297307
}
298308

299-
func (n*Node)do(ctx context.Context,methodstring,dataIndexint) (ret model.Map,errerror) {
309+
func (n*Node)do(ctx context.Context,methodstring) (ret model.Map,errerror) {
300310

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

306-
varcountint64
307-
308316
switchmethod {
309317
casehttp.MethodPost:
310318

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

337-
varidint64
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
iferr!=nil {
342353
returnnil,err
343354
}
344355

345-
ret= model.Map{
346-
"code":200,
347-
"count":count,
348-
"id":id,
349-
}
350-
351356
iflen(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
casehttp.MethodPut:
366-
count,err=executor.GetActionExecutor(n.executor).Update(ctx,n.tableName,n.Data[dataIndex],n.Where[dataIndex])
367-
iferr!=nil {
368-
returnnil,err
369-
}
370-
371-
ret= model.Map{
372-
"code":200,
373-
"count":count,
374-
}
375371
casehttp.MethodDelete:
376-
count,err=executor.GetActionExecutor(n.executor).Delete(ctx,n.tableName,n.Where[dataIndex])
377-
iferr!=nil {
378-
returnnil,err
379-
}
380372

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

387-
ifret==nil {
388-
returnnil,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+
iferr!=nil {
385+
returnnil,err
389386
}
390387

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

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

421-
return model.Map{
422-
"code":200,
423-
},nil
410+
n.Ret=ret
411+
returnn.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
returnact
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+
iflen(c.rawCondition)>0 {
41+
c.condition[consts.Raw]=c.rawCondition
42+
}
4143
returnc.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
typeActionConfigstruct {
99
requestConfig*RequestConfig
1010
access*Access
11-
functions*Functions
11+
functions*functions
1212
rowKeyGenFuncMapmap[string]RowKeyGenFuncHandler
1313
defaultRoleFuncDefaultRole
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
typeConfigstruct {
3030
Access*Access
3131

32-
Functions*Functions
32+
Functions*functions
3333

3434
MaxTreeWidthint
3535
MaxTreeDeepint
@@ -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
returna

‎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
typeActionExecutorinterface {
10-
Insert(ctx context.Context,tablestring,dataany) (idint64,countint64,errerror)
11-
Update(ctx context.Context,tablestring,data model.Map,where model.Map) (countint64,errerror)
12-
Delete(ctx context.Context,tablestring,where model.Map) (countint64,errerror)
10+
Do(ctx context.Context,reqActionExecutorReq) (ret model.Map,errerror)
11+
}
12+
13+
typeActionExecutorReqstruct {
14+
Methodstring
15+
Tablestring
16+
Data []model.Map
17+
Where []model.Map
1318
}
1419

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

1822
funcRegActionExecutor(namestring,eActionExecutor) {

‎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
Descstring
1414
}
1515

16-
// todo Func会和下方functions混淆,不利于代码补全
1716
typeFuncstruct {
1817
ParamList []ParamItem
1918
Handlerfunc(ctx context.Context,param model.Map) (resany,errerror)
2019
}
2120

22-
typeFunctionsstruct {
21+
typefunctionsstruct {
2322
funcMapmap[string]Func
2423
}
2524

26-
func (f*Functions)Bind(namestring,_funcFunc) {
25+
func (f*functions)Bind(namestring,_funcFunc) {
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,namestring,param g.Map) (any,error) {
32+
func (f*functions)Call(ctx context.Context,namestring,param g.Map) (any,error) {
3433
returnf.funcMap[name].Handler(ctx,param)
3534
}
3635

‎config/query_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
typeQueryConfigstruct {
1111
access*Access
12-
functions*Functions
12+
functions*functions
1313
maxTreeDeepint
1414
maxTreeWidthint
1515
defaultRoleFuncDefaultRole

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp