Skip to content

Commit

Permalink
Merge pull request 99designs#75 from mathewbyrne/fix-import-dash
Browse files Browse the repository at this point in the history
Replace Invalid Characters in Package Name with an Underscore
  • Loading branch information
vektah committed Apr 4, 2018
2 parents 87dc702 + 3ae9089 commit 990b3c8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
7 changes: 5 additions & 2 deletions codegen/import_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codegen

import (
"path/filepath"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -29,6 +30,8 @@ func buildImports(types NamedTypes, destDir string) Imports {
return imports
}

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

func (s Imports) addPkg(types NamedTypes, destDir string, pkg string) (Imports, *Import) {
if pkg == "" {
return s, nil
Expand All @@ -40,11 +43,11 @@ func (s Imports) addPkg(types NamedTypes, destDir string, pkg string) (Imports,

localName := ""
if !strings.HasSuffix(destDir, pkg) {
localName = filepath.Base(pkg)
localName = invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_")
i := 1
imp := s.findByName(localName)
for imp != nil && imp.Package != pkg {
localName = filepath.Base(pkg) + strconv.Itoa(i)
localName = invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_") + strconv.Itoa(i)
imp = s.findByName(localName)
i++
if i > 10 {
Expand Down
58 changes: 58 additions & 0 deletions test/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
query "github.com/vektah/gqlgen/neelance/query"
schema "github.com/vektah/gqlgen/neelance/schema"
introspection1 "github.com/vektah/gqlgen/test/introspection"
invalid_identifier "github.com/vektah/gqlgen/test/invalid-identifier"
models "github.com/vektah/gqlgen/test/models"
)

Expand All @@ -29,6 +30,7 @@ type Resolvers interface {
Query_recursive(ctx context.Context, input *RecursiveInputSlice) (*bool, error)
Query_mapInput(ctx context.Context, input *map[string]interface{}) (*bool, error)
Query_collision(ctx context.Context) (*introspection1.It, error)
Query_invalidIdentifier(ctx context.Context) (*invalid_identifier.InvalidIdentifier, error)
}

type executableSchema struct {
Expand Down Expand Up @@ -127,6 +129,33 @@ func (ec *executionContext) _InnerObject_id(ctx context.Context, field graphql.C
return graphql.MarshalInt(res)
}

var invalidIdentifierImplementors = []string{"InvalidIdentifier"}

// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _InvalidIdentifier(ctx context.Context, sel []query.Selection, obj *invalid_identifier.InvalidIdentifier) graphql.Marshaler {
fields := graphql.CollectFields(ec.Doc, sel, invalidIdentifierImplementors, ec.Variables)
out := graphql.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias

switch field.Name {
case "__typename":
out.Values[i] = graphql.MarshalString("InvalidIdentifier")
case "id":
out.Values[i] = ec._InvalidIdentifier_id(ctx, field, obj)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}

return out
}

func (ec *executionContext) _InvalidIdentifier_id(ctx context.Context, field graphql.CollectedField, obj *invalid_identifier.InvalidIdentifier) graphql.Marshaler {
res := obj.ID
return graphql.MarshalInt(res)
}

var itImplementors = []string{"It"}

// nolint: gocyclo, errcheck, gas, goconst
Expand Down Expand Up @@ -219,6 +248,8 @@ func (ec *executionContext) _Query(ctx context.Context, sel []query.Selection) g
out.Values[i] = ec._Query_mapInput(ctx, field)
case "collision":
out.Values[i] = ec._Query_collision(ctx, field)
case "invalidIdentifier":
out.Values[i] = ec._Query_invalidIdentifier(ctx, field)
case "__schema":
out.Values[i] = ec._Query___schema(ctx, field)
case "__type":
Expand Down Expand Up @@ -433,6 +464,28 @@ func (ec *executionContext) _Query_collision(ctx context.Context, field graphql.
})
}

func (ec *executionContext) _Query_invalidIdentifier(ctx context.Context, field graphql.CollectedField) graphql.Marshaler {
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(r)
ec.Error(userErr)
ret = graphql.Null
}
}()
rctx := graphql.WithResolverContext(ctx, &graphql.ResolverContext{Field: field})
res, err := ec.resolvers.Query_invalidIdentifier(rctx)
if err != nil {
ec.Error(err)
return graphql.Null
}
if res == nil {
return graphql.Null
}
return ec._InvalidIdentifier(ctx, field.Selections, res)
})
}

func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.CollectedField) graphql.Marshaler {
res := ec.introspectSchema()
if res == nil {
Expand Down Expand Up @@ -1139,12 +1192,17 @@ type It {
id: ID!
}
type InvalidIdentifier {
id: Int!
}
type Query {
nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean
nestedOutputs: [[OuterObject]]
shapes: [Shape]
recursive(input: RecursiveInputSlice): Boolean
mapInput(input: Changes): Boolean
collision: It
invalidIdentifier: InvalidIdentifier
}
`)
5 changes: 5 additions & 0 deletions test/invalid-identifier/invalid-identifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package invalid_identifier

type InvalidIdentifier struct {
ID int
}
18 changes: 12 additions & 6 deletions test/resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
gqlerrors "github.com/vektah/gqlgen/neelance/errors"
"github.com/vektah/gqlgen/neelance/query"
"github.com/vektah/gqlgen/test/introspection"
invalid_identifier "github.com/vektah/gqlgen/test/invalid-identifier"
"github.com/vektah/gqlgen/test/models"
)

Expand Down Expand Up @@ -77,12 +78,13 @@ func mkctx(doc *query.Document, errFn func(e error) string) context.Context {
}

type testResolvers struct {
inner models.InnerObject
innerErr error
nestedInputs *bool
nestedInputsErr error
nestedOutputs [][]models.OuterObject
nestedOutputsErr error
inner models.InnerObject
innerErr error
nestedInputs *bool
nestedInputsErr error
nestedOutputs [][]models.OuterObject
nestedOutputsErr error
invalidIdentifier *invalid_identifier.InvalidIdentifier
}

func (r *testResolvers) Query_shapes(ctx context.Context) ([]Shape, error) {
Expand Down Expand Up @@ -113,6 +115,10 @@ func (r *testResolvers) Query_nestedOutputs(ctx context.Context) ([][]models.Out
return r.nestedOutputs, r.nestedOutputsErr
}

func (r *testResolvers) Query_invalidIdentifier(ctx context.Context) (*invalid_identifier.InvalidIdentifier, error) {
return r.invalidIdentifier, nil
}

type specialErr struct{}

func (*specialErr) Error() string {
Expand Down
5 changes: 5 additions & 0 deletions test/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ type It {
id: ID!
}

type InvalidIdentifier {
id: Int!
}

type Query {
nestedInputs(input: [[OuterInput]] = [[{inner: {id: 1}}]]): Boolean
nestedOutputs: [[OuterObject]]
shapes: [Shape]
recursive(input: RecursiveInputSlice): Boolean
mapInput(input: Changes): Boolean
collision: It
invalidIdentifier: InvalidIdentifier
}
3 changes: 2 additions & 1 deletion test/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Rectangle": "github.com/vektah/gqlgen/test.Rectangle",
"RecursiveInputSlice": "github.com/vektah/gqlgen/test.RecursiveInputSlice",
"It": "github.com/vektah/gqlgen/test/introspection.It",
"Changes": "map[string]interface{}"
"Changes": "map[string]interface{}",
"InvalidIdentifier": "github.com/vektah/gqlgen/test/invalid-identifier.InvalidIdentifier"
}

0 comments on commit 990b3c8

Please sign in to comment.