-
Notifications
You must be signed in to change notification settings - Fork 2
/
gogo.go
70 lines (59 loc) · 1.64 KB
/
gogo.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
62
63
64
65
66
67
68
69
70
package main
import (
"bytes"
"compress/gzip"
"context"
"io/ioutil"
"log"
"sync"
"unsafe"
_ "github.com/gogo/protobuf/gogoproto"
gogoproto "github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/proto"
dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/jhump/protoreflect/grpcreflect"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)
// copy from grpcreflect
type reflectClient struct {
ctx context.Context
stub rpb.ServerReflectionClient
connMu sync.Mutex
cancel context.CancelFunc
stream rpb.ServerReflection_ServerReflectionInfoClient
cacheMu sync.RWMutex
protosByName map[string]*dpb.FileDescriptorProto
// filesByName map[string]*desc.FileDescriptor
// filesBySymbol map[string]*desc.FileDescriptor
// filesByExtension map[extDesc]*desc.FileDescriptor
}
// fix gogo.proto include
func modifyClient(cli *grpcreflect.Client) {
rcli := (*reflectClient)(unsafe.Pointer(cli))
fd := getGogo()
// fix "gogo.proto" to full path
*fd.Name = "github.com/gogo/protobuf/gogoproto/gogo.proto"
rcli.protosByName["github.com/gogo/protobuf/gogoproto/gogo.proto"] = fd
}
func getGogo() *dpb.FileDescriptorProto {
data := gogoproto.FileDescriptor("gogo.proto")
if len(data) == 0 {
return nil
}
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
log.Println("getGogo NewReader failed", err)
return nil
}
data, err = ioutil.ReadAll(r)
if err != nil {
log.Println("getGogo ReadAll failed", err)
return nil
}
ret := &dpb.FileDescriptorProto{}
if err := proto.Unmarshal(data, ret); err != nil {
log.Println("getGogo Unmarshal failed", err)
return nil
}
return ret
}