Skip to content

Commit 9c2514c

Browse files
committed
Merge branch 'main' into kyle/process-plugins
2 parents c3e3a7a + 4ca7301 commit 9c2514c

File tree

38 files changed

+829
-283
lines changed

38 files changed

+829
-283
lines changed

.github/workflows/ci-python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424

2525
steps:
2626
- uses: actions/checkout@v3
27-
- uses: actions/setup-python@v3
27+
- uses: actions/setup-python@v4
2828
with:
2929
python-version: 3.9
3030
- name: Install python dependencies

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mysqlsh:
4141
# $ protoc --version
4242
# libprotoc 3.19.1
4343
# $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
44-
# $ go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto
44+
# $ go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto@latest
4545
proto: internal/plugin/codegen.pb.go internal/python/ast/ast.pb.go
4646

4747
internal/plugin/codegen.pb.go: protos/plugin/codegen.proto

docs/howto/named_parameters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ RETURNING *;
6161

6262
sqlc infers the nullability of any specified parameters, and often does exactly
6363
what you want. If you want finer control over the nullability of your
64-
parameters, you may use `sql.narg()` (**n**ullable arg) to override the default
65-
behavior. Using `sql.narg` tells sqlc to ignore whatever nullability it has
64+
parameters, you may use `sqlc.narg()` (**n**ullable arg) to override the default
65+
behavior. Using `sqlc.narg` tells sqlc to ignore whatever nullability it has
6666
inferred and generate a nullable parameter instead. There is no nullable
6767
equivalent of the `@` syntax.
6868

docs/howto/structs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ type Author struct {
4646
CreatedAt time.Time `json:"created_at"`
4747
}
4848
```
49+
50+
## More control
51+
52+
See the Type Overrides section of the Configuration File docs for fine-grained control over struct field types and tags.

docs/reference/config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ Each override document has the following keys:
225225
- 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.
226226
- `go_type`:
227227
- A fully qualified name to a Go type to use in the generated code.
228+
- `go_struct_tag`:
229+
- A reflect-style struct tag to use in the generated code, e.g. `a:"b" x:"y,z"`.
230+
If you want general json/db tags for all fields, use `emit_db_tags` and/or `emit_json_tags` instead.
228231
- `nullable`:
229232
- If true, use this type when a column is nullable. Defaults to `false`.
230233

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/kyleconroy/sqlc
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220209173558-ad29539cd2e9

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func pluginGoType(o config.Override) *plugin.ParsedGoType {
123123
Package: o.GoPackage,
124124
TypeName: o.GoTypeName,
125125
BasicType: o.GoBasicType,
126+
StructTags: o.GoStructTags,
126127
}
127128
}
128129

internal/codegen/golang/field.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Field struct {
1919
func (gf Field) Tag() string {
2020
tags := make([]string, 0, len(gf.Tags))
2121
for key, val := range gf.Tags {
22-
tags = append(tags, fmt.Sprintf("%s\"%s\"", key, val))
22+
tags = append(tags, fmt.Sprintf("%s:\"%s\"", key, val))
2323
}
2424
if len(tags) == 0 {
2525
return ""

internal/codegen/golang/go_type.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ import (
55
"github.com/kyleconroy/sqlc/internal/plugin"
66
)
77

8+
func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, col *plugin.Column) {
9+
for _, oride := range req.Settings.Overrides {
10+
if oride.GoType.StructTags == nil {
11+
continue
12+
}
13+
if !sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema) {
14+
// Different table.
15+
continue
16+
}
17+
if !sdk.MatchString(oride.ColumnName, col.Name) {
18+
// Different column.
19+
continue
20+
}
21+
// Add the extra tags.
22+
for k, v := range oride.GoType.StructTags {
23+
tags[k] = v
24+
}
25+
}
26+
}
27+
828
func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
929
// Check if the column's type has been overridden
1030
for _, oride := range req.Settings.Overrides {

internal/codegen/golang/result.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
7474
for _, column := range table.Columns {
7575
tags := map[string]string{}
7676
if req.Settings.Go.EmitDbTags {
77-
tags["db:"] = column.Name
77+
tags["db"] = column.Name
7878
}
7979
if req.Settings.Go.EmitJsonTags {
80-
tags["json:"] = JSONTagName(column.Name, req.Settings)
80+
tags["json"] = JSONTagName(column.Name, req.Settings)
8181
}
82+
addExtraGoStructTags(tags, req, column)
8283
s.Fields = append(s.Fields, Field{
8384
Name: StructName(column.Name, req.Settings),
8485
Type: goType(req, column),
@@ -283,10 +284,10 @@ func columnsToStruct(req *plugin.CodeGenRequest, name string, columns []goColumn
283284
}
284285
tags := map[string]string{}
285286
if req.Settings.Go.EmitDbTags {
286-
tags["db:"] = tagName
287+
tags["db"] = tagName
287288
}
288289
if req.Settings.Go.EmitJsonTags {
289-
tags["json:"] = JSONTagName(tagName, req.Settings)
290+
tags["json"] = JSONTagName(tagName, req.Settings)
290291
}
291292
gs.Fields = append(gs.Fields, Field{
292293
Name: fieldName,

0 commit comments

Comments
 (0)