-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetter_examples_test.go
140 lines (121 loc) · 3.16 KB
/
getter_examples_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
package set_test
import (
"fmt"
"github.com/nofeaturesonlybugs/set"
)
func ExampleGetterFunc() {
// A GetterFunc allows a function to be used as the producer for
// populating a struct.
// Note in this example the key names match the field names of the struct
// and Value.Fill is used to populate the struct.
// F has the correct signature to become a GetterFunc.
F := func(name string) interface{} {
switch name {
case "Name":
return "Bob"
case "Age":
return "42"
default:
return nil
}
}
myGetter := set.GetterFunc(F)
type T struct {
Name string
Age uint
}
var t T
_ = set.V(&t).Fill(myGetter) // error ignored for brevity
fmt.Println(t.Name, t.Age)
// Output: Bob 42
}
func ExampleGetterFunc_fillByTag() {
// In this example the key names are lower case and match values
// in the struct tags. To populate a struct by tag use Value.FillByTag.
// Also note that `address` returns a new GetterFunc used to populate
// the nested Address struct in Person.
F := func(key string) interface{} {
switch key {
case "name":
return "Bob"
case "age":
return "42"
case "address":
return set.GetterFunc(func(key string) interface{} {
switch key {
case "street1":
return "97531 Some Street"
case "street2":
return ""
case "city":
return "Big City"
case "state":
return "ST"
case "zip":
return "12345"
default:
return nil
}
})
default:
return nil
}
}
myGetter := set.GetterFunc(F)
type Address struct {
Street1 string `key:"street1"`
Street2 string `key:"street2"`
City string `key:"city"`
State string `key:"state"`
Zip string `key:"zip"`
}
type Person struct {
Name string `key:"name"`
Age uint `key:"age"`
Address Address `key:"address"`
}
var t Person
_ = set.V(&t).FillByTag("key", myGetter) // error ignored for brevity
fmt.Println(t.Name, t.Age)
fmt.Printf("%v, %v, %v %v\n", t.Address.Street1, t.Address.City, t.Address.State, t.Address.Zip)
// Output: Bob 42
// 97531 Some Street, Big City, ST 12345
}
func ExampleMapGetter() {
// A map can also be used as a Getter by casting to MapGetter.
//
// Note that the map key must be either string or interface{}.
//
// If the map contains nested maps that can be used as MapGetter
// then those maps can populate nested structs in the hierarchy.
m := map[string]interface{}{
"name": "Bob",
"age": 42,
"address": map[interface{}]string{
"street1": "97531 Some Street",
"street2": "",
"city": "Big City",
"state": "ST",
"zip": "12345",
},
}
myGetter := set.MapGetter(m)
type Address struct {
Street1 string `key:"street1"`
Street2 string `key:"street2"`
City string `key:"city"`
State string `key:"state"`
Zip string `key:"zip"`
}
type Person struct {
Name string `key:"name"`
Age uint `key:"age"`
Address Address `key:"address"`
}
var t Person
_ = set.V(&t).FillByTag("key", myGetter) // error ignored for brevity
fmt.Println(t.Name, t.Age)
fmt.Printf("%v, %v, %v %v\n", t.Address.Street1, t.Address.City, t.Address.State, t.Address.Zip)
// Output: Bob 42
// 97531 Some Street, Big City, ST 12345
}