From 907292679fff3dbc38f9bb2361f47953ed7aea75 Mon Sep 17 00:00:00 2001 From: Ugorji Nwoke Date: Mon, 14 Mar 2016 13:17:57 -0400 Subject: [PATCH] codecgen: Use build tags to determine whether vendoring is enabled or not. Defaults genCheckVendor to false. For 1.5 and 1.6, set genCheckVendor appropriately using the environmental variable GO15VENDOREXPERIMENT. Also, support "interface {}" as synonym for "interface{}" to accomodate some changes seen in tip. Fixes #147 --- codec/gen.go | 8 +++----- codec/gen_15.go | 12 ++++++++++++ codec/gen_16.go | 12 ++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 codec/gen_15.go create mode 100644 codec/gen_16.go diff --git a/codec/gen.go b/codec/gen.go index 8544f04f..ac8cc6dd 100644 --- a/codec/gen.go +++ b/codec/gen.go @@ -12,7 +12,6 @@ import ( "io" "io/ioutil" "math/rand" - "os" "reflect" "regexp" "sort" @@ -126,6 +125,7 @@ var ( genExpectArrayOrMapErr = errors.New("unexpected type. Expecting array/map/slice") genBase64enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__") genQNameRegex = regexp.MustCompile(`[A-Za-z_.]+`) + genCheckVendor bool ) // genRunner holds some state used during a Gen run. @@ -1626,8 +1626,6 @@ func (x *genV) MethodNamePfx(prefix string, prim bool) string { } -var genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") == "1" - // genImportPath returns import path of a non-predeclared named typed, or an empty string otherwise. // // This handles the misbehaviour that occurs when 1.5-style vendoring is enabled, @@ -1678,7 +1676,7 @@ func genNonPtr(t reflect.Type) reflect.Type { func genTitleCaseName(s string) string { switch s { - case "interface{}": + case "interface{}", "interface {}": return "Intf" default: return strings.ToUpper(s[0:1]) + s[1:] @@ -1781,7 +1779,7 @@ func (x genInternal) FastpathLen() (l int) { func genInternalZeroValue(s string) string { switch s { - case "interface{}": + case "interface{}", "interface {}": return "nil" case "bool": return "false" diff --git a/codec/gen_15.go b/codec/gen_15.go new file mode 100644 index 00000000..ab76c310 --- /dev/null +++ b/codec/gen_15.go @@ -0,0 +1,12 @@ +// 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,!go1.6 + +package codec + +import "os" + +func init() { + genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") == "1" +} diff --git a/codec/gen_16.go b/codec/gen_16.go new file mode 100644 index 00000000..87c04e2e --- /dev/null +++ b/codec/gen_16.go @@ -0,0 +1,12 @@ +// 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.6 + +package codec + +import "os" + +func init() { + genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") != "0" +}