Skip to content

Commit

Permalink
internal/config: support golang overrides for slices (#2339)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andrewmbenton committed Jun 20, 2023
1 parent 3b21fdf commit 8d365df
Show file tree
Hide file tree
Showing 19 changed files with 354 additions and 10 deletions.
25 changes: 19 additions & 6 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,33 @@ Each mapping of the `overrides` collection has the following keys:

- `db_type`:
- 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.
- `column`
- `column`:
- 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.
- `go_type`:
- A fully qualified name to a Go type to use in the generated code.
- `go_struct_tag`:
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
- `nullable`:
- If true, use this type when a column is nullable. Defaults to `false`.
- If `true`, use this type when a column is nullable. Defaults to `false`.

For more complicated import paths, the `go_type` can also be an object.
When generating code, entries using the `column` key will always have preference over
entries using the `db_type` key in order to generate the struct.

For more complicated import paths, the `go_type` can also be an object with the following keys:

- `import`:
- The import path for the package where the type is defined.
- `package`:
- 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.
- `type`:
- The type name itself, without any package prefix.
- `pointer`:
- If set to `true`, generated code will use pointers to the type rather than the type itself.
- `slice`:
- If set to `true`, generated code will use a slice of the type rather than the type itself.

An example:

```yaml
version: "2"
Expand All @@ -220,9 +236,6 @@ sql:
pointer: true
```

When generating code, entries using the `column` key will always have preference over
entries using the `db_type` key in order to generate the struct.

#### kotlin

> 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.
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func pluginGoType(o config.Override) *plugin.ParsedGoType {
// Note that there is a slight mismatch between this and the
// proto api. The GoType on the override is the unparsed type,
// which could be a qualified path or an object, as per
// https://docs.sqlc.dev/en/latest/reference/config.html#renaming-struct-fields
// https://docs.sqlc.dev/en/v1.18.0/reference/config.html#type-overriding
return &plugin.ParsedGoType{
ImportPath: o.GoImportPath,
Package: o.GoPackage,
Expand Down
4 changes: 1 addition & 3 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,7 @@ func (i *importer) interfaceImports() fileImports {
}

func (i *importer) modelImports() fileImports {
std, pkg := buildImports(i.Settings, nil, func(prefix string) bool {
return i.usesType(prefix)
})
std, pkg := buildImports(i.Settings, nil, i.usesType)

if len(i.Enums) > 0 {
std["fmt"] = struct{}{}
Expand Down
4 changes: 4 additions & 0 deletions internal/config/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type GoType struct {
Package string `json:"package" yaml:"package"`
Name string `json:"type" yaml:"type"`
Pointer bool `json:"pointer" yaml:"pointer"`
Slice bool `json:"slice" yaml:"slice"`
Spec string
BuiltIn bool
}
Expand Down Expand Up @@ -104,6 +105,9 @@ func (gt GoType) Parse() (*ParsedGoType, error) {
if gt.Pointer {
o.TypeName = "*" + o.TypeName
}
if gt.Slice {
o.TypeName = "[]" + o.TypeName
}
return &o, nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
tags string[]
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2",
"sql": [{
"schema": "query.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"go": {
"sql_package": "pgx/v4",
"package": "query",
"out": "query",
"overrides": [{
"column": "authors.tags",
"go_type": {
"type": "NullInt64",
"import": "database/sql",
"slice": true
}
}]
}
}
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
tags string[]
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "2",
"sql": [{
"schema": "query.sql",
"queries": "query.sql",
"engine": "postgresql",
"gen": {
"go": {
"sql_package": "pgx/v5",
"package": "query",
"out": "query",
"overrides": [{
"column": "authors.tags",
"go_type": {
"type": "NullInt64",
"import": "database/sql",
"slice": true
}
}]
}
}
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
tags string[]
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

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.

Loading

0 comments on commit 8d365df

Please sign in to comment.