Skip to content

Commit 81465d6

Browse files
committed
调整字段名风格 + SqlExecutor懒处理生成model
1 parent 40939b2 commit 81465d6

File tree

13 files changed

+259
-289
lines changed

13 files changed

+259
-289
lines changed

config/access.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"github.com/glennliao/apijson-go/consts"
6+
"github.com/gogf/gf/v2/frame/g"
7+
"github.com/samber/lo"
8+
)
9+
10+
type AccessConditionReq struct {
11+
Table string
12+
TableAccessRoleList []string
13+
Method string
14+
NodeReq g.Map //节点的请求数据
15+
NodeRole string // 节点的角色
16+
}
17+
18+
type RoleReq struct {
19+
Table string
20+
Method string
21+
NodeRole string // 前端传入的节点的角色, 目前未传入则为空
22+
}
23+
24+
// AccessCondition 根据传入的ctx获取用户信息, 结合req 中的信息 返回需要添加到sql的where条件
25+
type AccessCondition func(ctx context.Context, req AccessConditionReq) (g.Map, error)
26+
27+
// DefaultRole nodeRole 为前端显式指定的role, 需要此函数中判断该role是不是用户角色之一, 返回最终该节点的角色
28+
type DefaultRole func(ctx context.Context, req RoleReq) (string, error)
29+
30+
var (
31+
// AccessVerify 是否权限验证
32+
AccessVerify = false
33+
// AccessConditionFunc 自定义权限限制条件
34+
AccessConditionFunc AccessCondition
35+
// DefaultRoleFunc 自定义获取节点的默认角色
36+
DefaultRoleFunc DefaultRole = func(ctx context.Context, req RoleReq) (string, error) {
37+
return consts.UNKNOWN, nil
38+
}
39+
)
40+
41+
// 自定义设置从ctx获取用户id和角色的key
42+
var (
43+
RoleKey = "ajg.role" // ctx 中role 的key
44+
UserIdKey = "ajg.userId"
45+
)
46+
47+
// 设置 _access/_request 自定义表名
48+
var (
49+
TableAccess = "_access"
50+
TableRequest = "_request"
51+
)
52+
53+
// ========================= 角色 =======================
54+
// 角色列表
55+
// access 中填写的角色应在角色列表中
56+
57+
var (
58+
roleList = []string{consts.UNKNOWN, consts.LOGIN, consts.OWNER, consts.ADMIN}
59+
)
60+
61+
// AddRole 增加自定义角色
62+
func AddRole(name string) {
63+
if !lo.Contains(roleList, name) {
64+
roleList = append(roleList, name)
65+
}
66+
}
67+
68+
func RoleList() []string { return roleList }

config/config.go

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,5 @@
11
package config
22

