Browse Source

codec: codecgen: gen.go: move stripVendor to a function that is kept in sync

Ugorji Nwoke 8 years ago
parent
commit
0053ebfd9d
2 changed files with 30 additions and 17 deletions
  1. 15 8
      codec/codecgen/gen.go
  2. 15 9
      codec/gen.go

+ 15 - 8
codec/codecgen/gen.go

@@ -138,14 +138,7 @@ func Generate(outfile, buildTag, codecPkgPath string, uid int64, useUnsafe bool,
 		tv.CodecPkgName = "codec"
 	} else {
 		// HACK: always handle vendoring. It should be typically on in go 1.6, 1.7
-		s := tv.ImportPath
-		const vendorStart = "vendor/"
-		const vendorInline = "/vendor/"
-		if i := strings.LastIndex(s, vendorInline); i >= 0 {
-			tv.ImportPath = s[i+len(vendorInline):]
-		} else if strings.HasPrefix(s, vendorStart) {
-			tv.ImportPath = s[len(vendorStart):]
-		}
+		tv.ImportPath = stripVendor(tv.ImportPath)
 	}
 	astfiles := make([]*ast.File, len(infiles))
 	for i, infile := range infiles {
@@ -297,6 +290,20 @@ func gen1(frunName, tmplStr string, tv interface{}) (frun *os.File, err error) {
 	return
 }
 
+// copied from ../gen.go (keep in sync).
+func stripVendor(s string) string {
+	// 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/"
+	if i := strings.LastIndex(s, vendorInline); i >= 0 {
+		s = s[i+len(vendorInline):]
+	} else if strings.HasPrefix(s, vendorStart) {
+		s = s[len(vendorStart):]
+	}
+	return s
+}
+
 func main() {
 	o := flag.String("o", "", "out file")
 	c := flag.String("c", genCodecPath, "codec path")

+ 15 - 9
codec/gen.go

@@ -1653,15 +1653,8 @@ func (x *genV) MethodNamePfx(prefix string, prim bool) string {
 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/"
-		if i := strings.LastIndex(s, vendorInline); i >= 0 {
-			s = s[i+len(vendorInline):]
-		} else if strings.HasPrefix(s, vendorStart) {
-			s = s[len(vendorStart):]
-		}
+		// HACK: always handle vendoring. It should be typically on in go 1.6, 1.7
+		s = stripVendor(s)
 	}
 	return
 }
@@ -1884,6 +1877,19 @@ func genInternalSortType(s string, elem bool) string {
 	panic("sorttype: unexpected type: " + s)
 }
 
+func stripVendor(s string) string {
+	// 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/"
+	if i := strings.LastIndex(s, vendorInline); i >= 0 {
+		s = s[i+len(vendorInline):]
+	} else if strings.HasPrefix(s, vendorStart) {
+		s = s[len(vendorStart):]
+	}
+	return s
+}
+
 // var genInternalMu sync.Mutex
 var genInternalV genInternal
 var genInternalTmplFuncs template.FuncMap