Skip to content

Commit

Permalink
Merge pull request 99designs#107 from vektah/fix-vendor-normalization
Browse files Browse the repository at this point in the history
Fix vendor normalization
  • Loading branch information
vektah authored May 7, 2018
2 parents f177396 + 1e8bd6b commit 9722a11
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 7 deletions.
5 changes: 0 additions & 5 deletions codegen/type_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ func pkgAndType(name string) (string, string) {
return normalizeVendor(strings.Join(parts[:len(parts)-1], ".")), parts[len(parts)-1]
}

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

func (n NamedTypes) getType(t common.Type) *Type {
var modifiers []string
usePtr := true
Expand Down
12 changes: 11 additions & 1 deletion codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codegen

import (
"go/types"
"regexp"
"strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -176,7 +177,7 @@ func bindObject(t types.Type, object *Object, imports Imports) error {
field.Type.Modifiers = modifiersFromGoType(structField.Type())
field.GoVarName = structField.Name()

switch field.Type.FullSignature() {
switch normalizeVendor(field.Type.FullSignature()) {
case normalizeVendor(structField.Type().String()):
// everything is fine

Expand Down Expand Up @@ -215,3 +216,12 @@ func modifiersFromGoType(t types.Type) []string {
}
}
}

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]
}
14 changes: 14 additions & 0 deletions codegen/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package codegen

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNormalizeVendor(t *testing.T) {
require.Equal(t, "bar/baz", normalizeVendor("foo/vendor/bar/baz"))
require.Equal(t, "[]bar/baz", normalizeVendor("[]foo/vendor/bar/baz"))
require.Equal(t, "*bar/baz", normalizeVendor("*foo/vendor/bar/baz"))
require.Equal(t, "*[]*bar/baz", normalizeVendor("*[]*foo/vendor/bar/baz"))
}
116 changes: 116 additions & 0 deletions test/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"
"time"

"remote_api"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -85,6 +87,12 @@ type testResolvers struct {
queryDate func(ctx context.Context, filter models.DateFilter) (bool, error)
}

func (r *testResolvers) Query_viewer(ctx context.Context) (*Viewer, error) {
return &Viewer{
User: &remote_api.User{"Bob"},
}, nil
}

func (r *testResolvers) Query_date(ctx context.Context, filter models.DateFilter) (bool, error) {
return r.queryDate(ctx, filter)
}
Expand Down
8 changes: 8 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ input DateFilter {
op: DATE_FILTER_OP = EQ
}

type User {
name: String
}
type Viewer {
user: User
}

type Query {
path: [Element]
date(filter: DateFilter!): Boolean!
viewer: Viewer
}
4 changes: 3 additions & 1 deletion test/types.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"Element": "github.com/vektah/gqlgen/test.Element"
"Element": "github.com/vektah/gqlgen/test.Element",
"Viewer": "github.com/vektah/gqlgen/test.Viewer",
"User": "remote_api.User"
}
5 changes: 5 additions & 0 deletions test/vendor/remote_api/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package remote_api

type User struct {
Name string
}
7 changes: 7 additions & 0 deletions test/viewer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package test

import "remote_api"

type Viewer struct {
User *remote_api.User
}

0 comments on commit 9722a11

Please sign in to comment.