-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
283 lines (219 loc) · 5.64 KB
/
parser.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package boa
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"reflect"
"strconv"
"strings"
)
var (
config = map[string]any{}
flattenConfig = map[string]any{}
defaults = map[string]any{}
)
// ParseConfig reads the config from an io.Reader.
//
// The config may be in JSON or in JSONC ( json with comment)
// Allowed format for comments are
// * single line ( //... )
// * multiligne ( /*...*/ )
func ParseConfig(jsoncConfig io.Reader) error {
jsonc, err := io.ReadAll(jsoncConfig)
if err != nil {
return fmt.Errorf("fail to read from reader: %v", err)
}
cleanJson := removeComment(jsonc)
d := json.NewDecoder(bytes.NewReader(cleanJson))
d.UseNumber()
err = d.Decode(&config)
if err != nil {
return fmt.Errorf("fail to parse JSON: %v", err)
}
flatten("", config, flattenConfig)
return nil
}
func removeComment(src []byte) []byte {
output := make([]byte, 0, len(src))
var prev byte
inString := false
inSingleLineComment := false
inMultiligneComment := false
for _, b := range src {
switch b {
case '"':
inString = !inString
case '/':
if !inString && prev == '/' {
output = output[0 : len(output)-1]
inSingleLineComment = true
}
if inMultiligneComment && prev == '*' {
inMultiligneComment = false
continue
}
case '*':
if !inString && prev == '/' {
output = output[0 : len(output)-1]
inMultiligneComment = true
}
case '\n':
inSingleLineComment = false
}
prev = b
if !inSingleLineComment && !inMultiligneComment {
output = append(output, b)
}
}
return output
}
func flatten(prefix string, src map[string]any, dst map[string]any) {
if len(prefix) > 0 {
prefix += "."
}
for k, v := range src {
switch child := v.(type) {
case map[string]any:
flatten(prefix+k, child, dst)
default:
dst[prefix+k] = v
}
}
}
// SetDefault set the default value for this key.
func SetDefault(key string, value any) {
switch reflect.TypeOf(value).Kind() {
case reflect.Int,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint32,
reflect.Uint64:
defaults[key] = json.Number(fmt.Sprintf("%d", value))
case reflect.Float64:
defaults[key] = json.Number(strconv.FormatFloat(value.(float64), 'g', -1, 64))
default:
defaults[key] = value
}
}
// GetString returns the value associated with the key as a string.
func GetString(key string) string {
return getValue[string](key)
}
// GetBool returns the value associated with the key as a bool.
func GetBool(key string) bool {
return getValue[bool](key)
}
// GetInt returns the value associated with the key as an int.
func GetInt(key string) int {
val := getValue[json.Number](key)
i, err := strconv.ParseInt(string(val), 10, 64)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an int", val))
}
return int(i)
}
// GetInt32 returns the value associated with the key as an int32
func GetInt32(key string) int32 {
val := getValue[json.Number](key)
i, err := strconv.ParseInt(string(val), 10, 32)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an int32", val))
}
return int32(i)
}
// GetInt64 returns the value associated with the key as an int64.
func GetInt64(key string) int64 {
val := getValue[json.Number](key)
i, err := strconv.ParseInt(string(val), 10, 64)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an int64", val))
}
return i
}
// GetUint returns the value associated with the key as an uint.
func GetUint(key string) uint {
val := getValue[json.Number](key)
n := cast[json.Number](val)
i, err := strconv.ParseUint(string(n), 10, 64)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an uint", val))
}
return uint(i)
}
// GetUint32 returns the value associated with the key as an uint32.
func GetUint32(key string) uint32 {
val := getValue[json.Number](key)
i, err := strconv.ParseUint(string(val), 10, 32)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an uint32", val))
}
return uint32(i)
}
// GetUint64 returns the value associated with the key as an uint64.
func GetUint64(key string) uint64 {
val := getValue[json.Number](key)
i, err := strconv.ParseUint(string(val), 10, 64)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an uint64", val))
}
return uint64(i)
}
// GetFloat64 returns the value associated with the key as a float64.
func GetFloat64(key string) float64 {
val := getValue[json.Number](key)
f, err := strconv.ParseFloat(string(val), 64)
if err != nil {
panic(fmt.Sprintf("Can't parse number '%s' as an float64", val))
}
return f
}
// GetAny returns any value associated with the key.
func GetAny(key string) any {
return getValue[any](key)
}
// GetMap returns the map associated with the key.
// Numbers will be of type json.Number
// returns nil if the key does not exist
func GetMap(key string) map[string]any {
nProp := strings.Split(key, ".")
nested := config
for i, prop := range nProp {
if i == len(nProp)-1 {
if v, ok := nested[prop]; ok {
return cast[map[string]any](v)
}
continue
}
nested, _ = nested[prop].(map[string]any)
}
return getDefault[map[string]any](key)
}
func getValue[T any](key string) (val T) {
v, ok := flattenConfig[key]
if ok {
return cast[T](v)
}
return getDefault[T](key)
}
func getDefault[T any](key string) (val T) {
v, ok := defaults[key]
if ok {
return cast[T](v)
}
log.Printf("no value found for key '%s', using nil value instead", key)
var zeroVal T
if reflect.TypeOf(zeroVal) == reflect.TypeOf(json.Number("")) {
return cast[T](json.Number("0"))
}
return zeroVal
}
func cast[T any](v any) T {
s, ok := v.(T)
if !ok {
panic(fmt.Sprintf("'%v' is not a %s", v, reflect.TypeOf(s)))
}
return s
}