Skip to content

Commit cd4e3df

Browse files
committed
调整g.Map 成 model.Map
1 parent e925541 commit cd4e3df

File tree

15 files changed

+174
-94
lines changed

15 files changed

+174
-94
lines changed

action/action.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"github.com/glennliao/apijson-go/config/db"
66
"github.com/glennliao/apijson-go/consts"
7+
"github.com/glennliao/apijson-go/model"
78
"github.com/gogf/gf/v2/database/gdb"
89
"github.com/gogf/gf/v2/errors/gerror"
910
"github.com/gogf/gf/v2/frame/g"
@@ -17,7 +18,7 @@ type Action struct {
1718
tagRequest *db.Request
1819
method string
1920

20-
req g.Map
21+
req model.Map
2122

2223
err error
2324

@@ -27,7 +28,7 @@ type Action struct {
2728
AccessVerify bool
2829
}
2930

30-
func New(ctx context.Context, method string, req g.Map) *Action {
31+
func New(ctx context.Context, method string, req model.Map) *Action {
3132

3233
request, err := checkTag(req, method)
3334
if err != nil {
@@ -65,12 +66,14 @@ func (a *Action) parse() error {
6566
}
6667
}
6768

68-
var list []g.Map
69-
_v, ok := v.(g.Map)
69+
var list []model.Map
70+
_v, ok := v.(model.Map)
7071
if ok { // 将所有node都假设成列表, 如果单个则看成一个元素的批量
71-
list = []g.Map{_v}
72+
list = []model.Map{_v}
7273
} else {
73-
list = gconv.SliceMap(v)
74+
for _, m := range gconv.Maps(v) {
75+
list = append(list, m)
76+
}
7477
}
7578

7679
node := newNode(key, list, structure, a.tagRequest.Executor[key])
@@ -89,14 +92,14 @@ func (a *Action) parse() error {
8992
return nil
9093
}
9194

92-
func (a *Action) Result() (g.Map, error) {
95+
func (a *Action) Result() (model.Map, error) {
9396

9497
err := a.parse()
9598
if err != nil {
9699
return nil, err
97100
}
98101

99-
ret := g.Map{}
102+
ret := model.Map{}
100103

101104
for _, k := range a.tagRequest.ExecQueue {
102105
node := a.children[k]
@@ -142,7 +145,7 @@ func (a *Action) Result() (g.Map, error) {
142145
return ret, err
143146
}
144147

145-
func checkTag(req g.Map, method string) (*db.Request, error) {
148+
func checkTag(req model.Map, method string) (*db.Request, error) {
146149
_tag, ok := req["tag"]
147150
if !ok {
148151
return nil, gerror.New("tag 缺失")

action/node.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,47 @@ import (
77
"github.com/glennliao/apijson-go/config/executor"
88
"github.com/glennliao/apijson-go/config/functions"
99
"github.com/glennliao/apijson-go/consts"
10+
"github.com/glennliao/apijson-go/model"
1011
"github.com/glennliao/apijson-go/util"
1112
"github.com/gogf/gf/v2/errors/gerror"
12-
"github.com/gogf/gf/v2/frame/g"
1313
"github.com/gogf/gf/v2/util/gconv"
1414
"github.com/samber/lo"
1515
"net/http"
1616
"strings"
1717
)
1818

1919
type Node struct {
20-
req []g.Map
20+
req []model.Map
2121
ctx context.Context
2222
action *Action
2323
Key string
2424
TableName string
2525
Role string
2626

27-
Data []g.Map // 需写入数据库的数据
28-
Where []g.Map // 条件
29-
RowKey string // 主键
27+
Data []model.Map // 需写入数据库的数据
28+
Where []model.Map // 条件
29+
RowKey string // 主键
3030

3131
structure *db.Structure
3232
executor string
3333

3434
keyNode map[string]*Node
3535
}
3636

37-
func newNode(key string, req []g.Map, structure *db.Structure, executor string) Node {
37+
func newNode(key string, req []model.Map, structure *db.Structure, executor string) Node {
3838
return Node{
3939
Key: key, req: req, structure: structure, executor: executor,
4040
}
4141
}
4242

4343
func (n *Node) parseReq(method string) {
44-
n.Data = []g.Map{}
45-
n.Where = []g.Map{}
44+
n.Data = []model.Map{}
45+
n.Where = []model.Map{}
4646

4747
for i, item := range n.req {
4848

49-
n.Data = append(n.Data, g.Map{})
50-
n.Where = append(n.Where, g.Map{})
49+
n.Data = append(n.Data, model.Map{})
50+
n.Where = append(n.Where, model.Map{})
5151

5252
for key, val := range item {
5353
if key == consts.Role {
@@ -231,7 +231,7 @@ func (n *Node) reqUpdate() error {
231231

232232
if strings.HasSuffix(key, consts.FunctionsKeySuffix) {
233233
functionName, paramKeys := util.ParseFunctionsStr(updateVal.(string))
234-
var param = g.Map{}
234+
var param = model.Map{}
235235
for _, paramKey := range paramKeys {
236236
if paramKey == consts.FunctionOriReqParam {
237237
param[paramKey] = n.Data[i]
@@ -283,7 +283,7 @@ func (n *Node) reqUpdateBeforeDo() error {
283283
return nil
284284
}
285285

286-
func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map, err error) {
286+
func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.Map, err error) {
287287

288288
err = EmitHook(ctx, BeforeDo, n, method)
289289
if err != nil {
@@ -295,7 +295,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map,
295295
switch method {
296296
case http.MethodPost:
297297

298-
var rowKeyVal g.Map
298+
var rowKeyVal model.Map
299299

300300
access, err := db.GetAccess(n.Key, true)
301301
if err != nil {
@@ -329,7 +329,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map,
329329
return nil, err
330330
}
331331

332-
ret = g.Map{
332+
ret = model.Map{
333333
"code": 200,
334334
"count": count,
335335
"id": id,
@@ -355,7 +355,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map,
355355
return nil, err
356356
}
357357

358-
ret = g.Map{
358+
ret = model.Map{
359359
"code": 200,
360360
"count": count,
361361
}
@@ -365,7 +365,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map,
365365
return nil, err
366366
}
367367

368-
ret = g.Map{
368+
ret = model.Map{
369369
"code": 200,
370370
"count": count,
371371
}
@@ -383,7 +383,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret g.Map,
383383
return
384384
}
385385

386-
func (n *Node) execute(ctx context.Context, method string) (g.Map, error) {
386+
func (n *Node) execute(ctx context.Context, method string) (model.Map, error) {
387387

388388
err := n.reqUpdateBeforeDo()
389389
if err != nil {
@@ -405,7 +405,7 @@ func (n *Node) execute(ctx context.Context, method string) (g.Map, error) {
405405
}
406406
}
407407

408-
return g.Map{
408+
return model.Map{
409409
"code": 200,
410410
}, nil
411411
}

config/executor/action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package executor
22

33
import (
44
"context"
5-
"github.com/gogf/gf/v2/frame/g"
5+
"github.com/glennliao/apijson-go/model"
66
"github.com/samber/lo"
77
)
88

99
type ActionExecutor interface {
1010
Insert(ctx context.Context, table string, data any) (id int64, count int64, err error)
11-
Update(ctx context.Context, table string, data g.Map, where g.Map) (count int64, err error)
12-
Delete(ctx context.Context, table string, where g.Map) (count int64, err error)
11+
Update(ctx context.Context, table string, data model.Map, where model.Map) (count int64, err error)
12+
Delete(ctx context.Context, table string, where model.Map) (count int64, err error)
1313
}
1414

1515
var actionExecutorMap = map[string]ActionExecutor{}

config/executor/query.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package executor
33
import (
44
"context"
55
"github.com/glennliao/apijson-go/config/db"
6-
"github.com/gogf/gf/v2/frame/g"
6+
"github.com/glennliao/apijson-go/model"
77
"github.com/samber/lo"
88
)
99

1010
type QueryExecutor interface {
11-
ParseCondition(conditions g.MapStrAny, accessVerify bool) error
12-
ParseCtrl(ctrl g.Map) error
13-
List(page int, count int, needTotal bool) (list []g.Map, total int64, err error)
14-
One() (g.Map, error)
11+
ParseCondition(conditions model.MapStrAny, accessVerify bool) error
12+
ParseCtrl(ctrl model.Map) error
13+
List(page int, count int, needTotal bool) (list []model.Map, total int64, err error)
14+
One() (model.Map, error)
1515
EmptyResult()
1616
}
1717

framework/database/table.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package database
2+
3+
import (
4+
"github.com/glennliao/table-sync/tablesync"
5+
"time"
6+
)
7+
8+
type Access struct {
9+
tablesync.TableMeta `tableName:"_access" charset:"utf8mb4" comment:"权限配置"`
10+
Id uint32 `ddl:"primaryKey"`
11+
Debug int8 `ddl:"not null;default:0;comment:是否调试表,开发环境可用"`
12+
Name string `ddl:"size:32;not null;comment:实际表名"`
13+
Alias string `ddl:"size:32;uniqueIndex;comment:表别名,外部调用"`
14+
Get string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许get的权限角色列表"`
15+
Head string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许head的权限角色列表"`
16+
Gets string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许gets的权限角色列表"`
17+
Heads string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许heads的权限角色列表"`
18+
Post string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许post的权限角色列表"`
19+
Put string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许put的权限角色列表"`
20+
Delete string `ddl:"size:128;not null;default:'LOGIN,OWNER,ADMIN';comment:允许delete的权限角色列表"`
21+
CreatedAt *time.Time `ddl:"notnull;default:CURRENT_TIMESTAMP;comment:创建时间"`
22+
Detail string `ddl:"size:512;"`
23+
RowKey string `ddl:"size:32;comment:(逻辑)主键字段名,联合主键使用,分割"`
24+
FieldsGet string `ddl:"type:json;comment:get查询时字段配置"`
25+
RowKeyGen string `ddl:"comment:rowKey生成策略"`
26+
Executor string `ddl:"size:32;comment:执行器"`
27+
}
28+
29+
type Request struct {
30+
tablesync.TableMeta `tableName:"_request" charset:"utf8mb4" comment:"请求参数校验配置"`
31+
Id uint32 `ddl:"primaryKey"`
32+
Debug int8 `ddl:"not null;default:0;comment:是否调试,开发环境可用"`
33+
Tag string `ddl:"not null;size:32;not null;comment:标签名(表别名)"`
34+
Version string `ddl:"not null;size:8;comment:版本号"`
35+
Method string `ddl:"not null;size:5;comment:请求方式"`
36+
Structure string `ddl:"not null;type:json;comment:请求结构"`
37+
Detail string `ddl:"size:512;comment:描述说明"`
38+
CreatedAt *time.Time `ddl:"NOT NULL;comment:创建时间"`
39+
ExecQueue string `ddl:"size:512;comment:节点执行队列,使用,分割 请求结构确定的,不用每次计算依赖关系"`
40+
Executor string `ddl:"type:json;comment:节点执行器,格式为Tag:executor;Tag2:executor 未配置为default"`
41+
}
42+
43+
type Function struct {
44+
tablesync.TableMeta `tableName:"_function" charset:"utf8mb4" comment:"远程函数(暂未使用)"`
45+
Id uint32 `ddl:"primaryKey"`
46+
Debug int8 `ddl:"not null;default:0;comment:是否调试,开发环境可用"`
47+
UserId string `ddl:"not null;comment:管理员id"`
48+
Name string `ddl:"size:50;comment:方法名"`
49+
Arguments string `ddl:"size:100;comment:参数类型列表"`
50+
Demo string `ddl:"size:256;comment:参数示例"`
51+
Type string `ddl:"size:16;comment:返回值类型"`
52+
Tag string `ddl:"not null;size:32;not null;comment:标签名(表别名)"`
53+
Version string `ddl:"not null;size:8;comment:版本号"`
54+
Method string `ddl:"not null;size:5;comment:请求方式"`
55+
Detail string `ddl:"size:512;comment:描述说明"`
56+
CreatedAt *time.Time `ddl:"NOT NULL;comment:创建时间"`
57+
Back string `ddl:"size:128;comment:返回值示例"`
58+
}

framework/gf_orm/action.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gf_orm
33
import (
44
"context"
55
"github.com/glennliao/apijson-go/config/executor"
6+
"github.com/glennliao/apijson-go/model"
67
"github.com/gogf/gf/v2/errors/gerror"
78
"github.com/gogf/gf/v2/frame/g"
89
"strings"
@@ -26,7 +27,7 @@ func (a *ActionExecutor) Insert(ctx context.Context, table string, data any) (id
2627
return id, count, nil
2728
}
2829

29-
func (a *ActionExecutor) Update(ctx context.Context, table string, data g.Map, where g.Map) (count int64, err error) {
30+
func (a *ActionExecutor) Update(ctx context.Context, table string, data model.Map, where model.Map) (count int64, err error) {
3031
m := g.DB(a.DbName).Model(table).Ctx(ctx)
3132

3233
for k, v := range where {
@@ -58,7 +59,7 @@ func (a *ActionExecutor) Update(ctx context.Context, table string, data g.Map, w
5859
return count, err
5960
}
6061

61-
func (a *ActionExecutor) Delete(ctx context.Context, table string, where g.Map) (count int64, err error) {
62+
func (a *ActionExecutor) Delete(ctx context.Context, table string, where model.Map) (count int64, err error) {
6263
if len(where) == 0 {
6364
return 0, gerror.New("where不能为空")
6465
}

framework/gf_orm/query.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/glennliao/apijson-go/config/db"
77
"github.com/glennliao/apijson-go/config/executor"
88
"github.com/glennliao/apijson-go/consts"
9+
"github.com/glennliao/apijson-go/model"
910
"github.com/glennliao/apijson-go/util"
1011
"github.com/gogf/gf/v2/database/gdb"
1112
"github.com/gogf/gf/v2/frame/g"
@@ -22,7 +23,7 @@ type SqlExecutor struct {
2223

2324
//保存where条件 [ ["user_id",">", 123], ["user_id","<=",345] ]
2425
Where [][]any
25-
accessCondition g.Map
26+
accessCondition model.Map
2627

2728
Columns []string
2829
Order string
@@ -53,7 +54,7 @@ func New(ctx context.Context, accessVerify bool, role string, access *db.Access)
5354

5455
// ParseCondition 解析查询条件
5556
// accessVerify 内部调用时, 不校验是否可使用该种查询方式
56-
func (e *SqlExecutor) ParseCondition(conditions g.MapStrAny, accessVerify bool) error {
57+
func (e *SqlExecutor) ParseCondition(conditions model.MapStrAny, accessVerify bool) error {
5758

5859
for key, condition := range conditions {
5960
switch {
@@ -67,7 +68,7 @@ func (e *SqlExecutor) ParseCondition(conditions g.MapStrAny, accessVerify bool)
6768
e.Where = append(e.Where, []any{key[0 : len(key)-1], consts.SqlRegexp, gconv.String(condition)})
6869

6970
case key == "@raw" && !accessVerify:
70-
e.accessCondition = condition.(g.Map)
71+
e.accessCondition = condition.(model.Map)
7172

7273
default:
7374
e.Where = append(e.Where, []any{key, consts.SqlEqual, condition})
@@ -163,7 +164,7 @@ func (e *SqlExecutor) parseMultiCondition(k string, condition any) {
163164
var exp = regexp.MustCompile(`^[\s\w][\w()]+`) // 匹配 field, COUNT(field)
164165

165166
// ParseCtrl 解析 @column,@group等控制类
166-
func (e *SqlExecutor) ParseCtrl(ctrl g.Map) error {
167+
func (e *SqlExecutor) ParseCtrl(ctrl model.Map) error {
167168

168169
fieldStyle := config.GetDbFieldStyle()
169170
tableName := e.access.Name
@@ -314,7 +315,7 @@ func (e *SqlExecutor) EmptyResult() {
314315
e.WithEmptyResult = true
315316
}
316317

317-
func (e *SqlExecutor) List(page int, count int, needTotal bool) (list []g.Map, total int64, err error) {
318+
func (e *SqlExecutor) List(page int, count int, needTotal bool) (list []model.Map, total int64, err error) {
318319

319320
if e.WithEmptyResult {
320321
return nil, 0, err
@@ -340,10 +341,14 @@ func (e *SqlExecutor) List(page int, count int, needTotal bool) (list []g.Map, t
340341
return nil, 0, err
341342
}
342343

343-
return all.List(), total, nil
344+
for _, item := range all.List() {
345+
list = append(list, item)
346+
}
347+
348+
return list, total, nil
344349
}
345350

346-
func (e *SqlExecutor) One() (g.Map, error) {
351+
func (e *SqlExecutor) One() (model.Map, error) {
347352
if e.WithEmptyResult {
348353
return nil, nil
349354
}

0 commit comments

Comments
 (0)