1
1
package magic
2
2
3
3
import (
4
+ "fmt"
4
5
"reflect"
5
6
"testing"
6
7
"time"
@@ -10,6 +11,13 @@ func val(i interface{}) reflect.Value {
10
11
return reflect .ValueOf (i )
11
12
}
12
13
14
+ func assert (t * testing.T , i1 , i2 interface {}) {
15
+ t .Helper ()
16
+ if i1 != i2 {
17
+ t .Fatalf ("%v != %v" , i1 , i2 )
18
+ }
19
+ }
20
+
13
21
type testType1 struct {
14
22
ID int
15
23
Name string
@@ -44,12 +52,17 @@ type testType6 struct {
44
52
Created int64
45
53
}
46
54
47
- type testType7 struct {
48
- T * testType1
49
- }
55
+ func timeToUnix (from , to reflect.Value ) (bool , error ) {
56
+ if to .Type () != reflect .TypeOf (int64 (0 )) {
57
+ return false , nil
58
+ }
59
+ t , ok := from .Interface ().(time.Time )
60
+ if ok {
61
+ to .SetInt (t .Unix ())
62
+ return true , nil
63
+ }
50
64
51
- type testType8 struct {
52
- T * testType2
65
+ return false , nil
53
66
}
54
67
55
68
func TestMapStruct (t * testing.T ) {
@@ -60,6 +73,12 @@ func TestMapStruct(t *testing.T) {
60
73
assert (t , err , nil )
61
74
assert (t , t2 .ID , t1 .ID )
62
75
assert (t , t2 .Name , t1 .Name )
76
+
77
+ t2 = testType2 {}
78
+ err = Map (& t1 , & t2 )
79
+ assert (t , err , nil )
80
+ assert (t , t2 .ID , t1 .ID )
81
+ assert (t , t2 .Name , t1 .Name )
63
82
}
64
83
65
84
func TestMapStructWithPointers (t * testing.T ) {
@@ -130,19 +149,6 @@ func TestMapPointersSlice(t *testing.T) {
130
149
assert (t , t1 [0 ].Tags [0 ], t2 [0 ].Tags [0 ])
131
150
}
132
151
133
- func timeToUnix (from , to reflect.Value ) (bool , error ) {
134
- if to .Type () != reflect .TypeOf (int64 (0 )) {
135
- return false , nil
136
- }
137
- t , ok := from .Interface ().(time.Time )
138
- if ok {
139
- to .SetInt (t .Unix ())
140
- return true , nil
141
- }
142
-
143
- return false , nil
144
- }
145
-
146
152
func TestInvalidType (t * testing.T ) {
147
153
s1 := struct {
148
154
ID int
@@ -171,21 +177,37 @@ func TestInvalidSlice(t *testing.T) {
171
177
172
178
func TestPtrToType (t * testing.T ) {
173
179
i := 4385
180
+ type Foo1 struct {
181
+ Bar int
182
+ }
183
+ type Foo2 struct {
184
+ Bar int
185
+ S float64
186
+ }
187
+
188
+ f := Foo1 {56 }
174
189
s1 := struct {
175
- ID * int
176
- }{& i }
190
+ ID * int
191
+ Foo * Foo1
192
+ }{& i , & f }
177
193
s2 := struct {
178
- ID int
194
+ ID int
195
+ Foo Foo2
179
196
}{}
180
197
181
198
err := Map (s1 , & s2 )
182
199
assert (t , err , nil )
183
200
assert (t , * s1 .ID , s2 .ID )
201
+ assert (t , s1 .Foo .Bar , s2 .Foo .Bar )
184
202
185
203
s1 .ID = nil
204
+ s1 .Foo = nil
186
205
err = Map (s1 , & s2 )
187
206
assert (t , err , nil )
188
207
assert (t , s2 .ID , i )
208
+ if s1 .Foo != nil {
209
+ t .Fatal (s1 .Foo )
210
+ }
189
211
}
190
212
191
213
func TestTypeToPtr (t * testing.T ) {
@@ -201,6 +223,44 @@ func TestTypeToPtr(t *testing.T) {
201
223
assert (t , s1 .ID , * s2 .ID )
202
224
}
203
225
226
+ func TestConvertable (t * testing.T ) {
227
+ type Maps map [string ]string
228
+ type S1 struct {
229
+ V map [string ]string
230
+ }
231
+ type S2 struct {
232
+ V Maps
233
+ }
234
+
235
+ s1 := S1 {map [string ]string {
236
+ "foo" : "bar" ,
237
+ }}
238
+ s2 := S2 {}
239
+
240
+ err := Map (s1 , & s2 )
241
+ assert (t , err , nil )
242
+ assert (t , len (s1 .V ), len (s2 .V ))
243
+ assert (t , s1 .V ["foo" ], s2 .V ["foo" ])
244
+ }
245
+
246
+ func TestMapError (t * testing.T ) {
247
+ type S1 struct {
248
+ ID int
249
+ }
250
+ s1 := S1 {45 }
251
+ s2 := []string {}
252
+
253
+ err := Map (s1 , s2 )
254
+ if err == nil || err .Error () != "[]string is not addressable" {
255
+ t .Fatal (err )
256
+ }
257
+
258
+ err = Map (s1 , & s2 )
259
+ if err == nil || err .Error () != "Cannot map magic.S1 to *[]string" {
260
+ t .Fatal (err )
261
+ }
262
+ }
263
+
204
264
func TestConverter (t * testing.T ) {
205
265
now := time .Now ()
206
266
t5 := testType5 {45 , now }
@@ -210,6 +270,12 @@ func TestConverter(t *testing.T) {
210
270
assert (t , err , nil )
211
271
assert (t , t5 .ID , t6 .ID )
212
272
assert (t , t6 .Created , now .Unix ())
273
+
274
+ e := fmt .Errorf ("E" )
275
+ err = Map (t5 , & t6 , WithConverters (func (v1 , v2 reflect.Value ) (bool , error ) {
276
+ return false , e
277
+ }))
278
+ assert (t , err .Error (), "Created: E" )
213
279
}
214
280
215
281
func TestMapping (t * testing.T ) {
@@ -226,9 +292,3 @@ func TestMapping(t *testing.T) {
226
292
assert (t , err , nil )
227
293
assert (t , t1 .ID , t2 .UUID )
228
294
}
229
-
230
- func assert (t * testing.T , i1 , i2 interface {}) {
231
- if i1 != i2 {
232
- t .Fatalf ("%v != %v" , i1 , i2 )
233
- }
234
- }
0 commit comments