From 5099b68ed379e9b07d62052b23e326a7e95dbf75 Mon Sep 17 00:00:00 2001 From: Ugorji Nwoke Date: Tue, 20 Sep 2016 21:51:53 -0400 Subject: [PATCH] codec: PreferArrayOverSlice support 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 --- codec/decode.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/codec/decode.go b/codec/decode.go index 8b7a1b0d..6c66842a 100644 --- a/codec/decode.go +++ b/codec/decode.go @@ -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 } // ------------------------------------ @@ -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)