forked from ohler55/ojg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
276 lines (246 loc) · 7.84 KB
/
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
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
// Copyright (c) 2020, Peter Ohler, All rights reserved.
package alt_test
import (
"fmt"
"time"
"github.com/ohler55/ojg"
"github.com/ohler55/ojg/alt"
"github.com/ohler55/ojg/gen"
"github.com/ohler55/ojg/oj"
"github.com/ohler55/ojg/sen"
)
func ExampleDecompose() {
type Sample struct {
Int int
Str string
}
sample := Sample{Int: 3, Str: "three"}
// Decompose and add a CreateKey to indicate the type with a full path.
simple := alt.Decompose(&sample, &ojg.Options{CreateKey: "^", FullTypePath: true})
fmt.Println(oj.JSON(simple, &oj.Options{Sort: true}))
// Output: {"^":"github.com/ohler55/ojg/alt_test/Sample","int":3,"str":"three"}
}
func ExampleDup() {
type Sample struct {
Int int
Str string
}
sample := []interface{}{&Sample{Int: 3, Str: "three"}, 42}
// Dup creates a deep duplicate of a simple type and decomposes any
// structs according to the optional options just like alt.Decompose does.
simple := alt.Decompose(sample, &ojg.Options{CreateKey: "^"})
fmt.Println(oj.JSON(simple, &ojg.Options{Sort: true}))
// Output: [{"^":"Sample","int":3,"str":"three"},42]
}
func ExampleRecomposer_Recompose() {
type Sample struct {
Int int
Str string
}
// Recomposers are reuseable. Create one and use the default reflect composer (nil).
r, err := alt.NewRecomposer("^", map[interface{}]alt.RecomposeFunc{&Sample{}: nil})
if err != nil {
panic(err)
}
var v interface{}
// Recompose without providing a struct to populate.
v, err = r.Recompose(map[string]interface{}{"^": "Sample", "int": 3, "str": "three"})
if err != nil {
panic(err)
}
fmt.Printf("type: %T\n", v)
if sample, _ := v.(*Sample); sample != nil {
fmt.Printf("sample: {Int: %d, Str: %q}\n", sample.Int, sample.Str)
}
// Output:
// type: *alt_test.Sample
// sample: {Int: 3, Str: "three"}
}
func ExampleNewRecomposer() {
type Sample struct {
Int int
Str string
}
// Recomposers are reuseable. Create one and use the default reflect composer (nil).
r, err := alt.NewRecomposer("^", map[interface{}]alt.RecomposeFunc{&Sample{}: nil})
if err != nil {
panic(err)
}
var v interface{}
// Recompose without providing a struct to populate.
v, err = r.Recompose(map[string]interface{}{"^": "Sample", "int": 3, "str": "three"})
if err != nil {
panic(err)
}
fmt.Printf("type: %T\n", v)
if sample, _ := v.(*Sample); sample != nil {
fmt.Printf("sample: {Int: %d, Str: %q}\n", sample.Int, sample.Str)
}
// Output:
// type: *alt_test.Sample
// sample: {Int: 3, Str: "three"}
}
func ExampleRecompose() {
type Sample struct {
Int int
Str string
}
// Simplified sample data or JSON as a map[string]interface{}.
data := map[string]interface{}{"int": 3, "str": "three"}
var sample Sample
// Recompose into the sample struct. Panic on failure.
v, err := alt.Recompose(data, &sample)
if err != nil {
panic(err)
}
fmt.Printf("type: %T\n", v)
fmt.Printf("sample: {Int: %d, Str: %q}\n", sample.Int, sample.Str)
// Output:
// type: *alt_test.Sample
// sample: {Int: 3, Str: "three"}
}
func ExampleMustRecompose() {
type Sample struct {
Int int
Str string
}
// Simplified sample data or JSON as a map[string]interface{}.
data := map[string]interface{}{"int": 3, "str": "three"}
var sample Sample
// Recompose into the sample struct. Panic on failure.
v := alt.MustRecompose(data, &sample)
fmt.Printf("type: %T\n", v)
fmt.Printf("sample: {Int: %d, Str: %q}\n", sample.Int, sample.Str)
// Output:
// type: *alt_test.Sample
// sample: {Int: 3, Str: "three"}
}
func ExampleRecomposer_MustRecompose() {
type Sample struct {
Int int
When time.Time
}
// Create a new Recomposer that uses "^" as the create key and register a
// default reflection recompose function (nil). A time recomposer from an
// integer is also included in the new recomposer compser options.
r := alt.MustNewRecomposer("^",
map[interface{}]alt.RecomposeFunc{&Sample{}: nil},
map[interface{}]alt.RecomposeAnyFunc{&time.Time{}: func(v interface{}) (interface{}, error) {
if s, _ := v.(string); 0 < len(s) {
return time.ParseInLocation(time.RFC3339, s, time.UTC)
}
return nil, fmt.Errorf("can not convert a %v to a time.Time", v)
}})
// Simplified sample data or JSON as a map[string]interface{} with an
// included create key using "^" to avoid possible conflicts with other
// fields in the struct.
data := map[string]interface{}{"^": "Sample", "int": 3, "when": "2021-02-09T01:02:03Z"}
v := r.MustRecompose(data)
if sample, _ := v.(*Sample); sample != nil {
fmt.Printf("sample: {Int: %d, When: %q}\n", sample.Int, sample.When.Format(time.RFC3339))
}
// Output:
// sample: {Int: 3, When: "2021-02-09T01:02:03Z"}
}
func ExampleMustNewRecomposer() {
type Sample struct {
Int int
When time.Time
}
// Create a new Recomposer that uses "^" as the create key and register a
// default reflection recompose function (nil). A time recomposer from an
// integer is also included in the new recomposer compser options.
r := alt.MustNewRecomposer("^",
map[interface{}]alt.RecomposeFunc{&Sample{}: nil},
map[interface{}]alt.RecomposeAnyFunc{&time.Time{}: func(v interface{}) (interface{}, error) {
if s, _ := v.(string); 0 < len(s) {
return time.ParseInLocation(time.RFC3339, s, time.UTC)
}
return nil, fmt.Errorf("can not convert a %v to a time.Time", v)
}})
// Simplified sample data or JSON as a map[string]interface{} with an
// included create key using "^" to avoid possible conflicts with other
// fields in the struct.
data := map[string]interface{}{"^": "Sample", "int": 3, "when": "2021-02-09T01:02:03Z"}
v := r.MustRecompose(data)
if sample, _ := v.(*Sample); sample != nil {
fmt.Printf("sample: {Int: %d, When: %q}\n", sample.Int, sample.When.Format(time.RFC3339))
}
// Output:
// sample: {Int: 3, When: "2021-02-09T01:02:03Z"}
}
func ExampleAlter() {
src := map[string]interface{}{"a": 1, "b": 4, "c": 9}
// Alter the src as needed avoiding duplicating when possible.
val := alt.Alter(src)
// Modify src should change val since they are the same map.
src["d"] = 16
fmt.Println(sen.String(val, &oj.Options{Sort: true}))
// Output: {a:1 b:4 c:9 d:16}
}
func ExampleGenAlter() {
m := map[string]interface{}{"a": 1, "b": 4, "c": 9}
// Convert to a gen.Node.
node := alt.GenAlter(m)
fmt.Println(sen.String(node, &oj.Options{Sort: true}))
obj, _ := node.(gen.Object)
fmt.Printf("member type: %T\n", obj["b"])
// Output: {a:1 b:4 c:9}
// member type: gen.Int
}
func ExampleRecomposer_RegisterComposer() {
type Sample struct {
Int int
When time.Time
}
r := alt.MustNewRecomposer("^", nil)
err := r.RegisterComposer(&Sample{}, nil)
if err != nil {
panic(err)
}
err = r.RegisterAnyComposer(time.Time{},
func(v interface{}) (interface{}, error) {
if secs, ok := v.(int); ok {
return time.Unix(int64(secs), 0), nil
}
return nil, fmt.Errorf("can not convert a %T to a time.Time", v)
})
if err != nil {
panic(err)
}
data := map[string]interface{}{"^": "Sample", "int": 3, "when": 1612872722}
sample, _ := r.MustRecompose(data).(*Sample)
fmt.Printf("sample.Int: %d\n", sample.Int)
fmt.Printf("sample.When: %d\n", sample.When.Unix())
// Output:
// sample.Int: 3
// sample.When: 1612872722
}
func ExampleRecomposer_RegisterAnyComposer() {
type Sample struct {
Int int
When time.Time
}
r := alt.MustNewRecomposer("^", nil)
err := r.RegisterComposer(&Sample{}, nil)
if err != nil {
panic(err)
}
err = r.RegisterAnyComposer(time.Time{},
func(v interface{}) (interface{}, error) {
if secs, ok := v.(int); ok {
return time.Unix(int64(secs), 0), nil
}
return nil, fmt.Errorf("can not convert a %T to a time.Time", v)
})
if err != nil {
panic(err)
}
data := map[string]interface{}{"^": "Sample", "int": 3, "when": 1612872722}
sample, _ := r.MustRecompose(data).(*Sample)
fmt.Printf("sample.Int: %d\n", sample.Int)
fmt.Printf("sample.When: %d\n", sample.When.Unix())
// Output:
// sample.Int: 3
// sample.When: 1612872722
}