/
validate.go
184 lines (158 loc) · 4.48 KB
/
validate.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
package config
import (
"fmt"
"reflect"
"sort"
"strings"
"unicode"
"github.com/BurntSushi/toml"
"github.com/agext/levenshtein"
)
// UnexpectedKey represents result of validation
type UnexpectedKey struct {
Key string
SuggestKey string
}
func normalizeKey(f reflect.StructField) string {
key := string(unicode.ToLower([]rune(f.Name)[0])) + f.Name[1:]
if s := f.Tag.Get("toml"); s != "" {
key = s
}
return key
}
func addKeyToCandidates(f reflect.StructField, candidates []string) []string {
key := normalizeKey(f)
return append(candidates, key)
}
func makeCandidates(t reflect.Type) []string {
if t.Kind() == reflect.Map || t.Kind() == reflect.Ptr {
return makeCandidates(t.Elem())
}
var candidates []string
fields := reflect.VisibleFields(t)
for _, f := range fields {
if s := f.Tag.Get("conf"); s == "ignore" {
continue
}
if s := f.Tag.Get("conf"); s == "parent" {
candidates = addKeyToCandidates(f, candidates)
childCandidates := makeCandidates(f.Type)
candidates = append(candidates, childCandidates...)
continue
}
candidates = addKeyToCandidates(f, candidates)
}
return candidates
}
func keySuggestion(given string, candidates []string) string {
for _, candidate := range candidates {
dist := levenshtein.Distance(given, candidate, nil)
if dist < 3 {
return candidate
}
}
return ""
}
// ValidateConfigFile detect unexpected key in configfile
func ValidateConfigFile(file string) ([]UnexpectedKey, error) {
config := &Config{}
md, err := toml.DecodeFile(file, config)
if err != nil {
return nil, fmt.Errorf("failed to test config: %s", err)
}
var c Config
candidates := makeCandidates(reflect.TypeOf(c))
var unexpectedKeys []UnexpectedKey
var detectedKeys []string
undecodedKeys := md.Undecoded()
sort.Slice(undecodedKeys, func(i, j int) bool {
return undecodedKeys[i].String() < undecodedKeys[j].String()
})
/**
```
[plugin.checks.incorrect]
command = "test command"
action = { command = "test command", user = "test user", en = { TEST_KEY = "VALUE_1" } }
[plugins.check.incorrect]
command = "test command"
```
undecodedKeys -> [
plugin.checks.incorrect.action.en,
plugin.checks.incorrect.action.en.TEST_KEY,
plugins.check.incorrect,
plugins.check.incorrect.command,
]
**/
for _, v := range undecodedKeys {
/*
v: plugin.checks.incorrect.action.en
splitedKey -> [plugin, checks, incorrect, action, en]
topKey -> plugin
lastKey -> en
parentKey -> plugin.checks.incorrect.action
*/
splitedKey := strings.Split(v.String(), ".")
topKey := splitedKey[0]
lastKey := splitedKey[len(splitedKey)-1]
parentKey := strings.Join(splitedKey[:len(splitedKey)-1], ".")
// When parentKey (e.g., plugin.checks.incorrect.action.en) or topKey (e.g., plugins) already exists in detected keys,
// childKey (e.g., plugin.checks.incorrect.action.en.TEST_KEY, plugins.check.incorrect.command) isn't detected.
if containKey(detectedKeys, parentKey) || containKey(detectedKeys, topKey) {
continue
}
var key string
var suggestKey string
if containKey(candidates, topKey) {
key = v.String() // same as parantKey + "." + lastKey
suggestResult := keySuggestion(lastKey, candidates)
if suggestResult == "" {
suggestKey = ""
} else {
suggestKey = parentKey + "." + suggestResult
}
} else {
key = topKey
suggestKey = keySuggestion(topKey, candidates)
}
unexpectedKeys = append(unexpectedKeys, UnexpectedKey{
key,
suggestKey,
})
detectedKeys = append(detectedKeys, key)
}
/*
detect [plugin.<unexpected>.<???>]
<unexpected> should be "metrics" or "checks" or "metadata"
default suggestion of [plugin.<unexpected>.<???>] is plugin.metrics.<???>
*/
for k1, v := range config.Plugin {
if k1 != "metrics" && k1 != "checks" && k1 != "metadata" {
suggestResult := keySuggestion(k1, []string{"metrics", "checks", "metadata"})
for k2 := range v {
var key string = fmt.Sprintf("plugin.%s.%s", k1, k2)
var suggestKey string
if suggestResult == "" {
suggestKey = fmt.Sprintf("plugin.metrics.%s", k2)
} else {
suggestKey = fmt.Sprintf("plugin.%s.%s", suggestResult, k2)
}
unexpectedKeys = append(unexpectedKeys, UnexpectedKey{
key,
suggestKey,
})
}
}
}
sort.Slice(unexpectedKeys, func(i, j int) bool {
return unexpectedKeys[i].Key < unexpectedKeys[j].Key
})
return unexpectedKeys, nil
}
func containKey(target []string, want string) bool {
for _, v := range target {
if v == want {
return true
}
}
return false
}