-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_test.go
139 lines (116 loc) · 2.8 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
package structmapper_test
import (
"fmt"
"net"
"strings"
"github.com/anexia-it/go-structmapper"
)
func ExampleMapper_ToMap() {
type ExampleStruct struct {
A string
B int `mapper:"bee"`
C []string
D map[string]int
E net.IP
Ignored string `mapper:"-"`
}
// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
panic(err)
}
source := &ExampleStruct{
A: "a",
B: 5,
C: []string{"c0", "c1"},
D: map[string]int{
"d0": 1,
"d1": 2,
},
E: net.ParseIP("127.0.0.1"),
Ignored: "should be ignored",
}
m, err := sm.ToMap(source)
if err != nil {
panic(err)
}
c := []string{}
for _, v := range m["C"].([]interface{}) {
c = append(c, v.(string))
}
d := m["D"].(map[interface{}]interface{})
fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", m["A"], m["bee"], strings.Join(c, ","),
d["d0"], d["d1"], m["E"], m["Ignored"])
// Output: A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=%!s(<nil>)
}
func ExampleMapper_ToStruct() {
type ExampleStruct struct {
A string
B int `mapper:"bee"`
C []string
D map[string]int
E net.IP
Ignored string `mapper:"-"`
}
// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
panic(err)
}
source := map[string]interface{}{
"A": "a",
"bee": 5,
"C": []string{"c0", "c1"},
"D": map[interface{}]interface{}{
"d0": 1,
"d1": 2,
},
"E": "127.0.0.1",
}
target := &ExampleStruct{}
if err := sm.ToStruct(source, target); err != nil {
panic(err)
}
fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", target.A, target.B, strings.Join(target.C, ","),
target.D["d0"], target.D["d1"], target.E, target.Ignored)
// Output: A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=
}
func ExampleMapper_roundtrip() {
type ExampleStruct struct {
A string
B int `mapper:"bee"`
C []string
D map[string]int
E net.IP
Ignored string `mapper:"-"`
}
// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
panic(err)
}
source := &ExampleStruct{
A: "a",
B: 5,
C: []string{"c0", "c1"},
D: map[string]int{
"d0": 1,
"d1": 2,
},
E: net.ParseIP("127.0.0.1"),
Ignored: "should be ignored",
}
// Convert to map
m, err := sm.ToMap(source)
if err != nil {
panic(err)
}
target := &ExampleStruct{}
// Convert back to struct
if err := sm.ToStruct(m, target); err != nil {
panic(err)
}
fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", target.A, target.B, strings.Join(target.C, ","),
target.D["d0"], target.D["d1"], target.E, target.Ignored)
// Output: A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=
}