-
Notifications
You must be signed in to change notification settings - Fork 815
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
Support JSON tags for nullable enum structs #2121
Support JSON tags for nullable enum structs #2121
Conversation
Move tag utility functions in to own source file
Sorry for the tag @kyleconroy but do you have an opinion on this one? If you are in agreement with my config proposal, I have time over the weekend to implement this and update the PR. If you'd rather leave this feature out entirely, no problem, feel free to close the issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikemherron Apologies for not getting to this one sooner.
I think your last point is correct. This could be a breaking change for users, as the JSON format of nullable enums will change. Let's go with your second plan of adding a new configuration option to control this behavior.
No problem @kyleconroy, I should have time to do this over the next week. |
internal/cmd/shim.go
Outdated
QueryParameterLimit: s.QueryParameterLimit, | ||
EmitInterface: s.EmitInterface, | ||
EmitJsonTags: s.EmitJSONTags, | ||
EmitJsonTagsOnNullEnumStructs: s.EmitJSONTagsOnNullEnumStructs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, EmitJsonTagsOnNullEnumStructs
is the longest config property name so causes go fmt
to realign all the config structs, making the PR look a bit busier than it should in places like this.
JSON tags on null enum structs are now off by defaullt, and must be turned on with the |
After thinking about it a bit more, I decided that the extra configuration option isn't worth it. If emit_json_tags is on, we'll add the tags to nullable enums. I cleaned up the code slightly as well. |
Add JSON tags on to generated null enum structs. Resolves #2115.
However, while this works, it may be better implemented as a dedicated config option because:
emit_json_tags
option. Theemit_db_tags
is ignored, as it doesn't really make sense in this context since the struct does not represent a database entity. I think this is a logical descision, but it may initially be confusing to users why the null enums have one type of tag but not the other.json_tags_case_style
being"none"
, the tag name should fall back to the database column name. However, for theValid
property of the Null enum structs there is no corresponding database column, so"valid"
will always be used which might be inconsistent with other fields.An alternative to this (which I am happy to implement) would be to introduce a new config option, documented as so:
Which resolves the issues I raised. It feels a bit clunky, but I think it is clearer, and keeps the default behaviour as-is while supporting niche use-cases.
Opened the PR for feedback; I will wait for maintainer feedback before starting on the additional config option.