Skip to content

Commit 3557532

Browse files
feat: add append optiom
1 parent 7dbfca4 commit 3557532

File tree

6 files changed

+131
-241
lines changed

6 files changed

+131
-241
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ sql:
2121
options:
2222
package: db
2323
sql_package: pgx/v5
24+
server_type: grpc
2425
```
2526
2627
## Post-process
2728
28-
After execute `sqlc generate` you need to organize imorts, compile protocol buffer and fix go.mod.
29+
After execute `sqlc generate` you need to organize imports, compile protocol buffer and fix go.mod.
2930

3031
1. Install the required tools:
3132

@@ -58,6 +59,8 @@ buf generate
5859
go mod tidy
5960
```
6061

62+
5. If you define more than one package, you’ll need to add them to the generated **registry.go** file.
63+
6164
## Plugin options
6265

6366
You can use all of the [Golang plugin's options](https://docs.sqlc.dev/en/latest/reference/config.html#go) as well as the options described below.
@@ -77,15 +80,14 @@ sql:
7780
- plugin: go-server
7881
out: "db"
7982
options:
80-
package: "db"
81-
emit_json_tags: true
82-
server_type: "grpc" # The server type: grpc or connect.
83+
server_type: "grpc" # The server type: grpc or connect.
8384
module: "my-module" # The module name for the generated go.mod.
8485
tracing: false # If true, enable distributed tracing with open telemetry.
8586
litefs: false # If true, enable support for distributed SQLite powered by embedded LiteFS.
86-
litestream: false # If true, enable support for continuous backup sqlite to S3 powered by embeded Litestream
87-
migration_path: "" # If you want to execute database migrations on startup
88-
skip_go_mod: false # If true, skip the generation of the go.mod
87+
litestream: false # If true, enable support for continuous backup sqlite to S3 powered by embeded Litestream.
88+
migration_path: "" # If you want to execute database migrations on startup.
89+
skip_go_mod: false # If true, skip the generation of the go.mod.
90+
append: false # If true, enable the append mode and do not generate the editable files.
8991
```
9092

9193
## Building from source

internal/gen.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -324,26 +324,9 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
324324
})
325325
}
326326

327-
if options.ServerType == "" {
328-
options.ServerType = "grpc"
329-
}
330-
var (
331-
err error
332-
serverFiles []*plugin.File
333-
)
334-
switch options.ServerType {
335-
case "connect":
336-
serverFiles, err = connectFiles(req, options, enums, structs, queries)
337-
if err != nil {
338-
return nil, err
339-
}
340-
case "grpc":
341-
serverFiles, err = grpcFiles(req, options, enums, structs, queries)
342-
if err != nil {
343-
return nil, err
344-
}
345-
default:
346-
return nil, fmt.Errorf("invalid server_type %q. Choose 'connect' or 'grpc'", options.ServerType)
327+
serverFiles, err := serverFiles(req, options, enums, structs, queries)
328+
if err != nil {
329+
return nil, err
347330
}
348331
resp.Files = append(resp.Files, serverFiles...)
349332

internal/opts/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Options struct {
5050
MigrationPath string `json:"migration_path,omitempty" yaml:"migration_path"`
5151
SkipGoMod bool `json:"skip_go_mod,omitempty" yaml:"skip_go_mod"`
5252
ServerType string `json:"server_type,omitempty" yaml:"server_type"`
53+
Append bool `json:"append,omitempty" yaml:"append"`
5354
}
5455

5556
type GlobalOptions struct {

internal/server.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,134 @@ import (
77
"io"
88
"io/fs"
99
"log"
10+
"path/filepath"
1011
"sort"
1112
"strings"
1213
"text/template"
1314

1415
"github.com/sqlc-dev/plugin-sdk-go/plugin"
1516
"github.com/sqlc-dev/sqlc-gen-go/internal/opts"
17+
connecttemplates "github.com/walterwanderley/sqlc-connect/templates"
1618
"github.com/walterwanderley/sqlc-grpc/converter"
1719
"github.com/walterwanderley/sqlc-grpc/metadata"
20+
grpctemplates "github.com/walterwanderley/sqlc-grpc/templates"
1821
)
1922

23+
func serverFiles(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) ([]*plugin.File, error) {
24+
var (
25+
tmplFS fs.FS
26+
tmplFuncs template.FuncMap
27+
)
28+
29+
switch options.ServerType {
30+
case "", "grpc": // the default server type
31+
tmplFS = grpctemplates.Files
32+
tmplFuncs = grpctemplates.Funcs
33+
case "connect":
34+
tmplFS = connecttemplates.Files
35+
tmplFuncs = connecttemplates.Funcs
36+
default:
37+
return nil, fmt.Errorf("invalid server_type %q. Choose 'connect' or 'grpc'", options.ServerType)
38+
}
39+
def := toServerDefinition(req, options, enums, structs, queries)
40+
if err := def.Validate(); err != nil {
41+
return nil, err
42+
}
43+
pkg := def.Packages[0]
44+
depth := make([]string, 0)
45+
for i := 0; i < len(strings.Split(options.Out, string(filepath.Separator)))+1; i++ {
46+
depth = append(depth, "..")
47+
}
48+
toRootPath := filepath.Join(depth...)
49+
files := make([]*plugin.File, 0)
50+
err := fs.WalkDir(tmplFS, ".", func(path string, d fs.DirEntry, err error) error {
51+
if err != nil {
52+
log.Println("ERROR ", err.Error())
53+
return err
54+
}
55+
56+
if d.IsDir() {
57+
return nil
58+
}
59+
60+
newPath := strings.TrimSuffix(path, ".tmpl")
61+
62+
if strings.HasSuffix(newPath, "templates.go") {
63+
return nil
64+
}
65+
log.Println(path, "...")
66+
if strings.HasSuffix(newPath, "service.proto") {
67+
protoContent, err := execServerTemplate(tmplFS, tmplFuncs, path, pkg, false)
68+
if err != nil {
69+
return err
70+
}
71+
protoFile := filepath.Join(toRootPath, "proto", converter.ToSnakeCase(pkg.Package), "v1", (converter.ToSnakeCase(pkg.Package) + ".proto"))
72+
files = append(files, &plugin.File{
73+
Name: protoFile,
74+
Contents: protoContent,
75+
})
76+
return nil
77+
}
78+
79+
if strings.HasSuffix(newPath, "adapters.go") || strings.HasSuffix(newPath, "service.go") || strings.HasSuffix(newPath, "service.factory.go") {
80+
if options.Append && strings.HasSuffix(newPath, "service.factory.go") {
81+
return nil
82+
}
83+
content, err := execServerTemplate(tmplFS, tmplFuncs, path, pkg, true)
84+
if err != nil {
85+
return err
86+
}
87+
files = append(files, &plugin.File{
88+
Name: newPath,
89+
Contents: content,
90+
})
91+
return nil
92+
}
93+
94+
if options.Append {
95+
return nil
96+
}
97+
98+
if strings.HasSuffix(newPath, "tracing.go") && !def.DistributedTracing {
99+
return nil
100+
}
101+
102+
if strings.HasSuffix(newPath, "migration.go") && def.MigrationPath == "" {
103+
return nil
104+
}
105+
106+
if strings.HasSuffix(newPath, "litestream.go") && !(def.Database() == "sqlite" && def.Litestream) {
107+
return nil
108+
}
109+
110+
if (strings.HasSuffix(newPath, "litefs.go") || strings.HasSuffix(newPath, "forward.go")) && !(def.Database() == "sqlite" && def.LiteFS) {
111+
return nil
112+
}
113+
114+
content, err := execServerTemplate(tmplFS, tmplFuncs, path, def, strings.HasSuffix(newPath, ".go"))
115+
if err != nil {
116+
return err
117+
}
118+
files = append(files, &plugin.File{
119+
Name: filepath.Join(toRootPath, newPath),
120+
Contents: content,
121+
})
122+
123+
return nil
124+
})
125+
if err != nil {
126+
return nil, err
127+
}
128+
if !options.SkipGoMod {
129+
files = append(files, &plugin.File{
130+
Name: filepath.Join(toRootPath, "go.mod"),
131+
Contents: []byte(fmt.Sprintf("module %s\n", def.GoModule)),
132+
})
133+
}
134+
135+
return files, nil
136+
}
137+
20138
func toServerDefinition(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) *metadata.Definition {
21139
module := options.Module
22140
if module == "" {

internal/server_connect.go

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)