-
Notifications
You must be signed in to change notification settings - Fork 765
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 struct tags #534
Comments
Has anyone pickup this enhancement yet? It would be useful for generating fake data on model fields. If not I would like to give it a try with proper PSR predicated. |
Is there a way to achieve this now? type User struct {
Password string `json:"-"`
} |
up |
any solution for this issue please? |
@kyleconroy Would you be open to a solution that utilised the config overrides? Something like the following: overrides:
- column: "chat.id"
custom_struct_tag_key: "gorm"
go_type:
type: "int"
# OR
overrides:
- column: "chat.id"
go_type:
custom_struct_tag_key: "gorm"
I think this might also need something like Further to the above, this wouldn't override json tags, it would be in addition to emitting json tags. It seems to me that the conversation around including these in the schema is not the right place for this. Struct tags are really just a go concern, so possibly even including it in the definition of the |
@kyleconroy can we hope to see a feature like this in the foreseeable future? In my project, we started using Gorm way back in time, and it has been something we have been considering moving away from (towards sqlc). However, we rely on some 3rd party code that uses Gorm under the hood, so completely running away from it isn't feasible at the moment. If there were a way to let sqlc generate our Gorm-compatible models, this would be fantastic. It's the one thing that's stopping us from getting all in on sqlc. The only other alternative is still keep two copies of each model, which isn't that bad, but possibly error-prone. |
I agree that this seems like a good thing to add to the configuration file. |
We're facing this right now as well. I'm game to work on it, if we can get to an agreement about the design. FWIW, I would prefer adding custom struct tags to the config file, using something like: overrides:
- column: chat.id
go_struct_tag: 'validate:"required" sometagtype:"some_value"' |
I like that design |
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
Implemented in #1569 if anyone wants to kick the tires. |
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
* internal/config: use strings.Trim{Prefix,Suffix} This is equivalent and slightly simpler. * Makefile: fix vtproto 'go install' command * internal/codegen/golang: simplify template tag condition 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. * internal/codegen/golang: tweak tag formatting 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. * all: add support for custom Go struct 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 #534 * go.mod: bump to Go 1.18 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.
When working with structs, struct tags are used to add additional information to a field. This is for example very common when using validation packages. But currently there is no way to add additional struct tags to take advantage of this.
I think it would be a great idea to add a comment syntax just like the
-- name:NameForQuery
for table columns.A possible option to express these custom struct tags would be to write create table statements like this:
or alternatively:
The text was updated successfully, but these errors were encountered: