소스 검색

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 년 전
부모
커밋
5cd0f2b3b6
1개의 변경된 파일11개의 추가작업 그리고 0개의 파일을 삭제
  1. 11 0
      codec/codecgen/gen.go

+ 11 - 0
codec/codecgen/gen.go

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