Skip to content

feat(json-tags): Add case style config option #905

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

Merged
merged 3 commits into from
Mar 8, 2021
Merged

feat(json-tags): Add case style config option #905

merged 3 commits into from
Mar 8, 2021

Conversation

chutommy
Copy link
Contributor

@chutommy chutommy commented Feb 22, 2021

This PR allows users to choose which case style will be used to generate JSON tags.
The style can be selected by adding the json_tags_case_style setting key.

  • camelCase - "camel"
  • PascalCase - "pascal"
  • snake_case - "snake" (default)

Changes

  • Add JSONTagsCaseStyle field to package settings and set its default value to snake.

    • config/config.go
    • config/v_one.go
  • Implement SetCaseStyle function which converts the tag name to the specified case style.

    • codegen/golang/field.go
  • Wrap JSON tags name with the SetCaseStyle function.

  • Add test for each case style.

    • endtoend/testdata/camel_case/...
    • endtoend/testdata/pascal_case/...
    • endtoend/testdata/snake_case/...
  • Add json_tags_case_style key to the documentation.

Resolve #878

Copy link
Collaborator

@kyleconroy kyleconroy left a comment

Choose a reason for hiding this comment

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

First off, the code looks great. Thank you for updating the documentation as well as writing tests.

However, this change includes two breaking chages.

First, sqlc currently doesn't format the JSON tags in any particular case. The default is to just use the name of the column in the database. This behavior needs to stay the default.

Second, renames do not currently apply to JSON tags. I don't think we should start doing that now.

If you could fix up these two things, I can merge this.

This feature allows users to choose which case style will be used
to generate JSON tags. The style can be selected by adding
the `json_tags_case_style` setting key.

- camelCase - "camel"
- PascalCase - "pascal"
- snake_case - "snake"
- column name in the DB - "none" (default)

Changes (in `internal/`):

- Add `JSONTagsCaseStyle` field to package settings.
- Implement `SetCaseStyle` function which converts the tag name to the specified case style.
- Wrap JSON tags definition with the `JSONTagName` function which applies `SetCaseStyle` if `json_tags_case_style` key is set.
- Add test for each case style.
- Add `json_tags_case_style` key to documentation.

Resolve #878
@chutommy
Copy link
Contributor Author

chutommy commented Mar 1, 2021

The default is to just use the name of the column in the database. This behavior needs to stay the default.

I removed the renames, a sure thing, I totally agree.

renames do not currently apply to JSON tags. I don't think we should start doing that now.

I added a wrapper which applies the case style formatter only if the json_tags_case_style is set. If its value is none or has no value (the key is omitted) the name of the column in the database is used (the default).

Changes:

  • Removed the renames
  • Added JSONTagName which decides if SetCaseStyle is applied:
func JSONTagName(name string, settings config.CombinedSettings) string {
	style := settings.Go.JSONTagsCaseStyle
	if style == "" || style == "none" {
		return name
	} else {
		return SetCaseStyle(name, style)
	}
}
  • Changed the function pattern of the SetCaseStyle:
- func SetCaseStyle(name string, settings config.CombinedSettings) string {
+ func SetCaseStyle(name string, style string) string {
  • Updated the documentation

@chutommy chutommy requested a review from kyleconroy March 1, 2021 09:03
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.

camelCase
2 participants