Skip to content

Commit

Permalink
build: Migrate from TinyGo to Go 1.21 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Jun 22, 2023
1 parent e9a28f2 commit 6a72f5b
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 630 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ jobs:
- uses: actions/checkout@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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ sqlc-gen-kotlin:
cd plugin && go build -o ~/bin/sqlc-gen-kotlin ./main.go

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

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

require (
buf.build/gen/go/sqlc/sqlc/protocolbuffers/go v1.30.0-20230621221448-196413f69ab3.1
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
)

require (
github.com/josharian/intern v1.0.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
require google.golang.org/protobuf v1.30.0 // indirect
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
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=
131 changes: 0 additions & 131 deletions internal/core/config_easyjson.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/core/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

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

Expand Down
2 changes: 1 addition & 1 deletion internal/core/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sort"
"strings"

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

type Importer struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/mysql_type.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package core

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

Expand Down
2 changes: 1 addition & 1 deletion internal/core/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package core
import (
"log"

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

Expand Down
47 changes: 36 additions & 11 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@ import (
"bufio"
"bytes"
"context"
"io"
_ "embed"
"encoding/json"
"strings"
"text/template"

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

"github.com/tabbed/sqlc-gen-kotlin/internal/core"
"github.com/tabbed/sqlc-gen-kotlin/internal/tmpl"
"github.com/tabbed/sqlc-go/sdk"
)

func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error) {
//go:embed tmpl/ktmodels.tmpl
var ktModelsTmpl string

//go:embed tmpl/ktsql.tmpl
var ktSqlTmpl string

//go:embed tmpl/ktiface.tmpl
var ktIfaceTmpl string

func Offset(v int) int {
return v + 1
}

func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
var conf core.Config
if len(req.PluginOptions) > 0 {
if err := easyjson.Unmarshal(req.PluginOptions, &conf); err != nil {
if err := json.Unmarshal(req.PluginOptions, &conf); err != nil {
return nil, err
}
}
Expand All @@ -36,6 +50,17 @@ func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error
Queries: queries,
}

funcMap := template.FuncMap{
"lowerTitle": sdk.LowerTitle,
"comment": sdk.DoubleSlashComment,
"imports": i.Imports,
"offset": Offset,
}

modelsFile := template.Must(template.New("table").Funcs(funcMap).Parse(ktModelsTmpl))
sqlFile := template.Must(template.New("table").Funcs(funcMap).Parse(ktSqlTmpl))
ifaceFile := template.Must(template.New("table").Funcs(funcMap).Parse(ktIfaceTmpl))

core.DefaultImporter = i

tctx := core.KtTmplCtx{
Expand All @@ -50,11 +75,11 @@ func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error

output := map[string]string{}

execute := func(name string, f func(io.Writer, core.KtTmplCtx) error) error {
execute := func(name string, t *template.Template) error {
var b bytes.Buffer
w := bufio.NewWriter(&b)
tctx.SourceName = name
err := f(w, tctx)
err := t.Execute(w, tctx)
w.Flush()
if err != nil {
return err
Expand All @@ -66,13 +91,13 @@ func Generate(ctx context.Context, req *plugin.Request) (*plugin.Response, error
return nil
}

if err := execute("Models.kt", tmpl.KtModels); err != nil {
if err := execute("Models.kt", modelsFile); err != nil {
return nil, err
}
if err := execute("Queries.kt", tmpl.KtIface); err != nil {
if err := execute("Queries.kt", ifaceFile); err != nil {
return nil, err
}
if err := execute("QueriesImpl.kt", tmpl.KtSQL); err != nil {
if err := execute("QueriesImpl.kt", sqlFile); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit 6a72f5b

Please sign in to comment.