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 all commits
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`:
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
- `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`:
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
- `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 @@ -110,6 +110,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
QueryParameterLimit: s.QueryParameterLimit,
OmitUnusedStructs: s.OmitUnusedStructs,
BuildTags: s.BuildTags,
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type tmplCtx struct {
EmitAllEnumValues bool
UsesCopyFrom bool
UsesBatch bool
BuildTags string
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -143,6 +144,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
Enums: enums,
Structs: structs,
SqlcVersion: req.SqlcVersion,
BuildTags: golang.BuildTags,
}

if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && golang.SqlDriver != SQLDriverGoSQLDriverMySQL {
Expand Down
36 changes: 30 additions & 6 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{{define "dbFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "dbFile"}}
{{if .BuildTags}}
//go:build {{.BuildTags}}

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

Expand All @@ -24,7 +28,11 @@ import (

{{end}}

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

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

Expand All @@ -48,7 +56,11 @@ import (
{{end}}
{{end}}

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

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

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

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

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

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

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

{{define "batchFile"}}// Code generated by sqlc. DO NOT EDIT.
{{define "batchFile"}}
{{if .BuildTags}}
//go: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 @@ -156,6 +156,7 @@ type SQLGo struct {
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
}

type SQLJSON struct {
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 @@ -53,6 +53,7 @@ type v1PackageSettings struct {
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
Rules []string `json:"rules" yaml:"rules"`
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
}

func v1ParseConfig(rd io.Reader) (Config, error) {
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
33 changes: 33 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"
}
]
}
3 changes: 2 additions & 1 deletion internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"query_parameter_limit": 1,
"output_batch_file_name": "",
"json_tags_id_uppercase": false,
"omit_unused_structs": false
"omit_unused_structs": false,
"build_tags": ""
},
"json": {
"out": "gen",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"query_parameter_limit": 1,
"output_batch_file_name": "",
"json_tags_id_uppercase": false,
"omit_unused_structs": false
"omit_unused_structs": false,
"build_tags": ""
},
"json": {
"out": "",
Expand Down
Loading