-
Notifications
You must be signed in to change notification settings - Fork 55
/
export_test.go
354 lines (291 loc) · 6.34 KB
/
export_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
package config
import (
"bytes"
"errors"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/jsonutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestExport(t *testing.T) {
is := assert.New(t)
c := New("test")
str := c.ToJSON()
is.Eq("", str)
err := c.LoadStrings(JSON, jsonStr)
is.Nil(err)
str = c.ToJSON()
is.Contains(str, `"name":"app"`)
buf := &bytes.Buffer{}
_, err = c.WriteTo(buf)
is.Nil(err)
// test dump
buf = &bytes.Buffer{}
_, err = c.DumpTo(buf, "invalid")
is.Err(err)
_, err = c.DumpTo(buf, Yml)
is.Err(err)
_, err = c.DumpTo(buf, JSON)
is.Nil(err)
}
func TestDumpTo_encode_error(t *testing.T) {
is := assert.New(t)
c := NewEmpty("test")
is.NoErr(c.Set("age", 34))
drv := NewDriver(JSON, JSONDecoder, func(v any) (out []byte, err error) {
return nil, errors.New("encode data error")
})
c.WithDriver(drv)
// encode error
buf := &bytes.Buffer{}
_, err := c.DumpTo(buf, JSON)
is.ErrMsg(err, "encode data error")
is.Empty(c.ToJSON())
}
func TestConfig_Structure(t *testing.T) {
is := assert.New(t)
cfg := Default()
cfg.ClearAll()
err := cfg.LoadStrings(JSON, `{
"age": 28,
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)
is.Nil(err)
type User struct {
Age int // always float64 from JSON
Name string
Sports []string
}
user := &User{}
// map all data
err = MapStruct("", user)
is.Nil(err)
is.Eq(28, user.Age)
is.Eq("inhere", user.Name)
is.Eq("pingPong", user.Sports[0])
// map all data
u1 := &User{}
err = Decode(u1)
is.Nil(err)
is.Eq(28, u1.Age)
is.Eq("inhere", u1.Name)
is.Eq("pingPong", u1.Sports[0])
// - auto convert string to int
// age use string in JSON
cfg1 := New("test")
err = cfg1.LoadStrings(JSON, `{
"age": "26",
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)
is.Nil(err)
user1 := &User{}
err = cfg1.MapStruct("", user1)
is.Nil(err)
dump.P(*user1)
// map some data
err = cfg.LoadStrings(JSON, `{
"sec": {
"key": "val",
"age": 120,
"tags": [12, 34]
}
}`)
is.Nil(err)
some := struct {
Age int
Key string
Tags []int
}{}
err = BindStruct("sec", &some)
is.Nil(err)
is.Eq(120, some.Age)
is.Eq(12, some.Tags[0])
cfg.ClearAll()
// custom data
cfg = New("test")
err = cfg.LoadData(map[string]any{
"key": "val",
"age": 120,
"tags": []int{12, 34},
})
is.NoErr(err)
s1 := struct {
Age int
Kye string
Tags []int
}{}
err = cfg.BindStruct("", &s1)
is.Nil(err)
is.Eq(120, s1.Age)
is.Eq(12, s1.Tags[0])
// key not exist
err = cfg.BindStruct("not-exist", &s1)
is.Err(err)
is.Eq("this key does not exist in the config", err.Error())
// invalid dst
err = cfg.BindStruct("sec", "invalid")
is.Err(err)
cfg.ClearAll()
}
func TestMapStruct_embedded_struct_squash_false(t *testing.T) {
loader := NewWithOptions("test", func(options *Options) {
options.DecoderConfig.TagName = "json"
options.DecoderConfig.Squash = false
})
assert.False(t, loader.Options().DecoderConfig.Squash)
err := loader.LoadStrings(JSON, `{
"c": "12",
"test1": {
"b": "34"
}
}`)
assert.NoErr(t, err)
dump.Println(loader.Data())
assert.Eq(t, 12, loader.Int("c"))
assert.Eq(t, 34, loader.Int("test1.b"))
type Test1 struct {
B int `json:"b"`
}
type Test2 struct {
Test1
C int `json:"c"`
}
cfg := &Test2{}
err = loader.MapStruct("", cfg)
assert.NoErr(t, err)
dump.Println(cfg)
assert.Eq(t, 34, cfg.Test1.B)
type Test3 struct {
*Test1
C int `json:"c"`
}
cfg1 := &Test3{}
err = loader.MapStruct("", cfg1)
assert.NoErr(t, err)
dump.Println(cfg1)
assert.Eq(t, 34, cfg1.Test1.B)
loader.SetData(map[string]any{
"c": 120,
"b": 340,
})
dump.Println(loader.Data())
cfg2 := &Test3{}
err = loader.BindStruct("", cfg2)
cfg3 := &Test3{}
_ = jsonutil.DecodeString(`{"c": 12, "b": 34}`, cfg3)
dump.Println(cfg2, cfg3)
}
func TestMapStruct_embedded_struct_squash_true(t *testing.T) {
loader := NewWithOptions("test", func(options *Options) {
options.DecoderConfig.TagName = "json"
options.DecoderConfig.Squash = true
})
assert.True(t, loader.Options().DecoderConfig.Squash)
err := loader.LoadStrings(JSON, `{
"c": "12",
"test1": {
"b": "34"
}
}`)
assert.NoErr(t, err)
dump.Println(loader.Data())
assert.Eq(t, 12, loader.Int("c"))
assert.Eq(t, 34, loader.Int("test1.b"))
type Test1 struct {
B int `json:"b"`
}
// use value - will not set ok
type Test2 struct {
Test1
// Test1 `json:",squash"`
C int `json:"c"`
}
cfg := &Test2{}
err = loader.MapStruct("", cfg)
assert.NoErr(t, err)
dump.Println(cfg)
assert.Eq(t, 0, cfg.Test1.B)
// use pointer
type Test3 struct {
*Test1
C int `json:"c"`
}
cfg1 := &Test3{}
err = loader.MapStruct("", cfg1)
assert.NoErr(t, err)
dump.Println(cfg1)
assert.Eq(t, 34, cfg1.B)
assert.Eq(t, 34, cfg1.Test1.B)
loader.SetData(map[string]any{
"c": 120,
"b": 340,
})
dump.Println(loader.Data())
cfg2 := &Test3{}
err = loader.BindStruct("", cfg2)
cfg3 := &Test3{}
_ = jsonutil.DecodeString(`{"c": 12, "b": 34}`, cfg3)
dump.Println(cfg2, cfg3)
}
func TestMapOnExists(t *testing.T) {
cfg := Default()
cfg.ClearAll()
err := cfg.LoadStrings(JSON, `{
"age": 28,
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)
assert.NoErr(t, err)
assert.NoErr(t, MapOnExists("not-exists", nil))
user := &struct {
Age int
Name string
Sports []string
}{}
assert.NoErr(t, MapOnExists("", user))
assert.Eq(t, 28, user.Age)
assert.Eq(t, "inhere", user.Name)
}
func TestConfig_BindStruct_set_DecoderConfig(t *testing.T) {
cfg := NewWith("test", func(c *Config) {
c.opts.DecoderConfig = nil
})
err := cfg.LoadStrings(JSON, `{
"age": 28,
"name": "inhere",
"sports": ["pingPong", "跑步"]
}`)
assert.NoErr(t, err)
user := &struct {
Age int
Name string
Sports []string
}{}
assert.NoErr(t, cfg.BindStruct("", user))
assert.Eq(t, 28, user.Age)
assert.Eq(t, "inhere", user.Name)
// not use ptr
assert.Err(t, cfg.BindStruct("", *user))
}
func TestConfig_BindStruct_error(t *testing.T) {
// cfg := NewEmpty()
}
func TestConfig_BindStruct_default(t *testing.T) {
type MyConf struct {
Env string `default:"${APP_ENV | dev}"`
Debug bool `default:"${APP_DEBUG | false}"`
}
cfg := NewWithOptions("test", ParseEnv, ParseDefault)
// cfg.SetData(map[string]any{
// "env": "prod",
// "debug": "true",
// })
mc := &MyConf{}
err := cfg.Decode(mc)
dump.P(mc)
assert.NoErr(t, err)
assert.Eq(t, "dev", mc.Env)
assert.False(t, mc.Debug)
}