-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelper.go
79 lines (74 loc) · 1.81 KB
/
helper.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
package core
import (
"errors"
"fmt"
"strings"
)
func getString(data map[string]any, key ...string) (string, error) {
if len(key) <= 0 {
panic("key must be provided at least once")
}
for i := 0; i < len(key)-1; i++ {
value, ok := data[key[i]]
if !ok {
return "", fmt.Errorf("%s doesn't exist", strings.Join(key[0:i+1], "."))
}
data, ok = value.(map[string]any)
if !ok {
return "", fmt.Errorf("%s is not a map", strings.Join(key[0:i+1], "."))
}
}
value, ok := data[key[len(key)-1]]
if !ok {
return "", fmt.Errorf("%s doesn't exist", strings.Join(key, "."))
}
str, ok := value.(string)
if !ok {
return str, errors.New("must be a string")
}
return str, nil
}
func getBool(data map[string]any, key ...string) (bool, error) {
if len(key) <= 0 {
panic("key must be provided at least once")
}
for i := 0; i < len(key)-1; i++ {
value, ok := data[key[i]]
if !ok {
return false, fmt.Errorf("%s doesn't exist", strings.Join(key[0:i+1], "."))
}
data, ok = value.(map[string]any)
if !ok {
return false, fmt.Errorf("%s is not a map", strings.Join(key[0:i+1], "."))
}
}
value, ok := data[key[len(key)-1]]
if !ok {
return false, fmt.Errorf("%s doesn't exist", strings.Join(key, "."))
}
b, ok := value.(bool)
if !ok {
return b, errors.New("must be a bool")
}
return b, nil
}
// isValidLevel tests if the given input is valid level config.
func isValidLevel(levelCfg string) bool {
validLevel := []string{"debug", "info", "warn", "error", "none"}
for i := range validLevel {
if validLevel[i] == levelCfg {
return true
}
}
return false
}
// isValidLevel tests if the given input is valid format config.
func isValidFormat(format string) bool {
validFormat := []string{"json", "logfmt"}
for i := range validFormat {
if validFormat[i] == format {
return true
}
}
return false
}