-
Notifications
You must be signed in to change notification settings - Fork 0
/
maptostruct_test.go
93 lines (75 loc) · 1.67 KB
/
maptostruct_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
package maptostruct
import (
"reflect"
"testing"
"time"
)
type StructTest struct {
Foo string `json:"foo"`
}
type MapToStructTest struct {
String string `json:"string"`
Int int `json:"int"`
Float float64 `json:"float"`
Array []int `json:"array"`
Bool bool `json:"bool"`
TimeT time.Time `json:"time_time"`
TimeD time.Duration `json:"time_duration"`
Struct StructTest `json:"struct"`
Map map[string]interface{} `json:"map"`
}
func TestMapToStruct(t *testing.T) {
data := make(map[string]interface{})
mpTest := make(map[string]interface{})
mpTest["foo"] = "bar"
data["string"] = "foo"
data["int"] = 1
data["float"] = 0.300004
data["array"] = []int{1, 2, 3}
data["bool"] = true
data["time_time"] = time.Now()
data["time_duration"] = time.Since(time.Unix(1600000000, 0))
data["struct"] = StructTest{ Foo: "Bar" }
data["map"] = mpTest
var out MapToStructTest
if err := MapToStruct(data, &out); err != nil {
t.Error(err)
t.Fail()
}
if out.String != data["string"] {
t.Error("fail on string")
t.Fail()
}
if out.Int != data["int"] {
t.Error("fail on int")
t.Fail()
}
if out.Float != data["float"] {
t.Error("fail on float")
t.Fail()
}
if !reflect.DeepEqual(out.Array, data["array"]) {
t.Error("fail on array")
t.Fail()
}
if out.Bool != data["bool"] {
t.Error("fail on bool")
t.Fail()
}
if !out.TimeT.Equal(data["time_time"].(time.Time)) {
t.Error("fail on time.Time")
t.Fail()
}
if out.TimeD != data["time_duration"] {
t.Error("fail on time.Duration")
t.Fail()
}
if out.Struct != data["struct"] {
t.Error("fail on struct")
t.Fail()
}
if !reflect.DeepEqual(out.Map, data["map"]) {
t.Error("fail on map")
t.Fail()
}
}