Skip to content

Commit 58d5b87

Browse files
committed
Fixed some null/nil values not being decoded properly. Fixes #69
1 parent 72bc83c commit 58d5b87

File tree

3 files changed

+35
-27
lines changed

3 files changed

+35
-27
lines changed

encoding/decoder.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,33 @@
33
package encoding
44

55
import (
6-
"errors"
6+
7+
// "errors"
78
"reflect"
8-
"runtime"
9+
// "runtime"
910
"strconv"
1011
"strings"
1112
)
1213

1314
// Decode decodes map[string]interface{} into a struct. The first parameter
1415
// must be a pointer.
1516
func Decode(dst interface{}, src interface{}) (err error) {
16-
defer func() {
17-
if r := recover(); r != nil {
18-
if _, ok := r.(runtime.Error); ok {
19-
panic(r)
20-
}
21-
if v, ok := r.(string); ok {
22-
err = errors.New(v)
23-
} else {
24-
err = r.(error)
25-
}
26-
}
27-
}()
17+
// defer func() {
18+
// if r := recover(); r != nil {
19+
// if _, ok := r.(runtime.Error); ok {
20+
// panic(r)
21+
// }
22+
// if v, ok := r.(string); ok {
23+
// err = errors.New(v)
24+
// } else {
25+
// err = r.(error)
26+
// }
27+
// }
28+
// }()
2829

2930
dv := reflect.ValueOf(dst)
3031
sv := reflect.ValueOf(src)
32+
3133
if dv.Kind() != reflect.Ptr || dv.IsNil() {
3234
return &InvalidDecodeError{reflect.TypeOf(dst)}
3335
}
@@ -419,6 +421,10 @@ func decodeObjectInterface(s *decodeState, sv reflect.Value) map[string]interfac
419421

420422
// decodeLiteralInterface returns the interface of the source value
421423
func decodeLiteralInterface(s *decodeState, sv reflect.Value) interface{} {
424+
if !sv.IsValid() {
425+
return nil
426+
}
427+
422428
return sv.Interface()
423429
}
424430

results.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ func (r *ResultRows) Next() bool {
104104
}
105105

106106
r.current = row
107-
if row != nil {
108-
return true
109-
}
107+
return true
110108
}
111109

112110
// Check if all rows have been loaded

results_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,23 @@ func (s *RethinkSuite) TestRowsScanSlice(c *test.C) {
3636
}
3737

3838
func (s *RethinkSuite) TestRowsPartiallyNilSlice(c *test.C) {
39-
row, err := Expr([]interface{}{
40-
map[string]interface{}{"num": 1},
41-
map[string]interface{}{"num": 2},
42-
nil,
43-
nil,
44-
map[string]interface{}{"num": 5},
45-
}).Run(sess)
39+
row, err := Expr(map[string]interface{}{
40+
"item": []interface{}{
41+
map[string]interface{}{"num": 1},
42+
nil,
43+
},
44+
}).RunRow(sess)
4645
c.Assert(err, test.IsNil)
4746

48-
var response []interface{}
49-
err = row.ScanAll(&response)
47+
var response map[string]interface{}
48+
err = row.Scan(&response)
5049
c.Assert(err, test.IsNil)
51-
c.Assert(response, JsonEquals, []interface{}{1, 2, nil, nil, 5})
50+
c.Assert(response, JsonEquals, map[string]interface{}{
51+
"item": []interface{}{
52+
map[string]interface{}{"num": 1},
53+
nil,
54+
},
55+
})
5256
}
5357

5458
func (s *RethinkSuite) TestRowsScanMap(c *test.C) {

0 commit comments

Comments
 (0)