From 309e5c6db1c7af0da9b75b14aeff16885264122d Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Thu, 3 May 2018 18:49:15 +1000 Subject: [PATCH] Automatically add a _ suffix to reserved words --- codegen/input_test.go | 73 +++++++++++++++++++++++++++++++++++++++++ codegen/object.go | 9 ++--- codegen/object_build.go | 44 +++++++++++++++++++++++-- 3 files changed, 119 insertions(+), 7 deletions(-) diff --git a/codegen/input_test.go b/codegen/input_test.go index f75d7250eb8..c4191fd7952 100644 --- a/codegen/input_test.go +++ b/codegen/input_test.go @@ -90,3 +90,76 @@ func TestComplexInputTypes(t *testing.T) { require.NoError(t, err) } + +func TestKeywordInputFields(t *testing.T) { + err := generate("input_keywords_fields", ` + input Object { + break: String! + default: String! + func: String! + interface: String! + select: String! + case: String! + defer: String! + go: String! + map: String! + struct: String! + chan: String! + else: String! + goto: String! + package: String! + switch: String! + const: String! + fallthrough: String! + if: String! + range: String! + type: String! + continue: String! + for: String! + import: String! + return: String! + var: String! + } + type Query { + test(input: Object): Boolean! + } + `) + + require.NoError(t, err) +} + +func TestInputKeywordArgs(t *testing.T) { + err := generate("input_keyword_args", ` + type Query { + test( + break: String!, + default: String!, + func: String!, + interface: String!, + select: String!, + case: String!, + defer: String!, + go: String!, + map: String!, + struct: String!, + chan: String!, + else: String!, + goto: String!, + package: String!, + switch: String!, + const: String!, + fallthrough: String!, + if: String!, + range: String!, + type: String!, + continue: String!, + for: String!, + import: String!, + return: String!, + var: String!, + ): Boolean! + } + `) + + require.NoError(t, err) +} diff --git a/codegen/object.go b/codegen/object.go index f1aafbaba0c..06291517817 100644 --- a/codegen/object.go +++ b/codegen/object.go @@ -34,9 +34,10 @@ type Field struct { type FieldArgument struct { *Type - GQLName string // The name of the argument in graphql - Object *Object // A link back to the parent object - Default interface{} // The default value + GQLName string // The name of the argument in graphql + GoVarName string // The name of the var in go + Object *Object // A link back to the parent object + Default interface{} // The default value } type Objects []*Object @@ -67,7 +68,7 @@ func (f *Field) ResolverDeclaration() string { res += fmt.Sprintf(", obj *%s", f.Object.FullName()) } for _, arg := range f.Args { - res += fmt.Sprintf(", %s %s", arg.GQLName, arg.Signature()) + res += fmt.Sprintf(", %s %s", arg.GoVarName, arg.Signature()) } result := f.Signature() diff --git a/codegen/object_build.go b/codegen/object_build.go index 3b9e092dc71..20fd867f7d9 100644 --- a/codegen/object_build.go +++ b/codegen/object_build.go @@ -42,6 +42,43 @@ func (cfg *Config) buildObjects(types NamedTypes, prog *loader.Program, imports return objects, nil } +var keywords = []string{ + "break", + "default", + "func", + "interface", + "select", + "case", + "defer", + "go", + "map", + "struct", + "chan", + "else", + "goto", + "package", + "switch", + "const", + "fallthrough", + "if", + "range", + "type", + "continue", + "for", + "import", + "return", + "var", +} + +func sanitizeGoName(name string) string { + for _, k := range keywords { + if name == k { + return name + "_" + } + } + return name +} + func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, error) { obj := &Object{NamedType: types[typ.TypeName()]} @@ -53,9 +90,10 @@ func (cfg *Config) buildObject(types NamedTypes, typ *schema.Object) (*Object, e var args []FieldArgument for _, arg := range field.Args { newArg := FieldArgument{ - GQLName: arg.Name.Name, - Type: types.getType(arg.Type), - Object: obj, + GQLName: arg.Name.Name, + Type: types.getType(arg.Type), + Object: obj, + GoVarName: sanitizeGoName(arg.Name.Name), } if !newArg.Type.IsInput && !newArg.Type.IsScalar {