From a456aed3f4154a99ae61a3625ddc8e615bc03772 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 21 Jun 2023 16:08:42 -0700 Subject: [PATCH 1/4] sqlc-gen-python: Build using Go 1.21rc2 --- Makefile | 3 +-- go.mod | 7 +++++-- go.sum | 3 +++ internal/gen.go | 12 ++++++------ internal/imports.go | 36 +++++++++++++++++++----------------- internal/postgresql_type.go | 4 ++-- 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index ab8a5ee..e601904 100644 --- a/Makefile +++ b/Makefile @@ -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 /Users/kyle/projects/goroot/bin/go build -o sqlc-gen-python.wasm main.go openssl sha256 plugin/sqlc-gen-python.wasm - diff --git a/go.mod b/go.mod index f2ab595..b8be99c 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,10 @@ require ( 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 + google.golang.org/protobuf v1.30.0 ) -require github.com/josharian/intern v1.0.0 // indirect +require ( + buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1 // indirect + github.com/josharian/intern v1.0.0 // indirect +) diff --git a/go.sum b/go.sum index f660426..eb122f3 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -14,3 +16,4 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T 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/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/internal/gen.go b/internal/gen.go index ce3d2e0..5a3528d 100644 --- a/internal/gen.go +++ b/internal/gen.go @@ -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" @@ -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 } } @@ -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 { @@ -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{ diff --git a/internal/imports.go b/internal/imports.go index e5702d0..b78f9bd 100644 --- a/internal/imports.go +++ b/internal/imports.go @@ -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 { @@ -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 { @@ -39,7 +32,7 @@ func (i importSpec) String() string { } type importer struct { - Settings *codegen.Settings + Settings *plugin.Settings Models []Struct Queries []Query Enums []Enum @@ -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 } @@ -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} } } } diff --git a/internal/postgresql_type.go b/internal/postgresql_type.go index 8579e90..225233e 100644 --- a/internal/postgresql_type.go +++ b/internal/postgresql_type.go @@ -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 { From 1dd5e766c1dba3e04e6ccad24cd2f0a115ee2f3b Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 21 Jun 2023 16:19:06 -0700 Subject: [PATCH 2/4] deps: Use go-sdk v1.18.0 --- go.mod | 8 +++----- go.sum | 5 +++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index b8be99c..dccbc3b 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +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 + github.com/tabbed/sqlc-go v1.18.0 google.golang.org/protobuf v1.30.0 ) -require ( - buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1 // indirect - github.com/josharian/intern v1.0.0 // indirect -) +require github.com/josharian/intern v1.0.0 // indirect diff --git a/go.sum b/go.sum index eb122f3..4a7f5a6 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +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= From ddc27dfc6475bb7b61501b9925f785876569ea50 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 21 Jun 2023 16:24:12 -0700 Subject: [PATCH 3/4] Use go1.21rc --- .github/workflows/ci.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d774d8e..0941f54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 From a6378664e40cd070e76d8e77818042d9ad9d35de Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 21 Jun 2023 16:25:36 -0700 Subject: [PATCH 4/4] Don't hardcode path --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e601904..9a262fc 100644 --- a/Makefile +++ b/Makefile @@ -4,5 +4,5 @@ sqlc-gen-python: cd plugin && go build -o ~/bin/sqlc-gen-python ./main.go sqlc-gen-python.wasm: - cd plugin && GOOS=wasip1 GOARCH=wasm /Users/kyle/projects/goroot/bin/go build -o sqlc-gen-python.wasm main.go + cd plugin && GOOS=wasip1 GOARCH=wasm go build -o sqlc-gen-python.wasm main.go openssl sha256 plugin/sqlc-gen-python.wasm