Skip to content

Commit 3201fab

Browse files
committed
RowKey 使用结构体取代纯map传递
1 parent 8ea7a4e commit 3201fab

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-
func New(ctx context.Context, method string, req model.Map, requestCfg *config.RequestConfig) *Action {
45+
func New(ctx context.Context, actionConfig *config.ActionConfig, method string, req model.Map) *Action {
4546

46-
request, err := checkTag(req, method, requestCfg)
47+
request, err := checkTag(req, method, actionConfig)
4748
if err != 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
return a
6365
}
@@ -157,7 +159,7 @@ func (a *Action) Result() (model.Map, error) {
157159
return ret, err
158160
}
159161

160-
func checkTag(req model.Map, method string, requestCfg *config.RequestConfig) (*config.Request, error) {
162+
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.Request, error) {
161163
_tag, ok := req["tag"]
162164
if !ok {
163165
return nil, 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
for i, item := range n.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
if err != nil {
178183
return err
179184
}
180185

181186
if method == http.MethodPost {
182-
for k, v := range where.Where() {
187+
for k, v := range condition.Where() {
183188
n.Data[i][k] = v
184189
}
185190
} else {
186-
for k, v := range where.Where() {
191+
for k, v := range condition.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
return nil, 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+
if access.RowKeyGen != "" {
315+
for i, _ := range n.Data {
316+
317+
rowKeyVal, err = n.action.actionConfig.RowKeyGen(ctx, access.RowKeyGen, n.TableName, n.Data[i])
318+
if err != nil {
319+
return nil, err
320+
}
321+
322+
for k, v := range rowKeyVal {
323+
if k == consts.RowKey {
324+
n.Data[i][access.RowKey] = v
325+
} else {
326+
n.Data[i][k] = v
327+
}
328+
}
329+
330+
}
331+
}
327332

328333
var id int64
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, method string, 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
type ConditionReq struct {
1811
AccessName string // _access 中的alias
1912
TableAccessRoleList []string
@@ -23,18 +16,32 @@ type ConditionReq struct {
2316
}
2417

2518
type ConditionRet struct {
26-
condition map[string]any
19+
condition map[string]any
20+
rawCondition map[string]any
21+
}
22+
23+
func NewConditionRet() *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(k string, v any) { // todo any?
3032
c.condition[k] = v
3133
}
3234

35+
func (c *ConditionRet) AddRaw(k string, v any) { // 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
return c.condition
3542
}
3643

37-
type AccessCondition func(ctx context.Context, req ConditionReq) (*ConditionRet, error)
44+
type AccessCondition func(ctx context.Context, req ConditionReq, condition *ConditionRet) error
3845

3946
type RoleReq struct {
4047
AccessName string
@@ -48,8 +55,8 @@ func defaultRole(ctx context.Context, req RoleReq) (string, error) {
4855
return consts.UNKNOWN, nil
4956
}
5057

51-
func defaultCondition(ctx context.Context, req ConditionReq) (*ConditionRet, error) {
52-
return &ConditionRet{}, nil
58+
func defaultCondition(ctx context.Context, req ConditionReq, condition *ConditionRet) error {
59+
return nil
5360
}
5461

5562
type Access struct {

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+
type ActionConfig struct {
9+
requestConfig RequestConfig
10+
access *Access
11+
functions *Functions
12+
rowKeyGenFuncMap map[string]RowKeyGenFuncHandler
13+
defaultRoleFunc DefaultRole
14+
}
15+
16+
func (c *ActionConfig) NoVerify() bool {
17+
return c.access.NoVerify
18+
}
19+
20+
func (c *ActionConfig) DefaultRoleFunc() DefaultRole {
21+
return c.defaultRoleFunc
22+
}
23+
24+
func (c *ActionConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig, error) {
25+
return c.access.GetAccess(key, noVerify)
26+
}
27+
28+
func (c *ActionConfig) CallFunc(ctx context.Context, name string, param model.Map) (any, error) {
29+
return c.functions.Call(ctx, name, param)
30+
}
31+
32+
func (c *ActionConfig) GetRequest(tag string, method string, version string) (*Request, error) {
33+
return c.requestConfig.GetRequest(tag, method, version)
34+
}
35+
36+
func (c *ActionConfig) RowKeyGen(ctx context.Context, genFuncName string, accessName string, data model.Map) (model.Map, error) {
37+
if f, 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+
return ret.data, err
45+
}
46+
47+
return nil, 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
func New() *Config {
@@ -76,7 +77,7 @@ func New() *Config {
7677
return a
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
if requestListProvider != 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
return c.queryConfig
120121
}
122+
123+
func (c *Config) ActionConfig() *ActionConfig {
124+
return c.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
type Func struct {
11+
// todo 调整成结构体
1112
Handler func(ctx context.Context, param model.Map) (res any, err error)
1213
}
1314

0 commit comments

Comments
 (0)