forked from oleiade/reflections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
215 lines (179 loc) · 4.34 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
package reflections_test
import (
"fmt"
"log"
"reflect"
"github.com/oleiade/reflections"
)
type MyStruct struct {
MyEmbeddedStruct
FirstField string `matched:"first tag"`
SecondField int `matched:"second tag"`
ThirdField string `unmatched:"third tag"`
}
type MyEmbeddedStruct struct {
EmbeddedField string
}
func ExampleGetField() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
fieldsToExtract := []string{"FirstField", "ThirdField"}
for _, fieldName := range fieldsToExtract {
value, err := reflections.GetField(s, fieldName)
if err != nil {
log.Fatal(err)
}
fmt.Println(value)
}
}
func ExampleGetFieldKind() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
var firstFieldKind reflect.Kind
var secondFieldKind reflect.Kind
var err error
// GetFieldKind will return reflect.String
firstFieldKind, err = reflections.GetFieldKind(s, "FirstField")
if err != nil {
log.Fatal(err)
}
fmt.Println(firstFieldKind)
// GetFieldKind will return reflect.Int
secondFieldKind, err = reflections.GetFieldKind(s, "SecondField")
if err != nil {
log.Fatal(err)
}
fmt.Println(secondFieldKind)
}
func ExampleGetFieldType() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
var firstFieldType string
var secondFieldType string
var err error
// GetFieldType will return reflect.String
firstFieldType, err = reflections.GetFieldType(s, "FirstField")
if err != nil {
log.Fatal(err)
}
fmt.Println(firstFieldType)
// GetFieldType will return reflect.Int
secondFieldType, err = reflections.GetFieldType(s, "SecondField")
if err != nil {
log.Fatal(err)
}
fmt.Println(secondFieldType)
}
func ExampleGetFieldTag() {
s := MyStruct{}
tag, err := reflections.GetFieldTag(s, "FirstField", "matched")
if err != nil {
log.Fatal(err)
}
fmt.Println(tag)
tag, err = reflections.GetFieldTag(s, "ThirdField", "unmatched")
if err != nil {
log.Fatal(err)
}
fmt.Println(tag)
}
func ExampleHasField() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
// has == true
has, _ := reflections.HasField(s, "FirstField")
fmt.Println(has)
// has == false
has, _ = reflections.HasField(s, "FourthField")
fmt.Println(has)
}
func ExampleFields() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
var fields []string
// Fields will list every structure exportable fields.
// Here, it's content would be equal to:
// []string{"FirstField", "SecondField", "ThirdField"}
fields, _ = reflections.Fields(s)
fmt.Println(fields)
}
func ExampleItems() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
var structItems map[string]interface{}
// Items will return a field name to
// field value map
structItems, _ = reflections.Items(s)
fmt.Println(structItems)
}
func ExampleItemsDeep() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
MyEmbeddedStruct: MyEmbeddedStruct{
EmbeddedField: "embedded value",
},
}
var structItems map[string]interface{}
// ItemsDeep will return a field name to
// field value map, including fields from
// anonymous embedded structs
structItems, _ = reflections.ItemsDeep(s)
fmt.Println(structItems)
}
func ExampleTags() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
var structTags map[string]string
// Tags will return a field name to tag content
// map. Nota that only field with the tag name
// you've provided which will be matched.
// Here structTags will contain:
// {
// "FirstField": "first tag",
// "SecondField": "second tag",
// }
structTags, _ = reflections.Tags(s, "matched")
fmt.Println(structTags)
}
func ExampleSetField() {
s := MyStruct{
FirstField: "first value",
SecondField: 2,
ThirdField: "third value",
}
// In order to be able to set the structure's values,
// a pointer to it has to be passed to it.
err := reflections.SetField(&s, "FirstField", "new value")
if err != nil {
log.Fatal(err)
}
// If you try to set a field's value using the wrong type,
// an error will be returned
err = reflections.SetField(&s, "FirstField", 123) // err != nil
if err != nil {
log.Fatal(err)
}
}