/
format.go
73 lines (64 loc) · 1.84 KB
/
format.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
71
72
73
package pbcloudevents
import (
"github.com/cloudevents/sdk-go/v2/event"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/kconwayinvision/cloudevents-go-protobuf/internal/pb"
)
const (
// MediaTypeProtobuf is used to indicate that the event is in protobuf
// encoding. Using a non-standard name to guard against any breaking changes
// in the official protobuf schema before it becomes 1.0. The short SHA
// identifies the commit where protobuf was added to the specification.
MediaTypeProtobuf = "application/cloudevents+protobuf-ad5f142"
// MediaTypeProtobufJSON is a non-standard encoding that uses the protobuf
// JSON encoding rather than binary. This is useful for maintaining human
// readability of messages.
MediaTypeProtobufJSON = "application/cloudevents+protobuf+json-ad5f142"
)
type FormatProtobuf struct{}
func (FormatProtobuf) MediaType() string {
return MediaTypeProtobuf
}
func (FormatProtobuf) Marshal(e *event.Event) ([]byte, error) {
pbe, err := sdkToProto(e)
if err != nil {
return nil, err
}
return proto.Marshal(pbe)
}
func (FormatProtobuf) Unmarshal(b []byte, e *event.Event) error {
pbe := &pb.CloudEvent{}
if err := proto.Unmarshal(b, pbe); err != nil {
return err
}
e2, err := protoToSDK(pbe)
if err != nil {
return err
}
*e = *e2
return nil
}
type FormatProtobufJSON struct{}
func (FormatProtobufJSON) MediaType() string {
return MediaTypeProtobuf
}
func (FormatProtobufJSON) Marshal(e *event.Event) ([]byte, error) {
pbe, err := sdkToProto(e)
if err != nil {
return nil, err
}
return protojson.Marshal(pbe)
}
func (FormatProtobufJSON) Unmarshal(b []byte, e *event.Event) error {
pbe := &pb.CloudEvent{}
if err := protojson.Unmarshal(b, pbe); err != nil {
return err
}
e2, err := protoToSDK(pbe)
if err != nil {
return err
}
*e = *e2
return nil
}