-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterceptor.go
58 lines (47 loc) · 1.26 KB
/
interceptor.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
package superstream
import (
"fmt"
"github.com/IBM/sarama"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
)
func startInterceptors(config interface{}, client *Client) {
if config == nil {
client.handleError(fmt.Sprintf(" startInterceptors: config is nil"))
return
}
if config, ok := config.(*sarama.Config); ok {
ConfigSaramaInterceptor(config, client)
return
} else {
client.handleError(fmt.Sprintf(" startInterceptors: unsupported sdk"))
fmt.Println("superstream: unsupported sdk")
return
}
}
func protoToJson(msgBytes []byte, desc protoreflect.MessageDescriptor) ([]byte, error) {
newMsg := dynamicpb.NewMessage(desc)
err := proto.Unmarshal(msgBytes, newMsg)
if err != nil {
return nil, err
}
jsonBytes, err := protojson.Marshal(newMsg)
if err != nil {
return nil, err
}
return jsonBytes, nil
}
func jsonToProto(msgBytes []byte, desc protoreflect.MessageDescriptor) ([]byte, error) {
newMsg := dynamicpb.NewMessage(desc)
err := protojson.Unmarshal(msgBytes, newMsg)
if err != nil {
return nil, err
}
protoBytes, err := proto.Marshal(newMsg)
if err != nil {
return nil, err
}
return protoBytes, nil
}