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

Use exact capitalization from field names overridden in config #2237

Merged
merged 1 commit into from
Jun 11, 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
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}}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this because of a bug in the go filter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, the go filter was just being erroneously applied in situations where the field name and its capitalization were being explicitly overridden in the gqlgen config. This was a problem because that filter changes the capitalization of field names that are already valid in Go in a slightly opinionated way, which was unwanted behavior in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Cool.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ianling Is there a way (in config or otherwise) to disable this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YogeshLele disable what exactly? If you want to specify a particular capitalization for a field in your schema, this PR made it so you can do so without gqlgen overriding it.

{{ $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!
}