Browse Source

codec: use conditional build tags for reflect.ArrayOf support.

Consequently, Now, PreferArrayOverSlice is only applicable from go 1.5 and beyond.

Updates #113
Ugorji Nwoke 9 years ago
parent
commit
faddd6128c
3 changed files with 35 additions and 4 deletions
  1. 5 4
      codec/decode.go
  2. 16 0
      codec/decode_go.go
  3. 14 0
      codec/decode_go14.go

+ 5 - 4
codec/decode.go

@@ -168,6 +168,9 @@ type DecodeOptions struct {
 	//
 	//
 	// This only impacts decoding into a nil interface{}.
 	// This only impacts decoding into a nil interface{}.
 	// Consequently, it has no effect on codecgen.
 	// Consequently, it has no effect on codecgen.
+	//
+	// *Note*: This only applies if using go1.5 and above,
+	// as it requires reflect.ArrayOf support which was absent before go1.5.
 	PreferArrayOverSlice bool
 	PreferArrayOverSlice bool
 }
 }
 
 
@@ -619,10 +622,8 @@ func (f *decFnInfo) kInterfaceNaked() (rvn reflect.Value) {
 			d.decode(v2)
 			d.decode(v2)
 			n.ss = n.ss[:l]
 			n.ss = n.ss[:l]
 			rvn = reflect.ValueOf(v2).Elem()
 			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
+			if reflectArrayOfSupported && d.stid == 0 && d.h.PreferArrayOverSlice {
+				rvn = reflectArrayOf(rvn)
 			}
 			}
 		} else {
 		} else {
 			rvn = reflect.New(d.h.SliceType).Elem()
 			rvn = reflect.New(d.h.SliceType).Elem()

+ 16 - 0
codec/decode_go.go

@@ -0,0 +1,16 @@
+// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
+// Use of this source code is governed by a MIT license found in the LICENSE file.
+
+// +build go1.5
+
+package codec
+
+import "reflect"
+
+const reflectArrayOfSupported = true
+
+func reflectArrayOf(rvn reflect.Value) (rvn2 reflect.Value) {
+	rvn2 = reflect.New(reflect.ArrayOf(rvn.Len(), intfTyp)).Elem()
+	reflect.Copy(rvn2, rvn)
+	return
+}

+ 14 - 0
codec/decode_go14.go

@@ -0,0 +1,14 @@
+// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
+// Use of this source code is governed by a MIT license found in the LICENSE file.
+
+// +build !go1.5
+
+package codec
+
+import "reflect"
+
+const reflectArrayOfSupported = false
+
+func reflectArrayOf(rvn reflect.Value) (rvn2 reflect.Value) {
+	panic("reflect.ArrayOf unsupported")
+}