Skip to content

Commit 7f895d0

Browse files
authored
Update proto and support async (#22)
* refactor * update * update * update * process options in plugins * update
1 parent 09ee5c5 commit 7f895d0

File tree

9 files changed

+2200
-1269
lines changed

9 files changed

+2200
-1269
lines changed

Makefile

-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
internal/plugin/codegen.pb.go: protos/plugin/codegen.proto
2-
protoc -I ./protos \
3-
--go_out=. \
4-
--go_opt=module=github.com/swuecho/sqlc-fs \
5-
--go-vtproto_out=. \
6-
--go-vtproto_opt=module=github.com/swuecho/sqlc-fs,features=marshal+unmarshal+size \
7-
./protos/plugin/codegen.proto
8-
91
gen:
102
go build -o bin/sqlc-fs
113
sqlc generate -f ./testdata/sqlc.json

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,38 @@ go install github.com/swuecho/sqlc-fs@latest
3939
]
4040
```
4141
42+
for async query:
43+
44+
```json5
45+
// sqlc.json
46+
{
47+
"version": "2",
48+
"plugins": [
49+
{
50+
"name": "fs",
51+
"process": {
52+
"cmd": "sqlc-fs"
53+
}
54+
}
55+
],
56+
"sql": [
57+
{
58+
"schema": "schema.sql",
59+
"queries": "query/",
60+
"engine": "postgresql",
61+
"codegen": [
62+
{
63+
"out": "gen",
64+
"plugin": "fs",
65+
"options": {
66+
"async": true
67+
}
68+
}
69+
]
70+
}
71+
]
72+
```
73+
4274
## Dev & Contribution
4375
4476
```build

gen.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"encoding/json"
7+
"fmt"
78
"strings"
89
"text/template"
910

@@ -15,17 +16,43 @@ import (
1516

1617
func Generate(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
1718
structs := buildStructs(req)
19+
20+
options, err := parseOptions(req)
21+
if err != nil {
22+
return nil, errors.Errorf("error parse options: %w", err)
23+
}
24+
1825
queries, err := buildQueries(req, structs)
1926
if err != nil {
2027
return nil, errors.Errorf("error generating queries: %w", err)
2128
}
22-
return generate(req, structs, queries)
29+
return generate(req, structs, queries, options)
30+
}
31+
32+
type FSharpOption struct {
33+
Async bool
34+
}
35+
36+
func parseOptions(req *plugin.CodeGenRequest) (*FSharpOption, error) {
37+
if req.Settings.Codegen != nil {
38+
if len(req.Settings.Codegen.Options) != 0 {
39+
var options *FSharpOption
40+
dec := json.NewDecoder(bytes.NewReader(req.Settings.Codegen.Options))
41+
dec.DisallowUnknownFields()
42+
if err := dec.Decode(&options); err != nil {
43+
return options, fmt.Errorf("unmarshalling options: %s", err)
44+
}
45+
return options, nil
46+
}
47+
}
48+
return new(FSharpOption), nil
2349
}
2450

2551
type tmplCtx struct {
2652
Q string
2753
Structs []Struct
2854
Queries []Query
55+
Options *FSharpOption
2956

3057
// XXX: race
3158
SourceName string
@@ -35,7 +62,7 @@ func (t *tmplCtx) OutputQuery(sourceName string) bool {
3562
return t.SourceName == sourceName
3663
}
3764

38-
func generate(req *plugin.CodeGenRequest, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
65+
func generate(req *plugin.CodeGenRequest, structs []Struct, queries []Query, options *FSharpOption) (*plugin.CodeGenResponse, error) {
3966
funcMap := template.FuncMap{
4067
"stem": sdk.Stem,
4168
"pascalCase": sdk.ToPascalCase,
@@ -58,6 +85,7 @@ func generate(req *plugin.CodeGenRequest, structs []Struct, queries []Query) (*p
5885
Q: "\"\"\"",
5986
Queries: queries,
6087
Structs: structs,
88+
Options: options,
6189
}
6290

6391
output := map[string]string{}

internal/plugin/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
the go file in plugin folder is copied from sqlc(c13b156)

0 commit comments

Comments
 (0)