-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathconvert_test.go
263 lines (219 loc) · 5.88 KB
/
convert_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package structs_test
import (
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/testutil/assert"
)
func TestTryToMap(t *testing.T) {
mp, err := structs.TryToMap(nil)
assert.Empty(t, mp)
assert.NoErr(t, err)
type User struct {
Name string
Age int
city string
}
u := User{
Name: "inhere",
Age: 34,
city: "somewhere",
}
mp, err = structs.TryToMap(u)
assert.NoErr(t, err)
dump.P(mp)
assert.Contains(t, mp, "Name")
assert.Contains(t, mp, "Age")
assert.NotContains(t, mp, "city")
mp, err = structs.TryToMap(&u)
assert.NoErr(t, err)
assert.NotEmpty(t, mp)
// dump.P(mp)
mp = structs.MustToMap(&u)
assert.NotEmpty(t, mp)
// dump.P(mp)
// test to string map
smp := structs.MustToSMap(&u)
assert.NotEmpty(t, smp)
assert.ContainsKeys(t, smp, []string{"Name", "Age"})
smp = structs.ToSMap(&u)
assert.NotEmpty(t, smp)
assert.ContainsKeys(t, smp, []string{"Name", "Age"})
_, err = structs.TryToSMap("invalid")
assert.Err(t, err)
smp, err = structs.TryToSMap(&u)
assert.NoErr(t, err)
assert.NotEmpty(t, smp)
assert.ContainsKeys(t, smp, []string{"Name", "Age"})
assert.NotEmpty(t, structs.ToString(&u))
// test error
assert.Panics(t, func() {
structs.MustToMap("abc")
})
assert.Panics(t, func() {
structs.MustToSMap("abc")
})
}
func TestToMap_useTag(t *testing.T) {
type User1 struct {
Name string `json:"name"`
Age int `json:"age"`
city string
}
u1 := &User1{
Name: "inhere",
Age: 34,
city: "somewhere",
}
mp := structs.ToMap(u1)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age"})
assert.NotContains(t, mp, "city")
// export unexported field
mp = structs.MustToMap(u1, structs.ExportPrivate)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "city"})
}
type Extra struct {
City string `json:"city"`
Github string `json:"github"`
}
type Extra1 struct {
ExtraSub
City string `json:"city"`
Github string `json:"github"`
}
type ExtraSub struct {
ESubKey string `json:"e_sub_key"`
}
type User struct {
Extra `json:"extra"`
Name string `json:"name"`
Age int `json:"age"`
}
type User1 struct {
Extra
Name string `json:"name"`
Age int `json:"age"`
}
type User2 struct {
Extra1
Name string `json:"name"`
Age int `json:"age"`
}
func TestToMap_nestStruct(t *testing.T) {
e := Extra{
City: "chengdu",
Github: "https://github.com/inhere",
}
u := &User{
Name: "inhere",
Age: 30,
Extra: e,
}
mp := structs.MustToMap(u)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "extra"})
assert.ContainsKeys(t, mp["extra"], []string{"city", "github"})
// use pointer
type UserPtrSub struct {
*Extra `json:"extra"`
Name string `json:"name"`
Age int `json:"age"`
}
u2 := &UserPtrSub{
Name: "inhere",
Age: 30,
Extra: &e,
}
mp = structs.MustToMap(u2)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "extra"})
assert.ContainsKeys(t, mp["extra"], []string{"city", "github"})
}
func TestToMap_anonymousStruct(t *testing.T) {
u := &User1{
Name: "inhere",
Age: 30,
Extra: Extra{
City: "chengdu",
Github: "https://github.com/inhere",
},
}
mp := structs.MustToMap(u, structs.MergeAnonymous)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "city", "github"})
assert.NotContainsKey(t, mp, "extra")
assert.NotContainsKeys(t, mp, []string{"extra"})
u2 := &User2{
Name: "inhere",
Age: 30,
Extra1: Extra1{
ExtraSub: ExtraSub{
ESubKey: "sub key",
},
City: "chengdu",
Github: "https://github.com/inhere",
},
}
mp = structs.MustToMap(u2, structs.MergeAnonymous)
dump.P(mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "city", "github", "e_sub_key"})
assert.NotContainsKey(t, mp, "extra")
}
func TestTryToMap_customTag(t *testing.T) {
type User struct {
Name string `export:"name"`
Age int `export:"age"`
FullName string `export:"full_name"`
}
u1 := User{
Name: "inhere",
Age: 34,
FullName: "inhere xyz",
}
mp, err := structs.TryToMap(u1, structs.WithMapTagName("export"))
assert.NoErr(t, err)
assert.NotEmpty(t, mp)
assert.ContainsKeys(t, mp, []string{"name", "age", "full_name"})
}
// https://github.com/gookit/goutil/issues/192
func TestIssue192(t *testing.T) {
// go code
type Structure struct {
// I don't want to expose too many infos, but here it goes only a set of float64 numbers, without any complex type or pointers
}
type MyStruct struct {
ID string `json:"id" gorm:"column:id"`
RefId int `json:"ref_id" gorm:"column:ref_id"`
PhotoURL string `json:"photoURL" gorm:"column:photo_url"`
Year int `json:"year" gorm:"column:year"`
SomeDate string `json:"someDate" gorm:"column:some_date"`
Trim string `json:"trim" gorm:"column:trim"`
Type string `json:"type" gorm:"column:type"`
BodyType string `json:"bodyType" gorm:"column:body_type"`
Counter int `json:"counter" gorm:"column:counter"`
Used bool `json:"used" gorm:"column:used"`
Price float64 `json:"price" gorm:"column:price"`
Value float64 `json:"value" gorm:"column:value"`
Amount float64 `json:"amount" gorm:"-:all"`
MaxAmount float64 `json:"maxAmount" gorm:"column:max_amount"`
StockNo string `json:"stockNo" gorm:"column:stock_no"`
ExpiresAt *string `json:"expiresAt" gorm:"column:expires_at"`
UpdatedBy *string `json:"updatedBy" gorm:"column:updated_by"`
Location string `json:"location" gorm:"column:location"`
IsFavorite bool `json:"is_favorite,omitempty" gorm:"-"`
Structure *Structure `json:"structure,omitempty" gorm:"-:all"`
}
data := MyStruct{
ID: "ABC123",
RefId: 1,
Year: 1990,
SomeDate: "2020-01-03 15:02:15",
Counter: 9001,
Price: 20,
}
toMap, err := structs.TryToMap(data) // <- it panics!
assert.NoErr(t, err)
dump.P(toMap)
}