Skip to content

Commit 8d365df

Browse files
internal/config: support golang overrides for slices (sqlc-dev#2339)
* internal/config: support an additional boolean field named "slice" on the "go_type" override property, and update docs * revert unrelated import string prefix change, and add pgx tests
1 parent 3b21fdf commit 8d365df

File tree

19 files changed

+354
-10
lines changed

19 files changed

+354
-10
lines changed

docs/reference/config.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,33 @@ Each mapping of the `overrides` collection has the following keys:
189189

190190
- `db_type`:
191191
- The PostgreSQL or MySQL type to override. Find the full list of supported types in [postgresql_type.go](https://github.com/kyleconroy/sqlc/blob/main/internal/codegen/golang/postgresql_type.go#L12) or [mysql_type.go](https://github.com/kyleconroy/sqlc/blob/main/internal/codegen/golang/mysql_type.go#L12). Note that for Postgres you must use the pg_catalog prefixed names where available. Can't be used if the `column` key is defined.
192-
- `column`
192+
- `column`:
193193
- In case the type overriding should be done on specific a column of a table instead of a type. `column` should be of the form `table.column` but you can be even more specific by specifying `schema.table.column` or `catalog.schema.table.column`. Can't be used if the `db_type` key is defined.
194194
- `go_type`:
195195
- A fully qualified name to a Go type to use in the generated code.
196196
- `go_struct_tag`:
197197
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
198198
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
199199
- `nullable`:
200-
- If true, use this type when a column is nullable. Defaults to `false`.
200+
- If `true`, use this type when a column is nullable. Defaults to `false`.
201201

202-
For more complicated import paths, the `go_type` can also be an object.
202+
When generating code, entries using the `column` key will always have preference over
203+
entries using the `db_type` key in order to generate the struct.
204+
205+
For more complicated import paths, the `go_type` can also be an object with the following keys:
206+
207+
- `import`:
208+
- The import path for the package where the type is defined.
209+
- `package`:
210+
- The package name where the type is defined. This should only be necessary when your import path doesn't end with the desired package name.
211+
- `type`:
212+
- The type name itself, without any package prefix.
213+
- `pointer`:
214+
- If set to `true`, generated code will use pointers to the type rather than the type itself.
215+
- `slice`:
216+
- If set to `true`, generated code will use a slice of the type rather than the type itself.
217+
218+
An example:
203219

204220
```yaml
205221
version: "2"
@@ -220,9 +236,6 @@ sql:
220236
pointer: true
221237
```
222238

223-
When generating code, entries using the `column` key will always have preference over
224-
entries using the `db_type` key in order to generate the struct.
225-
226239
#### kotlin
227240

228241
> Removed in v1.17.0 and replaced by the [sqlc-gen-kotlin](https://github.com/tabbed/sqlc-gen-kotlin) plugin. Follow the [migration guide](../guides/migrating-to-sqlc-gen-kotlin) to switch.

internal/cmd/shim.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func pluginGoType(o config.Override) *plugin.ParsedGoType {
114114
// Note that there is a slight mismatch between this and the
115115
// proto api. The GoType on the override is the unparsed type,
116116
// which could be a qualified path or an object, as per
117-
// https://docs.sqlc.dev/en/latest/reference/config.html#renaming-struct-fields
117+
// https://docs.sqlc.dev/en/v1.18.0/reference/config.html#type-overriding
118118
return &plugin.ParsedGoType{
119119
ImportPath: o.GoImportPath,
120120
Package: o.GoPackage,

internal/codegen/golang/imports.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ func (i *importer) interfaceImports() fileImports {
257257
}
258258

259259
func (i *importer) modelImports() fileImports {
260-
std, pkg := buildImports(i.Settings, nil, func(prefix string) bool {
261-
return i.usesType(prefix)
262-
})
260+
std, pkg := buildImports(i.Settings, nil, i.usesType)
263261

264262
if len(i.Enums) > 0 {
265263
std["fmt"] = struct{}{}

internal/config/go_type.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type GoType struct {
1313
Package string `json:"package" yaml:"package"`
1414
Name string `json:"type" yaml:"type"`
1515
Pointer bool `json:"pointer" yaml:"pointer"`
16+
Slice bool `json:"slice" yaml:"slice"`
1617
Spec string
1718
BuiltIn bool
1819
}
@@ -104,6 +105,9 @@ func (gt GoType) Parse() (*ParsedGoType, error) {
104105
if gt.Pointer {
105106
o.TypeName = "*" + o.TypeName
106107
}
108+
if gt.Slice {
109+
o.TypeName = "[]" + o.TypeName
110+
}
107111
return &o, nil
108112
}
109113

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text,
6+
tags string[]
7+
);
8+
9+
-- name: GetAuthor :one
10+
SELECT * FROM authors
11+
WHERE id = $1 LIMIT 1;

internal/endtoend/testdata/overrides_array/postgresql/pgx/v4/query/db.go

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

internal/endtoend/testdata/overrides_array/postgresql/pgx/v4/query/models.go

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

internal/endtoend/testdata/overrides_array/postgresql/pgx/v4/query/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "2",
3+
"sql": [{
4+
"schema": "query.sql",
5+
"queries": "query.sql",
6+
"engine": "postgresql",
7+
"gen": {
8+
"go": {
9+
"sql_package": "pgx/v4",
10+
"package": "query",
11+
"out": "query",
12+
"overrides": [{
13+
"column": "authors.tags",
14+
"go_type": {
15+
"type": "NullInt64",
16+
"import": "database/sql",
17+
"slice": true
18+
}
19+
}]
20+
}
21+
}
22+
}]
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text,
6+
tags string[]
7+
);
8+
9+
-- name: GetAuthor :one
10+
SELECT * FROM authors
11+
WHERE id = $1 LIMIT 1;

internal/endtoend/testdata/overrides_array/postgresql/pgx/v5/query/db.go

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

internal/endtoend/testdata/overrides_array/postgresql/pgx/v5/query/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.

internal/endtoend/testdata/overrides_array/postgresql/pgx/v5/query/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "2",
3+
"sql": [{
4+
"schema": "query.sql",
5+
"queries": "query.sql",
6+
"engine": "postgresql",
7+
"gen": {
8+
"go": {
9+
"sql_package": "pgx/v5",
10+
"package": "query",
11+
"out": "query",
12+
"overrides": [{
13+
"column": "authors.tags",
14+
"go_type": {
15+
"type": "NullInt64",
16+
"import": "database/sql",
17+
"slice": true
18+
}
19+
}]
20+
}
21+
}
22+
}]
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text,
6+
tags string[]
7+
);
8+
9+
-- name: GetAuthor :one
10+
SELECT * FROM authors
11+
WHERE id = $1 LIMIT 1;

internal/endtoend/testdata/overrides_array/postgresql/stdlib/query/db.go

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

internal/endtoend/testdata/overrides_array/postgresql/stdlib/query/models.go

Lines changed: 16 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)