Skip to content
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

Merged
merged 6 commits into from
Jun 10, 2022

Conversation

josharian
Copy link
Contributor

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

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.
@josharian
Copy link
Contributor Author

Ping.

@kyleconroy kyleconroy merged commit 304ba5d into sqlc-dev:main Jun 10, 2022
@nahwinrajan
Copy link

nahwinrajan commented Feb 16, 2023

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:

  • author suggestion per field on the SQL Table
  • Kyle's counter recommendation which is on override level.

may I know which design actually implemented and some example ?
also does this implementation also work for struct that are generated from DML (select, insert, update, etc) ?

Am hopeful it is the TS idea since it can really integrate well with validator library.

@josharian
Copy link
Contributor Author

@nahwinrajan search for go_struct_tag in https://docs.sqlc.dev/en/latest/reference/config.html

@josharian josharian deleted the custom-go-struct-tag branch March 14, 2023 17:43
@FrankFang
Copy link

FrankFang commented May 26, 2023

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"'

@adeyemialameen04
Copy link

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

@adeyemialameen04
Copy link

Honestly the first design would have been better, adding all of these to the config file is just nuts

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.

Add support for custom struct tags
5 participants