diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..bbd00d7 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,28 @@ +name: go + +on: + push: + tags: + - "v*" + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: stable + # More assembly might b + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..ff5eeef --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,7 @@ +builds: + - id: wasm + main: ./plugin + goos: ["wasip1"] + goarch: ["wasm"] +archives: + - format: binary diff --git a/Makefile b/Makefile index f409492..febaf91 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ test: bin/sqlc-gen-go.wasm all: bin/sqlc-gen-go bin/sqlc-gen-go.wasm -bin/sqlc-gen-go: bin go.mod go.sum $(wildcard **/*.go) +bin/sqlc-gen-go: bin go.mod go.sum $(wildcard **/*.go) $(wildcard internal/templates/**/*.tmpl) cd plugin && go build -o ../bin/sqlc-gen-go ./main.go bin/sqlc-gen-go.wasm: bin/sqlc-gen-go diff --git a/internal/templates/stdlib/queryCode.tmpl b/internal/templates/stdlib/queryCode.tmpl index cf56000..f07c1c7 100644 --- a/internal/templates/stdlib/queryCode.tmpl +++ b/internal/templates/stdlib/queryCode.tmpl @@ -22,11 +22,15 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}} {{if eq .Cmd ":one"}} {{range .Comments}}//{{.}} {{end -}} -func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) { +func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}} {{if ne .Arg.Pair "" }},{{end}} {{ .Ret.Name }}OptionalInitFunc func(*{{.Ret.DefineType}})) ({{.Ret.DefineType}}, error) { {{- template "queryCodeStdExec" . }} {{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }} var {{.Ret.Name}} {{.Ret.Type}} {{- end}} + if {{ .Ret.Name }}OptionalInitFunc != nil { + {{ .Ret.Name }}OptionalInitFunc(&{{.Ret.ReturnName}}) + } + err := row.Scan({{.Ret.Scan}}) return {{.Ret.ReturnName}}, err } @@ -35,7 +39,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}} {{if eq .Cmd ":many"}} {{range .Comments}}//{{.}} {{end -}} -func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) { +func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}} {{if ne .Arg.Pair "" }},{{end}} {{ .Ret.Name }}OptionalInitFunc func(*{{.Ret.DefineType}}), {{ .Ret.Name }}OptionalFilterFunc func({{.Ret.DefineType}}) bool) ([]{{.Ret.DefineType}}, error) { {{- template "queryCodeStdExec" . }} if err != nil { return nil, err @@ -47,10 +51,18 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}} var items []{{.Ret.DefineType}} {{end -}} for rows.Next() { - var {{.Ret.Name}} {{.Ret.Type}} + var {{.Ret.Name}} {{.Ret.Type}} + if {{ .Ret.Name }}OptionalInitFunc != nil { + {{ .Ret.Name }}OptionalInitFunc(&{{.Ret.ReturnName}}) + } + if err := rows.Scan({{.Ret.Scan}}); err != nil { return nil, err } + + if {{ .Ret.Name }}OptionalFilterFunc != nil && !{{ .Ret.Name }}OptionalFilterFunc({{.Ret.ReturnName}}) { + continue + } items = append(items, {{.Ret.ReturnName}}) } if err := rows.Close(); err != nil {