Skip to content

Commit 4507ede

Browse files
feat(plugin): Use gRPC interface for codegen plugin communication (#2930)
* feat(plugin): Use gRPC interface for codegen plugin communication * rename proto rpc service and messages * make invoke methods more generic * remove vtproto and add regular grpc buf plugin
1 parent a225849 commit 4507ede

File tree

23 files changed

+346
-6986
lines changed

23 files changed

+346
-6986
lines changed

buf.gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ plugins:
55
- plugin: buf.build/protocolbuffers/go:v1.30.0
66
out: internal
77
opt: paths=source_relative
8-
- plugin: buf.build/community/planetscale-vtprotobuf:v0.4.0
8+
- plugin: buf.build/grpc/go:v1.3.0
99
out: internal
1010
opt: paths=source_relative

cmd/sqlc-gen-json/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/sqlc-dev/sqlc/internal/codegen/json"
1111
"github.com/sqlc-dev/sqlc/internal/plugin"
12+
"google.golang.org/protobuf/proto"
1213
)
1314

1415
func main() {
@@ -19,19 +20,19 @@ func main() {
1920
}
2021

2122
func run() error {
22-
var req plugin.CodeGenRequest
23+
var req plugin.GenerateRequest
2324
reqBlob, err := io.ReadAll(os.Stdin)
2425
if err != nil {
2526
return err
2627
}
27-
if err := req.UnmarshalVT(reqBlob); err != nil {
28+
if err := proto.Unmarshal(reqBlob, &req); err != nil {
2829
return err
2930
}
3031
resp, err := json.Generate(context.Background(), &req)
3132
if err != nil {
3233
return err
3334
}
34-
respBlob, err := resp.MarshalVT()
35+
respBlob, err := proto.Marshal(resp)
3536
if err != nil {
3637
return err
3738
}

internal/cmd/generate.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sync"
1515

1616
"golang.org/x/sync/errgroup"
17+
"google.golang.org/grpc"
1718
"google.golang.org/grpc/status"
1819

1920
"github.com/sqlc-dev/sqlc/internal/codegen/golang"
@@ -380,10 +381,10 @@ func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.C
380381
return c.Result(), false
381382
}
382383

383-
func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, result *compiler.Result) (string, *plugin.CodeGenResponse, error) {
384+
func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, result *compiler.Result) (string, *plugin.GenerateResponse, error) {
384385
defer trace.StartRegion(ctx, "codegen").End()
385386
req := codeGenRequest(result, combo)
386-
var handler ext.Handler
387+
var handler grpc.ClientConnInterface
387388
var out string
388389
switch {
389390
case sql.Plugin != nil:
@@ -453,6 +454,7 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
453454
default:
454455
return "", nil, fmt.Errorf("missing language backend")
455456
}
456-
resp, err := handler.Generate(ctx, req)
457+
client := plugin.NewCodegenServiceClient(handler)
458+
resp, err := client.Generate(ctx, req)
457459
return out, resp, err
458460
}

internal/cmd/shim.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ func pluginQueryParam(p compiler.Parameter) *plugin.Parameter {
223223
}
224224
}
225225

