Skip to content

Commit 9a403e7

Browse files
authored
feat: 合并_access_ext和_request_ext 到 _access和_request (glennliao#8)
* feat: 合并_access_ext和_request_ext 到 _access和_request * fix: update sql and ci
1 parent 6e026a6 commit 9a403e7

File tree

18 files changed

+409
-326
lines changed

18 files changed

+409
-326
lines changed

.github/workflows/todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Todo tests
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: [ "main" ,"dev"]
66
pull_request:
7-
branches: [ "main" ]
7+
branches: [ "main", "dev" ]
88

99
jobs:
1010

action/action.go

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,6 @@ import (
1010
"strings"
1111
)
1212

13-
type Structure struct {
14-
Must []string `json:"MUST,omitempty"`
15-
Refuse []string `json:"REFUSE,omitempty"`
16-
17-
Unique []string `json:"UNIQUE,omitempty"`
18-
19-
// 不存在时添加
20-
Insert g.Map `json:"INSERT,omitempty"`
21-
// 不存在时就添加,存在时就修改
22-
Update g.Map `json:"UPDATE,omitempty"`
23-
// 存在时替换
24-
Replace g.Map `json:"REPLACE,omitempty"`
25-
// 存在时移除
26-
Remove []string `json:"REMOVE,omitempty"`
27-
}
28-
2913
// Action 非get查询的request表中的请求
3014
type Action struct {
3115
ctx context.Context
@@ -36,7 +20,7 @@ type Action struct {
3620

3721
err error
3822

39-
children map[string]Node
23+
children map[string]*Node
4024
keyNode map[string]*Node
4125
}
4226

@@ -55,7 +39,7 @@ func New(ctx context.Context, method string, req g.Map) *Action {
5539
tagRequest: request,
5640
method: method,
5741
req: req,
58-
children: map[string]Node{},
42+
children: map[string]*Node{},
5943
keyNode: map[string]*Node{},
6044
}
6145
return a
@@ -71,23 +55,13 @@ func (a *Action) parse() error {
7155
if strings.HasSuffix(key, "[]") {
7256
structuresKey = structuresKey[0 : len(structuresKey)-2]
7357
}
74-
structureMap, ok := structures[key]
58+
structure, ok := structures[key]
7559
if !ok {
76-
if structureMap, ok = structures[structuresKey]; !ok { //User[]可读取User或者User[]
60+
if structure, ok = structures[structuresKey]; !ok { //User[]可读取User或者User[]
7761
return gerror.New("structure错误: 400, 缺少" + key)
7862
}
7963
}
8064

81-
structure := Structure{}
82-
err := gconv.Scan(structureMap, &structure)
83-
if err != nil {
84-
return err
85-
}
86-
87-
// todo 初始化时完成map2struct,不用每次都scan生成
88-
structure.Must = strings.Split(structure.Must[0], ",")
89-
structure.Refuse = strings.Split(structure.Refuse[0], ",")
90-
9165
var list []g.Map
9266
_v, ok := v.(g.Map)
9367
if ok { // 将所有node都假设成列表, 如果单个则看成一个元素的批量
@@ -100,12 +74,12 @@ func (a *Action) parse() error {
10074
node.ctx = a.ctx
10175
a.keyNode[key] = &node
10276
node.keyNode = a.keyNode
103-
err = node.parse(a.ctx, a.method)
77+
err := node.parse(a.ctx, a.method)
10478
if err != nil {
10579
return err
10680
}
10781

108-
a.children[key] = node
82+
a.children[key] = &node
10983
}
11084

11185
return nil
@@ -120,6 +94,28 @@ func (a *Action) Result() (g.Map, error) {
12094

12195
ret := g.Map{}
12296

97+
for _, hook := range hooks {
98+
if hook.BeforeExec != nil {
99+
for _, k := range a.tagRequest.ExecQueue {
100+
node := a.children[k]
101+
err = hook.BeforeExec(node, a.method)
102+
if err != nil {
103+
return nil, err
104+
}
105+
}
106+
107+
}
108+
}
109+
110+
for _, k := range a.tagRequest.ExecQueue {
111+
112+
node := a.children[k]
113+
err = node.reqUpdate()
114+
if err != nil {
115+
return nil, err
116+
}
117+
}
118+
123119
err = g.DB().Transaction(a.ctx, func(ctx context.Context, tx *gdb.TX) error {
124120
for _, k := range a.tagRequest.ExecQueue {
125121

@@ -132,5 +128,22 @@ func (a *Action) Result() (g.Map, error) {
132128
return nil
133129
})
134130

131+
if err != nil {
132+
return nil, err
133+
}
134+
135+
for _, hook := range hooks {
136+
if hook.AfterExec != nil {
137+
for _, k := range a.tagRequest.ExecQueue {
138+
node := a.children[k]
139+
err = hook.AfterExec(node, a.method)
140+
if err != nil {
141+
return nil, err
142+
}
143+
}
144+
145+
}
146+
}
147+
135148
return ret, err
136149
}

action/hook.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package action
22

33
type Hook struct {
4-
// todo 区分 事务内和事务外的hook, 减少事务时长
5-
Before func(n *Node, method string) error
6-
After func(n *Node, method string) error
4+
// Exec 事务外
5+
BeforeExec func(n *Node, method string) error
6+
AfterExec func(n *Node, method string) error
7+
8+
// Do 事务内
9+
BeforeDo func(n *Node, method string) error
10+
AfterDo func(n *Node, method string) error
711
}
812

913
var hooks []Hook

action/node.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ type Node struct {
2424
Where []g.Map // 条件
2525
RowKey string // 主键
2626

27-
structure Structure
27+
structure *db.Structure
2828

2929
keyNode map[string]*Node
3030
}
3131

32-
func newNode(key string, req []g.Map, structure Structure) Node {
32+
func newNode(key string, req []g.Map, structure *db.Structure) Node {
3333
return Node{
3434
Key: key, req: req, structure: structure,
3535
}
@@ -72,7 +72,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
7272

7373
key := n.Key
7474
if strings.HasSuffix(key, consts.ListKeySuffix) {
75-
key = key[0 : len(key)-2] // todo 提取util, 获取非数组的key
75+
key = key[0 : len(key)-2]
7676
}
7777
access, err := db.GetAccess(key, true)
7878

@@ -216,6 +216,7 @@ func (n *Node) checkReq() error {
216216
return nil
217217
}
218218

219+
// reqUpdate 处理 Update/Insert等
219220
func (n *Node) reqUpdate() error {
220221

221222
for i, _ := range n.req {
@@ -224,11 +225,11 @@ func (n *Node) reqUpdate() error {
224225
if strings.HasSuffix(key, consts.FunctionsKeySuffix) {
225226
functionName, paramKeys := functions.ParseFunctionsStr(updateVal.(string))
226227
var param = g.Map{}
227-
for _, key := range paramKeys {
228-
if key == "$req" {
229-
param[key] = n.Data[i]
228+
for _, paramKey := range paramKeys {
229+
if paramKey == consts.FunctionOriReqParam {
230+
param[paramKey] = n.Data[i]
230231
} else {
231-
param[key] = n.Data[i][key]
232+
param[paramKey] = n.Data[i][paramKey]
232233
}
233234
}
234235
k := key[0 : len(key)-2]
@@ -250,10 +251,20 @@ func (n *Node) reqUpdate() error {
250251
}
251252
}
252253

254+
}
255+
256+
return nil
257+
}
258+
259+
// reqUpdate 处理 Update/Insert等 (事务内)
260+
func (n *Node) reqUpdateBeforeDo() error {
261+
262+
for i, _ := range n.req {
263+
253264
for k, v := range n.Data[i] {
254-
if strings.HasSuffix(k, "@") {
265+
if strings.HasSuffix(k, consts.RefKeySuffix) {
255266
refNodeKey, refCol := parseRefCol(v.(string))
256-
if strings.HasSuffix(refNodeKey, "[]") { // 双列表
267+
if strings.HasSuffix(refNodeKey, consts.ListKeySuffix) { // 双列表
257268
n.Data[i][k] = n.keyNode[refNodeKey].Data[i][config.GetDbFieldStyle()(n.ctx, n.TableName, refCol)]
258269
} else {
259270
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][config.GetDbFieldStyle()(n.ctx, n.TableName, refCol)]
@@ -265,18 +276,19 @@ func (n *Node) reqUpdate() error {
265276
return nil
266277
}
267278

268-
func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err error) {
279+
func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map, err error) {
269280

270-
// todo 此处运行会导致事务时长与hook时长相关,特别是hook中运行了io类型的操作, 故需要调整到事务外去执行, 且如果事务失败, 则不执行after, 可以改成增加error
271281
for _, hook := range hooks {
272-
if hook.Before != nil {
273-
err := hook.Before(n, method)
282+
if hook.BeforeDo != nil {
283+
err = hook.BeforeDo(n, method)
274284
if err != nil {
275285
return nil, err
276286
}
277287
}
278288
}
279289

290+
var count int64
291+
280292
switch method {
281293
case consts.MethodPost:
282294

@@ -295,7 +307,9 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
295307

296308
}
297309

298-
id, count, err := db.Insert(ctx, n.TableName, n.Data)
310+
var id int64
311+
312+
id, count, err = db.Insert(ctx, n.TableName, n.Data)
299313
if err != nil {
300314
return nil, err
301315
}
@@ -317,7 +331,7 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
317331
}
318332

319333
case consts.MethodPut:
320-
count, err := db.Update(ctx, n.TableName, n.Data[i], n.Where[i])
334+
count, err = db.Update(ctx, n.TableName, n.Data[dataIndex], n.Where[dataIndex])
321335
if err != nil {
322336
return nil, err
323337
}
@@ -327,7 +341,7 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
327341
"count": count,
328342
}
329343
case consts.MethodDelete:
330-
count, err := db.Delete(ctx, n.TableName, n.Where[i])
344+
count, err = db.Delete(ctx, n.TableName, n.Where[dataIndex])
331345
if err != nil {
332346
return nil, err
333347
}
@@ -343,8 +357,8 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
343357
}
344358

345359
for _, hook := range hooks {
346-
if hook.After != nil {
347-
err := hook.After(n, method)
360+
if hook.AfterDo != nil {
361+
err = hook.AfterDo(n, method)
348362
if err != nil {
349363
return nil, err
350364
}
@@ -356,14 +370,11 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
356370

357371
func (n *Node) execute(ctx context.Context, method string) (g.Map, error) {
358372

359-
// 参数替换
360-
err := n.reqUpdate() // todo 处理放到事务外, 减短事务时长
373+
err := n.reqUpdateBeforeDo()
361374
if err != nil {
362375
return nil, err
363376
}
364377

365-
// 执行操作
366-
367378
if method == consts.MethodPost { // 新增时可以合并新增
368379
ret, err := n.do(ctx, method, 0)
369380
if err != nil {
@@ -372,12 +383,11 @@ func (n *Node) execute(ctx context.Context, method string) (g.Map, error) {
372383
return ret, nil
373384
} else {
374385
for i, _ := range n.req {
375-
_, err = n.do(ctx, method, i)
386+
_, err := n.do(ctx, method, i)
376387
if err != nil {
377388
return nil, err
378389
}
379390
}
380-
381391
}
382392

383393
return g.Map{

config/access.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,10 @@ var (
3838
}
3939
)
4040

41-
// 自定义设置从ctx获取用户id和角色的key
42-
43-
var (
44-
UserIdKey = "ajg.userId"
45-
)
46-
4741
// 设置 _access/_request 自定义表名
4842
var (
49-
TableAccess = "_access"
50-
TableAccessExt = "_access_ext"
51-
TableRequest = "_request"
52-
TableRequestExt = "_request_ext"
43+
TableAccess = "_access"
44+
TableRequest = "_request"
5345
)
5446

5547
// ========================= 角色 =======================
@@ -60,7 +52,6 @@ var (
6052
roleList = []string{consts.UNKNOWN, consts.LOGIN, consts.OWNER, consts.ADMIN}
6153
)
6254

63-
// AddRole 增加自定义角色
6455
func AddRole(name string) {
6556
if !lo.Contains(roleList, name) {
6657
roleList = append(roleList, name)

consts/node.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const MaxTreeWidth = 5
44
const MaxTreeDeep = 5
55

66
const (
7-
ListKeySuffix = "[]"
8-
FunctionsKeySuffix = "()"
7+
ListKeySuffix = "[]"
8+
RefKeySuffix = "@"
9+
FunctionsKeySuffix = "()"
10+
FunctionOriReqParam = "$req"
911
)

consts/op.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ package consts
22

33
const (
44
Role = "@role"
5+
6+
Page = "page" // page num
7+
Count = "count" // page size
8+
Query = "query"
59
)

0 commit comments

Comments
 (0)