-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_rpc.go
59 lines (47 loc) · 1.15 KB
/
generator_rpc.go
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
53
54
55
56
57
58
59
package specraft
import (
"io"
"net/rpc"
"github.com/hashicorp/go-plugin"
)
type Generator interface {
Generate(opts any, patterns ...string) (io.Reader, error)
}
type GeneratorRPC struct {
client *rpc.Client
}
type GeneratorGenerateRequest struct {
Patterns []string
Opts any
}
type GeneratorGenerateResponse struct {
Data io.Reader
Err error
}
func (g *GeneratorRPC) Generate(patterns ...string) (io.Reader, error) {
res := &GeneratorGenerateResponse{}
req := &GeneratorGenerateRequest{
Patterns: patterns,
}
err := g.client.Call("Plugin.Generate", req, res)
if err != nil {
return nil, err
}
return res.Data, res.Err
}
type GeneratorRPCServer struct {
Impl Generator
}
func (s *GeneratorRPCServer) Generate(req GeneratorGenerateRequest, res *GeneratorGenerateResponse) error {
res.Data, res.Err = s.Impl.Generate(req.Opts, req.Patterns...)
return nil
}
type GeneratorPlugin struct {
Impl Generator
}
func (g *GeneratorPlugin) Server(*plugin.MuxBroker) (any, error) {
return &GeneratorRPCServer{Impl: g.Impl}, nil
}
func (GeneratorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (any, error) {
return &GeneratorRPC{client: c}, nil
}