@@ -2,22 +2,51 @@ package ext
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+
7
+ "google.golang.org/grpc"
8
+ "google.golang.org/grpc/codes"
9
+ "google.golang.org/grpc/status"
5
10
6
11
"github.com/sqlc-dev/sqlc/internal/plugin"
7
12
)
8
13
9
14
type Handler interface {
10
- Generate (context.Context , * plugin.CodeGenRequest ) (* plugin.CodeGenResponse , error )
15
+ Generate (context.Context , * plugin.GenerateRequest ) (* plugin.GenerateResponse , error )
16
+
17
+ Invoke (ctx context.Context , method string , args any , reply any , opts ... grpc.CallOption ) error
18
+ NewStream (ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ) (grpc.ClientStream , error )
11
19
}
12
20
13
21
type wrapper struct {
14
- fn func (context.Context , * plugin.CodeGenRequest ) (* plugin.CodeGenResponse , error )
22
+ fn func (context.Context , * plugin.GenerateRequest ) (* plugin.GenerateResponse , error )
15
23
}
16
24
17
- func (w * wrapper ) Generate (ctx context.Context , req * plugin.CodeGenRequest ) (* plugin.CodeGenResponse , error ) {
25
+ func (w * wrapper ) Generate (ctx context.Context , req * plugin.GenerateRequest ) (* plugin.GenerateResponse , error ) {
18
26
return w .fn (ctx , req )
19
27
}
20
28
21
- func HandleFunc (fn func (context.Context , * plugin.CodeGenRequest ) (* plugin.CodeGenResponse , error )) Handler {
29
+ func (w * wrapper ) Invoke (ctx context.Context , method string , args any , reply any , opts ... grpc.CallOption ) error {
30
+ req , ok := args .(* plugin.GenerateRequest )
31
+ if ! ok {
32
+ return fmt .Errorf ("args isn't a GenerateRequest" )
33
+ }
34
+ resp , ok := reply .(* plugin.GenerateResponse )
35
+ if ! ok {
36
+ return fmt .Errorf ("reply isn't a GenerateResponse" )
37
+ }
38
+ res , err := w .Generate (ctx , req )
39
+ if err != nil {
40
+ return err
41
+ }
42
+ resp .Files = res .Files
43
+ return nil
44
+ }
45
+
46
+ func (w * wrapper ) NewStream (ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ) (grpc.ClientStream , error ) {
47
+ return nil , status .Error (codes .Unimplemented , "" )
48
+ }
49
+
50
+ func HandleFunc (fn func (context.Context , * plugin.GenerateRequest ) (* plugin.GenerateResponse , error )) Handler {
22
51
return & wrapper {fn }
23
52
}
0 commit comments