Skip to content

Commit ae3bce1

Browse files
joeybloggsjoeybloggs
authored andcommitted
add interface tests + code cleanup
1 parent 30ce335 commit ae3bce1

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

decoder.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ func (d *decoder) parseMapData() {
139139

140140
func (d *decoder) traverseStruct(v reflect.Value, typ reflect.Type, namespace []byte) (set bool) {
141141

142-
// typ := v.Type()
143142
l := len(namespace)
144143
first := l == 0
145144

@@ -195,7 +194,6 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
195194
}
196195
}
197196

198-
// fmt.Println("KIND:", kind)
199197
switch kind {
200198
case reflect.Interface, reflect.Invalid:
201199
return
@@ -546,7 +544,7 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
546544
case reflect.Struct:
547545

548546
typ := v.Type()
549-
// fmt.Println("Type:", typ)
547+
550548
// if we get here then no custom time function declared so use RFC3339 by default
551549
if typ == timeType {
552550

decoder_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,3 +1336,46 @@ func TestDecoderIncreasingKeys(t *testing.T) {
13361336
Equal(t, test2.Array[2], int(2))
13371337
Equal(t, test2.Array[10], int(10))
13381338
}
1339+
1340+
func TestDecoderInterface(t *testing.T) {
1341+
1342+
var iface interface{}
1343+
1344+
d := NewDecoder()
1345+
1346+
values := map[string][]string{
1347+
"": {"2"},
1348+
}
1349+
1350+
var i int
1351+
1352+
iface = &i
1353+
1354+
err := d.Decode(iface, values)
1355+
Equal(t, err, nil)
1356+
Equal(t, i, 2)
1357+
1358+
iface = i
1359+
1360+
PanicMatches(t, func() { d.Decode(iface, values) }, "interface must be a pointer")
1361+
1362+
values = map[string][]string{
1363+
"Value": {"testVal"},
1364+
}
1365+
1366+
type test struct {
1367+
Value string
1368+
}
1369+
1370+
var tst test
1371+
1372+
iface = &tst
1373+
1374+
err = d.Decode(iface, values)
1375+
Equal(t, err, nil)
1376+
Equal(t, tst.Value, "testVal")
1377+
1378+
iface = tst
1379+
1380+
PanicMatches(t, func() { d.Decode(iface, values) }, "interface must be a pointer")
1381+
}

0 commit comments

Comments
 (0)