forked from goraz/onion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonion_test.go
430 lines (371 loc) · 12.3 KB
/
onion_test.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package onion
import (
"fmt"
"os"
"reflect"
"testing"
"time"
"strings"
. "github.com/smartystreets/goconvey/convey"
)
type layerMock struct {
data map[string]interface{}
}
type AnonNested struct {
Key4 int64
Key5 int64
Key5Again int `onion:"key5"`
}
type Nested struct {
N0 string
N1 int
N2 bool
F32 float32
F64 float64
}
type AnonIgnored struct {
Six string
}
type structExample struct {
Key0 int
Universe string `onion:"key1"`
Key2 string
Key3 bool
Du time.Duration
AnonNested
Nested `onion:"nested"`
AnonIgnored `onion:"-"`
Another Nested `onion:"nested"`
Ignored string `onion:"-"`
}
type EmbededTest struct {
Operator string `onion:"operator"`
Aggregator string `onion:"aggregator"`
Ignored string `onion:"-"`
}
type Worker struct {
Inner struct {
Tmp2 int `onion:"tmp2"`
EmbededTest
Tmp int `onion:"tmp"`
Ignored string `onion:"-"`
} `onion:"inner"`
}
type layerLazyMock struct {
}
func (lm layerMock) Load() (map[string]interface{}, error) {
return lm.data, nil
}
func (lm layerLazyMock) Get(p ...string) (interface{}, bool) {
if len(p) == 0 {
return nil, false
}
return strings.Join(p, "-"), true
}
func getMap(prefix string, s ...interface{}) map[string]interface{} {
tmp := make(map[string]interface{})
for i := range s {
tmp[fmt.Sprintf("%s%d", prefix, i)] = s[i]
}
return tmp
}
func TestOnion(t *testing.T) {
Convey("Onion basic functionality", t, func() {
lm := &layerMock{}
lm.data = getMap("key", 42, "universe", "answer", true, float32(20.88), float64(200.123), int64(100))
lm.data["nested"] = getMap("n", "a", 99, true)
lm.data["du"] = time.Minute
t1 := make(map[interface{}]interface{})
t1["str1"] = 1
t1["int64"] = int64(64)
t1["oct"] = "0777"
t1["hex"] = "0xf"
t1["str2"] = "hi"
t1["other"] = struct{}{}
t1["what"] = getMap("n", "a")
t2 := make(map[interface{}]interface{})
for k, v := range t1 {
t2[k] = v
}
t1["nested"] = t2
t2[true] = false
lm.data["yes"] = t1
lm.data["slice1"] = []string{"a", "b", "c"}
lm.data["slice2"] = []interface{}{"a", "b", "c"}
lm.data["slice3"] = []interface{}{"a", "b", true}
lm.data["slice4"] = []int{1, 2, 3}
lm.data["ignored"] = "ignore me"
lm.data["strAsSlice"] = "one,two,three"
lm.data["dur"] = time.Minute
lm.data["durstring"] = "1h2m3s"
lm.data["durstringinvalid"] = "ertyuiop"
lm.data["durint"] = 100000000
lm.data["durint64"] = int64(100000000)
lm.data["booldur"] = true
o := New()
So(o.AddLayer(lm), ShouldBeNil)
Convey("Get direct variable", func() {
So(o.GetInt("key0"), ShouldEqual, 42)
So(o.GetString("key1"), ShouldEqual, "universe")
So(o.GetFloat64("key1"), ShouldEqual, 0)
So(o.GetString("key2"), ShouldEqual, "answer")
So(o.GetBool("key3"), ShouldBeTrue)
So(o.GetInt("key4"), ShouldEqual, 20)
So(o.GetFloat32("key4"), ShouldEqual, 20.88)
So(o.GetInt("key5"), ShouldEqual, 200)
So(o.GetFloat64("key5"), ShouldEqual, 200.123)
So(o.GetInt("key6"), ShouldEqual, 100)
So(o.GetInt64("key0"), ShouldEqual, 42)
So(o.GetFloat64("key0"), ShouldEqual, 42.0)
So(o.GetInt64("key4"), ShouldEqual, 20)
So(o.GetInt64("key5"), ShouldEqual, 200)
So(o.GetInt64("key6"), ShouldEqual, 100)
d, _ := time.ParseDuration("1h2m3s")
So(o.GetDuration("durstring"), ShouldEqual, d)
So(o.GetDuration("durstringinvalid"), ShouldEqual, 0)
So(o.GetDuration("not-set-value"), ShouldEqual, 0)
So(o.GetDuration("durint"), ShouldEqual, time.Duration(100000000))
So(o.GetDuration("durint64"), ShouldEqual, time.Duration(100000000))
So(o.GetDuration("booldur"), ShouldEqual, 0)
So(o.GetDuration("dur"), ShouldEqual, time.Minute)
})
Convey("Get default value", func() {
So(o.GetIntDefault("key1", 0), ShouldEqual, 0)
So(o.GetIntDefault("nokey1", 0), ShouldEqual, 0)
So(o.GetStringDefault("key0", ""), ShouldEqual, "")
So(o.GetStringDefault("nokey0", ""), ShouldEqual, "")
So(o.GetBoolDefault("key0", false), ShouldBeFalse)
So(o.GetBoolDefault("nokey0", false), ShouldBeFalse)
So(o.GetInt64Default("key1", 0), ShouldEqual, 0)
So(o.GetInt64Default("nokey1", 0), ShouldEqual, 0)
So(o.GetInt64Default("", 0), ShouldEqual, 0) // Empty key
So(o.GetInt64Default("key3", 10000), ShouldEqual, 10000)
So(o.GetFloat32Default("", 0), ShouldEqual, 0) // Empty key
So(o.GetFloat32Default("key3", 10000), ShouldEqual, 10000)
So(o.GetFloat64Default("", 0.123), ShouldEqual, 0.123) // Empty key
So(o.GetFloat64Default("key3", 10000.123), ShouldEqual, 10000.123)
})
Convey("Get nested variable", func() {
So(o.GetStringDefault("nested.n0", ""), ShouldEqual, "a")
So(o.GetInt64Default("nested.n1", 0), ShouldEqual, 99)
So(o.GetIntDefault("nested.n1", 0), ShouldEqual, 99)
So(o.GetBoolDefault("nested.n2", false), ShouldEqual, true)
So(o.GetIntDefault("yes.str1", 0), ShouldEqual, 1)
So(o.GetFloat32Default("yes.int64", 0), ShouldEqual, 64)
So(o.GetInt64Default("yes.oct", 0), ShouldEqual, 511)
So(o.GetInt64Default("yes.hex", 0), ShouldEqual, 15)
So(o.GetStringDefault("yes.str2", ""), ShouldEqual, "hi")
So(o.GetStringDefault("yes.nested.str2", ""), ShouldEqual, "hi")
So(o.GetStringDefault("yes.what.n0", ""), ShouldEqual, "a")
})
Convey("Get nested default variable", func() {
So(o.GetStringDefault("nested.n01", ""), ShouldEqual, "")
So(o.GetStringDefault("key0.n01", ""), ShouldEqual, "")
So(o.GetInt64Default("nested.n11", 0), ShouldEqual, 0)
So(o.GetIntDefault("nested.n11", 0), ShouldEqual, 0)
So(o.GetBoolDefault("nested.n21", false), ShouldEqual, false)
So(o.GetStringDefault("yes.nested.no", "def"), ShouldEqual, "def")
So(o.GetStringDefault("yes.nested.other.key", "def"), ShouldEqual, "def")
So(o.GetStringDefault("yes.what.no", "def"), ShouldEqual, "def")
})
Convey("change delimiter", func() {
So(o.GetDelimiter(), ShouldEqual, ".")
o.SetDelimiter("/")
So(o.GetDelimiter(), ShouldEqual, "/")
Convey("get with modified delimiter", func() {
So(o.GetStringDefault("nested/n0", ""), ShouldEqual, "a")
So(o.GetInt64Default("nested/n1", 0), ShouldEqual, 99)
So(o.GetIntDefault("nested/n1", 0), ShouldEqual, 99)
So(o.GetBoolDefault("nested/n2", false), ShouldEqual, true)
So(o.GetStringDefault("nested.n0", ""), ShouldEqual, "")
So(o.GetInt64Default("nested.n1", 0), ShouldEqual, 0)
So(o.GetIntDefault("nested.n1", 0), ShouldEqual, 0)
So(o.GetBoolDefault("nested.n2", false), ShouldEqual, false)
So(o.GetStringDefault("key0/n01", ""), ShouldEqual, "")
})
o.SetDelimiter("")
So(o.GetDelimiter(), ShouldEqual, ".")
})
Convey("delegate to structure", func() {
So(o.GetString("ignored"), ShouldEqual, "ignore me")
s := structExample{}
o.GetStruct("", &s)
ex := structExample{
Key0: 42,
Universe: "universe",
Key2: "answer",
Key3: true,
Du: time.Minute,
AnonNested: AnonNested{
Key4: 20,
Key5: 200,
Key5Again: 200,
},
Nested: Nested{
N0: "a",
N1: 99,
N2: true,
},
Another: Nested{
N0: "a",
N1: 99,
N2: true,
},
Ignored: "",
}
fmt.Printf("\n%+v\n", s)
fmt.Printf("%+v\n", ex)
So(reflect.DeepEqual(s, ex), ShouldBeTrue)
var tmp []string
o.GetStruct("", tmp)
So(tmp, ShouldBeNil)
})
Convey("slice test", func() {
So(reflect.DeepEqual(o.GetStringSlice("slice1"), []string{"a", "b", "c"}), ShouldBeTrue)
So(reflect.DeepEqual(o.GetStringSlice("slice2"), []string{"a", "b", "c"}), ShouldBeTrue)
So(o.GetStringSlice("slice3"), ShouldBeNil)
So(o.GetStringSlice("slice4"), ShouldBeNil)
So(o.GetStringSlice("notslice3"), ShouldBeNil)
So(o.GetStringSlice("yes.str1"), ShouldBeNil)
So(reflect.DeepEqual(o.GetStringSlice("yes.str2"), []string{"hi"}), ShouldBeTrue)
So(reflect.DeepEqual(o.GetStringSlice("strAsSlice"), []string{"one", "two", "three"}), ShouldBeTrue)
})
})
Convey("Test layer overwrite", t, func() {
lm1 := &layerMock{getMap("test", 1, true)}
lm2 := &layerMock{getMap("test", 2, false)}
os.Setenv("TEST0", "3")
os.Setenv("TEST1", "True")
os.Setenv("TEST2", "INVALIDBOOL")
lm3 := NewEnvLayer("TEST0", "TEST1", "TEST2")
o := New()
o.AddLayer(lm1)
So(o.GetInt64Default("test0", 0), ShouldEqual, 1)
So(o.GetBoolDefault("test1", false), ShouldBeTrue)
o.AddLayer(lm2)
So(o.GetInt64Default("test0", 0), ShouldEqual, 2)
So(o.GetBoolDefault("test1", true), ShouldBeFalse)
o.AddLayer(lm3) // Special case in ENV loader
So(o.GetInt64Default("test0", 0), ShouldEqual, 3)
So(o.GetFloat64Default("test0", 0), ShouldEqual, 3.0)
So(o.GetBoolDefault("test1", false), ShouldBeTrue)
So(o.GetBoolDefault("test2", false), ShouldBeFalse)
})
Convey("test direct creation", t, func() {
o := &Onion{}
So(o.GetIntDefault("empty", 1000), ShouldEqual, 1000)
lm := &layerMock{getMap("test", 1, true)}
o1 := &Onion{}
o1.AddLayer(lm)
So(o1.GetIntDefault("test0", 0), ShouldEqual, 1)
})
Convey("test lazy loader", t, func() {
o := New()
o.AddLazyLayer(layerLazyMock{})
So(o.GetString("a.b.c.d"), ShouldEqual, "a-b-c-d")
})
Convey("test bug with inner struct", t, func() {
o := New()
def := NewDefaultLayer()
def.SetDefault("inner.operator", "op")
def.SetDefault("inner.aggregator", "agg")
def.SetDefault("inner.tmp", 99)
def.SetDefault("inner.tmp2", 101)
o.AddLayer(def)
cfg := &Worker{}
o.GetStruct("", cfg)
So(cfg.Inner.Operator, ShouldEqual, "op")
So(cfg.Inner.Aggregator, ShouldEqual, "agg")
So(cfg.Inner.Tmp2, ShouldEqual, 101)
So(cfg.Inner.Tmp, ShouldEqual, 99)
So(cfg.Inner.Ignored, ShouldEqual, "")
})
Convey("test for register variables", t, func() {
o := New()
intVar := o.RegisterInt("test.int", 10)
int64Var := o.RegisterInt64("test.int64", 10)
float64Var := o.RegisterFloat64("test.float64", 10)
float32Var := o.RegisterFloat32("test.float32", 10)
stringVar := o.RegisterString("test.string", "TEST")
boolVar := o.RegisterBool("test.bool", false)
durationVar := o.RegisterDuration("test.duration", time.Millisecond)
def := NewDefaultLayer()
def.SetDefault("test.int", 100)
def.SetDefault("test.int64", 100)
def.SetDefault("test.string", "TEST_SET")
def.SetDefault("test.float64", 100.11)
def.SetDefault("test.float32", 100.11)
def.SetDefault("test.bool", true)
def.SetDefault("test.duration", "1s")
So(o.AddLayer(def), ShouldBeNil)
o.Load()
So(intVar.Int(), ShouldEqual, 100)
So(int64Var.Int64(), ShouldEqual, 100)
So(float64Var.Float64(), ShouldEqual, 100.11)
So(float32Var.Float32(), ShouldEqual, 100.11)
So(stringVar.String(), ShouldEqual, "TEST_SET")
So(boolVar.Bool(), ShouldBeTrue)
So(durationVar.Duration(), ShouldEqual, time.Second)
o.Reset()
o.Load()
So(o.GetInt("test.int"), ShouldBeZeroValue)
})
}
func BenchmarkOion(b *testing.B) {
for i := 0; i < b.N; i++ {
benconion.GetInt("key0")
benconion.GetString("key1")
benconion.GetString("key2")
benconion.GetBool("key3")
benconion.GetInt("key4")
benconion.GetInt("key5")
benconion.GetInt("key6")
benconion.GetInt64("key0")
benconion.GetInt64("key4")
benconion.GetInt64("key5")
benconion.GetInt64("key6")
benconion.GetDuration("durstring")
benconion.GetDuration("durstringinvalid")
benconion.GetDuration("not-set-value")
benconion.GetDuration("durint")
benconion.GetDuration("durint64")
benconion.GetDuration("booldur")
benconion.GetDuration("dur")
}
}
func BenchmarkStruct(b *testing.B) {
for i := 0; i < b.N; i++ {
s := structExample{}
benconion.GetStruct("", &s)
}
}
var benconion = New()
func init() {
lm := &layerMock{}
lm.data = getMap("key", 42, "universe", "answer", true, float32(20.88), float64(200), int64(100))
lm.data["nested"] = getMap("n", "a", 99, true)
t1 := make(map[interface{}]interface{})
t1["str1"] = 1
t1["str2"] = "hi"
t1["other"] = struct{}{}
t1["what"] = getMap("n", "a")
t2 := make(map[interface{}]interface{})
for k, v := range t1 {
t2[k] = v
}
t1["nested"] = t2
t2[true] = false
lm.data["yes"] = t1
lm.data["slice1"] = []string{"a", "b", "c"}
lm.data["slice2"] = []interface{}{"a", "b", "c"}
lm.data["slice3"] = []interface{}{"a", "b", true}
lm.data["slice4"] = []int{1, 2, 3}
lm.data["ignored"] = "ignore me"
lm.data["dur"] = time.Minute
lm.data["durstring"] = "1h2m3s"
lm.data["durstringinvalid"] = "ertyuiop"
lm.data["durint"] = 100000000
lm.data["durint64"] = int64(100000000)
lm.data["booldur"] = true
benconion.AddLayer(lm)
}