Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3201fab

Browse files
committed
RowKey 使用结构体取代纯map传递
1 parent8ea7a4e commit3201fab

File tree

15 files changed

+280
-87
lines changed

15 files changed

+280
-87
lines changed

‎.github/workflows/test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name:tests
2+
on:
3+
push:
4+
branches:[ "main" ,"dev"]
5+
pull_request:
6+
branches:[ "main", "dev" ]
7+
8+
jobs:
9+
simple-test:
10+
run:cd test && go test -short

‎action/action.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ type Action struct {
3838
// jsonFieldStyle 数据库返回的字段
3939
JsonFieldStyle config.FieldStyle
4040

41-
Functions*config.Functions
41+
Functions*config.Functions
42+
actionConfig*config.ActionConfig
4243
}
4344

44-
funcNew(ctx context.Context,methodstring,req model.Map,requestCfg*config.RequestConfig)*Action {
45+
funcNew(ctx context.Context,actionConfig*config.ActionConfig,methodstring,req model.Map)*Action {
4546

46-
request,err:=checkTag(req,method,requestCfg)
47+
request,err:=checkTag(req,method,actionConfig)
4748
iferr!=nil {
4849
panic(err)
4950
}
@@ -52,12 +53,13 @@ func New(ctx context.Context, method string, req model.Map, requestCfg *config.R
5253
delete(req,"version")
5354

5455
a:=&Action{
55-
ctx:ctx,
56-
tagRequest:request,
57-
method:method,
58-
req:req,
59-
children:map[string]*Node{},
60-
keyNode:map[string]*Node{},
56+
ctx:ctx,
57+
tagRequest:request,
58+
method:method,
59+
req:req,
60+
children:map[string]*Node{},
61+
keyNode:map[string]*Node{},
62+
actionConfig:actionConfig,
6163
}
6264
returna
6365
}
@@ -157,7 +159,7 @@ func (a *Action) Result() (model.Map, error) {
157159
returnret,err
158160
}
159161

160-
funccheckTag(req model.Map,methodstring,requestCfg*config.RequestConfig) (*config.Request,error) {
162+
funccheckTag(req model.Map,methodstring,requestCfg*config.ActionConfig) (*config.Request,error) {
161163
_tag,ok:=req["tag"]
162164
if!ok {
163165
returnnil,gerror.New("tag 缺失")

‎action/node.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,29 @@ func (n *Node) checkAccess(ctx context.Context, method string, accessRoles []str
166166
}
167167

168168
fori,item:=rangen.req {
169-
where,err:=n.action.Access.ConditionFunc(ctx, config.ConditionReq{
169+
170+
condition:=config.NewConditionRet()
171+
172+
conditionReq:= config.ConditionReq{
170173
AccessName:n.TableName,
171174
TableAccessRoleList:accessRoles,
172175
Method:method,
173176
NodeRole:n.Role,
174177
NodeReq:item,
175-
})
178+
}
179+
180+
err:=n.action.Access.ConditionFunc(ctx,conditionReq,condition)
176181

177182
iferr!=nil {
178183
returnerr
179184
}
180185

181186
ifmethod==http.MethodPost {
182-
fork,v:=rangewhere.Where() {
187+
fork,v:=rangecondition.Where() {
183188
n.Data[i][k]=v
184189
}
185190
}else {
186-
fork,v:=rangewhere.Where() {
191+
fork,v:=rangecondition.Where() {
187192
n.Where[i][k]=v
188193
}
189194
}
@@ -306,24 +311,24 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
306311
returnnil,err
307312
}
308313

309-
//if access.RowKeyGen != "" {
310-
//for i, _ := range n.Data {
311-
//
312-
//rowKeyVal, err =config.RowKeyGen(ctx, access.RowKeyGen, n.TableName, n.Data[i])
313-
//if err != nil {
314-
//return nil, err
315-
//}
316-
//
317-
//for k, v := range rowKeyVal {
318-
//if k == consts.RowKey {
319-
//n.Data[i][access.RowKey] = v
320-
//} else {
321-
//n.Data[i][k] = v
322-
//}
323-
//}
324-
//
325-
//}
326-
//}
314+
ifaccess.RowKeyGen!="" {
315+
fori,_:=rangen.Data {
316+
317+
rowKeyVal,err=n.action.actionConfig.RowKeyGen(ctx,access.RowKeyGen,n.TableName,n.Data[i])
318+
iferr!=nil {
319+
returnnil,err
320+
}
321+
322+
fork,v:=rangerowKeyVal {
323+
ifk==consts.RowKey {
324+
n.Data[i][access.RowKey]=v
325+
}else {
326+
n.Data[i][k]=v
327+
}
328+
}
329+
330+
}
331+
}
327332

328333
varidint64
329334

‎apijson.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (a *ApiJson) Use(p ...func(ctx context.Context, a *ApiJson)) *ApiJson {
4949
}
5050

5151
func (a*ApiJson)Load() {
52-
a.config.Load()
52+
a.config.ReLoad()
5353
}
5454

5555
func (a*ApiJson)Config()*config.Config {
@@ -71,7 +71,7 @@ func (a *ApiJson) NewQuery(ctx context.Context, req model.Map) *query.Query {
7171
}
7272

7373
func (a*ApiJson)NewAction(ctx context.Context,methodstring,req model.Map)*action.Action {
74-
act:=action.New(ctx,method,req,a.config.RequestConfig)
74+
act:=action.New(ctx,a.Config().ActionConfig(),method,req)
7575

7676
act.NoAccessVerify=a.config.Access.NoVerify
7777
act.DbFieldStyle=a.config.DbFieldStyle

‎config/access.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77
"github.com/samber/lo"
88
)
99

10-
// 设置 _access/_request 自定义表名
11-
// todo
12-
var (
13-
TableAccess="_access"
14-
TableRequest="_request"
15-
)
16-
1710
typeConditionReqstruct {
1811
AccessNamestring// _access 中的alias
1912
TableAccessRoleList []string
@@ -23,18 +16,32 @@ type ConditionReq struct {
2316
}
2417

2518
typeConditionRetstruct {
26-
conditionmap[string]any
19+
conditionmap[string]any
20+
rawConditionmap[string]any
21+
}
22+
23+
funcNewConditionRet()*ConditionRet {
24+
c:=ConditionRet{
25+
condition:map[string]any{},
26+
rawCondition:map[string]any{},
27+
}
28+
return&c
2729
}
2830

2931
func (c*ConditionRet)Add(kstring,vany) {// todo any?
3032
c.condition[k]=v
3133
}
3234

35+
func (c*ConditionRet)AddRaw(kstring,vany) {// todo any?
36+
c.rawCondition[k]=v
37+
}
38+
3339
func (c*ConditionRet)Where()map[string]any {
40+
c.condition[consts.Raw]=c.rawCondition
3441
returnc.condition
3542
}
3643

37-
typeAccessConditionfunc(ctx context.Context,reqConditionReq) (*ConditionRet,error)
44+
typeAccessConditionfunc(ctx context.Context,reqConditionReq,condition*ConditionRet)error
3845

3946
typeRoleReqstruct {
4047
AccessNamestring
@@ -48,8 +55,8 @@ func defaultRole(ctx context.Context, req RoleReq) (string, error) {
4855
returnconsts.UNKNOWN,nil
4956
}
5057

51-
funcdefaultCondition(ctx context.Context,reqConditionReq) (*ConditionRet,error) {
52-
return&ConditionRet{},nil
58+
funcdefaultCondition(ctx context.Context,reqConditionReq,condition*ConditionRet)error {
59+
returnnil
5360
}
5461

5562
typeAccessstruct {

‎config/action_config.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"github.com/glennliao/apijson-go/model"
6+
)
7+
8+
typeActionConfigstruct {
9+
requestConfigRequestConfig
10+
access*Access
11+
functions*Functions
12+
rowKeyGenFuncMapmap[string]RowKeyGenFuncHandler
13+
defaultRoleFuncDefaultRole
14+
}
15+
16+
func (c*ActionConfig)NoVerify()bool {
17+
returnc.access.NoVerify
18+
}
19+
20+
func (c*ActionConfig)DefaultRoleFunc()DefaultRole {
21+
returnc.defaultRoleFunc
22+
}
23+
24+
func (c*ActionConfig)GetAccessConfig(keystring,noVerifybool) (*AccessConfig,error) {
25+
returnc.access.GetAccess(key,noVerify)
26+
}
27+
28+
func (c*ActionConfig)CallFunc(ctx context.Context,namestring,param model.Map) (any,error) {
29+
returnc.functions.Call(ctx,name,param)
30+
}
31+
32+
func (c*ActionConfig)GetRequest(tagstring,methodstring,versionstring) (*Request,error) {
33+
returnc.requestConfig.GetRequest(tag,method,version)
34+
}
35+
36+
func (c*ActionConfig)RowKeyGen(ctx context.Context,genFuncNamestring,accessNamestring,data model.Map) (model.Map,error) {
37+
iff,exists:=c.rowKeyGenFuncMap[genFuncName];exists {
38+
req:=&RowKeyGenReq{
39+
AccessName:accessName,
40+
Data:data,
41+
}
42+
ret:=NewRowKeyGenRet()
43+
err:=f(ctx,req,ret)
44+
returnret.data,err
45+
}
46+
47+
returnnil,nil
48+
}
49+
50+
//
51+
//type ExecutorConfig struct {
52+
//NoVerify bool
53+
//accessConfig *AccessConfig
54+
//method string
55+
//role string
56+
//DBMeta *DBMeta
57+
//DbFieldStyle FieldStyle
58+
//JsonFieldStyle FieldStyle
59+
//}
60+
//
61+
//func NewExecutorConfig(accessConfig *AccessConfig, method string, noVerify bool) *ExecutorConfig {
62+
//return &ExecutorConfig{
63+
//accessConfig: accessConfig,
64+
//method: method,
65+
//NoVerify: noVerify,
66+
//}
67+
//}
68+
//
69+
//func (c *ExecutorConfig) SetRole(role string) {
70+
//c.role = role
71+
//}
72+
//
73+
//func (c *ExecutorConfig) TableName() string {
74+
//return c.accessConfig.Name
75+
//}
76+
//
77+
//func (c *ExecutorConfig) TableColumns() []string {
78+
//return c.DBMeta.GetTableColumns(c.accessConfig.Name)
79+
//}
80+
//
81+
//func (c *ExecutorConfig) GetFieldsGetOutByRole() []string {
82+
//var fieldsMap map[string]string
83+
//
84+
//if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
85+
//fieldsMap = val.Out
86+
//} else {
87+
//fieldsMap = c.accessConfig.FieldsGet["default"].Out
88+
//}
89+
//return lo.Keys(fieldsMap)
90+
//}
91+
//
92+
//func (c *ExecutorConfig) GetFieldsGetInByRole() map[string][]string {
93+
//var inFieldsMap map[string][]string
94+
//
95+
//if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
96+
//inFieldsMap = val.In
97+
//} else {
98+
//inFieldsMap = c.accessConfig.FieldsGet["default"].In
99+
//}
100+
//
101+
//return inFieldsMap
102+
//}
103+
//
104+
//func (c *ExecutorConfig) AccessRoles() []string {
105+
//switch c.method {
106+
//case http.MethodGet:
107+
//return c.accessConfig.Get
108+
//case http.MethodHead:
109+
//return c.accessConfig.Head
110+
//case http.MethodPost:
111+
//return c.accessConfig.Post
112+
//case http.MethodPut:
113+
//return c.accessConfig.Put
114+
//case http.MethodDelete:
115+
//return c.accessConfig.Delete
116+
//}
117+
//return []string{}
118+
//
119+
//}
120+
//
121+
//func (c *ExecutorConfig) Executor() string {
122+
//return c.accessConfig.Executor
123+
//
124+
//}

‎config/config.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ type Config struct {
5050

5151
accessList []AccessConfig// todo to access
5252

53-
RequestConfig*RequestConfig
53+
requestConfig*RequestConfig
5454

55-
queryConfig*QueryConfig
55+
queryConfig*QueryConfig
56+
actionConfig*ActionConfig
5657
}
5758

5859
funcNew()*Config {
@@ -76,7 +77,7 @@ func New() *Config {
7677
returna
7778
}
7879

79-
func (c*Config)Load() {
80+
func (c*Config)ReLoad() {
8081

8182
c.Access.accessConfigMap=make(map[string]AccessConfig)
8283

@@ -98,7 +99,7 @@ func (c *Config) Load() {
9899
requestListProvider:=requestListProviderMap[c.RequestListProvider]
99100
ifrequestListProvider!=nil {
100101
requestList:=requestListProvider(ctx)
101-
c.RequestConfig=NewRequestConfig(requestList)
102+
c.requestConfig=NewRequestConfig(requestList)
102103
}
103104

104105
dbMetaProvider:=dbMetaProviderMap[c.DbMetaProvider]
@@ -118,3 +119,7 @@ func (c *Config) Load() {
118119
func (c*Config)QueryConfig()*QueryConfig {
119120
returnc.queryConfig
120121
}
122+
123+
func (c*Config)ActionConfig()*ActionConfig {
124+
returnc.actionConfig
125+
}

‎config/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
typeFuncstruct {
11+
// todo 调整成结构体
1112
Handlerfunc(ctx context.Context,param model.Map) (resany,errerror)
1213
}
1314

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp