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

feat(codegen): add support for build tags (#2012) #2807

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ The `gen` mapping supports the following keys:
- `emit_all_enum_values`:
- If true, emit a function per enum type
that returns all valid enum values.
- `build_tags`:
- build tags to add to the generated code. if you do not want to add it, leave it unset.
Hyuga-Tsukui marked this conversation as resolved.
Show resolved Hide resolved
- `json_tags_id_uppercase`:
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
- `json_tags_case_style`:
Expand Down Expand Up @@ -389,6 +391,7 @@ packages:
emit_pointers_for_null_types: false
emit_enum_valid_method: false
emit_all_enum_values: false
build_tags: "some_tag"
json_tags_case_style: "camel"
omit_unused_structs: false
output_batch_file_name: "batch.go"
Expand Down Expand Up @@ -443,6 +446,8 @@ Each mapping in the `packages` collection has the following keys:
- `emit_all_enum_values`:
- If true, emit a function per enum type
that returns all valid enum values.
- `build_tags`:
- build tags to add to the generated code. if you do not want to add it, leave it unset.
Hyuga-Tsukui marked this conversation as resolved.
Show resolved Hide resolved
- `json_tags_case_style`:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
- `omit_unused_structs`:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
EmitPointersForNullTypes: s.EmitPointersForNullTypes,
EmitEnumValidMethod: s.EmitEnumValidMethod,
EmitAllEnumValues: s.EmitAllEnumValues,
BuildTags: s.BuildTags,
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
Package: s.Package,
Out: s.Out,
Expand Down
3 changes: 3 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type tmplCtx struct {
EmitAllEnumValues bool
UsesCopyFrom bool
UsesBatch bool

BuildTags string
Hyuga-Tsukui marked this conversation as resolved.
Show resolved Hide resolved
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -135,6 +137,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
EmitEnumValidMethod: golang.EmitEnumValidMethod,
EmitAllEnumValues: golang.EmitAllEnumValues,
BuildTags: golang.BuildTags,
UsesCopyFrom: usesCopyFrom(queries),
UsesBatch: usesBatch(queries),
SQLDriver: parseDriver(golang.SqlPackage),
Expand Down
42 changes: 36 additions & 6 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{{define "dbFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "dbFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}
// +build {{.BuildTags}}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The // +build syntax has been deprecated since Go 1.17. Let's remove it and only support //go:build.

{{end}}// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc {{.SqlcVersion}}

Expand All @@ -24,7 +29,12 @@ import (

{{end}}

{{define "interfaceFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "interfaceFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}
// +build {{.BuildTags}}

{{end}}// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc {{.SqlcVersion}}

Expand All @@ -48,7 +58,12 @@ import (
{{end}}
{{end}}

{{define "modelsFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "modelsFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}
// +build {{.BuildTags}}

{{end}}// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc {{.SqlcVersion}}

Expand Down Expand Up @@ -141,7 +156,12 @@ type {{.Name}} struct { {{- range .Fields}}
{{end}}
{{end}}

{{define "queryFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "queryFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}
// +build {{.BuildTags}}

{{end}}// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc {{.SqlcVersion}}
// source: {{.SourceName}}
Expand All @@ -166,7 +186,12 @@ import (
{{end}}
{{end}}

{{define "copyfromFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "copyfromFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}
// +build {{.BuildTags}}

{{end}}// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc {{.SqlcVersion}}
// source: {{.SourceName}}
Expand All @@ -191,7 +216,12 @@ import (
{{end}}
{{end}}

{{define "batchFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "batchFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}
// +build {{.BuildTags}}

{{end}}// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc {{.SqlcVersion}}
// source: {{.SourceName}}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type SQLGo struct {
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type v1PackageSettings struct {
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
Expand Down Expand Up @@ -176,6 +177,7 @@ func (c *V1GenerateSettings) Translate() Config {
OutputFilesSuffix: pkg.OutputFilesSuffix,
QueryParameterLimit: pkg.QueryParameterLimit,
OmitUnusedStructs: pkg.OmitUnusedStructs,
BuildTags: pkg.BuildTags,
},
},
StrictFunctionChecks: pkg.StrictFunctionChecks,
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_one.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
"emit_all_enum_values": {
"type": "boolean"
},
"build_tags": {
"type": "string"
},
"json_tags_case_style": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
"emit_all_enum_values": {
"type": "boolean"
},
"build_tags": {
"type": "string"
},
"json_tags_case_style": {
"type": "string"
},
Expand Down
34 changes: 34 additions & 0 deletions internal/endtoend/testdata/build_tags/postgresql/stdlib/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/build_tags/postgresql/stdlib/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);
15 changes: 15 additions & 0 deletions internal/endtoend/testdata/build_tags/postgresql/stdlib/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "authors",
"engine": "postgresql",
"schema": "schema.sql",
"queries": "query.sql",
"omit_unused_structs": true,
"emit_interface": true,
"build_tags": "some_tag"
}
]
}
1 change: 1 addition & 0 deletions internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"emit_result_struct_pointers": false,
"emit_params_struct_pointers": false,
"emit_methods_with_db_argument": false,
"build_tags": "",
"json_tags_case_style": "",
"package": "",
"out": "",
Expand Down
Loading
Loading