-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
61 lines (49 loc) · 1.56 KB
/
main.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
60
61
package main
import (
"context"
pluginpb "example/plugin"
"fmt"
"os"
"regexp"
"strings"
"unsafe"
"google.golang.org/grpc/metadata"
)
type plugin struct{}
func (_ *plugin) Val() string {
return "hello grpc-federation plugin"
}
func (_ *plugin) Example_Regexp_Compile(ctx context.Context, expr string) (*pluginpb.Regexp, error) {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
fmt.Fprintf(os.Stderr, "plugin: got metadata is %+v", md)
}
re, err := regexp.Compile(expr)
if err != nil {
return nil, err
}
return &pluginpb.Regexp{
Ptr: uint32(uintptr(unsafe.Pointer(re))),
}, nil
}
func (_ *plugin) Example_Regexp_Regexp_MatchString(ctx context.Context, re *pluginpb.Regexp, s string) (bool, error) {
return (*regexp.Regexp)(unsafe.Pointer(uintptr(re.Ptr))).MatchString(s), nil
}
func (_ *plugin) Example_Regexp_NewExample(_ context.Context) (*pluginpb.Example, error) {
return &pluginpb.Example{}, nil
}
func (_ *plugin) Example_Regexp_NewExamples(_ context.Context) ([]*pluginpb.Example, error) {
return []*pluginpb.Example{{}, {}}, nil
}
func (_ *plugin) Example_Regexp_FilterExamples(_ context.Context, v []*pluginpb.Example) ([]*pluginpb.Example, error) {
return v, nil
}
func (_ *plugin) Example_Regexp_Example_Concat(_ context.Context, _ *pluginpb.Example, v []string) (string, error) {
return strings.Join(v, ""), nil
}
func (_ *plugin) Example_Regexp_Example_MySplit(_ context.Context, _ *pluginpb.Example, s string, sep string) ([]string, error) {
return strings.Split(s, sep), nil
}
func main() {
pluginpb.RegisterRegexpPlugin(&plugin{})
}