-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathplugin.go
59 lines (50 loc) · 1.79 KB
/
plugin.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
//go:build tinygo.wasm
package main
import (
"context"
"time"
"github.com/knqyf263/go-plugin/tests/well-known/proto"
"github.com/knqyf263/go-plugin/types/known/durationpb"
"github.com/knqyf263/go-plugin/types/known/emptypb"
"github.com/knqyf263/go-plugin/types/known/structpb"
"github.com/knqyf263/go-plugin/types/known/timestamppb"
"github.com/knqyf263/go-plugin/types/known/wrapperspb"
)
// main is required for TinyGo to compile to Wasm.
func main() {
proto.RegisterKnownTypesTest(TestPlugin{})
proto.RegisterEmptyTest(TestPlugin{})
}
var _ proto.EmptyTest = (*TestPlugin)(nil)
type TestPlugin struct{}
func (p TestPlugin) Test(_ context.Context, request *proto.Request) (*proto.Response, error) {
c, err := p.GetC(request.GetC())
if err != nil {
return nil, err
}
return &proto.Response{
A: durationpb.New(2 * time.Minute),
B: timestamppb.New(request.GetB().AsTime().Add(request.GetA().AsDuration())),
C: c,
D: wrapperspb.Bool(!request.GetD().Value),
E: wrapperspb.Bytes(append(request.GetE().Value, []byte(`Value`)...)),
F: wrapperspb.Double(request.GetF().Value * 2),
G: wrapperspb.Float(request.GetG().Value * 2),
H: wrapperspb.Int32(request.GetH().Value * 2),
I: wrapperspb.Int64(request.GetI().Value * 2),
J: wrapperspb.String(request.GetJ().Value + "Value"),
K: wrapperspb.UInt32(request.GetK().Value * 2),
L: wrapperspb.UInt64(request.GetL().Value * 2),
}, nil
}
func (p TestPlugin) GetC(v *structpb.Value) (*structpb.Value, error) {
c := v.AsInterface().(map[string]interface{})
c["CA"] = c["CA"].(string) + "BBB"
c["CB"] = !c["CB"].(bool)
c["CC"] = c["CC"].(float64) * 2
c["CD"] = append(c["CD"].([]interface{}), "FOO")
return structpb.NewValue(c)
}
func (p TestPlugin) DoNothing(_ context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) {
return nil, nil
}