Skip to content

Commit 0651e60

Browse files
committed
just run ok
1 parent 5d1ab9f commit 0651e60

27 files changed

+522
-388
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
*.exe
2+
*.exe
3+
*.sqlite3

action/action.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package action
33
import (
44
"context"
55
"github.com/glennliao/apijson-go/config"
6-
"github.com/glennliao/apijson-go/config/db"
76
"github.com/glennliao/apijson-go/consts"
87
"github.com/glennliao/apijson-go/model"
98
"github.com/gogf/gf/v2/database/gdb"
@@ -16,7 +15,7 @@ import (
1615
// Action 非get查询的request表中的请求
1716
type Action struct {
1817
ctx context.Context
19-
tagRequest *db.Request
18+
tagRequest *config.Request
2019
method string
2120

2221
req model.Map
@@ -32,11 +31,19 @@ type Action struct {
3231
NoRequestVerify bool
3332

3433
Access *config.Access
34+
35+
// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
36+
DbFieldStyle config.FieldStyle
37+
38+
// jsonFieldStyle 数据库返回的字段
39+
JsonFieldStyle config.FieldStyle
40+
41+
Functions *config.Functions
3542
}
3643

37-
func New(ctx context.Context, method string, req model.Map) *Action {
44+
func New(ctx context.Context, method string, req model.Map, requestCfg *config.RequestConfig) *Action {
3845

39-
request, err := checkTag(req, method)
46+
request, err := checkTag(req, method, requestCfg)
4047
if err != nil {
4148
panic(err)
4249
}
@@ -150,7 +157,7 @@ func (a *Action) Result() (model.Map, error) {
150157
return ret, err
151158
}
152159

153-
func checkTag(req model.Map, method string) (*db.Request, error) {
160+
func checkTag(req model.Map, method string, requestCfg *config.RequestConfig) (*config.Request, error) {
154161
_tag, ok := req["tag"]
155162
if !ok {
156163
return nil, gerror.New("tag 缺失")
@@ -159,7 +166,7 @@ func checkTag(req model.Map, method string) (*db.Request, error) {
159166
tag := gconv.String(_tag)
160167
version := req["version"]
161168

162-
request, err := db.GetRequest(tag, method, gconv.String(version))
169+
request, err := requestCfg.GetRequest(tag, method, gconv.String(version))
163170
if err != nil {
164171
return nil, err
165172
}

action/node.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package action
33
import (
44
"context"
55
"github.com/glennliao/apijson-go/config"
6-
"github.com/glennliao/apijson-go/config/db"
76
"github.com/glennliao/apijson-go/config/executor"
8-
"github.com/glennliao/apijson-go/config/functions"
97
"github.com/glennliao/apijson-go/consts"
108
"github.com/glennliao/apijson-go/model"
119
"github.com/glennliao/apijson-go/util"
@@ -27,15 +25,15 @@ type Node struct {
2725
Where []model.Map // 条件
2826
RowKey string // 主键
2927

30-
structure *db.Structure
28+
structure *config.Structure
3129
executor string
3230

3331
keyNode map[string]*Node
3432

3533
access *config.Access
3634
}
3735

38-
func newNode(key string, req []model.Map, structure *db.Structure, executor string) Node {
36+
func newNode(key string, req []model.Map, structure *config.Structure, executor string) Node {
3937
return Node{
4038
Key: key, req: req, structure: structure, executor: executor,
4139
}
@@ -54,7 +52,7 @@ func (n *Node) parseReq(method string) {
5452
if key == consts.Role {
5553
n.Role = util.String(val)
5654
} else {
57-
key = config.GetDbFieldStyle()(n.ctx, n.TableName, key)
55+
key = n.action.DbFieldStyle(n.ctx, n.TableName, key)
5856

5957
if method == http.MethodDelete {
6058
n.Where[i][key] = val
@@ -82,7 +80,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
8280
if strings.HasSuffix(key, consts.ListKeySuffix) {
8381
key = key[0 : len(key)-2]
8482
}
85-
access, err := db.GetAccess(key, true)
83+
access, err := n.access.GetAccess(key, true)
8684

8785
if err != nil {
8886
return err
@@ -245,7 +243,7 @@ func (n *Node) reqUpdate() error {
245243
}
246244
}
247245
k := key[0 : len(key)-2]
248-
val, err := functions.Call(n.ctx, functionName, param)
246+
val, err := n.action.Functions.Call(n.ctx, functionName, param)
249247
if err != nil {
250248
return err
251249
}
@@ -278,9 +276,9 @@ func (n *Node) reqUpdateBeforeDo() error {
278276
if strings.HasSuffix(k, consts.RefKeySuffix) {
279277
refNodeKey, refCol := util.ParseRefCol(v.(string))
280278
if strings.HasSuffix(refNodeKey, consts.ListKeySuffix) { // 双列表
281-
n.Data[i][k] = n.keyNode[refNodeKey].Data[i][config.GetDbFieldStyle()(n.ctx, n.TableName, refCol)]
279+
n.Data[i][k] = n.keyNode[refNodeKey].Data[i][n.action.DbFieldStyle(n.ctx, n.TableName, refCol)]
282280
} else {
283-
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][config.GetDbFieldStyle()(n.ctx, n.TableName, refCol)]
281+
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][n.action.DbFieldStyle(n.ctx, n.TableName, refCol)]
284282
}
285283
}
286284
}
@@ -303,7 +301,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
303301

304302
var rowKeyVal model.Map
305303

306-
access, err := db.GetAccess(n.Key, true)
304+
access, err := n.access.GetAccess(n.Key, true)
307305
if err != nil {
308306
return nil, err
309307
}
@@ -343,7 +341,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
343341

344342
if len(n.Data) > 0 { //多条插入时返回值已经应该无意义了
345343

346-
jsonStyle := config.GetJsonFieldStyle()
344+
jsonStyle := n.action.JsonFieldStyle
347345
if rowKeyVal != nil {
348346
for k, v := range rowKeyVal {
349347
if k == consts.RowKey {

apijson.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ func New() *ApiJson {
2828
}
2929

3030
// Load load for defaultApiJson, 简化使用
31-
func Load(app func(ctx context.Context, a *ApiJson)) *ApiJson {
32-
DefaultApiJson.Use(app)
31+
func Load(apps ...func(ctx context.Context, a *ApiJson)) *ApiJson {
32+
33+
for _, app := range apps {
34+
DefaultApiJson.Use(app)
35+
}
36+
3337
DefaultApiJson.Load()
3438
return DefaultApiJson
3539
}

config/access.go

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type Access struct {
6363
DefaultRoleFunc DefaultRole
6464

6565
roleList []string
66+
67+
accessConfigMap map[string]AccessConfig
6668
}
6769

6870
func NewAccess() *Access {

config/access_config.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package config
2+
3+
import (
4+
"github.com/glennliao/apijson-go/util"
5+
"github.com/gogf/gf/v2/errors/gerror"
6+
"github.com/gogf/gf/v2/os/gtime"
7+
"github.com/samber/lo"
8+
"net/http"
9+
)
10+
11+
type FieldsGetValue struct {
12+
In map[string][]string
13+
Out map[string]string
14+
}
15+
16+
type AccessConfig struct {
17+
Debug int8
18+
Name string
19+
Alias string
20+
Get []string
21+
Head []string
22+
Gets []string
23+
Heads []string
24+
Post []string
25+
Put []string
26+
Delete []string
27+
CreatedAt *gtime.Time
28+
Detail string
29+
30+
RowKeyGen string // 主键生成策略
31+
RowKey string
32+
FieldsGet map[string]FieldsGetValue
33+
Executor string
34+
}
35+
36+
func (a *AccessConfig) GetFieldsGetOutByRole(role string) []string {
37+
var fieldsMap map[string]string
38+
39+
if val, exists := a.FieldsGet[role]; exists {
40+
fieldsMap = val.Out
41+
} else {
42+
fieldsMap = a.FieldsGet["default"].Out
43+
}
44+
return lo.Keys(fieldsMap)
45+
}
46+
47+
func (a *AccessConfig) GetFieldsGetInByRole(role string) map[string][]string {
48+
var inFieldsMap map[string][]string
49+
50+
if val, exists := a.FieldsGet[role]; exists {
51+
inFieldsMap = val.In
52+
} else {
53+
inFieldsMap = a.FieldsGet["default"].In
54+
}
55+
56+
return inFieldsMap
57+
}
58+
59+
func (a *Access) GetAccess(tableAlias string, accessVerify bool) (*AccessConfig, error) {
60+
tableAlias, _ = util.ParseNodeKey(tableAlias)
61+
access, ok := a.accessConfigMap[tableAlias]
62+
63+
if !ok {
64+
if accessVerify {
65+
return nil, gerror.Newf("access[%s]: 404", tableAlias)
66+
}
67+
return &AccessConfig{
68+
Debug: 0,
69+
Name: tableAlias,
70+
Alias: tableAlias,
71+
}, nil
72+
}
73+
74+
return &access, nil
75+
}
76+
77+
func (a *Access) GetAccessRole(table string, method string) ([]string, string, error) {
78+
access, ok := a.accessConfigMap[table]
79+
80+
if !ok {
81+
return nil, "", gerror.Newf("access[%s]: 404", table)
82+
}
83+
84+
switch method {
85+
case http.MethodGet:
86+
return access.Get, access.Name, nil
87+
case http.MethodHead:
88+
return access.Head, access.Name, nil
89+
case http.MethodPost:
90+
return access.Post, access.Name, nil
91+
case http.MethodPut:
92+
return access.Put, access.Name, nil
93+
case http.MethodDelete:
94+
return access.Delete, access.Name, nil
95+
}
96+
97+
return []string{}, access.Name, nil
98+
}

config/config.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
package config
22

33
type Config struct {
4-
Access *Access
5-
MaxTreeWidth int
6-
MaxTreeDeep int
4+
Access *Access
5+
6+
Functions *Functions
7+
8+
MaxTreeWidth int
9+
MaxTreeDeep int
10+
711
rowKeyGenFuncMap map[string]RowKeyGenFuncHandler
12+
13+
// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
14+
DbFieldStyle FieldStyle
15+
16+
// jsonFieldStyle 数据库返回的字段
17+
JsonFieldStyle FieldStyle
18+
19+
DbMeta *DBMeta
20+
21+
AccessList []AccessConfig // todo to access
22+
23+
RequestConfig *RequestConfig
824
}
925

1026
func New() *Config {
@@ -16,5 +32,10 @@ func New() *Config {
1632

1733
a.rowKeyGenFuncMap = make(map[string]RowKeyGenFuncHandler)
1834

35+
a.DbFieldStyle = CaseSnake
36+
a.JsonFieldStyle = CaseCamel
37+
38+
a.Functions = &Functions{}
39+
1940
return a
2041
}

0 commit comments

Comments
 (0)