-
-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathstruct_test.go
179 lines (149 loc) · 3.5 KB
/
struct_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
package structs
import (
"reflect"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestStruct_ToMap(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStruct_ToMap")
t.Run("invalid struct", func(t *testing.T) {
m, _ := ToMap(1)
var expected map[string]any
assert.Equal(expected, m)
})
t.Run("StructToMap", func(_ *testing.T) {
type People struct {
Name string `json:"name"`
age int
}
p := &People{
"test",
100,
}
pm, _ := ToMap(p)
var expected = map[string]any{"name": "test"}
assert.Equal(expected, pm)
})
t.Run("StructToMapWithJsonAttr", func(_ *testing.T) {
type People struct {
Name string `json:"name,omitempty"` // json tag with attribute
Phone string `json:"phone"` // json tag without attribute
Sex string `json:"-"` // ignore by "-"
Age int // ignore by no tag
email string // ignore by unexported
IsWorking bool `json:"is_working"`
}
p1 := People{
Name: "AAA", // exist
Phone: "1111",
Sex: "male",
Age: 100,
email: "11@gmail.com",
}
p1m, _ := ToMap(p1)
var expect1 = map[string]any{"name": "AAA", "phone": "1111", "is_working": false}
assert.Equal(expect1, p1m)
p2 := People{
Name: "",
Phone: "2222",
Sex: "male",
Age: 0,
email: "22@gmail.com",
IsWorking: true,
}
p2m, _ := ToMap(p2)
var expect2 = map[string]any{"phone": "2222", "is_working": true}
assert.Equal(expect2, p2m)
})
}
func Test_ToMap2(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStruct_ToMap")
type M struct {
Name string `json:"name"`
}
type S struct {
ID int `json:"id"`
M *M `json:"m"`
}
s1 := &S{
ID: 1,
}
var expect1 = map[string]any{"id": 1}
map1, err := ToMap(s1)
assert.IsNil(err)
assert.Equal(expect1, map1)
s2 := &S{
ID: 1,
M: &M{
Name: "test",
},
}
var expect2 = map[string]any{
"id": 1,
"m": map[string]any{
"name": "test",
}}
map2, err := ToMap(s2)
assert.IsNil(err)
assert.Equal(expect2, map2)
}
func TestStruct_Fields(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStruct_Fields")
type Parent struct {
A string `json:"a"`
B int `json:"b"`
C []string `json:"c"`
D map[string]any `json:"d"`
}
p1 := &Parent{
A: "1",
B: 11,
C: []string{"11", "22"},
D: map[string]any{"d1": 1, "d2": 2},
}
s := New(p1)
fields := s.Fields()
assert.Equal(4, len(fields))
assert.Equal(reflect.String, fields[0].Kind())
assert.Equal(reflect.Int, fields[1].Kind())
assert.Equal(reflect.Slice, fields[2].Kind())
assert.Equal(reflect.Map, fields[3].Kind())
}
func TestStruct_Field(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStruct_Field")
type Parent struct {
A string `json:"a"`
B int `json:"b"`
C []string `json:"c"`
D map[string]any `json:"d"`
}
p1 := &Parent{
A: "1",
B: 11,
C: []string{"11", "22"},
D: map[string]any{"d1": 1, "d2": 2},
}
s := New(p1)
a, ok := s.Field("A")
assert.Equal(true, ok)
assert.Equal(reflect.String, a.Kind())
assert.Equal("1", a.Value())
assert.Equal("a", a.tag.Name)
assert.Equal(false, a.tag.HasOption("omitempty"))
assert.Equal(false, a.tag.IsEmpty())
}
func TestStruct_IsStruct(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStruct_Field")
type Test1 struct{}
t1 := &Test1{}
t2 := 1
s1 := New(t1)
s2 := New(t2)
assert.Equal(true, s1.IsStruct())
assert.Equal(false, s2.IsStruct())
}