Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vendor normalization #107

Merged
merged 1 commit into from
May 7, 2018
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
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
}