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. (sqlc-dev#123)
* 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
@@ -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`
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