forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
52 lines (42 loc) · 1.5 KB
/
handler.go
File metadata and controls
52 lines (42 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ext
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/sqlc-dev/sqlc/internal/plugin"
)
type Handler interface {
Generate(context.Context, *plugin.GenerateRequest) (*plugin.GenerateResponse, error)
Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error
NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)
}
type wrapper struct {
fn func(context.Context, *plugin.GenerateRequest) (*plugin.GenerateResponse, error)
}
func (w *wrapper) Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.GenerateResponse, error) {
return w.fn(ctx, req)
}
func (w *wrapper) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error {
req, ok := args.(*plugin.GenerateRequest)
if !ok {
return fmt.Errorf("args isn't a GenerateRequest")
}
resp, ok := reply.(*plugin.GenerateResponse)
if !ok {
return fmt.Errorf("reply isn't a GenerateResponse")
}
res, err := w.Generate(ctx, req)
if err != nil {
return err
}
resp.Files = res.Files
return nil
}
func (w *wrapper) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func HandleFunc(fn func(context.Context, *plugin.GenerateRequest) (*plugin.GenerateResponse, error)) Handler {
return &wrapper{fn}
}