Skip to content

sqlc-gen-python: Build using Go 1.21rc2 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: '1.19'
- run: wget https://github.com/tinygo-org/tinygo/releases/download/v0.26.0/tinygo_0.26.0_amd64.deb
- run: sudo dpkg -i tinygo_0.26.0_amd64.deb
go-version: '1.21.0-rc.2'
- run: make
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ sqlc-gen-python:
cd plugin && go build -o ~/bin/sqlc-gen-python ./main.go

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

5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/tabbed/sqlc-gen-python
go 1.19

require (
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1
github.com/google/go-cmp v0.5.9
github.com/jinzhu/inflection v1.0.0
github.com/mailru/easyjson v0.7.7
github.com/tabbed/sqlc-go v1.16.0
google.golang.org/protobuf v1.28.1
github.com/tabbed/sqlc-go v1.18.0
google.golang.org/protobuf v1.30.0
)

require github.com/josharian/intern v1.0.0 // indirect
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1 h1:ze0HODAjPRXSkiqSpDTYq2baS4IVtRtDLSZY2p1ZCX4=
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1/go.mod h1:DSpReHp8PwHOeCfGymiiY4HSx2iVL358X7JRMciL7T0=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
Expand All @@ -10,7 +12,9 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/tabbed/sqlc-go v1.16.0 h1:EwPBXdGn5tyrLjcNiHRoQthWvJeF5NjG9Cx1WK5iFsY=
github.com/tabbed/sqlc-go v1.16.0/go.mod h1:mqMU5duZRGz5Wp/qJXwkERf+MXgGOZ8BmW/tH9KyvWA=
github.com/tabbed/sqlc-go v1.18.0 h1:GNE8b8xue8fKVptQnr3Z6DV8FqdokyDYML7O0kYtbe4=
github.com/tabbed/sqlc-go v1.18.0/go.mod h1:qx8ocsmviBDyRfLNuJQtdu0f5oqa8XBjKxMldl+Wm24=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
12 changes: 6 additions & 6 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"sort"
"strings"

"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
easyjson "github.com/mailru/easyjson"
plugin "github.com/tabbed/sqlc-go/codegen"
"github.com/tabbed/sqlc-go/metadata"
"github.com/tabbed/sqlc-go/sdk"

Expand Down Expand Up @@ -192,15 +192,15 @@ func makePyType(req *plugin.CodeGenRequest, col *plugin.Column) pyType {
func pyInnerType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
for _, oride := range req.Settings.Overrides {
if !pyTypeIsSet(oride.PythonType) {
if !pyTypeIsSet(oride) {
continue
}
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
return pyTypeString(oride.PythonType)
return oride.CodeType
}
if oride.DbType != "" && oride.DbType == columnType && oride.Nullable != (col.NotNull || col.IsArray) {
return pyTypeString(oride.PythonType)
return oride.CodeType
}
}

Expand Down Expand Up @@ -1091,7 +1091,7 @@ func HashComment(s string) string {
return "# " + strings.ReplaceAll(s, "\n", "\n# ")
}

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

resp := plugin.Response{}
resp := plugin.CodeGenResponse{}

for filename, code := range output {
resp.Files = append(resp.Files, &plugin.File{
Expand Down
36 changes: 19 additions & 17 deletions internal/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sort"
"strings"

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

type importSpec struct {
Expand All @@ -14,15 +14,8 @@ type importSpec struct {
Alias string
}

func pyTypeIsSet(t *codegen.PythonType) bool {
return t.Module != "" || t.Name != ""
}

func pyTypeString(t *codegen.PythonType) string {
if t.Name != "" && t.Module == "" {
return t.Name
}
return t.Module + "." + t.Name
func pyTypeIsSet(o *plugin.Override) bool {
return o.CodeType != ""
}

func (i importSpec) String() string {
Expand All @@ -39,7 +32,7 @@ func (i importSpec) String() string {
}

type importer struct {
Settings *codegen.Settings
Settings *plugin.Settings
Models []Struct
Queries []Query
Enums []Enum
Expand Down Expand Up @@ -112,12 +105,17 @@ func (i *importer) modelImportSpecs() (map[string]importSpec, map[string]importS
pkg := make(map[string]importSpec)

for _, o := range i.Settings.Overrides {
if pyTypeIsSet(o.PythonType) && o.PythonType.Module != "" {
if modelUses(pyTypeString(o.PythonType)) {
pkg[o.PythonType.Module] = importSpec{Module: o.PythonType.Module}
if pyTypeIsSet(o) {
mod, _, found := strings.Cut(o.CodeType, ".")
if !found {
continue
}
if modelUses(o.CodeType) {
pkg[mod] = importSpec{Module: mod}
}
}
}

return std, pkg
}

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

for _, o := range i.Settings.Overrides {
if pyTypeIsSet(o.PythonType) && o.PythonType.Module != "" {
if queryUses(pyTypeString(o.PythonType)) {
pkg[o.PythonType.Module] = importSpec{Module: o.PythonType.Module}
if pyTypeIsSet(o) {
mod, _, found := strings.Cut(o.CodeType, ".")
if !found {
continue
}
if queryUses(o.CodeType) {
pkg[mod] = importSpec{Module: mod}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package python
import (
"log"

"github.com/tabbed/sqlc-go/codegen"
"buf.build/gen/go/sqlc/sqlc/protocolbuffers/go/protos/plugin"
"github.com/tabbed/sqlc-go/sdk"
)

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

switch columnType {
Expand Down