-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample_test.go
358 lines (310 loc) · 6.95 KB
/
example_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
package structil
import (
"fmt"
)
func ExampleGetter() {
type Company struct {
Name string
Address string
Period int
}
type Person struct {
Name string
Age int
*Company
}
i := &Person{
Name: "Tony",
Age: 25,
Company: &Company{
Name: "Tiger inc.",
Address: "Tokyo",
Period: 3,
},
}
// i must be a struct or struct pointer
getter, err := NewGetter(i)
if err != nil {
panic(err)
}
name, _ := getter.String("Name") // get as string
age, _ := getter.Int("Age") // get as int
company, _ := getter.Get("Company") // get as interface{}
fmt.Printf(
"num of fields=%d\nfield names=%v\n'Name'=%s\n'Age'=%d\n'Company'=%+v",
getter.NumField(), // get num of fields
getter.Names(), // get field names
name,
age,
company,
)
// Output:
// num of fields=3
// field names=[Name Age Company]
// 'Name'=Tony
// 'Age'=25
// 'Company'={Name:Tiger inc. Address:Tokyo Period:3}
}
func ExampleGetter_MapGet() {
type Company struct {
Name string
Address string
Period int
}
type Person struct {
Name string
Age int
Companies []*Company
}
i := &Person{
Name: "Tony",
Age: 25,
Companies: []*Company{
{
Name: "Tiger inc.",
Address: "Tokyo",
Period: 3,
},
{
Name: "Dragon inc.",
Address: "Osaka",
Period: 4,
},
},
}
getter, err := NewGetter(i)
if err != nil {
panic(err)
}
// Each of "Companies" field are applied map function as follows.
fn := func(i int, g *Getter) (interface{}, error) {
period, _ := g.Int("Period")
name, _ := g.String("Name")
return fmt.Sprintf(
"You worked for %d years since you joined the company %s",
period,
name,
), nil
}
// 1st argeument must be a field name of array or slice field.
// function assigned 2nd argument is applied each "Companies" element.
intfs, err := getter.MapGet("Companies", fn)
if err != nil {
panic(err)
}
fmt.Printf("%#v", intfs)
// Output:
// []interface {}{"You worked for 3 years since you joined the company Tiger inc.", "You worked for 4 years since you joined the company Dragon inc."}
}
func ExampleFinder() {
type Group struct {
Name string
Boss string
}
type Company struct {
Name string
Address string
Period int
*Group
}
type School struct {
Name string
GraduatedYear int
}
type Person struct {
Name string
Age int
*Company
*School
}
i := &Person{
Name: "Joe Davis",
Age: 45,
Company: &Company{
Name: "XXX Cars inc.",
Address: "New York",
Period: 20,
Group: &Group{
Name: "YYY Group Holdings",
Boss: "Donald",
},
},
School: &School{
Name: "ABC College",
GraduatedYear: 1995,
},
}
// 2nd argument is the separator string for nested field names separating
finder, err := NewFinderWithSep(i, ">")
// Note:
// If "NewFinder(i)" is called instead of "NewFinderWithSep", default separator "." is automatically used.
// finder, err := NewFinder(i)
if err != nil {
panic(err)
}
// Finder provides method chain mechanism
m, err := finder.
// Find(...string) returns a Finder that fields in NESTED struct are looked up by field name arguments.
Find("School").
// Into(...string) returns a Finder that NESTED struct fields are looked up by field name arguments.
// This example looks up `person.Company.Address` field.
Into("Company").Find("Address").
// If multi arguments are assigned for Into method, then execute multi level nesting.
// This example looks up `person.Company.Group.Name` and `person.Company.Group.Boss` fields.
Into("Company", "Group").Find("Name", "Boss").
// ToMap converts from found struct fields to map.
ToMap()
if err != nil {
panic(err)
}
fmt.Printf("%#v", m)
// Output:
// map[string]interface {}{"Company>Address":"New York", "Company>Group>Boss":"Donald", "Company>Group>Name":"YYY Group Holdings", "School":structil.School{Name:"ABC College", GraduatedYear:1995}}
}
func ExampleFinder_FromKeys_yml() {
type Group struct {
Name string
Boss string
}
type Company struct {
Name string
Address string
Period int
*Group
}
type School struct {
Name string
GraduatedYear int
}
type Person struct {
Name string
Age int
*Company
*School
}
i := &Person{
Name: "Joe Davis",
Age: 45,
Company: &Company{
Name: "XXX Cars inc.",
Address: "New York",
Period: 20,
Group: &Group{
Name: "YYY Group Holdings",
Boss: "Donald",
},
},
School: &School{
Name: "ABC College",
GraduatedYear: 1995,
},
}
// testdata/finder_from_conf/ex_yml.yml as follows:
//
// Keys:
// - Company:
// - Group:
// - Name
// - Boss
// - Address
// - Period
// - Name
// - Age
// Get `FinderKeys` object by calling `NewFinderKeys` with config file dir and baseName
// This config file path is "testdata/finder_from_conf/ex_json.json"
fks, err := NewFinderKeys("testdata/finder_from_conf", "ex_yml")
if err != nil {
panic(err)
}
finder, err := NewFinder(i)
if err != nil {
panic(err)
}
// And build `Finder` object using `FromKeys` method with `FinderKeys` object
// This returns the same result as follows:
//
// finder = finder.Find("Name", "Age").
// Into("Company").Find("Address", "Period").
// Into("Company", "Group").Find("Name", "Boss")
m, err := finder.FromKeys(fks).ToMap()
if err != nil {
panic(err)
}
fmt.Printf("%#v", m)
// Output:
// map[string]interface {}{"Age":45, "Company.Address":"New York", "Company.Group.Boss":"Donald", "Company.Group.Name":"YYY Group Holdings", "Company.Period":20, "Name":"Joe Davis"}
}
func ExampleFinder_FromKeys_json() {
type Group struct {
Name string
Boss string
}
type Company struct {
Name string
Address string
Period int
*Group
}
type School struct {
Name string
GraduatedYear int
}
type Person struct {
Name string
Age int
*Company
*School
}
i := &Person{
Name: "Joe Davis",
Age: 45,
Company: &Company{
Name: "XXX Cars inc.",
Address: "New York",
Period: 20,
Group: &Group{
Name: "YYY Group Holdings",
Boss: "Donald",
},
},
School: &School{
Name: "ABC College",
GraduatedYear: 1995,
},
}
// testdata/finder_from_conf/ex_json.json as follows:
//
// {
// "Keys":[
// {
// "Company":[
// {
// "Group":[
// "Name",
// "Boss"
// ]
// },
// "Address",
// "Period"
// ]
// },
// "Name",
// "Age"
// ]
// }
fks, err := NewFinderKeys("testdata/finder_from_conf", "ex_json")
if err != nil {
panic(err)
}
finder, err := NewFinder(i)
if err != nil {
panic(err)
}
m, err := finder.FromKeys(fks).ToMap()
if err != nil {
panic(err)
}
fmt.Printf("%#v", m)
// Output:
// map[string]interface {}{"Age":45, "Company.Address":"New York", "Company.Group.Boss":"Donald", "Company.Group.Name":"YYY Group Holdings", "Company.Period":20, "Name":"Joe Davis"}
}