forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_config.go
More file actions
executable file
·128 lines (102 loc) · 2.72 KB
/
query_config.go
File metadata and controls
executable file
·128 lines (102 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package config
import (
"context"
"net/http"
"github.com/glennliao/apijson-go/model"
"github.com/samber/lo"
)
type QueryConfig struct {
access *Access
functions *functions
maxTreeDeep int
maxTreeWidth int
defaultRoleFunc DefaultRole
}
func (c *QueryConfig) NoVerify() bool {
return c.access.NoVerify
}
func (c *QueryConfig) DefaultRoleFunc() DefaultRole {
return c.defaultRoleFunc
}
func (c *QueryConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig, error) {
return c.access.GetAccess(key, noVerify)
}
func (c *QueryConfig) Func(name string) *Func {
return c.functions.funcMap[name]
}
func (c *QueryConfig) CallFunc(ctx context.Context, name string, param model.Map) (res any, err error) {
return c.functions.Call(ctx, name, param)
}
func (c *QueryConfig) MaxTreeDeep() int {
return c.maxTreeDeep
}
func (c *QueryConfig) MaxTreeWidth() int {
return c.maxTreeWidth
}
type ExecutorConfig struct {
NoVerify bool
accessConfig *AccessConfig
method string
role string
DBMeta *DBMeta
DbFieldStyle FieldStyle
JsonFieldStyle FieldStyle
}
func NewExecutorConfig(accessConfig *AccessConfig, method string, noVerify bool) *ExecutorConfig {
return &ExecutorConfig{
accessConfig: accessConfig,
method: method,
NoVerify: noVerify,
}
}
func (c *ExecutorConfig) SetRole(role string) {
c.role = role
}
func (c *ExecutorConfig) TableName() string {
return c.accessConfig.Name
}
func (c *ExecutorConfig) TableColumns() []string {
return c.DBMeta.GetTableColumns(c.accessConfig.Name)
}
func (c *ExecutorConfig) GetFieldsGetByRole() *FieldsGetValue {
if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
return val
}
return c.accessConfig.FieldsGet["default"]
}
func (c *ExecutorConfig) GetFieldsGetOutByRole() []string {
var fieldsMap map[string]string
if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
fieldsMap = val.Out
} else {
fieldsMap = c.accessConfig.FieldsGet["default"].Out
}
return lo.Keys(fieldsMap)
}
func (c *ExecutorConfig) GetFieldsGetInByRole() map[string][]string {
var inFieldsMap map[string][]string
if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
inFieldsMap = val.In
} else {
inFieldsMap = c.accessConfig.FieldsGet["default"].In
}
return inFieldsMap
}
func (c *ExecutorConfig) AccessRoles() []string {
switch c.method {
case http.MethodGet:
return c.accessConfig.Get
case http.MethodHead:
return c.accessConfig.Head
case http.MethodPost:
return c.accessConfig.Post
case http.MethodPut:
return c.accessConfig.Put
case http.MethodDelete:
return c.accessConfig.Delete
}
return []string{}
}
func (c *ExecutorConfig) Executor() string {
return c.accessConfig.Executor
}