-
Notifications
You must be signed in to change notification settings - Fork 804
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
add support for custom Go struct tags #1569
Conversation
This is equivalent and slightly simpler.
Rather than modeling when .Tag will be empty, check directly whether .Tag is empty. This simplifies the template and reduces the number of places that must be touched when adding new sources of struct tags.
Rather than inserting the colon at tag construction time, insert it at tag formatting time. This makes the input look a bit more natural. This matters more, as we are about to add another, more distant, place where we insert tags.
This change adds a new type of override: go_struct_tag. When provided for a field, it adds that struct tag to the generated code. The provided struct tag is parsed according to the standard package reflect rules, and its components are updated independently. This allows struct tag overrides to be compatible with (and optionally override) autogenerated json and db struct tags. Fixes sqlc-dev#534
The code uses some 1.18-only features, like strings.Cut and testing.F. The CI requires Go 1.18. Since Go 1.18 is now required, reflect that in the go.mod.
b016f14
to
9e00b39
Compare
Ping. |
Hi there, thank you for implementing this feature. I do not see this feature from documentation, and on original thread ( #534) there are two design that were discussed:
may I know which design actually implemented and some example ? Am hopeful it is the TS idea since it can really integrate well with validator library. |
@nahwinrajan search for |
How could a use it? Is there an example ❯ sqlc generate
error parsing sqlc.yaml: yaml: unmarshal errors:
line 12: field go_struct_tag not found in type config.SQLGo sqlc.yaml version: "2"
sql:
- engine: "postgresql"
queries: "config/sqls"
schema: "config/schema.sql"
gen:
go:
emit_json_tags: true
json_tags_case_style: snake
package: "queries"
out: "config/queries"
go_struct_tag: 'a:"b" x:"y,z"'
|
2yrs later, figured it out? 😂, honestly have no idea on how to use it |
Honestly the first design would have been better, adding all of these to the config file is just nuts |
This PR adds a new type of override: go_struct_tag.
When provided for a field, it adds that struct tag to the generated code.
The provided struct tag is parsed according to the standard
package reflect rules, and its components are updated independently.
This allows struct tag overrides to be compatible with (and optionally
override) autogenerated json and db struct tags.
This PR also includes some minor preparatory cleanup and fixes.
I recommend reviewing it one commit at a time.
The early commits are small and easy; the last commit contains the new feature.
Fixes #534