-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
603 lines (518 loc) · 14.7 KB
/
encode_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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package env
import (
"errors"
"fmt"
"net/url"
"os"
"strings"
"testing"
)
// The configEncode structure with custom MarshalEnv method.
type configEncode struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
AllowedHosts []string `env:"ALLOWED_HOSTS" sep:":"`
}
// MarshalEnv the custom method for marshalling.
func (c *configEncode) MarshalEnv() ([]string, error) {
tests := [][]string{
{"HOST", "192.168.0.1"},
{"PORT", "80"},
{"ALLOWED_HOSTS", "localhost"},
}
for _, item := range tests {
err := Set(item[0], item[1])
if err != nil {
return []string{}, err
}
}
return []string{
"HOST=192.168.0.1",
"PORT=80",
"ALLOWED_HOSTS=localhost",
}, nil
}
// The configEncodeErr structure with custom MarshalEnv method with error.
type configEncodeErr struct{}
// MarshalEnv the custom method for marshalling - always returns an error.
func (c *configEncodeErr) MarshalEnv() ([]string, error) {
return []string{}, errors.New("error message")
}
// TestUnmarshalEnvCustomMarshalErr tests custom marshalEnv with error.
func TestUnmarshalEnvCustomMarshalErr(t *testing.T) {
data := configEncodeErr{}
if _, err := marshalEnv("", data, false); err == nil {
t.Error("an error is expected for nil object")
}
}
// TestMarshalEnvDefaultKeyName tests marshalEnv with default key name.
func TestMarshalEnvDefaultKeyName(t *testing.T) {
var (
expected = "127.0.0.1"
data = struct {
Host string
}{
Host: expected,
}
)
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
if v := os.Getenv("Host"); v != expected {
t.Errorf("expected `%s` but `%v`", v, expected)
}
}
// TestMarshalEnvInvalidKey tests marshalEnv with invalid key name.
func TestMarshalEnvInvalidKey(t *testing.T) {
data := struct {
Host string `env:"HO$T"`
}{}
if _, err := marshalEnv("", data, false); err == nil {
t.Error("there must be an error for the invalid key name")
}
}
// TestMarshalEnvNilPointer tests marshalEnv function
// for uninitialized pointer.
func TestMarshalEnvNilPointer(t *testing.T) {
var data *struct{}
if _, err := marshalEnv("", data, false); err == nil {
t.Error("should be error for an uninitialized object")
}
}
// TestMarshalEnvNotStruct tests marshalEnv function for not struct.
func TestMarshalNotStruct(t *testing.T) {
var data string
if _, err := marshalEnv(data, "", false); err == nil {
t.Error("should be error for an object other than structure")
}
}
// TestMarshalEnv tests marshalEnv function with struct value.
func TestMarshalEnv(t *testing.T) {
data := struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
AllowedHosts []string `env:"ALLOWED_HOSTS" sep:"!"`
AllowedUsers [2]string `env:"ALLOWED_USERS" sep:":"`
}{
"192.168.0.5",
8080,
[]string{"localhost", "127.0.0.1"},
[2]string{"John", "Bob"},
}
// ...
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
// Test marshalling.
expected := data.Host
if v := Get("HOST"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = fmt.Sprintf("%d", data.Port)
if v := Get("PORT"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = strings.Join(data.AllowedHosts, "!")
if v := Get("ALLOWED_HOSTS"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = strings.Join(data.AllowedUsers[:], ":")
if v := Get("ALLOWED_USERS"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvPtr tests marshalEnv function for pointer of the struct value.
func TestMarshalEnvPtr(t *testing.T) {
data := &struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
AllowedHosts []string `env:"ALLOWED_HOSTS" sep:"!"`
AllowedUsers [2]string `env:"ALLOWED_USERS" sep:":"`
}{
"192.168.0.5",
8080,
[]string{"localhost", "127.0.0.1"},
[2]string{"John", "Bob"},
}
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
// Test marshalling.
expected := data.Host
if v := Get("HOST"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = fmt.Sprintf("%d", data.Port)
if v := Get("PORT"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = strings.Join(data.AllowedHosts, "!")
if v := Get("ALLOWED_HOSTS"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = strings.Join(data.AllowedUsers[:], ":")
if v := Get("ALLOWED_USERS"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvCustom tests marshalEnv function for object
// with custom MarshalEnv method.
func TestMarshalEnvCustom(t *testing.T) {
data := configEncode{
"localhost", // default: 192.168.0.1
8080, // default: 80
[]string{"localhost", "127.0.0.1"}, // default: 192.168.0.1
}
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
// Test marshalling.
expected := "192.168.0.1"
if v := Get("HOST"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = "80"
if v := Get("PORT"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = "localhost"
if v := Get("ALLOWED_HOSTS"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvCustomPtr tests marshalEnv function for pointer
// with custom MarshalEnv method.
func TestMarshalEnvCustomPtr(t *testing.T) {
scope := &configEncode{
"localhost", // default: 192.168.0.1
8080, // default: 80
[]string{"localhost", "127.0.0.1"}, // default: 192.168.0.1
}
os.Clearenv()
if _, err := marshalEnv("", scope, false); err != nil {
t.Error(err)
}
// Test marshalling.
expected := "192.168.0.1"
if v := Get("HOST"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = "80"
if v := Get("PORT"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = "localhost"
if v := Get("ALLOWED_HOSTS"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvURL tests marshaling of the URL.
func TestMarshalEnvURL(t *testing.T) {
type URLTestType struct {
KeyURLPlain url.URL `env:"KEY_URL_PLAIN"`
KeyURLPoint *url.URL `env:"KEY_URL_POINT"`
KeyURLPlainSlice []url.URL `env:"KEY_URL_PLAIN_SLICE" sep:"!"`
KeyURLPointSlice []*url.URL `env:"KEY_URL_POINT_SLICE" sep:"!"`
KeyURLPlainArray [2]url.URL `env:"KEY_URL_PLAIN_ARRAY" sep:"!"`
KeyURLPointArray [2]*url.URL `env:"KEY_URL_POINT_ARRAY" sep:"!"`
}
// var test string
data := URLTestType{
KeyURLPlain: url.URL{Scheme: "http", Host: "plain.goloop.one"},
KeyURLPoint: &url.URL{Scheme: "http", Host: "point.goloop.one"},
KeyURLPlainSlice: []url.URL{
{Scheme: "http", Host: "a.plain.goloop.one"},
{Scheme: "http", Host: "b.plain.goloop.one"},
},
KeyURLPointSlice: []*url.URL{
{Scheme: "http", Host: "a.point.goloop.one"},
{Scheme: "http", Host: "b.point.goloop.one"},
},
KeyURLPlainArray: [2]url.URL{
{Scheme: "http", Host: "c.plain.goloop.one"},
{Scheme: "http", Host: "d.plain.goloop.one"},
},
KeyURLPointArray: [2]*url.URL{
{Scheme: "http", Host: "c.point.goloop.one"},
{Scheme: "http", Host: "d.point.goloop.one"},
},
}
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
// Tests results.
expected := "http://plain.goloop.one"
if v := Get("KEY_URL_PLAIN"); v != expected {
t.Errorf("expected %s but %s", expected, v)
// t.Errorf("Incorrect marshaling plain url.URL: %s", v)
}
expected = "http://point.goloop.one"
if v := Get("KEY_URL_POINT"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
// Plain slice.
expected = "http://a.plain.goloop.one!http://b.plain.goloop.one"
if v := Get("KEY_URL_PLAIN_SLICE"); v != expected {
t.Errorf("expected %s but %s", expected, v)
// t.Errorf("Incorrect marshaling poin slice []url.URL: %s", v)
}
// Point slice.
expected = "http://a.point.goloop.one!http://b.point.goloop.one"
if v := Get("KEY_URL_POINT_SLICE"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
// Plain array.
expected = "http://c.plain.goloop.one!http://d.plain.goloop.one"
if v := Get("KEY_URL_PLAIN_ARRAY"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
// Point array.
expected = "http://c.point.goloop.one!http://d.point.goloop.one"
if v := Get("KEY_URL_POINT_ARRAY"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvStruct tests marshaling of the struct.
func TestMarshalEnvStruct(t *testing.T) {
type address struct {
Country string `env:"COUNTRY"`
}
type user struct {
Name string `env:"NAME"`
Address address `env:"ADDRESS"`
}
type client struct {
User user `env:"USER"`
HomePage url.URL `env:"HOME_PAGE"`
}
data := client{
User: user{
Name: "John",
Address: address{
Country: "USA",
},
},
HomePage: url.URL{Scheme: "http", Host: "goloop.one"},
}
// Marshaling.
os.Clearenv()
_, err := marshalEnv("", data, false)
if err != nil {
t.Error(err)
}
// Tests.
expected := data.User.Name
if v := os.Getenv("USER_NAME"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = data.User.Address.Country
if v := os.Getenv("USER_ADDRESS_COUNTRY"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = data.HomePage.String()
if v := os.Getenv("HOME_PAGE"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvStructPtr tests marshaling of the pointer on the struct.
func TestMarshalEnvStructPtr(t *testing.T) {
type address struct {
Country string `env:"COUNTRY"`
}
type user struct {
Name string `env:"NAME"`
Address *address `env:"ADDRESS"`
}
type client struct {
User *user `env:"USER"`
HomePage *url.URL `env:"HOME_PAGE"`
}
data := client{
User: &user{
Name: "John",
Address: &address{
Country: "USA",
},
},
HomePage: &url.URL{Scheme: "http", Host: "goloop.one"},
}
// Marshaling.
os.Clearenv()
_, err := marshalEnv("", data, false)
if err != nil {
t.Error(err)
}
// Tests.
expected := data.User.Name
if v := os.Getenv("USER_NAME"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = data.User.Address.Country
if v := os.Getenv("USER_ADDRESS_COUNTRY"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
expected = data.HomePage.String()
if v := os.Getenv("HOME_PAGE"); v != expected {
t.Errorf("expected %s but %s", expected, v)
}
}
// TestMarshalEnvNumberPtr tests marshalEnv for pointer
// of Int, Uint and Float types.
func TestMarshalEnvNumberPtr(t *testing.T) {
var (
keyInt = int(7)
keyInt8 = int8(7)
keyInt16 = int16(7)
keyInt32 = int32(7)
keyInt64 = int64(7)
keyUint = uint(7)
keyUint8 = uint8(7)
keyUint16 = uint16(7)
keyUint32 = uint32(7)
keyUint64 = uint64(7)
keyFloat32 = float32(7.0)
keyFloat64 = float64(7.0)
data = struct {
KeyInt *int `env:"KEY_INT"`
KeyInt8 *int8 `env:"KEY_INT8"`
KeyInt16 *int16 `env:"KEY_INT16"`
KeyInt32 *int32 `env:"KEY_INT32"`
KeyInt64 *int64 `env:"KEY_INT64"`
KeyUint *uint `env:"KEY_UINT"`
KeyUint8 *uint8 `env:"KEY_UINT8"`
KeyUint16 *uint16 `env:"KEY_UINT16"`
KeyUint32 *uint32 `env:"KEY_UINT32"`
KeyUint64 *uint64 `env:"KEY_UINT64"`
KeyFloat32 *float32 `env:"KEY_FLOAT32"`
KeyFloat64 *float64 `env:"KEY_FLOAT64"`
}{
KeyInt: &keyInt,
KeyInt8: &keyInt8,
KeyInt16: &keyInt16,
KeyInt32: &keyInt32,
KeyInt64: &keyInt64,
KeyUint: &keyUint,
KeyUint8: &keyUint8,
KeyUint16: &keyUint16,
KeyUint32: &keyUint32,
KeyUint64: &keyUint64,
KeyFloat32: &keyFloat32,
KeyFloat64: &keyFloat64,
}
)
// ...
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
// Tests.
keys := []string{
"KEY_INT", "KEY_INT8", "KEY_INT16", "KEY_INT32",
"KEY_INT64", "KEY_UINT", "KEY_UINT8", "KEY_UINT16",
"KEY_UINT32", "KEY_UINT64", "KEY_FLOAT32", "KEY_FLOAT64",
}
for _, key := range keys {
tmp := strings.Split(os.Getenv(key), ".") // recline fractional part
if tmp[0] != "7" {
t.Errorf("incorrect value set for %s: %s", key, tmp[0])
}
}
}
// TestMarshalEnvBoolPtr tests marshalEnv for pointer of bool.
func TestMarshalEnvBoolPtr(t *testing.T) {
var (
keyBool = true
data = struct {
KeyBool *bool `env:"KEY_BOOL"`
}{
KeyBool: &keyBool,
}
)
// ...
os.Clearenv()
if _, err := marshalEnv("", data, false); err != nil {
t.Error(err)
}
// Tests.
if v := Get("KEY_BOOL"); v != "true" {
t.Errorf("incorrect value set for KEY_BOOL: %s", v)
}
}
// TestMarshalEnvStringPtr tests marshalEnv for pointer of bool.
func TestMarshalEnvStringPtr(t *testing.T) {
var (
keyString = "Hello World"
value = struct {
KeyString *string `env:"KEY_STRING"`
}{
KeyString: &keyString,
}
)
// ...
os.Clearenv()
if _, err := marshalEnv("", value, false); err != nil {
t.Error(err)
}
// Tests.
if v := os.Getenv("KEY_STRING"); v != "Hello World" {
t.Errorf("incorrect value set for KEY_STRING: %s", v)
}
}
// TestMarshalEnvSlice tests marshalEnv function for slice.
func TestMarshalEnvSlice(t *testing.T) {
type chunk struct {
KeyInt []*int `env:"KEY_INT" sep:":"`
KeyInt8 []*int8 `env:"KEY_INT8" sep:":"`
KeyInt16 []*int16 `env:"KEY_INT16" sep:":"`
KeyInt32 []*int32 `env:"KEY_INT32" sep:":"`
KeyInt64 []*int64 `env:"KEY_INT64" sep:":"`
KeyUint []*uint `env:"KEY_UINT" sep:":"`
KeyUint8 []*uint8 `env:"KEY_UINT8" sep:":"`
KeyUint16 []*uint16 `env:"KEY_UINT16" sep:":"`
KeyUint32 []*uint32 `env:"KEY_UINT32" sep:":"`
KeyUint64 []*uint64 `env:"KEY_UINT64" sep:":"`
KeyFloat32 []*float32 `env:"KEY_FLOAT32" sep:":"`
KeyFloat64 []*float64 `env:"KEY_FLOAT64" sep:":"`
KeyString []*string `env:"KEY_STRING" sep:":"`
KeyBool []*bool `env:"KEY_BOOL" sep:":"`
}
var (
a, b, c int = 1, 2, 3
keyInt = []*int{&a, &b, &c}
s = chunk{KeyInt: keyInt}
)
if _, err := marshalEnv("", s, false); err != nil {
t.Error(err)
}
}
// TestUnmarshalMultiService tests unmarshaling of the
// data of environment by the specified prefix.
func TestUnmarshalMultiService(t *testing.T) {
type server struct {
Name string `env:"NAME"`
Host string `env:"HOST"`
Port int `env:"PORT"`
}
var (
serverA = server{}
serverB = server{}
)
os.Clearenv()
err := readParseStore("./fixtures/multiservice.env", true, true, true)
if err != nil {
t.Error(err)
}
Unmarshal("SERVICE_A_", &serverA)
Unmarshal("SERVICE_B_", &serverB)
if v := serverA.Name; v != "A" {
t.Errorf("expected `A` but `%s`", v)
}
if v := serverB.Name; v != "B" {
t.Errorf("expected `B` but `%s`", v)
}
}