-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmarshal_test.go
248 lines (202 loc) · 7.62 KB
/
unmarshal_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
package fig
import (
"testing"
)
type optionalTestStruct struct {
OptionalString string `fig:"optional_string"`
OptionalInt int `fig:"optional_int"`
OptionalInt64 int64 `fig:"optional_int64"`
OptionalBool bool `fig:"optional_bool"`
OptionalFloat float64 `fig:"optional_float"`
OptionalStringPtr *string `fig:"optional_string"`
OptionalIntPtr *int `fig:"optional_int"`
OptionalInt64Ptr *int64 `fig:"optional_int64"`
OptionalBoolPtr *bool `fig:"optional_bool"`
OptionalFloatPtr *float64 `fig:"optional_float"`
}
type requiredTestStruct struct {
RequiredString string `fig:"string" required:"true"`
RequiredInt int `fig:"int" required:"true"`
RequiredInt64 int64 `fig:"int64" required:"true"`
RequiredBool bool `fig:"bool" required:"true"`
RequiredFloat float64 `fig:"float" required:"true"`
}
type mixedTestStruct struct {
OptionalString string `fig:"optional_string"`
OptionalInt int `fig:"optional_int"`
OptionalInt64 int64 `fig:"optional_int64"`
OptionalBool bool `fig:"optional_bool"`
OptionalFloat float64 `fig:"optional_float"`
OptionalStringPtr *string `fig:"optional_string"`
OptionalIntPtr *int `fig:"optional_int"`
OptionalInt64Ptr *int64 `fig:"optional_int64"`
OptionalBoolPtr *bool `fig:"optional_bool"`
OptionalFloatPtr *float64 `fig:"optional_float"`
RequiredString string `fig:"string" required:"true"`
RequiredInt int `fig:"int" required:"true"`
RequiredInt64 int64 `fig:"int64" required:"true"`
RequiredBool bool `fig:"bool" required:"true"`
RequiredFloat float64 `fig:"float" required:"true"`
RequiredStringPtr *string `fig:"string" required:"true"`
RequiredIntPtr *int `fig:"int" required:"true"`
RequiredInt64Ptr *int64 `fig:"int64" required:"true"`
RequiredBoolPtr *bool `fig:"bool" required:"true"`
RequiredFloatPtr *float64 `fig:"float" required:"true"`
}
type testStructWithDefaults struct {
OptionalString string `fig:"optional_string" default:"foo"`
RequiredString string `fig:"required_string" required:"true" default:"bar"`
}
func TestUnmarshal(t *testing.T) {
t.Run("defined variables", func(t *testing.T) {
driver := testDriver{
vals: map[string]string{
"string": "string",
"int": "10",
"int64": "10000",
"bool": "true",
"float": "4592.1",
"optional_string": "opt_str",
"optional_int": "11",
"optional_int64": "2002",
"optional_bool": "true",
"optional_float": "456.7",
},
}
conf := New(driver)
var ts mixedTestStruct
err := conf.Unmarshal(&ts)
if err != nil {
t.Errorf("unexpected error from Unmarshal: %s", err)
}
t.Run("required are populated", func(t *testing.T) {
if ts.RequiredString != "string" {
t.Errorf("required string has incorrect value %s", ts.RequiredString)
}
if *ts.RequiredStringPtr != "string" {
t.Errorf("required *string has incorrect value %s", *ts.RequiredStringPtr)
}
if ts.RequiredInt != 10 {
t.Errorf("required int has incorrect value %d", ts.RequiredInt)
}
if *ts.RequiredIntPtr != 10 {
t.Errorf("required *int has incorrect value %d", *&ts.RequiredInt)
}
if ts.RequiredInt64 != 10000 {
t.Errorf("required int has incorrect value %d", ts.RequiredInt64)
}
if *ts.RequiredInt64Ptr != 10000 {
t.Errorf("required *int has incorrect value %d", *ts.RequiredInt64Ptr)
}
if ts.RequiredBool != true {
t.Errorf("required bool has incorrect value %t", ts.RequiredBool)
}
if *ts.RequiredBoolPtr != true {
t.Errorf("required *bool has incorrect value %t", *ts.RequiredBoolPtr)
}
if ts.RequiredFloat != 4592.1 {
t.Errorf("required float has incorrect value %f", ts.RequiredFloat)
}
if *ts.RequiredFloatPtr != 4592.1 {
t.Errorf("required *float has incorrect value %f", *ts.RequiredFloatPtr)
}
})
t.Run("optional are populated", func(t *testing.T) {
if ts.OptionalString != "opt_str" {
t.Errorf("optional string has incorrect value %s", ts.OptionalString)
}
if *ts.OptionalStringPtr != "opt_str" {
t.Errorf("optional *string has incorrect value %s", *ts.OptionalStringPtr)
}
if ts.OptionalInt != 11 {
t.Errorf("optional int has incorrect value %d", ts.OptionalInt)
}
if *ts.OptionalIntPtr != 11 {
t.Errorf("optional *int has incorrect value %d", *ts.OptionalIntPtr)
}
if ts.OptionalInt64 != 2002 {
t.Errorf("optional int64 has incorrect value %d", ts.OptionalInt64)
}
if *ts.OptionalInt64Ptr != 2002 {
t.Errorf("optional *int64 has incorrect value %d", *ts.OptionalInt64Ptr)
}
if ts.OptionalBool != true {
t.Errorf("optional bool has incorrect value %t", ts.OptionalBool)
}
if *ts.OptionalBoolPtr != true {
t.Errorf("optional *bool has incorrect value %t", *ts.OptionalBoolPtr)
}
if ts.OptionalFloat != 456.7 {
t.Errorf("optional float has incorrect value %f", ts.OptionalFloat)
}
if *ts.OptionalFloatPtr != 456.7 {
t.Errorf("optional *float has incorrect value %f", *ts.OptionalFloatPtr)
}
})
})
t.Run("undefined variables", func(t *testing.T) {
t.Run("optionals default to zero value", func(t *testing.T) {
var ts optionalTestStruct
driver := testDriver{vals: map[string]string{}}
conf := New(driver)
err := conf.Unmarshal(&ts)
if err != nil {
t.Errorf("config with all optional fields errored on unmarshal: %s", err)
}
if ts.OptionalString != "" {
t.Errorf("unmarshal on optional field resulted in non-zero string value %v", ts.OptionalString)
}
if ts.OptionalStringPtr != nil {
t.Errorf("unmarshal on optional field resulted in non-zero *string value %v", *ts.OptionalStringPtr)
}
if ts.OptionalInt != 0 {
t.Errorf("unmarshal on optional field resulted in non-zero int value %v", ts.OptionalInt)
}
if ts.OptionalIntPtr != nil {
t.Errorf("unmarshal on optional field resulted in non-zero *int value %v", *ts.OptionalIntPtr)
}
if ts.OptionalInt64 != 0 {
t.Errorf("unmarshal on optional field resulted in non-zero int64 value %v", ts.OptionalInt64)
}
if ts.OptionalInt64Ptr != nil {
t.Errorf("unmarshal on optional field resulted in non-zero *int64 value %v", *ts.OptionalInt64Ptr)
}
if ts.OptionalBool != false {
t.Errorf("unmarshal on optional field resulted in non-zero bool value %v", ts.OptionalBool)
}
if ts.OptionalBoolPtr != nil {
t.Errorf("unmarshal on optional field resulted in non-zero bool value %v", *ts.OptionalBoolPtr)
}
if ts.OptionalFloat != 0 {
t.Errorf("unmarshal on optional field resulted in non-zero bool value %v", ts.OptionalFloat)
}
if ts.OptionalFloatPtr != nil {
t.Errorf("unmarshal on optional field resulted in non-zero bool value %v", *ts.OptionalFloatPtr)
}
})
t.Run("required values error when missing", func(t *testing.T) {
var ts requiredTestStruct
driver := testDriver{vals: map[string]string{}}
conf := New(driver)
err := conf.Unmarshal(&ts)
if err == nil {
t.Errorf("missing required fields should error on unmarshal")
}
})
})
t.Run("default values are populated", func(t *testing.T) {
ts := testStructWithDefaults{}
driver := testDriver{vals: map[string]string{}}
conf := New(driver)
err := conf.Unmarshal(&ts)
if err != nil {
t.Errorf("unexpected error from unmarshal with defaults: %s", err)
}
if ts.OptionalString != "foo" {
t.Errorf("expected default value 'foo' for optional string, got %s", ts.OptionalString)
}
if ts.RequiredString != "bar" {
t.Errorf("expected default value 'bar' for required string, got %s", ts.RequiredString)
}
})
}