Skip to content

Commit 2be58f2

Browse files
committed
feat: rowkeyGen、hook、批量action
# feat 1. 增加rowKeyGen 2. 增加hook 3. 完成非公开请求的批量操作
1 parent 668a3b5 commit 2be58f2

File tree

13 files changed

+387
-157
lines changed

13 files changed

+387
-157
lines changed

.gitpod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tasks:
1313
- init: |
1414
mv demo/todo/config.yaml.example demo/todo/config.yaml
1515
mysql -e "CREATE DATABASE my_apijson;"
16-
mysql -p my_apijson < demo/todo/todo/todo.sql
16+
mysql -p my_apijson < demo/todo/doc/todo.sql
1717
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';"
1818
1919
command: |

README.md

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,25 @@
1-
# apijson-go [WIP]
1+
# apijson-go [下水阶段]
22
基于 go + goframe 实现的 apijson
33

4-
> 暂仍处于前期开发探索中, 请仅使用在 个人探索项目
4+
> ~~暂仍处于前期开发探索中, 请仅使用在 个人探索项目~~
55
6+
> 目前处于 【下水阶段】, 欢迎测试、issue、建议、pr
7+
8+
[RoadMap 阶段规划](./doc/roadmap.md)
69

710
# 快速体验
811
<a href="https://gitpod.io/#https://github.com/glennliao/apijson-go" target="_blank"><img src="https://gitpod.io/button/open-in-gitpod.svg" /> </a>
912

10-
创建后 执行 demo/todo/todo/tests 下的 *_test.go 访问测试
11-
12-
# 功能实现
13-
14-
- [x] 单表查询、单表数组查询
15-
- [x] 双表一对一关联查询、数组关联查询
16-
- [x] 双表一对多关联查询、数组关联查询
17-
- [x] @column, @order, @group, page, count
18-
- [x] 单表单条新增
19-
- [x] 单表单条修改
20-
- [x] 单表单条、批量删除
21-
- [x] Request表的tag校验
22-
- [x] MUST
23-
- [x] REFUSE
24-
- [x] 分页返回total@
25-
26-
- [x] 可用的权限方案
27-
- [x] get只有access中定义的才能访问
28-
- [x] 非get操作则必须与request指定一致才可请求
29-
- [x] 基于角色控制
30-
- [ ] 远程函数
31-
- [ ] 错误提示
32-
- [ ] 查询节点 自定义查询数据
33-
- [ ] 字段限制
34-
- [ ] 请求结构复杂度限制
13+
创建后 执行 demo/todo/tests 下的 *_test.go 访问测试
14+
3515

3616
## 文档参考
3717
1. [Get开放查询](./doc/query.md)
3818
2. [非开放请求](./doc/action.md)
3919
3. [权限控制](./doc/access.md)
40-
4. [roadmap](./doc/roadmap.md)
4120

4221

4322
# 开发指南
44-
4523
1. go >= 1.18
4624
2. 创建 mysql 数据库
4725
3. 导入 demo/todo/doc/todo.sql文件

README.zh-CN.md

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
1-
# apijson-go [WIP]
1+
# apijson-go [下水阶段]
22
基于 go + goframe 实现的 apijson
33

4-
> 暂仍处于前期开发探索中, 请仅使用在 个人探索项目
4+
> ~~暂仍处于前期开发探索中, 请仅使用在 个人探索项目~~
55
6+
> 目前处于 【下水阶段】, 欢迎测试、issue、建议、pr
7+
8+
[RoadMap 阶段规划](./doc/roadmap.md)
69

710
# 快速体验
811
<a href="https://gitpod.io/#https://github.com/glennliao/apijson-go" target="_blank"><img src="https://gitpod.io/button/open-in-gitpod.svg" /> </a>
912

