-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample_test.go
More file actions
144 lines (127 loc) · 3.29 KB
/
example_test.go
File metadata and controls
144 lines (127 loc) · 3.29 KB
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
package dynamicstruct
import (
"encoding/json"
"fmt"
"github.com/goldeneggg/structil"
)
func Example() {
type Hoge struct {
Key string
Value interface{}
}
hogePtr := &Hoge{
Key: "keystr",
Value: "valuestr",
}
// Add fields using Builder with AddXXX method chain
b := NewBuilder().
AddString("StringField").
AddInt("IntField").
AddFloat32("Float32Field").
AddBool("BoolField").
AddMap("MapField", SampleString, SampleFloat32).
AddStructPtr("StructPtrField", hogePtr).
AddSlice("SliceField", SampleInt).
AddInterfaceWithTag("SomeObjectField", true, `json:"some_object_field"`)
// Remove removes a field by assigned name
b = b.Remove("Float32Field")
// SetStructName sets the name of DynamicStruct
// Note: Default struct name is "DynamicStruct"
b.SetStructName("MyStruct")
// Build returns a DynamicStruct
ds, err := b.Build()
if err != nil {
panic(err)
}
// Print struct definition with Definition method
// Struct fields are automatically orderd by field name
fmt.Println(ds.Definition())
// Output:
// type MyStruct struct {
// BoolField bool
// IntField int
// MapField map[string]float32
// SliceField []int
// SomeObjectField *interface {} `json:"some_object_field"`
// StringField string
// StructPtrField struct {
// Key string
// Value interface {}
// }
// }
}
func Example_unmarshalJSON() {
type Hoge struct {
Key string `json:"key"`
Value interface{} `json:"value"`
}
var hogePtr *Hoge
b := NewBuilder().
AddStringWithTag("StringField", `json:"string_field"`).
AddIntWithTag("IntField", `json:"int_field"`).
AddFloat32WithTag("Float32Field", `json:"float32_field"`).
AddBoolWithTag("BoolField", `json:"bool_field"`).
AddStructPtrWithTag("StructPtrField", hogePtr, `json:"struct_ptr_field"`).
AddSliceWithTag("SliceField", "", `json:"slice_string_field"`)
ds, err := b.Build()
if err != nil {
panic(err)
}
// prints Go struct definition of this DynamicStruct
fmt.Println(ds.Definition())
// try json unmarshal with NewInterface
input := []byte(`
{
"string_field":"あいうえお",
"int_field":9876,
"float32_field":5.67,
"bool_field":true,
"struct_ptr_field":{
"key":"hogekey",
"value":"hogevalue"
},
"slice_string_field":[
"a",
"b"
]
}
`)
intf := ds.NewInterface() // returns a new interface of this DynamicStruct
err = json.Unmarshal(input, &intf)
if err != nil {
panic(err)
}
g, err := structil.NewGetter(intf)
if err != nil {
panic(err)
}
s, _ := g.String("StringField")
f, _ := g.Float32("Float32Field")
strct, _ := g.Get("StructPtrField")
sl, _ := g.Get("SliceField")
fmt.Printf(
"num of fields=%d\n'StringField'=%s\n'Float32Field'=%f\n'StructPtrField'=%+v\n'SliceField'=%+v",
g.NumField(),
s,
f,
strct,
sl,
)
// Output:
// type DynamicStruct struct {
// BoolField bool `json:"bool_field"`
// Float32Field float32 `json:"float32_field"`
// IntField int `json:"int_field"`
// SliceField []string `json:"slice_string_field"`
// StringField string `json:"string_field"`
// StructPtrField struct {
// Key string `json:"key"`
// Value interface {} `json:"value"`
// } `json:"struct_ptr_field"`
// }
// num of fields=6
// 'StringField'=あいうえお
// 'Float32Field'=5.670000
// 'StructPtrField'={Key:hogekey Value:hogevalue}
// 'SliceField'=[a b]
}