-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
312 lines (284 loc) · 7.8 KB
/
config.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2012 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"strings"
"launchpad.net/~niemeyer/goyaml/beta"
)
// Config ---------------------------------------------------------------------
// Config represents a configuration with convenient access methods.
type Config struct {
Root interface{}
}
// Get returns a nested config according to a dotted path.
func (cfg *Config) Get(path string) (*Config, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return nil, err
}
return &Config{Root: n}, nil
}
// Bool returns a bool according to a dotted path.
func (cfg *Config) Bool(path string) (bool, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return false, err
}
switch n := n.(type) {
case bool:
return n, nil
case string:
return strconv.ParseBool(n)
}
return false, typeMismatch("bool or string", n)
}
// Float64 returns a float64 according to a dotted path.
func (cfg *Config) Float64(path string) (float64, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return 0, err
}
switch n := n.(type) {
case float64:
return n, nil
case int:
return float64(n), nil
case string:
return strconv.ParseFloat(n, 64)
}
return 0, typeMismatch("float64, int or string", n)
}
// Int returns an int according to a dotted path.
func (cfg *Config) Int(path string) (int, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return 0, err
}
switch n := n.(type) {
case float64:
// encoding/json unmarshals numbers into floats, so we compare
// the string representation to see if we can return an int.
if i := int(n); fmt.Sprint(i) == fmt.Sprint(n) {
return i, nil
} else {
return 0, fmt.Errorf("Value can't be converted to int: %v", n)
}
case int:
return n, nil
case string:
if v, err := strconv.ParseInt(n, 10, 0); err == nil {
return int(v), nil
} else {
return 0, err
}
}
return 0, typeMismatch("float64, int or string", n)
}
// List returns a []interface{} according to a dotted path.
func (cfg *Config) List(path string) ([]interface{}, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return nil, err
}
if value, ok := n.([]interface{}); ok {
return value, nil
}
return nil, typeMismatch("[]interface{}", n)
}
// Map returns a map[string]interface{} according to a dotted path.
func (cfg *Config) Map(path string) (map[string]interface{}, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return nil, err
}
if value, ok := n.(map[string]interface{}); ok {
return value, nil
}
return nil, typeMismatch("map[string]interface{}", n)
}
// String returns a string according to a dotted path.
func (cfg *Config) String(path string) (string, error) {
n, err := Get(cfg.Root, path)
if err != nil {
return "", err
}
switch n := n.(type) {
case bool, float64, int:
return fmt.Sprint(n), nil
case string:
return n, nil
}
return "", typeMismatch("bool, float64, int or string", n)
}
// typeMismatch returns an error for an expected type.
func typeMismatch(expected string, got interface{}) error {
return fmt.Errorf("Type mismatch: expected %s; got %T", expected, got)
}
// Fetching -------------------------------------------------------------------
// Get returns a child of the given value according to a dotted path.
func Get(cfg interface{}, path string) (interface{}, error) {
parts := strings.Split(path, ".")
// Normalize path.
for k, v := range parts {
if v == "" {
if k == 0 {
parts = parts[1:]
} else {
return nil, fmt.Errorf("Invalid path %q", path)
}
}
}
// Get the value.
for pos, part := range parts {
switch c := cfg.(type) {
case []interface{}:
if i, error := strconv.ParseInt(part, 10, 0); error == nil {
if int(i) < len(c) {
cfg = c[i]
} else {
return nil, fmt.Errorf(
"Index out of range at %q: list has only %v items",
strings.Join(parts[:pos+1], "."), len(c))
}
} else {
return nil, fmt.Errorf("Invalid list index at %q",
strings.Join(parts[:pos+1], "."))
}
case map[string]interface{}:
if value, ok := c[part]; ok {
cfg = value
} else {
return nil, fmt.Errorf("Nonexistent map key at %q",
strings.Join(parts[:pos+1], "."))
}
default:
return nil, fmt.Errorf(
"Invalid type at %q: expected []interface{} or map[string]interface{}; got %T",
strings.Join(parts[:pos+1], "."), cfg)
}
}
return cfg, nil
}
// Parsing --------------------------------------------------------------------
// Must is a wrapper for parsing functions to be used during initialization.
// It panics on failure.
func Must(cfg *Config, err error) *Config {
if err != nil {
panic(err)
}
return cfg
}
// normalizeValue normalizes a unmarshalled value. This is needed because
// encoding/json doesn't support marshalling map[interface{}]interface{}.
func normalizeValue(value interface{}) (interface{}, error) {
switch value := value.(type) {
case map[interface{}]interface{}:
node := make(map[string]interface{}, len(value))
for k, v := range value {
key, ok := k.(string)
if !ok {
return nil, fmt.Errorf("Unsupported map key: %#v", k)
}
item, err := normalizeValue(v)
if err != nil {
return nil, fmt.Errorf("Unsupported map value: %#v", v)
}
node[key] = item
}
return node, nil
case map[string]interface{}:
node := make(map[string]interface{}, len(value))
for key, v := range value {
item, err := normalizeValue(v)
if err != nil {
return nil, fmt.Errorf("Unsupported map value: %#v", v)
}
node[key] = item
}
return node, nil
case []interface{}:
node := make([]interface{}, len(value))
for key, v := range value {
item, err := normalizeValue(v)
if err != nil {
return nil, fmt.Errorf("Unsupported list item: %#v", v)
}
node[key] = item
}
return node, nil
case bool, float64, int, string:
return value, nil
}
return nil, fmt.Errorf("Unsupported type: %T", value)
}
// JSON -----------------------------------------------------------------------
// ParseJson reads a JSON configuration from the given string.
func ParseJson(cfg string) (*Config, error) {
return parseJson([]byte(cfg))
}
// ParseJsonFile reads a JSON configuration from the given filename.
func ParseJsonFile(filename string) (*Config, error) {
cfg, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return parseJson(cfg)
}
// parseJson performs the real JSON parsing.
func parseJson(cfg []byte) (*Config, error) {
var out interface{}
var err error
if err = json.Unmarshal(cfg, &out); err != nil {
return nil, err
}
if out, err = normalizeValue(out); err != nil {
return nil, err
}
return &Config{Root: out}, nil
}
// RenderJson renders a YAML configuration.
func RenderJson(cfg interface{}) (string, error) {
b, err := json.Marshal(cfg)
if err != nil {
return "", err
}
return string(b), nil
}
// YAML -----------------------------------------------------------------------
// ParseYaml reads a YAML configuration from the given string.
func ParseYaml(cfg string) (*Config, error) {
return parseYaml([]byte(cfg))
}
// ParseYamlFile reads a YAML configuration from the given filename.
func ParseYamlFile(filename string) (*Config, error) {
cfg, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return parseYaml(cfg)
}
// parseYaml performs the real YAML parsing.
func parseYaml(cfg []byte) (*Config, error) {
var out interface{}
var err error
if err = goyaml.Unmarshal(cfg, &out); err != nil {
return nil, err
}
if out, err = normalizeValue(out); err != nil {
return nil, err
}
return &Config{Root: out}, nil
}
// RenderYaml renders a YAML configuration.
func RenderYaml(cfg interface{}) (string, error) {
b, err := goyaml.Marshal(cfg)
if err != nil {
return "", err
}
return string(b), nil
}