-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappers_test.go
184 lines (149 loc) · 4.02 KB
/
mappers_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
package mapper
import (
"github.com/google/go-cmp/cmp"
"strings"
"testing"
"time"
)
type ID struct {
Id string
}
type Person struct {
ID
Name string
Spouse *Person
Relations []*Relation
}
type Car struct {
Make string
Owner *Person
Driver Person
Passengers []Person
Tags *[]string
Numbers []int
Year time.Time
}
type Relation struct {
Name string
Friends []*Person
}
func TestStructToMap(t *testing.T) {
john := Person{Name: "John"}
mary := Person{Name: "Mary"}
john.Spouse = &mary
mary.Spouse = &john
friend1 := &Person{Name: "Friend1"}
friend2 := &Person{Name: "Friend2"}
// Add the nested relation
john.Relations = []*Relation{{Friends: []*Person{friend1, friend2}}}
now := time.Now()
tags := []string{"tag1", "tag2"}
car := &Car{
Make: "Toyota",
Owner: &john,
Driver: Person{Name: "Mark"}, // embedded entity
Passengers: []Person{john, mary},
Tags: &tags,
Numbers: []int{1, 2, 3},
Year: now,
}
mapper := New()
m, err := mapper.StructToMap(car)
if err != nil {
t.Errorf("Could not convert struct to map %v", err)
}
newCar := &Car{}
if err := mapper.MapToStruct(m, newCar); err != nil {
t.Errorf("Could not map to struct %v", err)
}
if car.Owner.Spouse.Name != newCar.Owner.Spouse.Name || car.Owner.Spouse.Spouse.Name != newCar.Owner.Spouse.Spouse.Name {
t.Errorf("The structs cycle did not match %v - %v", car, newCar)
}
if car.Driver.Spouse != nil && len(car.Driver.Relations) != 0 {
t.Errorf("The structs cycle did not match %v - %v", car, newCar)
}
// cmp.Equal does not handle cycles so break it
car.Owner.Spouse.Spouse = nil
newCar.Owner.Spouse.Spouse = nil
if !cmp.Equal(car, newCar) {
t.Errorf("The structs were not the same %v - %v", car, newCar)
}
}
func TestFilter(t *testing.T) {
john := Person{Name: "John"}
mapper := New()
mapper.MapFunc = func(inKey string, inVal interface{}) (mt MappingType, outKey string, outVal interface{}) {
return Default, strings.ToLower(inKey), inVal
}
m, err := mapper.StructToMap(&john)
if err != nil {
t.Errorf("Could not convert struct to map %v", err)
}
if _, ok := m["name"]; !ok {
t.Errorf("The lowercase key:'name' was not set om the map")
}
mapper.MapFunc = func(inKey string, inVal interface{}) (mt MappingType, outKey string, outVal interface{}) {
return Default, strings.Title(inKey), inVal
}
john.Name = ""
if err := mapper.MapToStruct(m, &john); err != nil {
t.Errorf("Could not map to struct %v", err)
}
if john.Name != "John" {
t.Errorf("Name should me John")
}
john.Name = ""
delete(m, "name")
m["Name"] = "Deere"
if err := mapper.MapToStruct(m, &john); err != nil {
t.Errorf("Could not map to struct %v", err)
}
if john.Name != "Deere" {
t.Errorf("Name should me Deere")
}
}
func TestZeroValues(t *testing.T) {
john := Person{Name: "John"}
mapper := New()
m, err := mapper.StructToMap(&john)
if err != nil {
t.Errorf("Could not convert struct to map %v", err)
}
if m["Spouse"] != nil {
t.Errorf("spouse sould be the nil reference %v", err)
}
}
func TestCaseSensitive(t *testing.T) {
john := Person{Name: "John"}
mapper := New()
err := mapper.MapToStruct(map[string]string{"name": "Deere"}, &john)
if err != nil {
t.Errorf("Could not convert map to struct %v", err)
}
if john.Name != "John" {
t.Errorf("name should be %v", "John")
}
err = mapper.MapToStruct(map[string]string{"Name": "Deere"}, &john)
if err != nil {
t.Errorf("Could not convert map to struct %v", err)
}
if john.Name != "Deere" {
t.Errorf("name should be %v", "Deere")
}
mapper.CaseSensitive = false
err = mapper.MapToStruct(map[string]string{"name": "John"}, &john)
if err != nil {
t.Errorf("Could not convert map to struct %v", err)
}
if john.Name != "John" {
t.Errorf("name should be %v", "John")
}
}
func TestErrorMessage(t *testing.T) {
john := Person{Name: "John"}
mapper := New()
err := mapper.MapToStruct(map[string]interface{}{"Name": false}, &john)
if err == nil {
t.Errorf("A conversion error is expected")
}
}