Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle GO15VENDOREXPERIMENT properly across Go versions #148

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions codec/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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/"
Expand Down