Skip to content

Commit f313c05

Browse files
Added documentation for type overriding and field renaming for the version 2 of the configuration file (#1695)
Co-authored-by: Euller Pereira <eullerpereira@grupobrisanet.com.br>
1 parent 0dc2c81 commit f313c05

File tree

1 file changed

+138
-1
lines changed

1 file changed

+138
-1
lines changed

docs/reference/config.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,102 @@ The `gen` mapping supports the following keys:
8787
- Customize the name of the querier file. Defaults to `querier.go`.
8888
- `output_files_suffix`:
8989
- If specified the suffix will be added to the name of the generated files.
90+
- `rename`:
91+
- Customize the name of generated struct fields. Explained in detail on the `Renaming fields` section.
92+
- `overrides`:
93+
- It is a collection of definitions that dictates which types are used to map a database types. Explained in detail on the `Type overriding` section.
94+
95+
##### Renaming fields
96+
97+
Struct field names are generated from column names using a simple algorithm:
98+
split the column name on underscores and capitalize the first letter of each
99+
part.
100+
101+
```
102+
account -> Account
103+
spotify_url -> SpotifyUrl
104+
app_id -> AppID
105+
```
106+
107+
If you're not happy with a field's generated name, use the `rename` mapping
108+
to pick a new name. The keys are column names and the values are the struct
109+
field name to use.
110+
111+
```yaml
112+
version: "2"
113+
sql:
114+
- schema: "postgresql/schema.sql"
115+
queries: "postgresql/query.sql"
116+
engine: "postgresql"
117+
gen:
118+
go:
119+
package: "authors"
120+
out: "postgresql"
121+
rename:
122+
spotify_url: "SpotifyURL"
123+
```
124+
125+
##### Type overriding
126+
127+
The default mapping of PostgreSQL/MySQL types to Go types only uses packages outside
128+
the standard library when it must.
129+
130+
For example, the `uuid` PostgreSQL type is mapped to `github.com/google/uuid`.
131+
If a different Go package for UUIDs is required, specify the package in the
132+
`overrides` array. In this case, I'm going to use the `github.com/gofrs/uuid`
133+
instead.
134+
135+
```yaml
136+
version: "2"
137+
sql:
138+
- schema: "postgresql/schema.sql"
139+
queries: "postgresql/query.sql"
140+
engine: "postgresql"
141+
gen:
142+
go:
143+
package: "authors"
144+
out: "postgresql"
145+
overrides:
146+
- db_type: "uuid"
147+
go_type: "github.com/gofrs/uuid.UUID"
148+
```
149+
150+
Each mapping of the `overrides` collection has the following keys:
151+
152+
- `db_type`:
153+
- 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.
154+
- `column`
155+
- 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.
156+
- `go_type`:
157+
- A fully qualified name to a Go type to use in the generated code.
158+
- `go_struct_tag`:
159+
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
160+
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
161+
- `nullable`:
162+
- If true, use this type when a column is nullable. Defaults to `false`.
163+
164+
For more complicated import paths, the `go_type` can also be an object.
165+
166+
```yaml
167+
version: "2"
168+
sql:
169+
- schema: "postgresql/schema.sql"
170+
queries: "postgresql/query.sql"
171+
engine: "postgresql"
172+
gen:
173+
go:
174+
package: "authors"
175+
out: "postgresql"
176+
overrides:
177+
- db_type: "uuid"
178+
go_type:
179+
import: "a/b/v2"
180+
package: "b"
181+
type: "MyType"
182+
```
183+
184+
When generating code, entries using the `column` key will always have preference over
185+
entries using the `db_type` key in order to generate the struct.
90186

91187
#### kotlin
92188

@@ -131,6 +227,48 @@ Each mapping in the `plugins` collection has the following keys:
131227
- `cmd`:
132228
- The executable to call when using this plugin
133229

230+
### global overrides
231+
232+
Sometimes, the same configuration must be done across various specfications of code generation.
233+
Then a global definition for type overriding and field renaming can be done using the `overrides` mapping the following manner:
234+
235+
```yaml
236+
version: "2"
237+
overrides:
238+
go:
239+
rename:
240+
id: "Identifier"
241+
overrides:
242+
- db_type: "timestampz"
243+
nullable: true
244+
engine: ""postgresql
245+
go_type:
246+
import: "gopkg.in/guregu/null.v4"
247+
package: "null"
248+
type: "Time"
249+
sql:
250+
- schema: "postgresql/schema.sql"
251+
queries: "postgresql/query.sql"
252+
engine: "postgresql"
253+
gen:
254+
go:
255+
package: "authors"
256+
out: "postgresql"
257+
- schema: "mysql/schema.sql"
258+
queries: "mysql/query.sql"
259+
engine: "mysql"
260+
gen:
261+
go:
262+
package: "authors"
263+
out: "mysql
264+
```
265+
266+
With the previous configuration, whenever a struct field is generated from a table column that is called `id`, it will generated as `Identifier`.
267+
Also, whenever there is a nullable `timestamp with time zone` column in a Postgres table, it will be generated as `null.Time`.
268+
Note that, the mapping for global type overrides has a field called `engine` that is absent in the regular type overrides. This field is only used when there are multiple definitions using multiple engines. Otherwise, the value of the `engine` key will be defaulted to the engine that is currently being used.
269+
270+
Currently, type overrides and field renaming, both global and regular, are only fully supported in Go.
271+
134272
## Version 1
135273

136274
```yaml
@@ -252,7 +390,6 @@ overrides:
252390
import: "a/b/v2"
253391
package: "b"
254392
type: "MyType"
255-
pointer: false # or true
256393
```
257394

258395
#### Per-Column Type Overrides

0 commit comments

Comments
 (0)