You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Implement per-column type overrides.
Often a user may want to override the Go type used for model
generation and query generation, but it's not a 1-to-1 mapping
of SQL types to Go type. Instead, the override to use depends
entirely on the column of a particular table. This commit adds the
functionality to override types of generated columns in models and query
parameters by adding a new 'overrides' configuration settings object
on a per-package basis.
This required tracking down a number of places where the column's Table
FQN wasn't properly propagated through to the the model or query
parameter Column. This change necessitated updating a bunch of tests to
match.
Lastly, I've added a test to prove the functionality works (in addition
to testing it on a large code base) and added a documentation section in
the README.
* update docs
Copy file name to clipboardExpand all lines: README.md
+42-5Lines changed: 42 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -324,8 +324,7 @@ instead.
324
324
"packages": [...],
325
325
"overrides": [
326
326
{
327
-
"go_type": "uuid.UUID",
328
-
"package": "github.com/gofrs/uuid",
327
+
"go_type": "github.com/gofrs/uuid.UUID",
329
328
"postgres_type": "uuid"
330
329
}
331
330
]
@@ -336,12 +335,50 @@ Each override document has the following keys:
336
335
-`postgres_type`:
337
336
- The PostgreSQL type to override. Find the full list of supported types in [gen.go](https://github.com/kyleconroy/sqlc/blob/master/internal/dinosql/gen.go#L438).
338
337
-`go_type`:
339
-
- The Go type, with package name, to use in the generated code.
340
-
-`package`:
341
-
- The full import path for the package.
338
+
- A fully qualified name to a Go type to use in the generated code.
342
339
-`null`:
343
340
- If true, use this type when a column in nullable. Defaults to `false`.
344
341
342
+
### Per-Column Type Overrides
343
+
344
+
Sometimes you would like to override the Go type used in model or query generation for
345
+
a specific field of a table and not on a type basis as described in the previous section.
346
+
347
+
This may be configured by specifying the `column` property in the override definition. `column`
348
+
should be of the form `table.column` buy you may be even more specify by specifying `schema.table.column`
349
+
or `catalog.schema.table.column`.
350
+
351
+
```
352
+
{
353
+
"version": "1",
354
+
"packages": [...],
355
+
"overrides": [
356
+
{
357
+
"column": "authors.id",
358
+
"go_type": "github.com/segmentio/ksuid.KSUID"
359
+
}
360
+
]
361
+
}
362
+
```
363
+
364
+
### Package Level Overrides
365
+
366
+
Overrides can be configured globally, as demonstrated in the previous sections, or they can be configured on a per-package which
367
+
scopes the override behavior to just a single package:
368
+
369
+
```
370
+
{
371
+
"version": "1",
372
+
"packages": [
373
+
{
374
+
...
375
+
"overrides": [...]
376
+
}
377
+
],
378
+
}
379
+
```
380
+
381
+
345
382
### Renaming Struct Fields
346
383
347
384
Struct field names are generated from column names using a simple algorithm:
returnfmt.Errorf("Override `column` specifier %q is not the proper format, expected '[catalog.][schema.]colname.tablename'", o.Column)
73
+
}
74
+
}
75
+
76
+
// validate GoType
77
+
lastDot:=strings.LastIndex(o.GoType, ".")
78
+
iflastDot==-1 {
79
+
returnfmt.Errorf("Package override `go_type` specificier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType)
80
+
}
81
+
lastSlash:=strings.LastIndex(o.GoType, "/")
82
+
iflastSlash==-1 {
83
+
returnfmt.Errorf("Package override `go_type` specificier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType)
84
+
}
85
+
o.goTypeName=o.GoType[lastSlash+1:]
86
+
o.goPackage=o.GoType[:lastDot]
87
+
isPointer:=o.GoType[0] =='*'
88
+
ifisPointer {
89
+
o.goPackage=o.goPackage[1:]
90
+
o.goTypeName="*"+o.goTypeName
91
+
}
92
+
93
+
returnnil
94
+
}
95
+
25
96
varErrMissingVersion=errors.New("no version number")
26
97
varErrUnknownVersion=errors.New("invalid version number")
0 commit comments