226-
func codeGenRequest(r *compiler.Result, settings config.CombinedSettings) *plugin.CodeGenRequest {
227-
return &plugin.CodeGenRequest{
226+
func codeGenRequest(r *compiler.Result, settings config.CombinedSettings) *plugin.GenerateRequest {
227+
return &plugin.GenerateRequest{
228228
Settings: pluginSettings(r, settings),
229229
Catalog: pluginCatalog(r.Catalog),
230230
Queries: pluginQueries(r),

internal/cmd/vet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
629629
return nil
630630
}
631631

632-
func vetConfig(req *plugin.CodeGenRequest) *vet.Config {
632+
func vetConfig(req *plugin.GenerateRequest) *vet.Config {
633633
return &vet.Config{
634634
Version: req.Settings.Version,
635635
Engine: req.Settings.Engine,

internal/codegen/golang/gen.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (t *tmplCtx) codegenQueryRetval(q Query) (string, error) {
103103
}
104104
}
105105

106-
func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {
106+
func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
107107
options, err := opts.Parse(req)
108108
if err != nil {
109109
return nil, err
@@ -127,7 +127,7 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR
127127
return generate(req, options, enums, structs, queries)
128128
}
129129

130-
func generate(req *plugin.CodeGenRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.CodeGenResponse, error) {
130+
func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.GenerateResponse, error) {
131131
i := &importer{
132132
Options: options,
133133
Queries: queries,
@@ -282,7 +282,7 @@ func generate(req *plugin.CodeGenRequest, options *opts.Options, enums []Enum, s
282282
return nil, err
283283
}
284284
}
285-
resp := plugin.CodeGenResponse{}
285+
resp := plugin.GenerateResponse{}
286286

287287
for filename, code := range output {
288288
resp.Files = append(resp.Files, &plugin.File{

internal/codegen/golang/go_type.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/sqlc-dev/sqlc/internal/plugin"
99
)
1010

11-
func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) {
11+
func addExtraGoStructTags(tags map[string]string, req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) {
1212
for _, override := range options.Overrides {
1313
oride := override.ShimOverride
1414
if oride.GoType.StructTags == nil {
@@ -33,7 +33,7 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, op
3333
}
3434
}
3535

36-
func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
36+
func goType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
3737
// Check if the column's type has been overridden
3838
for _, override := range options.Overrides {
3939
oride := override.ShimOverride
@@ -63,7 +63,7 @@ func goType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Colum
6363
return typ
6464
}
6565

66-
func goInnerType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
66+
func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
6767
columnType := sdk.DataType(col.Type)
6868
notNull := col.NotNull || col.IsArray
6969

internal/codegen/golang/mysql_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/sqlc-dev/sqlc/internal/plugin"
1010
)
1111

12-
func mysqlType(req *plugin.CodeGenRequest, options *opts.Options, col *plugin.Column) string {
12+
func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
1313
columnType := sdk.DataType(col.Type)
1414
notNull := col.NotNull || col.IsArray
1515
unsigned := col.Unsigned

internal/codegen/golang/opts/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type GlobalOptions struct {
4747
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
4848
}
4949

50-
func Parse(req *plugin.CodeGenRequest) (*Options, error) {
50+
func Parse(req *plugin.GenerateRequest) (*Options, error) {
5151
options, err := parseOpts(req)
5252
if err != nil {
5353
return nil, err
@@ -68,7 +68,7 @@ func Parse(req *plugin.CodeGenRequest) (*Options, error) {
6868
return options, nil
6969
}
7070

71-
func parseOpts(req *plugin.CodeGenRequest) (*Options, error) {
71+
func parseOpts(req *plugin.GenerateRequest) (*Options, error) {
7272
var options Options
7373
if len(req.PluginOptions) == 0 {
7474
return &options, nil
@@ -91,7 +91,7 @@ func parseOpts(req *plugin.CodeGenRequest) (*Options, error) {
9191
return &options, nil
9292
}
9393

94-
func parseGlobalOpts(req *plugin.CodeGenRequest) (*GlobalOptions, error) {
94+
func parseGlobalOpts(req *plugin.GenerateRequest) (*GlobalOptions, error) {
9595
var options GlobalOptions
9696
if len(req.GlobalOptions) == 0 {
9797
return &options, nil

internal/codegen/golang/opts/override.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (o *Override) Matches(n *plugin.Identifier, defaultSchema string) bool {
7676
return true
7777
}
7878

79-
func (o *Override) parse(req *plugin.CodeGenRequest) (err error) {
79+
func (o *Override) parse(req *plugin.GenerateRequest) (err error) {
8080
// validate deprecated postgres_type field
8181
if o.Deprecated_PostgresType != "" {
8282
fmt.Fprintf(os.Stderr, "WARNING: \"postgres_type\" is deprecated. Instead, use \"db_type\" to specify a type override.\n")

0 commit comments

Comments
 (0)