3-
import (
4-
"context"
5-
"github.com/glennliao/apijson-go/consts"
6-
"github.com/gogf/gf/v2/frame/g"
7-
"github.com/gogf/gf/v2/text/gstr"
8-
"github.com/samber/lo"
9-
"strings"
10-
)
11-
123
var (
134
Debug = false
145
)
15-
16-
type AccessConditionReq struct {
17-
Table string
18-
TableAccessRoleList []string
19-
Method string
20-
NodeReq g.Map //节点的请求数据
21-
NodeRole string // 节点的角色
22-
}
23-
24-
type RoleReq struct {
25-
Table string
26-
Method string
27-
NodeRole string // 前端传入的节点的角色, 目前未传入则为空
28-
}
29-
30-
// AccessCondition 根据传入的ctx获取用户信息, 结合req 中的信息 返回需要添加到sql的where条件
31-
type AccessCondition func(ctx context.Context, req AccessConditionReq) (g.Map, error)
32-
33-
// DefaultRole nodeRole 为前端显式指定的role, 需要此函数中判断该role是不是用户角色之一, 返回最终该节点的角色
34-
type DefaultRole func(ctx context.Context, req RoleReq) (string, error)
35-
36-
var (
37-
// AccessVerify 是否权限验证
38-
AccessVerify = false
39-
// AccessConditionFunc 自定义权限限制条件
40-
AccessConditionFunc AccessCondition
41-
// DefaultRoleFunc 自定义获取节点的默认角色
42-
DefaultRoleFunc DefaultRole = func(ctx context.Context, req RoleReq) (string, error) {
43-
return consts.UNKNOWN, nil
44-
}
45-
)
46-
47-
// 自定义设置从ctx获取用户id和角色的key
48-
var (
49-
RoleKey = "ajg.role" // ctx 中role 的key
50-
UserIdKey = "ajg.userId"
51-
)
52-
53-
// 设置 _access/_request 自定义表名
54-
var (
55-
TableAccess = "_access"
56-
TableRequest = "_request"
57-
)
58-
59-
// ========================= 角色 =======================
60-
// 角色列表
61-
// access 中填写的角色应在角色列表中
62-
63-
var (
64-
roleList = []string{consts.UNKNOWN, consts.LOGIN, consts.OWNER, consts.ADMIN}
65-
)
66-
67-
// AddRole 增加自定义角色
68-
func AddRole(name string) {
69-
if !lo.Contains(roleList, name) {
70-
roleList = append(roleList, name)
71-
}
72-
}
73-
74-
func RoleList() []string { return roleList }
75-
76-
// ========================= 请求方法 =======================
77-
78-
var methodList = []string{consts.MethodGet, consts.MethodHead, consts.MethodPost, consts.MethodPut, consts.MethodDelete}
79-
80-
func MethodList() []string { return methodList }
81-
82-
// ========================= 字段配置 =======================
83-
84-
// jsonFieldStyle dbFieldStyle 配置Json字段, 数据库命名风格
85-
var jsonFieldStyle = consts.CaseCamel
86-
var dbFieldStyle = consts.CaseSnake
87-
88-
// TODO 从配置文件读取命名风格
89-
90-
// SetJsonFieldStyle 设置返回的 json字段风格, 默认为 lowerCamelCase风格, 参考 gstr.CaseCamelLower
91-
func SetJsonFieldStyle(style string) {
92-
jsonFieldStyle = fieldStyle(style)
93-
}
94-
95-
// SetDbFieldStyle 设置数据库的字段风格, 默认为 snake_case风格, 参考 gstr.CaseSnake
96-
func SetDbFieldStyle(style string) {
97-
dbFieldStyle = fieldStyle(style)
98-
}
99-
100-
func fieldStyle(style string) int {
101-
102-
switch strings.ToUpper(style) {
103-
case consts.CASE_CAMEL:
104-
return consts.CaseCamel
105-
case consts.CASE_CAMEL_UPPER:
106-
return consts.CaseCamelUpper
107-
case consts.CASE_SNAKE:
108-
return consts.CaseSnake
109-
}
110-
111-
return consts.Origin
112-
}
113-
114-
func convFieldStyle(style int, field string) string {
115-
switch style {
116-
117-
case consts.CaseCamel:
118-
return gstr.CaseCamelLower(field)
119-
case consts.CaseCamelUpper:
120-
return gstr.CaseCamel(field)
121-
case consts.CaseSnake:
122-
return gstr.CaseSnake(field)
123-
default:
124-
return field
125-
}
126-
}
127-
128-
func ToDbField(field string) string {
129-
return convFieldStyle(dbFieldStyle, field)
130-
}
131-
132-
func ToJsonField(field string) string {
133-
return convFieldStyle(jsonFieldStyle, field)
134-
}

config/field.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"github.com/gogf/gf/v2/text/gstr"
6+
)
7+
8+
// ========================= 字段配置 =======================
9+
10+
type FieldStyle func(ctx context.Context, table string, column string) string
11+
12+
func CaseCamel(ctx context.Context, table string, column string) string {
13+
return gstr.CaseCamelLower(column)
14+
}
15+
16+
func CaseCamelUpper(ctx context.Context, table string, column string) string {
17+
return gstr.CaseCamel(column)
18+
}
19+
20+
func CaseSnake(ctx context.Context, table string, column string) string {
21+
return gstr.CaseSnake(column)
22+
}
23+
24+
// jsonFieldStyle 数据库返回的字段
25+
var jsonFieldStyleFunc FieldStyle = CaseCamel
26+
27+
// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
28+
var dbFieldStyleFunc = CaseSnake
29+
30+
// SetJsonFieldStyle 设置返回的 json字段风格,
31+
func SetJsonFieldStyle(style FieldStyle) {
32+
jsonFieldStyleFunc = style
33+
}
34+
35+
// SetDbFieldStyle 设置数据库的字段风格
36+
func SetDbFieldStyle(style FieldStyle) {
37+
dbFieldStyleFunc = style
38+
}
39+
40+
// GetJsonFieldStyle 设置返回的 json字段风格,
41+
func GetJsonFieldStyle() FieldStyle {
42+
if jsonFieldStyleFunc == nil {
43+
return func(ctx context.Context, table string, column string) string {
44+
return column
45+
}
46+
}
47+
return jsonFieldStyleFunc
48+
}
49+
50+
// GetDbFieldStyle 设置数据库的字段风格
51+
func GetDbFieldStyle() FieldStyle {
52+
if dbFieldStyleFunc == nil {
53+
return func(ctx context.Context, table string, column string) string {
54+
return column
55+
}
56+
}
57+
return dbFieldStyleFunc
58+
}

config/z_regexp_test.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

consts/field.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

db/const.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)