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

Reject operation or argument names that are Go keywords #195

Merged
merged 2 commits into from
May 12, 2022
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: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ When releasing a new version:

### Bug fixes:

- genqlient now explicitly rejects argument, operation, and type names which are Go keywords, rather than failing with an opaque error.

## v0.4.0

Version 0.4.0 adds several new configuration options, as well as additional methods to simplify the use of interfaces.
Expand Down
7 changes: 7 additions & 0 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func (g *generator) convertArguments(
name := "__" + operation.Name + "Input"
fields := make([]*goStructField, len(operation.VariableDefinitions))
for i, arg := range operation.VariableDefinitions {
if goKeywords[arg.Variable] {
return nil, errorf(arg.Position, "variable name must not be a go keyword")
}

_, options, err := g.parsePrecedingComment(arg, nil, arg.Position, queryOptions)
if err != nil {
return nil, err
Expand Down Expand Up @@ -317,6 +321,9 @@ func (g *generator) convertDefinition(
// Determine the name to use for this type.
var name string
if options.TypeName != "" {
if goKeywords[options.TypeName] {
return nil, errorf(pos, "typename option must not be a go keyword")
}
// If the user specified a name, use it!
name = options.TypeName
if namePrefix != nil && namePrefix.head == name && namePrefix.tail == nil {
Expand Down
2 changes: 2 additions & 0 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ func (g *generator) preprocessQueryDocument(doc *ast.QueryDocument) {
func (g *generator) addOperation(op *ast.OperationDefinition) error {
if op.Name == "" {
return errorf(op.Position, "operations must have operation-names")
} else if goKeywords[op.Name] {
return errorf(op.Position, "operation name must not be a go keyword")
}

queryDoc := &ast.QueryDocument{
Expand Down
3 changes: 3 additions & 0 deletions generate/testdata/errors/KeywordArgumentName.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query KeywordArgumentName($type: String) {
f(type: $type)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Query { f(type: String): String }
1 change: 1 addition & 0 deletions generate/testdata/errors/KeywordOperationName.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
query type { f }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Query { f: String }
6 changes: 6 additions & 0 deletions generate/testdata/errors/KeywordTypeName.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query KeywordTypeName {
# @genqlient(typename: "type")
f {
g
}
}
2 changes: 2 additions & 0 deletions generate/testdata/errors/KeywordTypeName.schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type Query { f: T }
type T { g: String }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/errors/KeywordArgumentName.graphql:1: variable name must not be a go keyword
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/errors/KeywordOperationName.graphql:1: operation name must not be a go keyword
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/errors/KeywordTypeName.schema.graphql:1: typename option must not be a go keyword
29 changes: 29 additions & 0 deletions generate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,32 @@ func goConstName(s string) string {
return ret
}, s)
}

// https://go.dev/ref/spec#Keywords
var goKeywords = map[string]bool{
"break": true,
"default": true,
"func": true,
"interface": true,
"select": true,
"case": true,
"defer": true,
"go": true,
"map": true,
"struct": true,
"chan": true,
"else": true,
"goto": true,
"package": true,
"switch": true,
"const": true,
"fallthrough": true,
"if": true,
"range": true,
"type": true,
"continue": true,
"for": true,
"import": true,
"return": true,
"var": true,
}