Skip to content

Commit

Permalink
feat(codegen): add support for build tags (sqlc-dev#2012)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyuga-Tsukui committed Oct 5, 2023
1 parent 20fda73 commit d3dde8e
Show file tree
Hide file tree
Showing 19 changed files with 507 additions and 192 deletions.
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.
- `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.
- `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
}

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}}

{{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

0 comments on commit d3dde8e

Please sign in to comment.