Skip to content

Commit 9227091

Browse files
committed
## 0.2.0-beta5 增加@alias+从json读取access/request配置
1 parent b2285c7 commit 9227091

File tree

15 files changed

+108
-37
lines changed

15 files changed

+108
-37
lines changed

@doc/v0.2.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
55
1. 由全局配置改成apijson实例,可创建不同实例对应不同的内容
66
2. 代码内部 传递的accessName 都为 _access 配置中的alias
7-
3. access/request配置可从配置文件中获取
7+
3. access/request配置可从配置文件中获取
8+
9+
# vX.x
10+
- 给action 增加then/catch/before/after等方法?

action/action.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package action
22

33
import (
44
"context"
5+
"strings"
6+
57
"github.com/glennliao/apijson-go/config"
68
"github.com/glennliao/apijson-go/consts"
79
"github.com/glennliao/apijson-go/model"
810
"github.com/gogf/gf/v2/database/gdb"
911
"github.com/gogf/gf/v2/errors/gerror"
1012
"github.com/gogf/gf/v2/frame/g"
1113
"github.com/gogf/gf/v2/util/gconv"
12-
"strings"
1314
)
1415

1516
// Action 非get查询的request表中的请求
1617
type Action struct {
1718
ctx context.Context
18-
tagRequest *config.Request
19+
tagRequest *config.RequestConfig
1920
method string
2021

2122
req model.Map
@@ -30,7 +31,7 @@ type Action struct {
3031
// 关闭 request 验证开关, 默认否
3132
NoRequestVerify bool
3233

33-
//Access *config.Access
34+
// Access *config.Access
3435

3536
// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
3637
DbFieldStyle config.FieldStyle
@@ -75,7 +76,7 @@ func (a *Action) parse() error {
7576
}
7677
structure, ok := structures[key]
7778
if !ok {
78-
if structure, ok = structures[structuresKey]; !ok { //User[]可读取User或者User[]
79+
if structure, ok = structures[structuresKey]; !ok { // User[]可读取User或者User[]
7980
return gerror.New("structure错误: 400, 缺少" + key)
8081
}
8182
}
@@ -158,7 +159,7 @@ func (a *Action) Result() (model.Map, error) {
158159
return ret, err
159160
}
160161

161-
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.Request, error) {
162+
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.RequestConfig, error) {
162163
_tag, ok := req["tag"]
163164
if !ok {
164165
return nil, gerror.New("tag 缺失")

action/hook.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const (
1010
)
1111

1212
type Hook struct {
13-
For string //
13+
For []string //
1414
// Exec 事务外
1515
BeforeNodeExec func(ctx context.Context, n *Node, method string) error
1616
AfterNodeExec func(ctx context.Context, n *Node, method string) error
@@ -23,7 +23,9 @@ type Hook struct {
2323
var hooksMap = map[string][]Hook{}
2424

2525
func RegHook(h Hook) {
26-
hooksMap[h.For] = append(hooksMap[h.For], h)
26+
for _, item := range h.For {
27+
hooksMap[item] = append(hooksMap[item], h)
28+
}
2729
}
2830

2931
func EmitHook(ctx context.Context, hookAt int, node *Node, method string) error {

config/action_config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package config
22

33
import (
44
"context"
5+
56
"github.com/glennliao/apijson-go/model"
67
)
78

89
type ActionConfig struct {
9-
requestConfig *RequestConfig
10+
requestConfig *RequestConfigs
1011
access *Access
1112
functions *functions
1213
rowKeyGenFuncMap map[string]RowKeyGenFuncHandler
@@ -29,7 +30,7 @@ func (c *ActionConfig) Func(name string) Func {
2930
return c.functions.funcMap[name]
3031
}
3132

32-
func (c *ActionConfig) GetRequest(tag string, method string, version string) (*Request, error) {
33+
func (c *ActionConfig) GetRequest(tag string, method string, version string) (*RequestConfig, error) {
3334
return c.requestConfig.GetRequest(tag, method, version)
3435
}
3536

config/config.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func RegAccessListProvider(name string, provider AccessListProvider) {
1010
accessListProviderMap[name] = provider
1111
}
1212

13-
type RequestListProvider func(ctx context.Context) []Request
13+
type RequestListProvider func(ctx context.Context) []RequestConfig
1414

1515
var requestListProviderMap = make(map[string]RequestListProvider)
1616

@@ -50,9 +50,9 @@ type Config struct {
5050

5151
accessList []AccessConfig
5252

53-
requestConfig *RequestConfig
54-
queryConfig *QueryConfig
55-
actionConfig *ActionConfig
53+
requestConfigs *RequestConfigs
54+
queryConfig *QueryConfig
55+
actionConfig *ActionConfig
5656
}
5757

5858
func New() *Config {
@@ -117,7 +117,7 @@ func (c *Config) ReLoad() {
117117
requestListProvider := requestListProviderMap[c.RequestListProvider]
118118
if requestListProvider != nil {
119119
requestList := requestListProvider(ctx)
120-
c.requestConfig = NewRequestConfig(requestList)
120+
c.requestConfigs = NewRequestConfig(requestList)
121121
}
122122

123123
dbMetaProvider := dbMetaProviderMap[c.DbMetaProvider]
@@ -134,7 +134,7 @@ func (c *Config) ReLoad() {
134134
}
135135

136136
c.actionConfig = &ActionConfig{
137-
requestConfig: c.requestConfig,
137+
requestConfig: c.requestConfigs,
138138
access: c.Access,
139139
functions: c.Functions,
140140
rowKeyGenFuncMap: c.rowKeyGenFuncMap,

config/functions.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/glennliao/apijson-go/model"
78
"github.com/gogf/gf/v2/frame/g"
89
)
@@ -15,6 +16,7 @@ type ParamItem struct {
1516

1617
type Func struct {
1718
ParamList []ParamItem
19+
Batch bool // 是否为批量处理, 例如在获取列表后一次性将id传入, 然后按照传入的参数数组返回结果数组
1820
Handler func(ctx context.Context, param model.Map) (res any, err error)
1921
}
2022

config/query_config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package config
22

33
import (
4-
"github.com/samber/lo"
54
"net/http"
5+
6+
"github.com/samber/lo"
67
)
78

89
type QueryConfig struct {

config/request_config.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package config
22

33
import (
4+
"strings"
5+
46
"github.com/glennliao/apijson-go/consts"
57
"github.com/gogf/gf/v2/errors/gerror"
68
"github.com/gogf/gf/v2/frame/g"
79
"github.com/gogf/gf/v2/os/gtime"
810
"github.com/gogf/gf/v2/util/gconv"
9-
"strings"
1011
)
1112

12-
type Request struct {
13+
type RequestConfig struct {
1314
Debug int8
1415
Version string
1516
Method string
@@ -38,13 +39,13 @@ type Structure struct {
3839
Remove []string `json:"REMOVE,omitempty"`
3940
}
4041

41-
type RequestConfig struct {
42-
requestMap map[string]*Request
42+
type RequestConfigs struct {
43+
requestMap map[string]*RequestConfig
4344
}
4445

45-
func NewRequestConfig(requestList []Request) *RequestConfig {
46-
c := RequestConfig{}
47-
requestMap := make(map[string]*Request)
46+
func NewRequestConfig(requestList []RequestConfig) *RequestConfigs {
47+
c := RequestConfigs{}
48+
requestMap := make(map[string]*RequestConfig)
4849

4950
for _, _item := range requestList {
5051
item := _item
@@ -71,7 +72,7 @@ func getRequestFullKey(tag string, method string, version string) string {
7172
return tag + "@" + method + "@" + version
7273
}
7374

74-
func (c *RequestConfig) GetRequest(tag string, method string, version string) (*Request, error) {
75+
func (c *RequestConfigs) GetRequest(tag string, method string, version string) (*RequestConfig, error) {
7576

7677
if version == "" || version == "-1" || version == "0" {
7778
version = "latest"

consts/node.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
Count = "count" // page size
1919
Total = "total"
2020
Query = "query"
21+
Alias = "@alias"
2122
)
2223

2324
const (

drivers/goframe/config/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var (
1616
ProviderName = "db"
1717
)
1818

19-
func RequestListProvider(ctx context.Context) []config.Request {
20-
var requestList []config.Request
19+
func RequestListProvider(ctx context.Context) []config.RequestConfig {
20+
var requestList []config.RequestConfig
2121
err := g.DB().Model(TableRequest).OrderAsc("version").Scan(&requestList)
2222
if err != nil {
2323
panic(err)
@@ -31,14 +31,14 @@ func RequestListProvider(ctx context.Context) []config.Request {
3131
}
3232

3333
// provider处理
34-
//if strings.ToLower(tag) != tag {
34+
// if strings.ToLower(tag) != tag {
3535
// // 本身大写, 如果没有外层, 则套一层
3636
// if _, ok := item.Structure[tag]; !ok {
3737
// item.Structure = map[string]any{
3838
// tag: item.Structure,
3939
// }
4040
// }
41-
//}
41+
// }
4242

4343
for k, v := range item.Structure {
4444
structure := config.Structure{}

drivers/json/config/config.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package config
2+
3+
import (
4+
"context"
5+
6+
"github.com/glennliao/apijson-go/config"
7+
"github.com/gogf/gf/v2/util/gconv"
8+
)
9+
10+
func RequestListProvider(ctx context.Context, jsonStr string) config.RequestListProvider {
11+
12+
return func(ctx context.Context) []config.RequestConfig {
13+
var requestList []config.RequestConfig
14+
err := gconv.Scan(jsonStr, &requestList)
15+
if err != nil {
16+
panic(err)
17+
}
18+
for i, request := range requestList {
19+
if _, ok := request.Structure[request.Tag]; !ok {
20+
requestList[i].Structure = map[string]*config.Structure{
21+
request.Tag: {
22+
Must: nil,
23+
Refuse: nil,
24+
Unique: nil,
25+
Insert: nil,
26+
Update: nil,
27+
Replace: nil,
28+
Remove: nil,
29+
},
30+
}
31+
}
32+
}
33+
return requestList
34+
}
35+
36+
}
37+
38+
func AccessListProvider(ctx context.Context, jsonStr string) config.AccessListProvider {
39+
return func(ctx context.Context) []config.AccessConfig {
40+
var accessList []config.AccessConfig
41+
err := gconv.Scan(jsonStr, &accessList)
42+
if err != nil {
43+
panic(err)
44+
}
45+
return accessList
46+
}
47+
}

query/node.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Node struct {
6060

6161
// 节点的请求数据
6262
req model.Map
63-
simpleReqVal string //非对象结构
63+
simpleReqVal string // 非对象结构
6464

6565
// 节点数据执行器
6666
executor executor.QueryExecutor
@@ -71,6 +71,8 @@ type Node struct {
7171
// 执行完毕
7272
finish bool
7373

74+
later bool // 后续执行
75+
7476
ret any
7577
err error
7678

@@ -178,7 +180,7 @@ func (n *Node) buildChild() error {
178180
return nil
179181
}
180182

181-
//最大深度检查
183+
// 最大深度检查
182184
maxDeep := n.queryContext.queryConfig.MaxTreeDeep()
183185
if len(strings.Split(n.Path, "/")) > maxDeep {
184186
return gerror.Newf("deep(%s) > %d", n.Path, maxDeep)

query/node_func.go

+8
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ func (h *funcNode) fetch() {
2525

2626
_func := queryConfig.Func(functionName)
2727

28+
if n.isList && _func.Batch {
29+
n.later = true
30+
return
31+
}
32+
2833
param := model.Map{}
2934

3035
for i, item := range _func.ParamList {
3136
valNode := n.queryContext.pathNodes[paramKeys[i]]
37+
// if valNode == nil {
38+
// continue
39+
// }
3240
if valNode.ret != nil {
3341
param[item.Name] = valNode.ret
3442
} else {

query/node_query.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (q *queryNode) fetch() {
261261
functionName, paramKeys := util.ParseFunctionsStr(v.(string))
262262
_func := queryConfig.Func(functionName)
263263

264-
if n.isList {
264+
if n.isList && n.ret != nil {
265265
for i, item := range n.ret.([]model.Map) {
266266

267267
param := model.Map{}

query/node_struct.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package query
22

33
import (
4+
"path/filepath"
5+
"strings"
6+
47
"github.com/glennliao/apijson-go/consts"
58
"github.com/glennliao/apijson-go/model"
69
"github.com/gogf/gf/v2/errors/gerror"
7-
"path/filepath"
8-
"strings"
910
)
1011

1112
type structNode struct {
@@ -134,9 +135,9 @@ func (h *structNode) result() {
134135
k = k[0 : len(k)-2]
135136
}
136137

137-
// todo 增加alias 用来重命名返回的key,避免前端调整取值
138-
if node.req["@alias"] != nil {
139-
k = node.req["@alias"].(string)
138+
// 增加alias 用来重命名返回的key,避免前端调整取值
139+
if node.req[consts.Alias] != nil {
140+
k = node.req[consts.Alias].(string)
140141
}
141142

142143
retMap[k], err = node.Result()
@@ -148,6 +149,7 @@ func (h *structNode) result() {
148149
n.err = err
149150
}
150151
}
152+
151153
n.ret = retMap
152154
}
153155
}

0 commit comments

Comments
 (0)