-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmapper_examples_test.go
413 lines (352 loc) · 9.7 KB
/
mapper_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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package set_test
import (
"database/sql"
"fmt"
"reflect"
"strings"
"time"
"github.com/nofeaturesonlybugs/set"
)
func ExampleMapper() {
// This example maps the same type (Person) with three different Mapper instances.
// Each Mapper instance has slightly different configuration to control the mapped
// names that are generated.
type CommonDb struct {
Pk int `t:"pk"`
CreatedTime string `t:"created_time"`
UpdatedTime string `t:"updated_time"`
}
type Person struct {
CommonDb `t:"common"`
Name string `t:"name"`
Age int `t:"age"`
}
var data Person
{
mapper := &set.Mapper{
Elevated: set.NewTypeList(CommonDb{}),
Join: "_",
}
mapping := mapper.Map(&data)
fmt.Println(strings.ReplaceAll(mapping.String(), "\t\t", " "))
}
{
fmt.Println("")
fmt.Println("lowercase with dot separators")
mapper := &set.Mapper{
Join: ".",
Transform: strings.ToLower,
}
mapping := mapper.Map(&data)
fmt.Println(strings.ReplaceAll(mapping.String(), "\t\t", " "))
}
{
fmt.Println("")
fmt.Println("specify tags")
mapper := &set.Mapper{
Join: "_",
Tags: []string{"t"},
}
mapping := mapper.Map(&data)
fmt.Println(strings.ReplaceAll(mapping.String(), "\t\t", " "))
}
// Output: [0 0] Pk
// [0 1] CreatedTime
// [0 2] UpdatedTime
// [1] Name
// [2] Age
//
// lowercase with dot separators
// [0 0] commondb.pk
// [0 1] commondb.createdtime
// [0 2] commondb.updatedtime
// [1] name
// [2] age
//
// specify tags
// [0 0] common_pk
// [0 1] common_created_time
// [0 2] common_updated_time
// [1] name
// [2] age
}
func ExampleMapper_treatAsScalar() {
// By default Mapper maps **all** exported fields in nested or embedded structs.
type S struct {
N sql.NullString // sql.NullString has public fields that can be mapped.
}
var s S
// Notice that **this** Mapper maps the public fields in the sql.NullString type.
fmt.Println("Without TreatAsScalar")
all := &set.Mapper{
Join: ".",
}
m := all.Map(&s)
fmt.Println(strings.ReplaceAll(m.String(), "\t\t", " "))
// While **this** Mapper treats N as a scalar field and does not map its public fields.
fmt.Println("TreatAsScalar")
scalar := &set.Mapper{
TreatAsScalar: set.NewTypeList(sql.NullString{}), // Fields of sql.NullString are now treated as scalars.
Join: ".",
}
m = scalar.Map(&s)
fmt.Println(strings.ReplaceAll(m.String(), "\t\t", " "))
// Output: Without TreatAsScalar
// [0 0] N.String
// [0 1] N.Valid
// TreatAsScalar
// [0] N
}
func ExampleMapper_treatAsScalarTime() {
// As a convenience time.Time and *time.Time are always treated as scalars.
type S struct {
T time.Time
TPtr *time.Time
}
var s S
// Even though this mapper does not configure TreatAsScalar the time.Time fields **are**
// treated as scalars.
all := &set.Mapper{
Join: ".",
}
m := all.Map(&s)
fmt.Println(strings.ReplaceAll(m.String(), "\t\t", " "))
// Output: [0] T
// [1] TPtr
}
func ExampleMapper_Bind() {
// This example demonstrates a simple flat struct of primitives.
type S struct {
Num int
Str string
}
var s S
// Create a mapper.
m := &set.Mapper{}
b, _ := m.Bind(&s) // err ignored for brevity
// b.Set errors ignored for brevity
_ = b.Set("Str", 3.14) // 3.14 coerced to "3.14"
_ = b.Set("Num", "42") // "42" coerced to 42
fmt.Println(s.Num, s.Str)
// Output: 42 3.14
}
func ExampleMapper_Bind_nesting() {
// This example demonstrates how nested structs are accessible via their mapped names.
type Nest struct {
V int
}
type S struct {
Num int
Str string
Foo Nest
}
var s S
// Create a mapper.
m := &set.Mapper{
Join: ".", // Nested and embedded fields join with DOT
}
b, _ := m.Bind(&s) // err ignored for brevity
// b.Set errors ignored for brevity
_ = b.Set("Str", 3.14) // 3.14 coerced to "3.14"
_ = b.Set("Num", "42") // "42" coerced to 42
_ = b.Set("Foo.V", 3.14) // 3.14 coerced to 3
fmt.Println(s.Num, s.Str, s.Foo.V)
// Output: 42 3.14 3
}
func ExampleMapper_Bind_embedded() {
// This example demonstrates how to access fields in an embedded struct.
// Notice in this example the field is named Embed.V -- the struct type name
// becomes part of the mapping name.
type Embed struct {
V int
}
type S struct {
Num int
Str string
Embed
}
var s S
// Create a mapper.
m := &set.Mapper{
Join: ".", // Nested and embedded fields join with DOT
}
b, _ := m.Bind(&s) // err ignored for brevity
// b.Set errors ignored for brevity
_ = b.Set("Str", 3.14) // 3.14 coerced to "3.14"
_ = b.Set("Num", "42") // "42" coerced to 42
_ = b.Set("Embed.V", 3.14) // 3.14 coerced to 3
fmt.Println(s.Num, s.Str, s.V)
// Output: 42 3.14 3
}
func ExampleMapper_Bind_elevatedEmbed() {
// This example demonstrates how to access fields in an elevated embedded struct.
// Elevated types can be embedded without their type name becoming part of the field name.
type Embed struct {
V int
}
type S struct {
Num int
Str string
Embed
}
var s S
// Create a mapper.
m := &set.Mapper{
Elevated: set.NewTypeList(Embed{}), // Elevate embedded fields of type Embed
Join: ".", // Nested and embedded fields join with DOT
}
b, _ := m.Bind(&s) // err ignored for brevity
// b.Set errors ignored for brevity
_ = b.Set("Str", 3.14) // 3.14 coerced to "3.14"
_ = b.Set("Num", "42") // "42" coerced to 42
_ = b.Set("V", 3.14) // 3.14 coerced to 3
fmt.Println(s.Num, s.Str, s.V)
// Output: 42 3.14 3
}
func ExampleMapper_Bind_reflectValue() {
// As a convenience Mapper.Bind will accept a reflect.Value to perform the binding.
type S struct {
Num int
Str string
}
var s, t, u S
// Create a mapper.
m := &set.Mapper{}
b, _ := m.Bind(reflect.ValueOf(&s)) // err ignored for brevity
// b.Set errors ignored for brevity
_ = b.Set("Str", 3.14)
_ = b.Set("Num", "42")
b.Rebind(reflect.ValueOf(&t))
_ = b.Set("Str", -3.14)
_ = b.Set("Num", "24")
// Even though the BoundMapping was created with reflect.Value it will still accept *S directly.
b.Rebind(&u)
_ = b.Set("Str", "Works!")
_ = b.Set("Num", uint(100))
fmt.Println("s", s.Num, s.Str)
fmt.Println("t", t.Num, t.Str)
fmt.Println("u", u.Num, u.Str)
// Output: s 42 3.14
// t 24 -3.14
// u 100 Works!
}
func ExampleMapper_Prepare() {
// This example demonstrates a simple flat struct of primitives.
type S struct {
Num int
Str string
}
var s S
// Create a mapper.
m := &set.Mapper{}
p, _ := m.Prepare(&s) // err ignored for brevity
// PreparedBindings require a call to Plan indicating field order.
_ = p.Plan("Str", "Num") // err ignored for brevity
_ = p.Set(3.14) // 3.14 coerced to "3.14"
_ = p.Set("42") // "42" coerced to 42
fmt.Println(s.Num, s.Str)
// Output: 42 3.14
}
func ExampleMapper_Prepare_nesting() {
// This example demonstrates how nested structs are accessible via their mapped names.
type Nest struct {
V int
}
type S struct {
Num int
Str string
Foo Nest
}
var s S
// Create a mapper.
m := &set.Mapper{
Join: ".", // Nested and embedded fields join with DOT
}
p, _ := m.Prepare(&s) // err ignored for brevity
// PreparedBindings require a call to Plan indicating field order.
_ = p.Plan("Str", "Num", "Foo.V") // err ignored for brevity
_ = p.Set(3.14) // 3.14 coerced to "3.14"
_ = p.Set("42") // "42" coerced to 42
_ = p.Set(3.14) // 3.14 coerced to 3
fmt.Println(s.Num, s.Str, s.Foo.V)
// Output: 42 3.14 3
}
func ExampleMapper_Prepare_embedded() {
// This example demonstrates how to access fields in an embedded struct.
// Notice in this example the field is named Embed.V -- the struct type name
// becomes part of the mapping name.
type Embed struct {
V int
}
type S struct {
Num int
Str string
Embed
}
var s S
// Create a mapper.
m := &set.Mapper{
Join: ".", // Nested and embedded fields join with DOT
}
p, _ := m.Prepare(&s) // err ignored for brevity
// PreparedBindings require a call to Plan indicating field order.
_ = p.Plan("Str", "Num", "Embed.V") // err ignored for brevity
_ = p.Set(3.14) // 3.14 coerced to "3.14"
_ = p.Set("42") // "42" coerced to 42
_ = p.Set(3.14) // 3.14 coerced to 3
fmt.Println(s.Num, s.Str, s.V)
// Output: 42 3.14 3
}
func ExampleMapper_Prepare_elevatedEmbed() {
// This example demonstrates how to access fields in an elevated embedded struct.
// Elevated types can be embedded without their type name becoming part of the field name.
type Embed struct {
V int
}
type S struct {
Num int
Str string
Embed
}
var s S
// Create a mapper.
m := &set.Mapper{
Elevated: set.NewTypeList(Embed{}), // Elevate embedded fields of type Embed
Join: ".", // Nested and embedded fields join with DOT
}
p, _ := m.Prepare(&s) // err ignored for brevity
// PreparedBindings require a call to Plan indicating field order.
_ = p.Plan("Str", "Num", "V") // err ignored for brevity
_ = p.Set(3.14) // 3.14 coerced to "3.14"
_ = p.Set("42") // "42" coerced to 42
_ = p.Set(3.14) // 3.14 coerced to 3
fmt.Println(s.Num, s.Str, s.V)
// Output: 42 3.14 3
}
func ExampleMapper_Prepare_reflectValue() {
// As a convenience Mapper.Prepare will accept a reflect.Value to perform the binding.
type S struct {
Num int
Str string
}
var s, t, u S
// Create a mapper.
m := &set.Mapper{}
p, _ := m.Prepare(reflect.ValueOf(&s)) // err ignored for brevity
_ = p.Plan("Str", "Num") // err ignored for brevity
_ = p.Set(3.14)
_ = p.Set("42")
p.Rebind(reflect.ValueOf(&t))
_ = p.Set(-3.14)
_ = p.Set("24")
// Even though the PreparedMapping was created with reflect.Value it will still accept *S directly.
p.Rebind(&u)
_ = p.Set("Works!")
_ = p.Set(uint(100))
fmt.Println("s", s.Num, s.Str)
fmt.Println("t", t.Num, t.Str)
fmt.Println("u", u.Num, u.Str)
// Output: s 42 3.14
// t 24 -3.14
// u 100 Works!
}