Skip to content

Commit 9c23016

Browse files
authored
rowkeyGen 策略 + hook (glennliao#11)
* feat: AccessCondition 增加非等于判断的支持 * feat: rowkeyGen 策略 + hook * fix: http.MethodXXX 替代 consts.MethodXXX * fix: demo/todo/tests
1 parent 9a403e7 commit 9c23016

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+816
-536
lines changed

.github/workflows/todo.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ jobs:
3636
- name: Test
3737
run: |
3838
39-
mysql -uroot -h 127.0.0.1 --port 3306 -pyourpassword my_apijson < demo/todo/doc/todo.sql
39+
mysql -uroot -h 127.0.0.1 --port 3306 -pyourpassword my_apijson < @demo/todo/doc/todo.sql
4040
41-
cd ./demo/todo
41+
cd ./@demo/todo
4242
mv config.yaml.example config.yaml
4343
4444
cd tests

.gitpod.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ ports:
1111

1212
tasks:
1313
- init: |
14-
mv demo/todo/config.yaml.example demo/todo/config.yaml
14+
mv @demo/todo/config.yaml.example @demo/todo/config.yaml
1515
mysql -e "CREATE DATABASE my_apijson;"
16-
mysql -p my_apijson < demo/todo/doc/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: |
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,18 @@ func AccessCondition(ctx context.Context, req config.AccessConditionReq) (g.Map,
7474
switch req.Table {
7575
case "t_user":
7676
if req.NodeRole == consts.OWNER {
77-
// a=b
78-
// a in []b
79-
// a = (sql)
80-
// a in [sql]
81-
// a not in sql
82-
return g.Map{"user_id": user.UserId, "@sql": []string{"a in ?", "asd"}}, nil
77+
return g.Map{
78+
"user_id": user.UserId,
79+
80+
"@raw": g.Map{
81+
"id > 0": "",
82+
},
83+
// g.Map{
84+
// "uid <=" : 1000,
85+
// "age >=" : 18,
86+
// "x in (?)":[]string{"1","2","3"}
87+
//}
88+
}, nil
8389
}
8490
case "t_todo":
8591
if req.NodeRole == consts.OWNER {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ func init() {
5252
return gconv.String(param["title"]), nil
5353
},
5454
})
55+
5556
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package handlers
1+
package app
22

33
import (
44
"context"
55
"github.com/glennliao/apijson-go/action"
66
"github.com/glennliao/apijson-go/config"
7-
"github.com/glennliao/apijson-go/consts"
87
"github.com/glennliao/apijson-go/query"
98
"github.com/gogf/gf/v2/frame/g"
9+
"net/http"
1010
)
1111

1212
func Get(ctx context.Context, req g.Map) (res g.Map, err error) {
@@ -21,16 +21,16 @@ func Head(ctx context.Context, req g.Map) (res g.Map, err error) {
2121
}
2222

2323
func Post(ctx context.Context, req g.Map) (res g.Map, err error) {
24-
act := action.New(ctx, consts.MethodPost, req)
24+
act := action.New(ctx, http.MethodPost, req)
2525
return act.Result()
2626
}
2727

2828
func Put(ctx context.Context, req g.Map) (res g.Map, err error) {
29-
act := action.New(ctx, consts.MethodPut, req)
29+
act := action.New(ctx, http.MethodPut, req)
3030
return act.Result()
3131
}
3232

3333
func Delete(ctx context.Context, req g.Map) (res g.Map, err error) {
34-
act := action.New(ctx, consts.MethodDelete, req)
34+
act := action.New(ctx, http.MethodDelete, req)
3535
return act.Result()
3636
}

@demo/todo/app/hook.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package app
2+
3+
import (
4+
"github.com/glennliao/apijson-go/action"
5+
)
6+
7+
func init() {
8+
action.RegHook(action.Hook{
9+
For: "*",
10+
BeforeExec: nil,
11+
AfterExec: nil,
12+
BeforeDo: nil,
13+
AfterDo: nil,
14+
})
15+
16+
//action.RegHook(action.Hook{
17+
// For: "*",
18+
// BeforeExec: func(ctx context.Context, n *action.Node, method string) error {
19+
// g.Log().Debug(ctx, " iam BeforeExec", n.TableName, method)
20+
// return nil
21+
// },
22+
// AfterExec: func(ctx context.Context, n *action.Node, method string) error {
23+
// g.Log().Debug(ctx, " iam AfterExec", n.TableName, method)
24+
// return nil
25+
// },
26+
// BeforeDo: func(ctx context.Context, n *action.Node, method string) error {
27+
// g.Log().Debug(ctx, " iam BeforeDo", n.TableName, method)
28+
// return nil
29+
// },
30+
// AfterDo: func(ctx context.Context, n *action.Node, method string) error {
31+
// g.Log().Debug(ctx, " iam AfterDo", n.TableName, method)
32+
// return nil
33+
// },
34+
//})
35+
//
36+
//action.RegHook(action.Hook{
37+
// For: "t_todox",
38+
// BeforeExec: func(ctx context.Context, n *action.Node, method string) error {
39+
// g.Log().Debug(ctx, " iam BeforeExec For todo", n.TableName, method)
40+
// return nil
41+
// },
42+
// AfterExec: func(ctx context.Context, n *action.Node, method string) error {
43+
// g.Log().Debug(ctx, " iam AfterExec For todo", n.TableName, method)
44+
// return nil
45+
// },
46+
// BeforeDo: func(ctx context.Context, n *action.Node, method string) error {
47+
// g.Log().Debug(ctx, " iam BeforeDo For todo", n.TableName, method)
48+
// return nil
49+
// },
50+
// AfterDo: func(ctx context.Context, n *action.Node, method string) error {
51+
// g.Log().Debug(ctx, " iam AfterDo For todo", n.TableName, method)
52+
// return nil
53+
// },
54+
//})
55+
}

@demo/todo/app/rowkey.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package app
2+
3+
import (
4+
"context"
5+
"github.com/glennliao/apijson-go/action"
6+
"github.com/gogf/gf/v2/frame/g"
7+
"github.com/gogf/gf/v2/os/gtime"
8+
)
9+
10+
func init() {
11+
// 设置 rowKey 生成策略
12+
action.RowKeyGenFunc("time", func(ctx context.Context, genParam g.Map, table string, data g.Map) (g.Map, error) {
13+
return g.Map{
14+
"rowKey": gtime.Now().Layout("20060102150405"),
15+
}, nil
16+
})
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ server:
22
address: ":8080"
33
dumpRouterMap: false
44

5+
logger:
6+
default:
7+
level: all
58

69
database:
710
logger:
File renamed without changes.

0 commit comments

Comments
 (0)