Skip to content

Commit

Permalink
codec: PreferArrayOverSlice support
Browse files Browse the repository at this point in the history
This controls whether we decode into a nil interface{} as an array or a slice.

As this is decoding into a nil interface{}, it only takes effect
during runtime reflection (not codecgen).

Fixes #170
  • Loading branch information
ugorji committed Sep 21, 2016
1 parent 98ef79d commit 5099b68
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion codec/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ type DecodeOptions struct {
// Note: Handles will be smart when using the intern functionality.
// So everything will not be interned.
InternString bool

// PreferArrayOverSlice controls whether to decode to an array or a slice.
//
// This only impacts decoding into a nil interface{}.
// Consequently, it has no effect on codecgen.
PreferArrayOverSlice bool
}

// ------------------------------------
Expand Down Expand Up @@ -611,8 +617,13 @@ func (f *decFnInfo) kInterfaceNaked() (rvn reflect.Value) {
n.ss = append(n.ss, nil)
var v2 interface{} = &n.ss[l]
d.decode(v2)
rvn = reflect.ValueOf(v2).Elem()
n.ss = n.ss[:l]
rvn = reflect.ValueOf(v2).Elem()
if d.stid == 0 && d.h.PreferArrayOverSlice {
rvn2 := reflect.New(reflect.ArrayOf(rvn.Len(), intfTyp)).Elem()
reflect.Copy(rvn2, rvn)
rvn = rvn2
}
} else {
rvn = reflect.New(d.h.SliceType).Elem()
d.decodeValue(rvn, nil)
Expand Down

0 comments on commit 5099b68

Please sign in to comment.