Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ addons:
postgresql: "9.4"

env:
- DBNAME=kallax_test DBUSER=postgres DBPASS=''
- DBNAME=kallax_test DBUSER=postgres DBPASS='' GOPATH=/tmp/whatever:$GOPATH

services:
- postgresql
Expand Down
12 changes: 7 additions & 5 deletions generator/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go/parser"
"go/token"
"go/types"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -459,16 +458,19 @@ func joinDirectory(directory string, files []string) []string {
return result
}

var goPath = os.Getenv("GOPATH")

func typeName(typ types.Type) string {
return removeGoPath(typ.String())
}

func removeGoPath(path string) string {
importPath := filepath.ToSlash(goPath + "/src/")
path = filepath.ToSlash(path)
return strings.Replace(path, importPath, "", -1)
for _, p := range parseutil.DefaultGoPath {
p = filepath.ToSlash(p + "/src/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Join?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Join will strip the last "/", which we want to replace later.

if strings.HasPrefix(path, p) {
return strings.Replace(path, p, "", -1)
}
}
return path
}

func isIgnoredField(s *types.Struct, idx int) bool {
Expand Down
3 changes: 1 addition & 2 deletions generator/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"go/parser"
"go/token"
"go/types"
"path/filepath"
"reflect"
"testing"

Expand Down Expand Up @@ -340,7 +339,7 @@ func (s *ProcessorSuite) processFixture(source string) *Package {
}

func (s *ProcessorSuite) TestDo() {
p := NewProcessor(filepath.Join(goPath, "src", "gopkg.in/src-d/go-kallax.v1"), []string{"README.md"})
p := NewProcessor(pkgAbsPath, []string{"README.md"})
pkg, err := p.Do()
s.NotNil(pkg)
s.NoError(err)
Expand Down
27 changes: 18 additions & 9 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package generator
import (
"bytes"
"fmt"
"go/build"
"go/types"
"io"
"os"
Expand All @@ -12,6 +11,8 @@ import (
"strings"
"text/template"

parseutil "gopkg.in/src-d/go-parse-utils.v1"

"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -433,8 +434,18 @@ func printDocumentWithNumbers(code string) {
}
}

const pkgPath = "gopkg.in/src-d/go-kallax.v1/generator"

var pkgAbsPath = func() string {
path, err := parseutil.DefaultGoPath.Abs(pkgPath)
if err != nil {
panic(err)
}
return path
}()

func loadTemplateText(filename string) string {
filename = filepath.Join(build.Default.GOPATH, "src/gopkg.in/src-d/go-kallax.v1/generator", filename)
filename = filepath.Join(pkgAbsPath, filename)
f, err := os.Open(filename)
if err != nil {
panic(err)
Expand Down Expand Up @@ -562,12 +573,12 @@ func writeFindByTpl(buf *bytes.Buffer, parent *Model, name string, f *Field, tpl
// for the passed Field, in an autogenerated 'FindBy';
// the second value returned is true if it was found a valid type
func findableTypeName(f *Field) (string, bool) {
analycedType := f.Node.Type()
analyzedType := f.Node.Type()
collectionAlreadyScanned := false
for {
valid, deepest := lookupValid(f.Node.Pkg(), analycedType)
valid, deepest := lookupValid(f.Node.Pkg(), analyzedType)
if valid != nil {
return shortName(f.Node.Pkg(), analycedType), true
return shortName(f.Node.Pkg(), analyzedType), true
}

if collectionAlreadyScanned {
Expand All @@ -579,7 +590,7 @@ func findableTypeName(f *Field) (string, bool) {
break
}

analycedType = singular
analyzedType = singular
collectionAlreadyScanned = true
}

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

func specialTypeShortName(typ types.Type) (string, bool) {
s := removeGoPath(typ.String())
s = strings.Replace(s, "*", "", -1)
s = strings.Replace(s, "..", "", -1)
s := removeGoPath(strings.TrimLeft(typ.String(), "*."))
special, ok := specialTypes[s]
return special, ok
}
Expand Down