Skip to content

Commit d9e5319

Browse files
committed
feat: maxCount
1 parent 2947052 commit d9e5319

File tree

16 files changed

+150
-128
lines changed

16 files changed

+150
-128
lines changed

action/hook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func RegHook(h Hook) {
2828

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

31-
hooks := append(hooksMap["*"], hooksMap[node.AccessName]...)
31+
hooks := append(hooksMap["*"], hooksMap[node.Key]...)
3232
for _, hook := range hooks {
3333

3434
var handler func(ctx context.Context, n *Node, method string) error

action/node.go

+21-17
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
)
1515

1616
type Node struct {
17-
req []model.Map
18-
ctx context.Context
19-
action *Action
20-
Key string
21-
tableName string
22-
AccessName string
23-
Role string
17+
req []model.Map
18+
ctx context.Context
19+
action *Action
20+
Key string
21+
IsList bool
22+
tableName string
23+
Role string
2424

2525
Data []model.Map // 需写入数据库的数据
2626
Where []model.Map // 条件
@@ -37,16 +37,19 @@ type Node struct {
3737

3838
func newNode(key string, req []model.Map, structure *config.Structure, executor string) Node {
3939

40-
accessName := key
41-
if strings.HasSuffix(accessName, "[]") {
42-
accessName = accessName[0 : len(accessName)-2]
40+
n := Node{
41+
Key: key, req: req, structure: structure, executor: executor,
4342
}
4443

45-
return Node{
46-
Key: key, req: req, structure: structure, executor: executor, AccessName: accessName,
44+
if strings.HasSuffix(key, consts.ListKeySuffix) {
45+
n.Key = key[0 : len(key)-len(consts.ListKeySuffix)]
46+
n.IsList = true
4747
}
48+
49+
return n
4850
}
4951

52+
// parse req data to data/where
5053
func (n *Node) parseReq(method string) {
5154
n.Data = []model.Map{}
5255
n.Where = []model.Map{}
@@ -80,9 +83,9 @@ func (n *Node) parseReq(method string) {
8083

8184
}
8285
}
83-
8486
}
8587

88+
// parse node
8689
func (n *Node) parse(ctx context.Context, method string) error {
8790

8891
key := n.Key
@@ -131,11 +134,13 @@ func (n *Node) parse(ctx context.Context, method string) error {
131134
return err
132135
}
133136

134-
n.whereUpdate(ctx, method, accessRoles)
137+
// 3. get where by accessCondition
138+
err = n.whereUpdate(ctx, method, accessRoles)
135139

136-
return nil
140+
return err
137141
}
138142

143+
// update node role
139144
func (n *Node) roleUpdate() error {
140145

141146
if val, exists := n.structure.Insert[consts.Role]; exists {
@@ -176,7 +181,6 @@ func (n *Node) checkAccess(ctx context.Context, method string, accessRoles []str
176181
return nil
177182
}
178183

179-
// ? todo 整合到哪
180184
func (n *Node) whereUpdate(ctx context.Context, method string, accessRoles []string) error {
181185

182186
for i, item := range n.req {
@@ -326,7 +330,7 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error)
326330
if access.RowKeyGen != "" {
327331
for i, _ := range n.Data {
328332

329-
rowKeyVal, err = n.action.actionConfig.RowKeyGen(ctx, access.RowKeyGen, n.AccessName, n.Data[i])
333+
rowKeyVal, err = n.action.actionConfig.RowKeyGen(ctx, access.RowKeyGen, n.Key, n.Data[i])
330334
if err != nil {
331335
return nil, err
332336
}

config/access_config.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package config
22

33
import (
4-
"github.com/glennliao/apijson-go/util"
54
"github.com/gogf/gf/v2/errors/gerror"
65
"github.com/gogf/gf/v2/os/gtime"
76
"github.com/samber/lo"
87
"net/http"
98
)
109

1110
type FieldsGetValue struct {
12-
In map[string][]string
13-
Out map[string]string
11+
In map[string][]string
12+
Out map[string]string
13+
MaxCount *int // 可使用的最大分页大小,默认100
1414
}
1515

1616
type AccessConfig struct {
@@ -29,7 +29,7 @@ type AccessConfig struct {
2929

3030
RowKeyGen string // 主键生成策略
3131
RowKey string
32-
FieldsGet map[string]FieldsGetValue
32+
FieldsGet map[string]*FieldsGetValue
3333
Executor string
3434
}
3535

@@ -57,7 +57,6 @@ func (a *AccessConfig) GetFieldsGetInByRole(role string) map[string][]string {
5757
}
5858

5959
func (a *Access) GetAccess(tableAlias string, noVerify bool) (*AccessConfig, error) {
60-
tableAlias, _ = util.ParseNodeKey(tableAlias)
6160
access, ok := a.accessConfigMap[tableAlias]
6261

6362
if !ok {

config/config.go

+17
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,28 @@ func (c *Config) ReLoad() {
8686

8787
if accessListProvider != nil {
8888
c.accessList = accessListProvider(ctx)
89+
90+
defaultMaxCount := 100
91+
8992
for _, access := range c.accessList {
9093
name := access.Alias
9194
if name == "" {
9295
name = access.Name
9396
}
97+
98+
if access.FieldsGet == nil {
99+
access.FieldsGet = map[string]*FieldsGetValue{}
100+
}
101+
102+
if _, exists := access.FieldsGet["default"]; !exists {
103+
access.FieldsGet["default"] = &FieldsGetValue{}
104+
}
105+
106+
for role, _ := range access.FieldsGet {
107+
if access.FieldsGet[role].MaxCount == nil {
108+
access.FieldsGet[role].MaxCount = &defaultMaxCount
109+
}
110+
}
94111
c.Access.accessConfigMap[access.Alias] = access
95112
}
96113
}

config/query_config.go

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ func (c *ExecutorConfig) TableColumns() []string {
6868
return c.DBMeta.GetTableColumns(c.accessConfig.Name)
6969
}
7070

71+
func (c *ExecutorConfig) GetFieldsGetByRole() *FieldsGetValue {
72+
73+
if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
74+
return val
75+
}
76+
77+
return c.accessConfig.FieldsGet["default"]
78+
}
79+
7180
func (c *ExecutorConfig) GetFieldsGetOutByRole() []string {
7281
var fieldsMap map[string]string
7382

consts/node.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ const (
1515
const (
1616
Role = "@role"
1717
Page = "page" // page num
18-
Count = "count" // page size // todo access中增加限制count,防止被恶意下载数据
18+
Count = "count" // page size
19+
Total = "total"
1920
Query = "query"
2021
)
2122

2223
const (
2324
OpLike = "$"
25+
OpIn = "{}"
2426
OpRegexp = "~"
2527
OpSub = "-"
2628
OpPLus = "+"

drivers/config/goframe/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func init() {
4949
if len(access.Delete) == 1 {
5050
access.Delete = strings.Split(access.Delete[0], ",")
5151
}
52+
53+
g.Dump(access.FieldsGet)
54+
5255
accessList = append(accessList, access)
5356
}
5457

drivers/executor_goframe/action.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (a *ActionExecutor) Update(ctx context.Context, table string, data model.Ma
8080
m := g.DB(a.DbName).Model(table).Ctx(ctx)
8181

8282
for k, v := range where {
83-
if strings.HasSuffix(k, "{}") {
83+
if strings.HasSuffix(k, consts.OpIn) {
8484
if vStr, ok := v.(string); ok {
8585
if vStr == "" {
8686
return nil, gerror.New("where的值不能为空")
@@ -91,7 +91,8 @@ func (a *ActionExecutor) Update(ctx context.Context, table string, data model.Ma
9191
continue
9292
}
9393
if k == consts.Raw {
94-
delete(where, k) // todo 处理 rawSql
94+
m = m.Where(v.(model.Map))
95+
delete(where, k)
9596
continue
9697
}
9798

@@ -101,17 +102,17 @@ func (a *ActionExecutor) Update(ctx context.Context, table string, data model.Ma
101102
}
102103

103104
for k, v := range data {
104-
if strings.HasSuffix(k, "+") {
105-
field := util.RemoveSuffix(k, "+")
105+
if strings.HasSuffix(k, consts.OpPLus) {
106+
field := util.RemoveSuffix(k, consts.OpPLus)
106107
data[field] = &gdb.Counter{
107108
Field: field,
108109
Value: gconv.Float64(v),
109110
}
110111
delete(data, k)
111112
continue
112113
}
113-
if strings.HasSuffix(k, "-") {
114-
field := util.RemoveSuffix(k, "-")
114+
if strings.HasSuffix(k, consts.OpSub) {
115+
field := util.RemoveSuffix(k, consts.OpSub)
115116
data[field] = &gdb.Counter{
116117
Field: field,
117118
Value: -gconv.Float64(v),

query/node.go

+46-14
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ type nodeHandler interface {
3030
nodeType() int
3131
}
3232

33+
type Page struct {
34+
Count int
35+
Page int
36+
}
37+
3338
type Node struct {
3439
ctx context.Context
3540
queryContext *Query
3641

37-
// 当前节点key Todos
42+
// 当前节点key Todos, 如果Todo[], 保存为Todo
3843
Key string
3944
// 当前节点path -> []/Todos
4045
Path string
@@ -43,7 +48,8 @@ type Node struct {
4348

4449
// 是否为列表节点
4550
isList bool
46-
page model.Map // 分页参数
51+
52+
page *Page // 分页参数
4753

4854
// 访问当前节点的角色
4955
role string
@@ -104,28 +110,23 @@ func newNode(query *Query, key string, path string, nodeReq any) *Node {
104110
finish: false,
105111
}
106112

107-
// 节点类型判断
108-
k, isList := util.ParseNodeKey(key)
113+
node.Key, node.isList = parseNodeKey(key, path)
109114

110-
if util.IsFirstUp(k) { // 大写开头, 为查询节点(对应数据库)
115+
// 节点类型判断
116+
if util.IsFirstUp(node.Key) { // 大写开头, 为查询节点(对应数据库)
111117
node.Type = NodeTypeQuery
112-
} else if strings.HasSuffix(k, consts.RefKeySuffix) {
118+
} else if strings.HasSuffix(node.Key, consts.RefKeySuffix) {
113119
node.Type = NodeTypeRef
114-
} else if strings.HasSuffix(k, consts.FunctionsKeySuffix) {
120+
} else if strings.HasSuffix(node.Key, consts.FunctionsKeySuffix) {
115121
node.Type = NodeTypeFunc
116122
} else {
117123
node.Type = NodeTypeStruct // 结构节点下应该必须存在查询节点
118124

119125
if query.NoAccessVerify == false {
120-
if lo.Contains(query.DbMeta.GetTableNameList(), k) {
126+
if lo.Contains(query.DbMeta.GetTableNameList(), node.Key) {
121127
node.Type = NodeTypeQuery
122128
}
123129
}
124-
125-
}
126-
127-
if isList || strings.HasSuffix(filepath.Dir(path), consts.ListKeySuffix) {
128-
node.isList = true
129130
}
130131

131132
switch node.Type {
@@ -151,6 +152,20 @@ func newNode(query *Query, key string, path string, nodeReq any) *Node {
151152
return node
152153
}
153154

155+
func parseNodeKey(inK string, path string) (k string, isList bool) {
156+
k = inK
157+
if strings.HasSuffix(k, consts.ListKeySuffix) {
158+
isList = true
159+
k = k[0 : len(k)-len(consts.ListKeySuffix)]
160+
} else {
161+
if strings.HasSuffix(filepath.Dir(path), consts.ListKeySuffix) { // parent is []
162+
isList = true
163+
}
164+
}
165+
166+
return
167+
}
168+
154169
func (n *Node) buildChild() error {
155170

156171
if n.Type == NodeTypeQuery && !util.HasFirstUpKey(n.req) { // 查询节点嵌套查询节点, 目前不支持
@@ -176,7 +191,7 @@ func (n *Node) buildChild() error {
176191
}
177192

178193
if n.isList {
179-
if lo.Contains([]string{"total", "page"}, key) {
194+
if lo.Contains([]string{consts.Total, consts.Page}, key) {
180195
continue
181196
}
182197
}
@@ -226,6 +241,23 @@ func (n *Node) parse() {
226241
g.Log().Debugf(n.ctx, "【node】(%s) <parse> ", n.Path)
227242
}
228243

244+
if n.isList {
245+
page := &Page{}
246+
if v, exists := n.req[consts.Page]; exists {
247+
page.Page = gconv.Int(v)
248+
}
249+
if v, exists := n.req[consts.Count]; exists {
250+
page.Count = gconv.Int(v)
251+
}
252+
if v, exists := n.req[consts.Query]; exists {
253+
switch gconv.String(v) {
254+
case "1", "2":
255+
n.needTotal = true
256+
}
257+
}
258+
n.page = page
259+
}
260+
229261
n.nodeHandler.parse()
230262

231263
if n.queryContext.PrintProcessLog {

0 commit comments

Comments
 (0)