-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathyaml_loader.go
237 lines (228 loc) · 5.73 KB
/
yaml_loader.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package beeorm
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
func (r *registry) InitByYaml(yaml map[string]any) error {
for key, data := range yaml {
dataAsMap, err := fixYamlMap(data, "orm")
if err != nil {
return err
}
for dataKey, value := range dataAsMap {
switch dataKey {
case "mysql":
err = validateOrmMysqlURI(r, value, key)
if err != nil {
return err
}
case "redis":
err = validateRedisURI(r, value, key)
if err != nil {
return err
}
case "sentinel":
err = validateSentinel(r, value, key)
if err != nil {
return err
}
case "local_cache":
limit, err := validateOrmInt(value, key)
if err != nil {
return err
}
r.RegisterLocalCache(key, limit)
}
}
}
for _, plugin := range r.plugins {
pluginInterfaceValidateRegistry, isInterface := plugin.(PluginInterfaceInitRegistryFromYaml)
if isInterface {
err := pluginInterfaceValidateRegistry.InitRegistryFromYaml(r, yaml)
if err != nil {
return err
}
}
}
return nil
}
func validateOrmMysqlURI(registry *registry, value any, key string) error {
def, err := fixYamlMap(value, key)
if err != nil {
return err
}
uri := ""
options := &MySQLOptions{}
for k, v := range def {
switch k {
case "uri":
uri, err = validateOrmString(v, "uri")
case "connMaxLifetime":
connMaxLifetime, err := validateOrmInt(v, "connMaxLifetime")
if err != nil {
return err
}
options.ConnMaxLifetime = time.Duration(connMaxLifetime) * time.Second
case "maxOpenConnections":
options.MaxOpenConnections, err = validateOrmInt(v, "maxOpenConnections")
if err != nil {
return err
}
case "maxIdleConnections":
options.MaxIdleConnections, err = validateOrmInt(v, "maxIdleConnections")
if err != nil {
return err
}
case "defaultEncoding":
options.DefaultEncoding, err = validateOrmString(v, "defaultEncoding")
if err != nil {
return err
}
case "defaultCollate":
options.DefaultCollate, err = validateOrmString(v, "defaultCollate")
if err != nil {
return err
}
case "ignoredTables":
options.IgnoredTables, err = validateOrmStrings(v, "ignoredTables")
if err != nil {
return err
}
}
}
registry.RegisterMySQL(uri, key, options)
return nil
}
func validateRedisURI(registry *registry, value any, key string) error {
asString, ok := value.(string)
if !ok {
return fmt.Errorf("redis uri '%v' is not valid", value)
}
parts := strings.Split(asString, "?")
elements := strings.Split(parts[0], ":")
dbNumber := ""
uri := ""
isSocket := strings.Index(parts[0], ".sock") > 0
l := len(elements)
switch l {
case 2:
dbNumber = elements[1]
uri = elements[0]
case 3:
if isSocket {
dbNumber = elements[1]
uri = elements[0]
} else {
dbNumber = elements[2]
uri = elements[0] + ":" + elements[1]
}
case 4:
dbNumber = elements[2]
uri = elements[0] + ":" + elements[1]
default:
return fmt.Errorf("redis uri '%v' is not valid", value)
}
db, err := strconv.ParseUint(dbNumber, 10, 64)
if err != nil {
return fmt.Errorf("redis uri '%v' is not valid", value)
}
var options *RedisOptions
if len(parts) == 2 && parts[1] != "" {
values, err := url.ParseQuery(parts[1])
if err != nil {
return fmt.Errorf("redis uri '%v' is not valid", value)
}
if values.Has("user") && values.Has("password") {
options = &RedisOptions{User: values.Get("user"), Password: values.Get("password")}
}
}
registry.RegisterRedis(uri, int(db), key, options)
return nil
}
func validateSentinel(registry *registry, value any, key string) error {
def, err := fixYamlMap(value, key)
if err != nil {
return err
}
for master, values := range def {
asSlice, ok := values.([]any)
if !ok {
return fmt.Errorf("sentinel '%v' is not valid", value)
}
asStrings := make([]string, len(asSlice))
for i, v := range asSlice {
asStrings[i] = fmt.Sprintf("%v", v)
}
db := 0
parts := strings.Split(master, "?")
elements := strings.Split(parts[0], ":")
l := len(elements)
if l >= 2 {
master = elements[0]
nr, err := strconv.ParseUint(elements[1], 10, 64)
if err != nil {
return fmt.Errorf("sentinel db '%v' is not valid", value)
}
db = int(nr)
}
options := &RedisOptions{Master: master, Sentinels: asStrings}
if len(parts) == 2 && parts[1] != "" {
extra, err := url.ParseQuery(parts[1])
if err != nil {
return fmt.Errorf("sentinel uri '%v' is not valid", master)
}
if extra.Has("user") && extra.Has("password") {
options.User = extra.Get("user")
options.Password = extra.Get("password")
}
}
registry.RegisterRedis("", db, key, options)
}
return nil
}
func fixYamlMap(value any, key string) (map[string]any, error) {
def, ok := value.(map[string]any)
if !ok {
def2, ok := value.(map[any]any)
if !ok {
return nil, fmt.Errorf("orm yaml key %s is not valid", key)
}
def = make(map[string]any)
for k, v := range def2 {
def[fmt.Sprintf("%v", k)] = v
}
}
return def, nil
}
func validateOrmInt(value any, key string) (int, error) {
asInt, ok := value.(int)
if !ok {
return 0, fmt.Errorf("orm value for %s: %v is not valid", key, value)
}
return asInt, nil
}
func validateOrmString(value any, key string) (string, error) {
asString, ok := value.(string)
if !ok {
return "", fmt.Errorf("orm value for %s: %v is not valid", key, value)
}
return asString, nil
}
func validateOrmStrings(value any, key string) ([]string, error) {
asSlice, ok := value.([]any)
if !ok {
return nil, fmt.Errorf("orm value for %s: %v is not valid", key, value)
}
asStrings := make([]string, len(asSlice))
for i, val := range asSlice {
asString, ok := val.(string)
if !ok {
return nil, fmt.Errorf("orm value for %s: %v is not valid", key, value)
}
asStrings[i] = asString
}
return asStrings, nil
}