-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
246 lines (232 loc) · 5.25 KB
/
settings.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
package gosettings
import "fmt"
import "strings"
import "strconv"
import "encoding/json"
// Settings {key,value} map of configuration parameters.
type Settings map[string]interface{}
// Section will create a new settings object with parameters
// starting with `prefix`.
func (setts Settings) Section(prefix string) Settings {
section := make(Settings)
for key, value := range setts {
if strings.HasPrefix(key, prefix) {
section[key] = value
}
}
return section
}
// AddPrefix to each and every keyname in the settings map.
func (setts Settings) AddPrefix(prefix string) Settings {
newsetts := make(Settings)
for key, value := range setts {
newsetts[prefix+key] = value
}
return newsetts
}
// Trim leading prefix from each and every keyname in the settings map.
func (setts Settings) Trim(prefix string) Settings {
trimmed := make(Settings)
for key, value := range setts {
trimmed[strings.TrimPrefix(key, prefix)] = value
}
return trimmed
}
// Filter settings parameter whose keyname contain the substring ``subs``.
func (setts Settings) Filter(subs string) Settings {
subsetts := make(Settings)
for key, value := range setts {
if strings.Contains(key, subs) {
subsetts[key] = value
}
}
return subsetts
}
// Mixin settings to override `setts` with a list of `settings`. Typical
// usage:
// Make(Settings).Mixin(setts1, setts2, setts3)
//
// Mixin mutates the receiving map, and applies settings from left to right.
func (setts Settings) Mixin(settings ...interface{}) Settings {
update := func(arg map[string]interface{}) {
for key, value := range arg {
setts[key] = value
}
}
for _, arg := range settings {
switch cnf := arg.(type) {
case Settings:
update(map[string]interface{}(cnf))
case map[string]interface{}:
update(cnf)
}
}
return setts
}
// Bool return the boolean value for key.
func (setts Settings) Bool(key string) bool {
value, ok := setts[key]
if !ok {
panic(fmt.Errorf("missing settings %q", key))
}
val, ok := value.(bool)
if !ok {
panic(fmt.Errorf("settings %q not a bool: %T", key, value))
}
return val
}
// Float64 return the float64 value for key.
func (setts Settings) Float64(key string) float64 {
value, ok := setts[key]
if !ok {
panic(fmt.Errorf("missing settings %q", key))
}
switch val := value.(type) {
case float64:
return val
case float32:
return float64(val)
case uint:
return float64(val)
case uint64:
return float64(val)
case uint32:
return float64(val)
case uint16:
return float64(val)
case uint8:
return float64(val)
case int:
return float64(val)
case int64:
return float64(val)
case int32:
return float64(val)
case int16:
return float64(val)
case int8:
return float64(val)
}
panic(fmt.Errorf("settings %v not a number: %T", key, value))
}
// Int64 return the int64 value for key.
func (setts Settings) Int64(key string) int64 {
value, ok := setts[key]
if !ok {
panic(fmt.Errorf("missing settings %q", key))
}
switch val := value.(type) {
case float64:
return int64(val)
case float32:
return int64(val)
case uint:
return int64(val)
case uint64:
return int64(val)
case uint32:
return int64(val)
case uint16:
return int64(val)
case uint8:
return int64(val)
case int:
return int64(val)
case int64:
return int64(val)
case int32:
return int64(val)
case int16:
return int64(val)
case int8:
return int64(val)
case string:
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
panic(err)
}
return i
}
panic(fmt.Errorf("settings %v not a number: %T", key, value))
}
// Uint64 return the uint64 value for key.
func (setts Settings) Uint64(key string) uint64 {
value, ok := setts[key]
if !ok {
panic(fmt.Errorf("missing settings %q", key))
}
switch val := value.(type) {
case float64:
return uint64(val)
case float32:
return uint64(val)
case uint:
return uint64(val)
case uint64:
return uint64(val)
case uint32:
return uint64(val)
case uint16:
return uint64(val)
case uint8:
return uint64(val)
case int:
return uint64(val)
case int64:
return uint64(val)
case int32:
return uint64(val)
case int16:
return uint64(val)
case int8:
return uint64(val)
case string:
u, err := strconv.ParseUint(val, 10, 64)
if err != nil {
panic(err)
}
return u
}
panic(fmt.Errorf("settings %v not a number: %T", key, value))
}
// String return the string value for key.
func (setts Settings) String(key string) string {
value, ok := setts[key]
if !ok {
panic(fmt.Errorf("missing settings %q", key))
}
val, ok := value.(string)
if !ok {
panic(fmt.Errorf("settings %v not string: %T", key, value))
}
return val
}
// Strings shall parse value as comma separated string items.
func (setts Settings) Strings(key string) []string {
value, ok := setts[key]
if !ok {
panic(fmt.Errorf("missing settings %q", key))
}
vals, ok := value.([]string)
if ok {
return vals
}
val, ok := value.(string)
if !ok {
panic(fmt.Errorf("settings %v not string: %T", key, value))
}
ss := strings.Split(val, ",")
outs := make([]string, 0)
for _, s := range ss {
if s = strings.Trim(s, " \t\r\n"); s == "" {
continue
}
outs = append(outs, s)
}
return outs
}
// PPrint pretty print settings and return as string.
func (setts Settings) PPrint() string {
data, _ := json.MarshalIndent(setts, "", " ")
return string(data)
}