Skip to content

Commit 7dc0e7c

Browse files
feat(codegen): Support setting Go build tags (#2012) (#2807)
* feat(codegen): add support for build tags (#2012) * Update docs/reference/config.md * Update internal/codegen/golang/gen.go --------- Co-authored-by: Kyle Gray <kyle@conroy.org>
1 parent c0936cc commit 7dc0e7c

File tree

20 files changed

+444
-139
lines changed

20 files changed

+444
-139
lines changed

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

internal/codegen/golang/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type tmplCtx struct {
3838
EmitAllEnumValues bool
3939
UsesCopyFrom bool
4040
UsesBatch bool
41+
BuildTags string
4142
}
4243

4344
func (t *tmplCtx) OutputQuery(sourceName string) bool {
@@ -143,6 +144,7 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
143144
Enums: enums,
144145
Structs: structs,
145146
SqlcVersion: req.SqlcVersion,
147+
BuildTags: golang.BuildTags,
146148
}
147149

148150
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && golang.SqlDriver != SQLDriverGoSQLDriverMySQL {

internal/codegen/golang/templates/template.tmpl

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

@@ -24,7 +28,11 @@ import (
2428

2529
{{end}}
2630

27-
{{define "interfaceFile"}}// Code generated by sqlc. DO NOT EDIT.
31+
{{define "interfaceFile"}}
32+
{{if .BuildTags}}
33+
//go:build {{.BuildTags}}
34+
35+
{{end}}// Code generated by sqlc. DO NOT EDIT.
2836
// versions:
2937
// sqlc {{.SqlcVersion}}
3038

@@ -48,7 +56,11 @@ import (
4856
{{end}}
4957
{{end}}
5058

51-
{{define "modelsFile"}}// Code generated by sqlc. DO NOT EDIT.
59+
{{define "modelsFile"}}
60+
{{if .BuildTags}}
61+
//go:build {{.BuildTags}}
62+
63+
{{end}}// Code generated by sqlc. DO NOT EDIT.
5264
// versions:
5365
// sqlc {{.SqlcVersion}}
5466

@@ -141,7 +153,11 @@ type {{.Name}} struct { {{- range .Fields}}
141153
{{end}}
142154
{{end}}
143155

144-
{{define "queryFile"}}// Code generated by sqlc. DO NOT EDIT.
156+
{{define "queryFile"}}
157+
{{if .BuildTags}}
158+
//go:build {{.BuildTags}}
159+
160+
{{end}}// Code generated by sqlc. DO NOT EDIT.
145161
// versions:
146162
// sqlc {{.SqlcVersion}}
147163
// source: {{.SourceName}}
@@ -166,7 +182,11 @@ import (
166182
{{end}}
167183
{{end}}
168184

169-
{{define "copyfromFile"}}// Code generated by sqlc. DO NOT EDIT.
185+
{{define "copyfromFile"}}
186+
{{if .BuildTags}}
187+
//go:build {{.BuildTags}}
188+
189+
{{end}}// Code generated by sqlc. DO NOT EDIT.
170190
// versions:
171191
// sqlc {{.SqlcVersion}}
172192
// source: {{.SourceName}}
@@ -191,7 +211,11 @@ import (
191211
{{end}}
192212
{{end}}
193213

194-
{{define "batchFile"}}// Code generated by sqlc. DO NOT EDIT.
214+
{{define "batchFile"}}
215+
{{if .BuildTags}}
216+
//go:build {{.BuildTags}}
217+
218+
{{end}}// Code generated by sqlc. DO NOT EDIT.
195219
// versions:
196220
// sqlc {{.SqlcVersion}}
197221
// source: {{.SourceName}}

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ type SQLGo struct {
156156
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
157157
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
158158
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
159+
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
159160
}
160161

161162
type SQLJSON struct {

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type v1PackageSettings struct {
5353
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
5454
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
5555
Rules []string `json:"rules" yaml:"rules"`
56+
BuildTags string `json:"build_tags,omitempty" yaml:"build_tags"`
5657
}
5758

5859
func v1ParseConfig(rd io.Reader) (Config, error) {
@@ -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: 33 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: 17 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)