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

Conversation

ianling
Copy link
Contributor

@ianling ianling commented Jun 11, 2022

Closes #1447

Behavior prior to this PR: given the following schema and gqlgen config:

Schema:

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

gqlgen.yml:

models:
  RenameFieldTest:
    fields:
      badName:
        fieldName: GOODnaME

gqlgen generates the following Go struct:

type RenameFieldTest struct {
	GOODnaMe   string `json:"badName"`  // <----- field name should be GOODnaME, but e is lowercase
	OtherField string `json:"otherField"`
}

Behavior after this PR (modelgen strictly adheres to field names overridden in the config):

type RenameFieldTest struct {
	GOODnaME   string `json:"badName"` // <----- the 'e' is now correctly capitalized
	OtherField string `json:"otherField"`

Field names that are not overridden in the config will be exactly the same as before this PR, so users will only see different behavior if they had an overridden name that happened to fall into one of these funny capitalization edge cases.

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs) (not necessary)

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 75.157% when pulling 75e7089 on ianling:capitalization into 3a64078 on 99designs:master.

@StevenACoffman StevenACoffman merged commit d07ec12 into 99designs:master Jun 11, 2022
@StevenACoffman
Copy link
Collaborator

Thanks! Nice catch!

@ianling ianling deleted the capitalization branch June 11, 2022 17:43
@@ -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.

@MohitVachhani
Copy link

Will this work for the cases of enums ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

should be able to override go field capitalization
6 participants