Skip to content

Commit

Permalink
Use exact capitalization from field names overridden in config
Browse files Browse the repository at this point in the history
  • Loading branch information
ianling committed Jun 11, 2022
1 parent 3a64078 commit 75e7089
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
14 changes: 9 additions & 5 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ type Object struct {

type Field struct {
Description string
Name string
Type types.Type
Tag string
// Name is the field's name as it appears in the schema
Name string
// GoName is the field's name as it appears in the generated Go code
GoName string
Type types.Type
Tag string
}

type Enum struct {
Expand Down Expand Up @@ -178,7 +181,7 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
}
}

name := field.Name
name := templates.ToGo(field.Name)
if nameOveride := cfg.Models[schemaType.Name].Fields[field.Name].FieldName; nameOveride != "" {
name = nameOveride
}
Expand All @@ -192,7 +195,8 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error {
}

f := &Field{
Name: name,
Name: field.Name,
GoName: name,
Type: typ,
Description: field.Description,
Tag: `json:"` + field.Name + `"`,
Expand Down
2 changes: 1 addition & 1 deletion plugin/modelgen/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{{- with .Description }}
{{.|prefixLines "// "}}
{{- end}}
{{ $field.Name|go }} {{$field.Type | ref}} `{{$field.Tag}}`
{{ $field.GoName }} {{$field.Type | ref}} `{{$field.Tag}}`
{{- end }}
}

Expand Down
4 changes: 4 additions & 0 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ func TestModelGeneration(t *testing.T) {
require.Nil(t, out.Recursive{}.FieldThree)
require.NotNil(t, out.Recursive{}.FieldFour)
})

t.Run("overridden struct field names use same capitalization as config", func(t *testing.T) {
require.NotNil(t, out.RenameFieldTest{}.GOODnaME)
})
}

func TestModelGenerationStructFieldPointers(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions plugin/modelgen/out/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions plugin/modelgen/out_struct_pointers/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions plugin/modelgen/testdata/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ models:
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingUnion
ExistingType:
model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingType
RenameFieldTest:
fields:
badName:
fieldName: GOODnaME

5 changes: 5 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,8 @@ type Recursive {
FieldThree: Recursive!
FieldFour: String!
}

type RenameFieldTest {
badName: String!
otherField: String!
}

0 comments on commit 75e7089

Please sign in to comment.