10-
创建后 执行 demo/todo/todo/tests 下的 *_test.go 访问测试
11-
12-
# 功能实现
13-
14-
- [x] 单表查询、单表数组查询
15-
- [x] 双表一对一关联查询、数组关联查询
16-
- [x] 双表一对多关联查询、数组关联查询
17-
- [x] @column, @order, @group, page, count
18-
- [x] 单表单条新增
19-
- [x] 单表单条修改
20-
- [x] 单表单条、批量删除
21-
- [x] Request表的tag校验
22-
- [x] MUST
23-
- [x] REFUSE
24-
- [x] 分页返回total@
25-
26-
- [x] 可用的权限方案
27-
- [x] get只有access中定义的才能访问
28-
- [x] 非get操作则必须与request指定一致才可请求
29-
- [x] 基于角色控制
30-
- [ ] 远程函数
31-
- [ ] 错误提示
32-
- [ ] 查询节点 自定义查询数据
33-
- [ ] 字段限制
34-
- [ ] 请求结构复杂度限制
13+
创建后 执行 demo/todo/tests 下的 *_test.go 访问测试
14+
3515

3616
## 文档参考
3717
1. [Get开放查询](./doc/query.md)
3818
2. [非开放请求](./doc/action.md)
3919
3. [权限控制](./doc/access.md)
40-
4. [roadmap](./doc/roadmap.md)
4120

42-
# 开发指南
4321

22+
# 开发指南
4423
1. go >= 1.18
4524
2. 创建 mysql 数据库
4625
3. 导入 demo/todo/doc/todo.sql文件
@@ -49,6 +28,7 @@
4928

5029

5130

31+
5232
# 感谢
5333
- [GoFrame](https://gitee.com/johng/gf)
5434
- [APIJSON](https://gitee.com/Tencent/APIJSON)

action/action.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Action struct {
3737
err error
3838

3939
children map[string]Node
40+
keyNode map[string]*Node
4041
}
4142

4243
func New(ctx context.Context, method string, req g.Map) *Action {
@@ -54,6 +55,7 @@ func New(ctx context.Context, method string, req g.Map) *Action {
5455
method: method,
5556
req: req,
5657
children: map[string]Node{},
58+
keyNode: map[string]*Node{},
5759
}
5860
return a
5961
}
@@ -64,9 +66,15 @@ func (a *Action) parse() error {
6466

6567
for key, v := range a.req {
6668

69+
structuresKey := key
70+
if strings.HasSuffix(key, "[]") {
71+
structuresKey = structuresKey[0 : len(structuresKey)-2]
72+
}
6773
structureMap, ok := structures[key]
6874
if !ok {
69-
return gerror.New("structure错误: 400, 缺少" + key)
75+
if structureMap, ok = structures[structuresKey]; !ok { //User[]可读取User或者User[]
76+
return gerror.New("structure错误: 400, 缺少" + key)
77+
}
7078
}
7179

7280
structure := Structure{}
@@ -75,10 +83,22 @@ func (a *Action) parse() error {
7583
return err
7684
}
7785

86+
// todo 初始化时完成map2struct,不用每次都scan生成
7887
structure.Must = strings.Split(structure.Must[0], ",")
7988
structure.Refuse = strings.Split(structure.Refuse[0], ",")
8089

81-
node := newNode(key, v.(g.Map), structure)
90+
var list []g.Map
91+
_v, ok := v.(g.Map)
92+
if ok { // 将所有node都假设成列表, 如果单个则看成一个元素的批量
93+
list = []g.Map{_v}
94+
} else {
95+
list = v.([]g.Map)
96+
}
97+
98+
node := newNode(key, list, structure)
99+
node.ctx = a.ctx
100+
a.keyNode[key] = &node
101+
node.keyNode = a.keyNode
82102
err = node.parse(a.ctx, a.method)
83103
if err != nil {
84104
return err
@@ -100,7 +120,9 @@ func (a *Action) Result() (g.Map, error) {
100120
ret := g.Map{}
101121

102122
err = g.DB().Transaction(a.ctx, func(ctx context.Context, tx *gdb.TX) error {
103-
for k, node := range a.children {
123+
for _, k := range a.tagRequest.ExecQueue {
124+
125+
node := a.children[k]
104126
ret[k], err = node.execute(ctx, a.method)
105127
if err != nil {
106128
return err

action/hook.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package action
2+
3+
type Hook struct {
4+
Before func(n *Node, method string) error
5+
After func(n *Node, method string) error
6+
}
7+
8+
var hooks []Hook
9+
10+
func RegHook(h Hook) {
11+
hooks = append(hooks, h)
12+
}

0 commit comments

Comments
 (0)