-
Notifications
You must be signed in to change notification settings - Fork 29
/
struct_helper_test.go
222 lines (185 loc) · 4.79 KB
/
struct_helper_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
package toolbox_test
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
)
func TestProcessStruct(t *testing.T) {
type Super struct {
Parent int
}
type User struct {
*Super
Name string `column:"name"`
DateOfBirth time.Time `column:"date" dateFormat:"2006-01-02 15:04:05.000000"`
Id int `autogenrated:"true"`
prv int
Other string `transient:"true"`
}
user := User{Id: 1, Other: "!@#", Name: "foo", Super: &Super{12}}
var userMap = make(map[string]interface{})
err := toolbox.ProcessStruct(&user, func(fieldType reflect.StructField, field reflect.Value) error {
value := field.Interface()
userMap[fieldType.Name] = value
return nil
})
assert.Nil(t, err)
assert.Equal(t, 5, len(userMap))
assert.Equal(t, 12, userMap["Parent"])
assert.Equal(t, 1, userMap["Id"])
assert.Equal(t, "!@#", userMap["Other"])
}
func TestBuildTagMapping(t *testing.T) {
type User struct {
Name string `column:"name"`
DateOfBirth time.Time `column:"date" dateFormat:"2006-01-02 15:04:05.000000"`
Id int `autogenrated:"true"`
Other string `transient:"true"`
}
{
tags := []string{"column", "autogenrated"}
result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", true, true, tags)
{
actual := len(result)
expected := 3
assert.Equal(t, actual, expected, "Extract mapping count")
}
{
actual, _ := result["name"]["fieldName"]
expected := "Name"
assert.Equal(t, actual, expected, "Extract name mapping")
}
{
actual, _ := result["id"]["autogenrated"]
expected := "true"
assert.Equal(t, actual, expected, "Extract id flaged as autogenerated")
}
}
{
tags := []string{"column", "autogenrated"}
result := toolbox.BuildTagMapping((*User)(nil), "fieldName", "transient", true, false, tags)
actual, _ := result["Name"]["fieldName"]
expected := "Name"
assert.Equal(t, actual, expected, "Extract name mapping")
}
{
tags := []string{"column", "autogenrated"}
result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", false, false, tags)
{
actual := len(result)
expected := 2
assert.Equal(t, actual, expected, "Extract mapping count")
}
}
type User2 struct {
Name string `json:"name" column:"name"`
DateOfBirth time.Time `json:"date" column:"date" dateFormat:"2006-01-02 15:04:05.000000"`
Id int `json:"id" autogenrated:"true"`
Other string `json:"other" transient:"true"`
}
{
tags := []string{"column", "autogenrated"}
result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", true, true, tags)
{
actual := len(result)
expected := 3
assert.Equal(t, actual, expected, "Extract mapping count")
}
{
actual, _ := result["name"]["fieldName"]
expected := "Name"
assert.Equal(t, actual, expected, "Extract name mapping")
}
{
actual, _ := result["id"]["autogenrated"]
expected := "true"
assert.Equal(t, actual, expected, "Extract id flaged as autogenerated")
}
}
}
func TestBuildEmbededStructTagMapping(t *testing.T) {
type Super struct {
Id int `autogenrated:"true"`
Name string `column:"name"`
}
type User struct {
*Super
DateOfBirth time.Time `column:"date" dateFormat:"2006-01-02 15:04:05.000000"`
Other string `transient:"true"`
}
tags := []string{"column", "autogenrated"}
result := toolbox.BuildTagMapping((*User)(nil), "column", "transient", true, true, tags)
{
actual := len(result)
expected := 3
assert.Equal(t, actual, expected, "Extract mapping count")
}
{
actual, _ := result["name"]["fieldName"]
expected := "Name"
assert.Equal(t, actual, expected, "Extract name mapping")
}
{
actual, _ := result["id"]["autogenrated"]
expected := "true"
assert.Equal(t, actual, expected, "Extract id flaged as autogenerated")
}
}
type Type4 struct {
Id int
}
type Type3 struct {
Name map[string]string
Type4 map[string]*Type4
}
type Type2 struct {
F1 int
F3 *Type3
}
type Type1 struct {
F1 int
F2 *Type2
F3 []interface{}
F4 map[string]interface{}
F5 []*Type3
}
type SuperType1 struct {
*Type1
}
type SuperType2 struct {
Type1
}
func Test_InitStruct(t *testing.T) {
{
var t1 = &Type1{}
toolbox.InitStruct(t1)
assert.NotNil(t, t1.F2)
assert.NotNil(t, t1.F3)
assert.NotNil(t, t1.F4)
assert.NotNil(t, t1.F5)
}
{
var t1 = &SuperType1{}
toolbox.InitStruct(t1)
assert.NotNil(t, t1.F2)
assert.NotNil(t, t1.F3)
assert.NotNil(t, t1.F4)
assert.NotNil(t, t1.F5)
}
{
var t1 = &SuperType2{}
toolbox.InitStruct(t1)
assert.NotNil(t, t1.F2)
assert.NotNil(t, t1.F3)
assert.NotNil(t, t1.F4)
assert.NotNil(t, t1.F5)
}
}
func Test_GetStructMeta(t *testing.T) {
var t1 = &Type1{}
toolbox.InitStruct(t1)
meta := toolbox.GetStructMeta(t1)
assert.NotNil(t, meta)
}