Skip to content

Commit 8537f80

Browse files
author
Miguel Molina
authored
Merge pull request #138 from erizocosmico/fix/gopath
allow usage of multiple gopaths
2 parents 1f10e8f + b0b0b26 commit 8537f80

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ addons:
1313
postgresql: "9.4"
1414

1515
env:
16-
- DBNAME=kallax_test DBUSER=postgres DBPASS=''
16+
- DBNAME=kallax_test DBUSER=postgres DBPASS='' GOPATH=/tmp/whatever:$GOPATH
1717

1818
services:
1919
- postgresql

generator/processor.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"go/parser"
88
"go/token"
99
"go/types"
10-
"os"
1110
"path/filepath"
1211
"reflect"
1312
"strings"
@@ -459,16 +458,19 @@ func joinDirectory(directory string, files []string) []string {
459458
return result
460459
}
461460

462-
var goPath = os.Getenv("GOPATH")
463-
464461
func typeName(typ types.Type) string {
465462
return removeGoPath(typ.String())
466463
}
467464

468465
func removeGoPath(path string) string {
469-
importPath := filepath.ToSlash(goPath + "/src/")
470466
path = filepath.ToSlash(path)
471-
return strings.Replace(path, importPath, "", -1)
467+
for _, p := range parseutil.DefaultGoPath {
468+
p = filepath.ToSlash(p + "/src/")
469+
if strings.HasPrefix(path, p) {
470+
return strings.Replace(path, p, "", -1)
471+
}
472+
}
473+
return path
472474
}
473475

474476
func isIgnoredField(s *types.Struct, idx int) bool {

generator/processor_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"go/parser"
66
"go/token"
77
"go/types"
8-
"path/filepath"
98
"reflect"
109
"testing"
1110

@@ -340,7 +339,7 @@ func (s *ProcessorSuite) processFixture(source string) *Package {
340339
}
341340

342341
func (s *ProcessorSuite) TestDo() {
343-
p := NewProcessor(filepath.Join(goPath, "src", "gopkg.in/src-d/go-kallax.v1"), []string{"README.md"})
342+
p := NewProcessor(pkgAbsPath, []string{"README.md"})
344343
pkg, err := p.Do()
345344
s.NotNil(pkg)
346345
s.NoError(err)

generator/template.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package generator
33
import (
44
"bytes"
55
"fmt"
6-
"go/build"
76
"go/types"
87
"io"
98
"os"
@@ -12,6 +11,8 @@ import (
1211
"strings"
1312
"text/template"
1413

14+
parseutil "gopkg.in/src-d/go-parse-utils.v1"
15+
1516
"golang.org/x/tools/imports"
1617
)
1718

@@ -433,8 +434,18 @@ func printDocumentWithNumbers(code string) {
433434
}
434435
}
435436

437+
const pkgPath = "gopkg.in/src-d/go-kallax.v1/generator"
438+
439+
var pkgAbsPath = func() string {
440+
path, err := parseutil.DefaultGoPath.Abs(pkgPath)
441+
if err != nil {
442+
panic(err)
443+
}
444+
return path
445+
}()
446+
436447
func loadTemplateText(filename string) string {
437-
filename = filepath.Join(build.Default.GOPATH, "src/gopkg.in/src-d/go-kallax.v1/generator", filename)
448+
filename = filepath.Join(pkgAbsPath, filename)
438449
f, err := os.Open(filename)
439450
if err != nil {
440451
panic(err)
@@ -562,12 +573,12 @@ func writeFindByTpl(buf *bytes.Buffer, parent *Model, name string, f *Field, tpl
562573
// for the passed Field, in an autogenerated 'FindBy';
563574
// the second value returned is true if it was found a valid type
564575
func findableTypeName(f *Field) (string, bool) {
565-
analycedType := f.Node.Type()
576+
analyzedType := f.Node.Type()
566577
collectionAlreadyScanned := false
567578
for {
568-
valid, deepest := lookupValid(f.Node.Pkg(), analycedType)
579+
valid, deepest := lookupValid(f.Node.Pkg(), analyzedType)
569580
if valid != nil {
570-
return shortName(f.Node.Pkg(), analycedType), true
581+
return shortName(f.Node.Pkg(), analyzedType), true
571582
}
572583

573584
if collectionAlreadyScanned {
@@ -579,7 +590,7 @@ func findableTypeName(f *Field) (string, bool) {
579590
break
580591
}
581592

582-
analycedType = singular
593+
analyzedType = singular
583594
collectionAlreadyScanned = true
584595
}
585596

@@ -640,9 +651,7 @@ func isSpecialType(pkg *types.Package, typ types.Type) bool {
640651
}
641652

642653
func specialTypeShortName(typ types.Type) (string, bool) {
643-
s := removeGoPath(typ.String())
644-
s = strings.Replace(s, "*", "", -1)
645-
s = strings.Replace(s, "..", "", -1)
654+
s := removeGoPath(strings.TrimLeft(typ.String(), "*."))
646655
special, ok := specialTypes[s]
647656
return special, ok
648657
}

0 commit comments

Comments
 (0)