Skip to content

Commit d3dde8e

Browse files
author
Hyuga Tsukui
committed
feat(codegen): add support for build tags (sqlc-dev#2012)
1 parent 20fda73 commit d3dde8e

19 files changed

Lines changed: 507 additions & 192 deletions

File tree

‎docs/reference/config.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ The `gen` mapping supports the following keys:
146146
- `emit_all_enum_values`:
147147
- If true, emit a function per enum type
148148
that returns all valid enum values.
149+
- `build_tags`:
150+
- build tags to add to the generated code. if you do not want to add it, leave it unset.
149151
- `json_tags_id_uppercase`:
150152
- If true, "Id" in json tags will be uppercase. If false, will be camelcase. Defaults to `false`
151153
- `json_tags_case_style`:
@@ -389,6 +391,7 @@ packages:
389391
emit_pointers_for_null_types: false
390392
emit_enum_valid_method: false
391393
emit_all_enum_values: false
394+
build_tags: "some_tag"
392395
json_tags_case_style: "camel"
393396
omit_unused_structs: false
394397
output_batch_file_name: "batch.go"
@@ -443,6 +446,8 @@ Each mapping in the `packages` collection has the following keys:
443446
- `emit_all_enum_values`:
444447
- If true, emit a function per enum type
445448
that returns all valid enum values.
449+
- `build_tags`:
450+
- build tags to add to the generated code. if you do not want to add it, leave it unset.
446451
- `json_tags_case_style`:
447452
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
448453
- `omit_unused_structs`:

‎internal/cmd/shim.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
9696
EmitPointersForNullTypes: s.EmitPointersForNullTypes,
9797
EmitEnumValidMethod: s.EmitEnumValidMethod,
9898
EmitAllEnumValues: s.EmitAllEnumValues,
99+
BuildTags: s.BuildTags,
99100
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
100101
Package: s.Package,
101102
Out: s.Out,

‎internal/codegen/golang/gen.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type tmplCtx struct {
3838
EmitAllEnumValues bool
3939
UsesCopyFrom bool
4040
UsesBatch bool
41+
42+
BuildTags string
4143
}
4244

4345
func (t *tmplCtx) OutputQuery(sourceName string) bool {
@@ -135,6 +137,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
135137
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
136138
EmitEnumValidMethod: golang.EmitEnumValidMethod,
137139
EmitAllEnumValues: golang.EmitAllEnumValues,
140+
BuildTags: golang.BuildTags,
138141
UsesCopyFrom: usesCopyFrom(queries),
139142
UsesBatch: usesBatch(queries),
140143
SQLDriver: parseDriver(golang.SqlPackage),

‎internal/codegen/golang/templates/template.tmpl‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
{{define "dbFile"}}// Code generated by sqlc. DO NOT EDIT.
1+
{{define "dbFile"}}
2+
{{if .BuildTags}}
3+
//go:build {{.BuildTags}}
4+
// +build {{.BuildTags}}
5+
6+
{{end}}// Code generated by sqlc. DO NOT EDIT.
27
// versions:
38
// sqlc {{.SqlcVersion}}
49

@@ -24,7 +29,12 @@ import (
2429

2530
{{end}}
2631

27-
{{define "interfaceFile"}}// Code generated by sqlc. DO NOT EDIT.
32+
{{define "interfaceFile"}}
33+
{{if .BuildTags}}
34+
//go:build {{.BuildTags}}
35+
// +build {{.BuildTags}}
36+
37+
{{end}}// Code generated by sqlc. DO NOT EDIT.
2838
// versions:
2939
// sqlc {{.SqlcVersion}}
3040

@@ -48,7 +58,12 @@ import (
4858
{{end}}
4959
{{end}}
5060

51-
{{define "modelsFile"}}// Code generated by sqlc. DO NOT EDIT.
61+
{{define "modelsFile"}}
62+
{{if .BuildTags}}
63+
//go:build {{.BuildTags}}
64+
// +build {{.BuildTags}}
65+
66+
{{end}}// Code generated by sqlc. DO NOT EDIT.
5267
// versions:
5368
// sqlc {{.SqlcVersion}}
5469

@@ -141,7 +156,12 @@ type {{.Name}} struct { {{- range .Fields}}
141156
{{end}}
142157
{{end}}
143158

144-
{{define "queryFile"}}// Code generated by sqlc. DO NOT EDIT.
159+
{{define "queryFile"}}
160+
{{if .BuildTags}}
161+
//go:build {{.BuildTags}}
162+
// +build {{.BuildTags}}
163+
164+
{{end}}// Code generated by sqlc. DO NOT EDIT.
145165
// versions:
146166
// sqlc {{.SqlcVersion}}
147167
// source: {{.SourceName}}
@@ -166,7 +186,12 @@ import (
166186
{{end}}
167187
{{end}}
168188

169-
{{define "copyfromFile"}}// Code generated by sqlc. DO NOT EDIT.
189+
{{define "copyfromFile"}}
190+
{{if .BuildTags}}
191+
//go:build {{.BuildTags}}
192+
// +build {{.BuildTags}}
193+
194+
{{end}}// Code generated by sqlc. DO NOT EDIT.
170195
// versions:
171196
// sqlc {{.SqlcVersion}}
172197
// source: {{.SourceName}}
@@ -191,7 +216,12 @@ import (
191216
{{end}}
192217
{{end}}
193218

194-
{{define "batchFile"}}// Code generated by sqlc. DO NOT EDIT.
219+
{{define "batchFile"}}
220+
{{if .BuildTags}}
221+
//go:build {{.BuildTags}}
222+
// +build {{.BuildTags}}
223+
224+
{{end}}// Code generated by sqlc. DO NOT EDIT.
195225
// versions:
196226
// sqlc {{.SqlcVersion}}
197227
// source: {{.SourceName}}

‎internal/config/config.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ type SQLGo struct {
140140
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
141141
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
142142
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
143+
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
143144
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
144145
Package string `json:"package" yaml:"package"`
145146
Out string `json:"out" yaml:"out"`

‎internal/config/v_one.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type v1PackageSettings struct {
3838
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
3939
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
4040
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
41+
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
4142
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
4243
SQLPackage string `json:"sql_package" yaml:"sql_package"`
4344
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
@@ -176,6 +177,7 @@ func (c *V1GenerateSettings) Translate() Config {
176177
OutputFilesSuffix: pkg.OutputFilesSuffix,
177178
QueryParameterLimit: pkg.QueryParameterLimit,
178179
OmitUnusedStructs: pkg.OmitUnusedStructs,
180+
BuildTags: pkg.BuildTags,
179181
},
180182
},
181183
StrictFunctionChecks: pkg.StrictFunctionChecks,

‎internal/config/v_one.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
"emit_all_enum_values": {
124124
"type": "boolean"
125125
},
126+
"build_tags": {
127+
"type": "string"
128+
},
126129
"json_tags_case_style": {
127130
"type": "string"
128131
},

‎internal/config/v_two.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
"emit_all_enum_values": {
130130
"type": "boolean"
131131
},
132+
"build_tags": {
133+
"type": "string"
134+
},
132135
"json_tags_case_style": {
133136
"type": "string"
134137
},

‎internal/endtoend/testdata/build_tags/postgresql/stdlib/go/db.go‎

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/build_tags/postgresql/stdlib/go/models.go‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)