Skip to content

Commit

Permalink
Merge pull request #570 from 99designs/vendor-packages-workaround
Browse files Browse the repository at this point in the history
Workaround for using packages with vendored code
  • Loading branch information
vektah authored Mar 4, 2019
2 parents f2d9c3f + 1e7aab6 commit 2888e96
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
22 changes: 9 additions & 13 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"fmt"
"go/token"
"go/types"
"regexp"
"strings"

"github.com/99designs/gqlgen/codegen/templates"

"github.com/99designs/gqlgen/internal/code"
"github.com/pkg/errors"
"github.com/vektah/gqlparser/ast"
Expand All @@ -29,6 +26,14 @@ func (c *Config) NewBinder(s *ast.Schema) (*Binder, error) {
return nil, err
}

for _, p := range pkgs {
for _, e := range p.Errors {
if e.Kind == packages.ListError {
return nil, p.Errors[0]
}
}
}

return &Binder{
pkgs: pkgs,
schema: s,
Expand Down Expand Up @@ -71,7 +76,7 @@ func (b *Binder) FindType(pkgName string, typeName string) (types.Type, error) {

func (b *Binder) getPkg(find string) *packages.Package {
for _, p := range b.pkgs {
if normalizeVendor(find) == normalizeVendor(p.PkgPath) {
if code.NormalizeVendor(find) == code.NormalizeVendor(p.PkgPath) {
return p
}
}
Expand Down Expand Up @@ -162,15 +167,6 @@ func (b *Binder) PointerTo(ref *TypeReference) *TypeReference {
return newRef
}

var modsRegex = regexp.MustCompile(`^(\*|\[\])*`)

func normalizeVendor(pkg string) string {
modifiers := modsRegex.FindAllString(pkg, 1)[0]
pkg = strings.TrimPrefix(pkg, modifiers)
parts := strings.Split(pkg, "/vendor/")
return modifiers + parts[len(parts)-1]
}

// TypeReference is used by args and field types. The Definition can refer to both input and output types.
type TypeReference struct {
Definition *ast.Definition
Expand Down
2 changes: 1 addition & 1 deletion codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (tm TypeMap) ReferencedPackages() []string {
if pkg == "" || inStrSlice(pkgs, pkg) {
continue
}
pkgs = append(pkgs, pkg)
pkgs = append(pkgs, code.QualifyPackagePath(pkg))
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/code/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NameForPackage(importPath string) string {
if v, ok := nameForPackageCache.Load(importPath); ok {
return v.(string)
}
importPath = QualifyPackagePath(importPath)
p, _ := packages.Load(nil, importPath)

if len(p) != 1 || p[0].Name == "" {
Expand Down
26 changes: 25 additions & 1 deletion internal/code/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package code

import (
"go/build"
"os"
"path/filepath"
"regexp"
"strings"
Expand All @@ -13,18 +15,40 @@ func PkgAndType(name string) (string, string) {
return "", name
}

return NormalizeVendor(strings.Join(parts[:len(parts)-1], ".")), parts[len(parts)-1]
return strings.Join(parts[:len(parts)-1], "."), parts[len(parts)-1]
}

var modsRegex = regexp.MustCompile(`^(\*|\[\])*`)

// NormalizeVendor takes a qualified package path and turns it into normal one.
// eg .
// github.com/foo/vendor/github.com/99designs/gqlgen/graphql becomes
// github.com/99designs/gqlgen/graphql
func NormalizeVendor(pkg string) string {
modifiers := modsRegex.FindAllString(pkg, 1)[0]
pkg = strings.TrimPrefix(pkg, modifiers)
parts := strings.Split(pkg, "/vendor/")
return modifiers + parts[len(parts)-1]
}

// QualifyPackagePath takes an import and fully qualifies it with a vendor dir, if one is required.
// eg .
// github.com/99designs/gqlgen/graphql becomes
// github.com/foo/vendor/github.com/99designs/gqlgen/graphql
//
// x/tools/packages only supports 'qualified package paths' so this will need to be done prior to calling it
// See https://github.com/golang/go/issues/30289
func QualifyPackagePath(importPath string) string {
wd, _ := os.Getwd()

pkg, err := build.Import(importPath, wd, 0)
if err != nil {
return importPath
}

return pkg.ImportPath
}

var invalidPackageNameChar = regexp.MustCompile(`[^\w]`)

func SanitizePackageName(pkg string) string {
Expand Down

0 comments on commit 2888e96

Please sign in to comment.