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

Allow underscore only fields and naming collisions to be aliased explicitly #541

Merged
merged 3 commits into from
Feb 18, 2019
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: 1 addition & 1 deletion codegen/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestConfigCheck(t *testing.T) {
err = config.normalize()
require.NoError(t, err)

err = config.check()
err = config.Check()
require.EqualError(t, err, "filenames exec.go and models.go are in the same directory but have different package definitions")
})
}
4 changes: 2 additions & 2 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func (b *builder) bindField(obj *Object, f *Field) error {
case obj.Type == config.MapType:
return nil
case b.Config.Models[obj.Name].Fields[f.Name].FieldName != "":
f.Name = b.Config.Models[obj.Name].Fields[f.Name].FieldName
f.GoFieldName = b.Config.Models[obj.Name].Fields[f.Name].FieldName
}

target, err := b.findBindTarget(obj.Type.(*types.Named), f.Name)
target, err := b.findBindTarget(obj.Type.(*types.Named), f.GoFieldName)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type ComplexityRoot struct {
{{ $object.Name|toCamel }} struct {
{{ range $field := $object.Fields -}}
{{ if not $field.IsReserved -}}
{{ $field.Name|toCamel }} {{ $field.ComplexitySignature }}
{{ $field.GoFieldName }} {{ $field.ComplexitySignature }}
{{ end }}
{{- end }}
}
Expand Down Expand Up @@ -86,8 +86,8 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
{{ if not $object.IsReserved }}
{{ range $field := $object.Fields }}
{{ if not $field.IsReserved }}
case "{{$object.Name}}.{{$field.Name}}":
if e.complexity.{{$object.Name|toCamel}}.{{$field.Name|toCamel}} == nil {
case "{{$object.Name}}.{{$field.GoFieldName}}":
if e.complexity.{{$object.Name|toCamel}}.{{$field.GoFieldName}} == nil {
break
}
{{ if $field.Args }}
Expand All @@ -96,7 +96,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return 0, false
}
{{ end }}
return e.complexity.{{$object.Name|toCamel}}.{{$field.Name|toCamel}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
return e.complexity.{{$object.Name|toCamel}}.{{$field.GoFieldName}}(childComplexity{{if $field.Args}}, {{$field.ComplexityArgs}} {{end}}), true
{{ end }}
{{ end }}
{{ end }}
Expand Down
4 changes: 4 additions & 0 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ func Call(p *types.Func) string {
}

func ToCamel(s string) string {
if s == "_" {
return "_"
}
buffer := make([]rune, 0, len(s))
upper := true
lastWasUpper := false
Expand Down Expand Up @@ -288,6 +291,7 @@ var keywords = []string{
"import",
"return",
"var",
"_",
}

// sanitizeKeywords prevents collisions with go keywords for arguments to resolver functions
Expand Down
Loading