Browse Source

codecgen: CLI: always assume that vendoring is enabled.

When generating the transient go file for running codecgen,
assume that vendoring is enabled.

This is typically true in go 1.6 and always true in go 1.7+.
When it is not true, we will be ok. Someone shouldn't run
codecgen on a package with vendor/ in its path, if vendoring is
not being done.

Fixes #164
Ugorji Nwoke 9 years ago
parent
commit
5cd0f2b3b6
1 changed files with 11 additions and 0 deletions
  1. 11 0
      codec/codecgen/gen.go

+ 11 - 0
codec/codecgen/gen.go

@@ -20,6 +20,7 @@ import (
 	"path/filepath"
 	"path/filepath"
 	"regexp"
 	"regexp"
 	"strconv"
 	"strconv"
+	"strings"
 	"text/template"
 	"text/template"
 	"time"
 	"time"
 )
 )
@@ -135,6 +136,16 @@ func Generate(outfile, buildTag, codecPkgPath string, uid int64, useUnsafe bool,
 	if tv.ImportPath == tv.CodecImportPath {
 	if tv.ImportPath == tv.CodecImportPath {
 		tv.CodecPkgFiles = true
 		tv.CodecPkgFiles = true
 		tv.CodecPkgName = "codec"
 		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):]
+		}
 	}
 	}
 	astfiles := make([]*ast.File, len(infiles))
 	astfiles := make([]*ast.File, len(infiles))
 	for i, infile := range infiles {
 	for i, infile := range infiles {