From 905d9cae73206f28441ceedb2449a3239181716f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Sat, 12 Mar 2016 17:24:51 +0000 Subject: [PATCH] Handle GO15VENDOREXPERIMENT properly across Go versions --- codec/gen.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/codec/gen.go b/codec/gen.go index 8544f04f..84a08a6f 100644 --- a/codec/gen.go +++ b/codec/gen.go @@ -15,6 +15,7 @@ import ( "os" "reflect" "regexp" + "runtime" "sort" "strconv" "strings" @@ -1626,17 +1627,34 @@ func (x *genV) MethodNamePfx(prefix string, prim bool) string { } -var genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") == "1" +var genCheckVendor bool + +func init() { + var major, minor int + if n, err := fmt.Sscanf(runtime.Version(), "go%d.%d", &major, &minor); err != nil || n != 2 { + return + } + + switch { + case major == 1 && minor == 5: + genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") == "1" + + case major == 1 && minor == 6: + genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") != "0" + + case major > 1, major == 1 && minor > 6: + genCheckVendor = true + } +} // 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, +// This handles the misbehaviour that occurs when vendoring is enabled, // where PkgPath returns the full path, including the vendoring pre-fix that should have been stripped. // We strip it here. func genImportPath(t reflect.Type) (s string) { s = t.PkgPath() if genCheckVendor { - // HACK: Misbehaviour occurs in go 1.5. May have to re-visit this later. // if s contains /vendor/ OR startsWith vendor/, then return everything after it. const vendorStart = "vendor/" const vendorInline = "/vendor/"