-
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
Merged
kyleconroy
merged 13 commits into
sqlc-dev:main
from
mikemherron:bugfix/null-enum-json-tag
Jun 9, 2023
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4fd9f1a
Support JSON tags for nullable enum structs
mikemherron dfee64d
Update tests with new JSON tags for Null Enum structs
mikemherron 89b095e
Add additional tests for different cases
mikemherron c66cc61
Additional test for none format option
mikemherron e278f78
Merge branch 'main' into bugfix/null-enum-json-tag
mikemherron e6d693f
add config option for json tags on null enum structs
mikemherron bfacad0
remove EmitJsonTagsOnNullEnumStructs from template context, not required
mikemherron 7100416
update json plugin test with new option
mikemherron 76fe23f
Remove added config
kyleconroy a20f0bf
Merge branch 'main' into bugfix/null-enum-json-tag
kyleconroy e40e399
Regenerate test output
kyleconroy 753da8e
remove last config
kyleconroy 1f0a61e
Remove tags.go
kyleconroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package golang | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kyleconroy/sqlc/internal/plugin" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func TagsToString(tags map[string]string) string { | ||
if len(tags) == 0 { | ||
return "" | ||
} | ||
tagParts := make([]string, 0, len(tags)) | ||
for key, val := range tags { | ||
tagParts = append(tagParts, fmt.Sprintf("%s:\"%s\"", key, val)) | ||
} | ||
sort.Strings(tagParts) | ||
return strings.Join(tagParts, " ") | ||
} | ||
|
||
func JSONTagName(name string, settings *plugin.Settings) string { | ||
style := settings.Go.JsonTagsCaseStyle | ||
if style == "" || style == "none" { | ||
return name | ||
} else { | ||
return SetCaseStyle(name, style) | ||
} | ||
} | ||
|
||
func SetCaseStyle(name string, style string) string { | ||
switch style { | ||
case "camel": | ||
return toCamelCase(name) | ||
case "pascal": | ||
return toPascalCase(name) | ||
case "snake": | ||
return toSnakeCase(name) | ||
default: | ||
panic(fmt.Sprintf("unsupported JSON tags case style: '%s'", style)) | ||
} | ||
} | ||
|
||
func toSnakeCase(s string) string { | ||
return strings.ToLower(s) | ||
} | ||
|
||
func toCamelCase(s string) string { | ||
return toCamelInitCase(s, false) | ||
} | ||
|
||
func toPascalCase(s string) string { | ||
return toCamelInitCase(s, true) | ||
} | ||
|
||
func toCamelInitCase(name string, initUpper bool) string { | ||
out := "" | ||
for i, p := range strings.Split(name, "_") { | ||
if !initUpper && i == 0 { | ||
out += p | ||
continue | ||
} | ||
if p == "id" { | ||
out += "ID" | ||
} else { | ||
out += strings.Title(p) | ||
} | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 causesgo fmt
to realign all the config structs, making the PR look a bit busier than it should in places like this.