Skip to content

Commit b1e19fb

Browse files
author
Dean Karn
committed
Add new default type settign tests
1 parent 8aec923 commit b1e19fb

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

modifiers/multi_test.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,202 @@ import (
88
. "github.com/go-playground/assert/v2"
99
)
1010

11+
func TestDefaultSetSpecialTypes(t *testing.T) {
12+
conform := New()
13+
14+
tests := []struct {
15+
name string
16+
field interface{}
17+
tags string
18+
vf func(field interface{})
19+
expectError bool
20+
}{
21+
{
22+
name: "default map",
23+
field: (map[string]struct{})(nil),
24+
tags: "default",
25+
vf: func(field interface{}) {
26+
m := field.(map[string]struct{})
27+
Equal(t, len(m), 0)
28+
},
29+
},
30+
{
31+
name: "default map with size",
32+
field: (map[string]struct{})(nil),
33+
tags: "default=5",
34+
vf: func(field interface{}) {
35+
m := field.(map[string]struct{})
36+
Equal(t, len(m), 0)
37+
},
38+
},
39+
{
40+
name: "set map with size",
41+
field: (map[string]struct{})(nil),
42+
tags: "set=5",
43+
vf: func(field interface{}) {
44+
m := field.(map[string]struct{})
45+
Equal(t, len(m), 0)
46+
},
47+
},
48+
{
49+
name: "default slice",
50+
field: ([]string)(nil),
51+
tags: "default",
52+
vf: func(field interface{}) {
53+
m := field.([]string)
54+
Equal(t, len(m), 0)
55+
Equal(t, cap(m), 0)
56+
},
57+
},
58+
{
59+
name: "default slice with capacity",
60+
field: ([]string)(nil),
61+
tags: "default=5",
62+
vf: func(field interface{}) {
63+
m := field.([]string)
64+
Equal(t, len(m), 0)
65+
Equal(t, cap(m), 5)
66+
},
67+
},
68+
{
69+
name: "set slice",
70+
field: ([]string)(nil),
71+
tags: "set",
72+
vf: func(field interface{}) {
73+
m := field.([]string)
74+
Equal(t, len(m), 0)
75+
Equal(t, cap(m), 0)
76+
},
77+
},
78+
{
79+
name: "set slice with capacity",
80+
field: ([]string)(nil),
81+
tags: "set=5",
82+
vf: func(field interface{}) {
83+
m := field.([]string)
84+
Equal(t, len(m), 0)
85+
Equal(t, cap(m), 5)
86+
},
87+
},
88+
{
89+
name: "default chan",
90+
field: (chan struct{})(nil),
91+
tags: "default",
92+
vf: func(field interface{}) {
93+
m := field.(chan struct{})
94+
Equal(t, len(m), 0)
95+
Equal(t, cap(m), 0)
96+
},
97+
},
98+
{
99+
name: "default chan with buffer",
100+
field: (chan struct{})(nil),
101+
tags: "default=5",
102+
vf: func(field interface{}) {
103+
m := field.(chan struct{})
104+
Equal(t, len(m), 0)
105+
Equal(t, cap(m), 5)
106+
},
107+
},
108+
{
109+
name: "default time.Time",
110+
field: time.Time{},
111+
tags: "default",
112+
vf: func(field interface{}) {
113+
m := field.(time.Time)
114+
Equal(t, m.Location(), time.Local)
115+
},
116+
},
117+
{
118+
name: "default time.Time utc",
119+
field: time.Time{},
120+
tags: "default=utc",
121+
vf: func(field interface{}) {
122+
m := field.(time.Time)
123+
Equal(t, m.Location(), time.UTC)
124+
},
125+
},
126+
{
127+
name: "default time.Time to value",
128+
field: time.Time{},
129+
tags: "default=2023-05-28T15:50:31Z",
130+
vf: func(field interface{}) {
131+
m := field.(time.Time)
132+
Equal(t, m.Location(), time.UTC)
133+
134+
tm, err := time.Parse(time.RFC3339Nano, "2023-05-28T15:50:31Z")
135+
Equal(t, err, nil)
136+
Equal(t, tm.Equal(m), true)
137+
138+
},
139+
},
140+
{
141+
name: "set time.Time",
142+
field: time.Time{},
143+
tags: "set",
144+
vf: func(field interface{}) {
145+
m := field.(time.Time)
146+
Equal(t, m.Location(), time.Local)
147+
},
148+
},
149+
{
150+
name: "set time.Time utc",
151+
field: time.Time{},
152+
tags: "set=utc",
153+
vf: func(field interface{}) {
154+
m := field.(time.Time)
155+
Equal(t, m.Location(), time.UTC)
156+
},
157+
},
158+
{
159+
name: "set time.Time to value",
160+
field: time.Time{},
161+
tags: "set=2023-05-28T15:50:31Z",
162+
vf: func(field interface{}) {
163+
m := field.(time.Time)
164+
Equal(t, m.Location(), time.UTC)
165+
166+
tm, err := time.Parse(time.RFC3339Nano, "2023-05-28T15:50:31Z")
167+
Equal(t, err, nil)
168+
Equal(t, tm.Equal(m), true)
169+
170+
},
171+
},
172+
{
173+
name: "default pointer to slice",
174+
field: (*[]string)(nil),
175+
tags: "default",
176+
vf: func(field interface{}) {
177+
m := field.([]string)
178+
Equal(t, len(m), 0)
179+
},
180+
},
181+
{
182+
name: "set pointer to slice",
183+
field: (*[]string)(nil),
184+
tags: "set",
185+
vf: func(field interface{}) {
186+
m := field.([]string)
187+
Equal(t, len(m), 0)
188+
},
189+
},
190+
}
191+
192+
for _, tc := range tests {
193+
tc := tc
194+
t.Run(tc.name, func(t *testing.T) {
195+
t.Parallel()
196+
err := conform.Field(context.Background(), &tc.field, tc.tags)
197+
if tc.expectError {
198+
NotEqual(t, err, nil)
199+
return
200+
}
201+
Equal(t, err, nil)
202+
tc.vf(tc.field)
203+
})
204+
}
205+
}
206+
11207
func TestSet(t *testing.T) {
12208

13209
type State int

mold.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ func (t *Transformer) setByField(ctx context.Context, orig reflect.Value, ct *cT
239239
err = t.setByIterable(ctx, current, ct)
240240
case reflect.Map:
241241
err = t.setByMap(ctx, current, ct)
242+
case reflect.Ptr:
243+
innerKind := current.Type().Elem().Kind()
244+
if innerKind == reflect.Slice || innerKind == reflect.Map {
245+
// is a nil pointer to a slice or map, nothing to do.
246+
return nil
247+
}
248+
// not a valid use of the dive tag
249+
fallthrough
242250
default:
243251
err = ErrInvalidDive
244252
}

0 commit comments

Comments
 (0)