/
jsonconfig.go
297 lines (264 loc) · 6.59 KB
/
jsonconfig.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
Copyright 2011 The go4 Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package jsonconfig defines a helper type for JSON objects to be
// used for configuration.
package jsonconfig // import "go4.org/jsonconfig"
import (
"fmt"
"sort"
"strconv"
"strings"
)
// Obj is a JSON configuration map.
type Obj map[string]interface{}
// ReadFile reads JSON config data from the specified open file, expanding
// all expressions. Use *ConfigParser.ReadFile instead if you
// need to set c.IncludeDirs.
func ReadFile(configPath string) (Obj, error) {
var c ConfigParser
return c.ReadFile(configPath)
}
func (jc Obj) RequiredObject(key string) Obj {
return jc.obj(key, false)
}
func (jc Obj) OptionalObject(key string) Obj {
return jc.obj(key, true)
}
func (jc Obj) obj(key string, optional bool) Obj {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if optional {
return make(Obj)
}
jc.appendError(fmt.Errorf("Missing required config key %q (object)", key))
return make(Obj)
}
m, ok := ei.(map[string]interface{})
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q to be an object, not %T", key, ei))
return make(Obj)
}
return m
}
func (jc Obj) RequiredString(key string) string {
return jc.string(key, nil)
}
func (jc Obj) OptionalString(key, def string) string {
return jc.string(key, &def)
}
func (jc Obj) string(key string, def *string) string {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if def != nil {
return *def
}
jc.appendError(fmt.Errorf("Missing required config key %q (string)", key))
return ""
}
s, ok := ei.(string)
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q to be a string", key))
return ""
}
return s
}
func (jc Obj) RequiredStringOrObject(key string) interface{} {
return jc.stringOrObject(key, true)
}
func (jc Obj) OptionalStringOrObject(key string) interface{} {
return jc.stringOrObject(key, false)
}
func (jc Obj) stringOrObject(key string, required bool) interface{} {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if !required {
return nil
}
jc.appendError(fmt.Errorf("Missing required config key %q (string or object)", key))
return ""
}
if _, ok := ei.(map[string]interface{}); ok {
return ei
}
if _, ok := ei.(string); ok {
return ei
}
jc.appendError(fmt.Errorf("Expected config key %q to be a string or object", key))
return ""
}
func (jc Obj) RequiredBool(key string) bool {
return jc.bool(key, nil)
}
func (jc Obj) OptionalBool(key string, def bool) bool {
return jc.bool(key, &def)
}
func (jc Obj) bool(key string, def *bool) bool {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if def != nil {
return *def
}
jc.appendError(fmt.Errorf("Missing required config key %q (boolean)", key))
return false
}
switch v := ei.(type) {
case bool:
return v
case string:
b, err := strconv.ParseBool(v)
if err != nil {
jc.appendError(fmt.Errorf("Config key %q has bad boolean format %q", key, v))
}
return b
default:
jc.appendError(fmt.Errorf("Expected config key %q to be a boolean", key))
return false
}
}
func (jc Obj) RequiredInt(key string) int {
return jc.int(key, nil)
}
func (jc Obj) OptionalInt(key string, def int) int {
return jc.int(key, &def)
}
func (jc Obj) int(key string, def *int) int {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if def != nil {
return *def
}
jc.appendError(fmt.Errorf("Missing required config key %q (integer)", key))
return 0
}
b, ok := ei.(float64)
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q to be a number", key))
return 0
}
return int(b)
}
func (jc Obj) RequiredInt64(key string) int64 {
return jc.int64(key, nil)
}
func (jc Obj) OptionalInt64(key string, def int64) int64 {
return jc.int64(key, &def)
}
func (jc Obj) int64(key string, def *int64) int64 {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if def != nil {
return *def
}
jc.appendError(fmt.Errorf("Missing required config key %q (integer)", key))
return 0
}
b, ok := ei.(float64)
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q to be a number", key))
return 0
}
return int64(b)
}
func (jc Obj) RequiredList(key string) []string {
return jc.requiredList(key, true)
}
func (jc Obj) OptionalList(key string) []string {
return jc.requiredList(key, false)
}
func (jc Obj) requiredList(key string, required bool) []string {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
if required {
jc.appendError(fmt.Errorf("Missing required config key %q (list of strings)", key))
}
return nil
}
eil, ok := ei.([]interface{})
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q to be a list, not %T", key, ei))
return nil
}
sl := make([]string, len(eil))
for i, ei := range eil {
s, ok := ei.(string)
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q index %d to be a string, not %T", key, i, ei))
return nil
}
sl[i] = s
}
return sl
}
func (jc Obj) noteKnownKey(key string) {
_, ok := jc["_knownkeys"]
if !ok {
jc["_knownkeys"] = make(map[string]bool)
}
jc["_knownkeys"].(map[string]bool)[key] = true
}
func (jc Obj) appendError(err error) {
ei, ok := jc["_errors"]
if ok {
jc["_errors"] = append(ei.([]error), err)
} else {
jc["_errors"] = []error{err}
}
}
// UnknownKeys returns the keys from the config that have not yet been discovered by one of the RequiredT or OptionalT calls.
func (jc Obj) UnknownKeys() []string {
ei, ok := jc["_knownkeys"]
var known map[string]bool
if ok {
known = ei.(map[string]bool)
}
var unknown []string
for k, _ := range jc {
if ok && known[k] {
continue
}
if strings.HasPrefix(k, "_") {
// Permit keys with a leading underscore as a
// form of comments.
continue
}
unknown = append(unknown, k)
}
sort.Strings(unknown)
return unknown
}
func (jc Obj) Validate() error {
unknown := jc.UnknownKeys()
for _, k := range unknown {
jc.appendError(fmt.Errorf("Unknown key %q", k))
}
ei, ok := jc["_errors"]
if !ok {
return nil
}
errList := ei.([]error)
if len(errList) == 1 {
return errList[0]
}
strs := make([]string, 0)
for _, v := range errList {
strs = append(strs, v.Error())
}
return fmt.Errorf("Multiple errors: " + strings.Join(strs, ", "))
}