Skip to content

Commit 7e8e0a3

Browse files
authored
sqlc-gen-python: Build using Go 1.21rc2 (#10)
* sqlc-gen-python: Build using Go 1.21rc2 * deps: Use go-sdk v1.18.0 * Use go1.21rc * Don't hardcode path
1 parent 827dea5 commit 7e8e0a3

File tree

7 files changed

+39
-35
lines changed

7 files changed

+39
-35
lines changed

.github/workflows/ci.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v3
13-
- uses: actions/setup-go@v3
13+
- uses: actions/setup-go@v4
1414
with:
15-
go-version: '1.19'
16-
- run: wget https://github.com/tinygo-org/tinygo/releases/download/v0.26.0/tinygo_0.26.0_amd64.deb
17-
- run: sudo dpkg -i tinygo_0.26.0_amd64.deb
15+
go-version: '1.21.0-rc.2'
1816
- run: make

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ sqlc-gen-python:
44
cd plugin && go build -o ~/bin/sqlc-gen-python ./main.go
55

66
sqlc-gen-python.wasm:
7-
cd plugin && tinygo build -o sqlc-gen-python.wasm -gc=leaking -scheduler=none -wasm-abi=generic -target=wasi main.go
7+
cd plugin && GOOS=wasip1 GOARCH=wasm go build -o sqlc-gen-python.wasm main.go
88
openssl sha256 plugin/sqlc-gen-python.wasm
9-

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ module github.com/tabbed/sqlc-gen-python
33
go 1.19
44

55
require (
6+
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1
67
github.com/google/go-cmp v0.5.9
78
github.com/jinzhu/inflection v1.0.0
89
github.com/mailru/easyjson v0.7.7
9-
github.com/tabbed/sqlc-go v1.16.0
10-
google.golang.org/protobuf v1.28.1
10+
github.com/tabbed/sqlc-go v1.18.0
11+
google.golang.org/protobuf v1.30.0
1112
)
1213

1314
require github.com/josharian/intern v1.0.0 // indirect

go.sum

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1 h1:ze0HODAjPRXSkiqSpDTYq2baS4IVtRtDLSZY2p1ZCX4=
2+
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1/go.mod h1:DSpReHp8PwHOeCfGymiiY4HSx2iVL358X7JRMciL7T0=
13
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
24
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
35
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
@@ -10,7 +12,9 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
1012
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
1113
github.com/tabbed/sqlc-go v1.16.0 h1:EwPBXdGn5tyrLjcNiHRoQthWvJeF5NjG9Cx1WK5iFsY=
1214
github.com/tabbed/sqlc-go v1.16.0/go.mod h1:mqMU5duZRGz5Wp/qJXwkERf+MXgGOZ8BmW/tH9KyvWA=
15+
github.com/tabbed/sqlc-go v1.18.0 h1:GNE8b8xue8fKVptQnr3Z6DV8FqdokyDYML7O0kYtbe4=
16+
github.com/tabbed/sqlc-go v1.18.0/go.mod h1:qx8ocsmviBDyRfLNuJQtdu0f5oqa8XBjKxMldl+Wm24=
1317
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1418
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
15-
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
16-
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
19+
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
20+
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=

internal/gen.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"sort"
1010
"strings"
1111

12+
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
1213
easyjson "github.com/mailru/easyjson"
13-
plugin "github.com/tabbed/sqlc-go/codegen"
1414
"github.com/tabbed/sqlc-go/metadata"
1515
"github.com/tabbed/sqlc-go/sdk"
1616

@@ -192,15 +192,15 @@ func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
192192
func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
193193
columnType := sdk.DataType(col.Type)
194194
for _, oride := range req.Settings.Overrides {
195-
if !pyTypeIsSet(oride.PythonType) {
195+
if !pyTypeIsSet(oride) {
196196
continue
197197
}
198198
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
199199
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
200-
return pyTypeString(oride.PythonType)
200+
return oride.CodeType
201201
}
202202
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != (col.NotNull || col.IsArray) {
203-
return pyTypeString(oride.PythonType)
203+
return oride.CodeType
204204
}
205205
}
206206

@@ -1091,7 +1091,7 @@ func HashComment(s string) string {
10911091
return "# " + strings.ReplaceAll(s, "\n", "\n# ")
10921092
}
10931093

1094-
func Generate(_ context.Context, req *plugin.Request) (*plugin.Response, error) {
1094+
func Generate(_ context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
10951095
var conf Config
10961096
if len(req.PluginOptions) > 0 {
10971097
if err := easyjson.Unmarshal(req.PluginOptions, &conf); err != nil {
@@ -1143,7 +1143,7 @@ func Generate(_ context.Context, req *plugin.Request) (*plugin.Response, error)
11431143
output[name] = string(result.Python)
11441144
}
11451145

1146-
resp := plugin.Response{}
1146+
resp := plugin.CodeGenResponse{}
11471147

11481148
for filename, code := range output {
11491149
resp.Files = append(resp.Files, &plugin.File{

internal/imports.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sort"
66
"strings"
77

8-
"github.com/tabbed/sqlc-go/codegen"
8+
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
99
)
1010

1111
type importSpec struct {
@@ -14,15 +14,8 @@ type importSpec struct {
1414
Alias string
1515
}
1616

17-
func pyTypeIsSet(t *codegen.PythonType) bool {
18-
return t.Module != "" || t.Name != ""
19-
}
20-
21-
func pyTypeString(t *codegen.PythonType) string {
22-
if t.Name != "" && t.Module == "" {
23-
return t.Name
24-
}
25-
return t.Module + "." + t.Name
17+
func pyTypeIsSet(o *plugin.Override) bool {
18+
return o.CodeType != ""
2619
}
2720

2821
func (i importSpec) String() string {
@@ -39,7 +32,7 @@ func (i importSpec) String() string {
3932
}
4033

4134
type importer struct {
42-
Settings *codegen.Settings
35+
Settings *plugin.Settings
4336
Models []Struct
4437
Queries []Query
4538
Enums []Enum
@@ -112,12 +105,17 @@ func (i *importer) modelImportSpecs() (map[string]importSpec, map[string]importS
112105
pkg := make(map[string]importSpec)
113106

114107
for _, o := range i.Settings.Overrides {
115-
if pyTypeIsSet(o.PythonType) && o.PythonType.Module != "" {
116-
if modelUses(pyTypeString(o.PythonType)) {
117-
pkg[o.PythonType.Module] = importSpec{Module: o.PythonType.Module}
108+
if pyTypeIsSet(o) {
109+
mod, _, found := strings.Cut(o.CodeType, ".")
110+
if !found {
111+
continue
112+
}
113+
if modelUses(o.CodeType) {
114+
pkg[mod] = importSpec{Module: mod}
118115
}
119116
}
120117
}
118+
121119
return std, pkg
122120
}
123121

@@ -158,9 +156,13 @@ func (i *importer) queryImportSpecs(fileName string) (map[string]importSpec, map
158156
}
159157

160158
for _, o := range i.Settings.Overrides {
161-
if pyTypeIsSet(o.PythonType) && o.PythonType.Module != "" {
162-
if queryUses(pyTypeString(o.PythonType)) {
163-
pkg[o.PythonType.Module] = importSpec{Module: o.PythonType.Module}
159+
if pyTypeIsSet(o) {
160+
mod, _, found := strings.Cut(o.CodeType, ".")
161+
if !found {
162+
continue
163+
}
164+
if queryUses(o.CodeType) {
165+
pkg[mod] = importSpec{Module: mod}
164166
}
165167
}
166168
}

internal/postgresql_type.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package python
33
import (
44
"log"
55

6-
"github.com/tabbed/sqlc-go/codegen"
6+
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
77
"github.com/tabbed/sqlc-go/sdk"
88
)
99

10-
func postgresType(req *codegen.Request, col *codegen.Column) string {
10+
func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
1111
columnType := sdk.DataType(col.Type)
1212

1313
switch columnType {

0 commit comments

Comments
 (0)