-
Notifications
You must be signed in to change notification settings - Fork 50
/
encoding_protobuf.go
69 lines (52 loc) · 1.8 KB
/
encoding_protobuf.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
package stream
import (
"fmt"
"github.com/justtrackio/gosoline/pkg/encoding/base64"
"google.golang.org/protobuf/proto"
)
type ProtobufEncodable interface {
ToMessage() (proto.Message, error)
EmptyMessage() proto.Message
FromMessage(message proto.Message) error
}
type protobufEncoder struct{}
func NewProtobufEncoder() MessageBodyEncoder {
return protobufEncoder{}
}
func (e protobufEncoder) Encode(data interface{}) ([]byte, error) {
msg, ok := data.(ProtobufEncodable)
if !ok {
return nil, fmt.Errorf("%T does not implement ProtobufEncodable", data)
}
protoMsg, err := msg.ToMessage()
if err != nil {
return nil, fmt.Errorf("failed to construct protobuf message: %w", err)
}
bytes, err := proto.Marshal(protoMsg)
if err != nil {
return nil, fmt.Errorf("failed to marshal protobuf message: %w", err)
}
// why do we need an extra layer of base64 here? Because, unlike JSON, protobuf makes use of all possible byte
// values (because it embeds byte strings without encoding and is in general a binary format) and therefore we
// need to encode it like that to ensure we can use the result in a json string.
return base64.Encode(bytes), nil
}
func (e protobufEncoder) Decode(data64 []byte, out interface{}) error {
msg, ok := out.(ProtobufEncodable)
if !ok {
return fmt.Errorf("%T does not implement ProtobufEncodable", out)
}
data, err := base64.Decode(data64)
if err != nil {
return fmt.Errorf("failed to decode protobuf base64 layer: %w", err)
}
// create an empty message from an empty struct
protoMsg := msg.EmptyMessage()
if err := proto.Unmarshal(data, protoMsg); err != nil {
return fmt.Errorf("failed to decode protobuf message: %w", err)
}
if err := msg.FromMessage(protoMsg); err != nil {
return fmt.Errorf("failed to convert protobuf message: %w", err)
}
return nil
}