-
Notifications
You must be signed in to change notification settings - Fork 218
/
protobuf.go
267 lines (250 loc) · 7.63 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package format
import (
"fmt"
"net/url"
stdtime "time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/cloudevents/sdk-go/v2/binding/format"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/types"
"github.com/cloudevents/sdk-go/binding/format/protobuf/v2/internal/pb"
)
const (
datacontenttype = "datacontenttype"
dataschema = "dataschema"
subject = "subject"
time = "time"
)
var (
zeroTime = stdtime.Time{}
// Protobuf is the built-in "application/cloudevents+protobuf" format.
Protobuf = protobufFmt{}
)
const (
ApplicationCloudEventsProtobuf = "application/cloudevents+protobuf"
)
// StringOfApplicationCloudEventsProtobuf returns a string pointer to
// "application/cloudevents+protobuf"
func StringOfApplicationCloudEventsProtobuf() *string {
a := ApplicationCloudEventsProtobuf
return &a
}
func init() {
format.Add(Protobuf)
}
type protobufFmt struct{}
func (protobufFmt) MediaType() string {
return ApplicationCloudEventsProtobuf
}
func (protobufFmt) Marshal(e *event.Event) ([]byte, error) {
pbe, err := ToProto(e)
if err != nil {
return nil, err
}
return proto.Marshal(pbe)
}
func (protobufFmt) Unmarshal(b []byte, e *event.Event) error {
pbe := &pb.CloudEvent{}
if err := proto.Unmarshal(b, pbe); err != nil {
return err
}
e2, err := FromProto(pbe)
if err != nil {
return err
}
*e = *e2
return nil
}
// convert an SDK event to a protobuf variant of the event that can be marshaled.
func ToProto(e *event.Event) (*pb.CloudEvent, error) {
container := &pb.CloudEvent{
Id: e.ID(),
Source: e.Source(),
SpecVersion: e.SpecVersion(),
Type: e.Type(),
Attributes: make(map[string]*pb.CloudEventAttributeValue),
}
if e.DataContentType() != "" {
container.Attributes[datacontenttype], _ = attributeFor(e.DataContentType())
}
if e.DataSchema() != "" {
container.Attributes[dataschema], _ = attributeFor(e.DataSchema())
}
if e.Subject() != "" {
container.Attributes[subject], _ = attributeFor(e.Subject())
}
if e.Time() != zeroTime {
container.Attributes[time], _ = attributeFor(e.Time())
}
for name, value := range e.Extensions() {
attr, err := attributeFor(value)
if err != nil {
return nil, fmt.Errorf("failed to encode attribute %s: %s", name, err)
}
container.Attributes[name] = attr
}
container.Data = &pb.CloudEvent_BinaryData{
BinaryData: e.Data(),
}
if e.DataContentType() == ContentTypeProtobuf {
anymsg := &anypb.Any{
TypeUrl: e.DataSchema(),
Value: e.Data(),
}
container.Data = &pb.CloudEvent_ProtoData{
ProtoData: anymsg,
}
}
return container, nil
}
func attributeFor(v interface{}) (*pb.CloudEventAttributeValue, error) {
vv, err := types.Validate(v)
if err != nil {
return nil, err
}
attr := &pb.CloudEventAttributeValue{}
switch vt := vv.(type) {
case bool:
attr.Attr = &pb.CloudEventAttributeValue_CeBoolean{
CeBoolean: vt,
}
case int32:
attr.Attr = &pb.CloudEventAttributeValue_CeInteger{
CeInteger: vt,
}
case string:
attr.Attr = &pb.CloudEventAttributeValue_CeString{
CeString: vt,
}
case []byte:
attr.Attr = &pb.CloudEventAttributeValue_CeBytes{
CeBytes: vt,
}
case types.URI:
attr.Attr = &pb.CloudEventAttributeValue_CeUri{
CeUri: vt.String(),
}
case types.URIRef:
attr.Attr = &pb.CloudEventAttributeValue_CeUriRef{
CeUriRef: vt.String(),
}
case types.Timestamp:
attr.Attr = &pb.CloudEventAttributeValue_CeTimestamp{
CeTimestamp: timestamppb.New(vt.Time),
}
default:
return nil, fmt.Errorf("unsupported attribute type: %T", v)
}
return attr, nil
}
func valueFrom(attr *pb.CloudEventAttributeValue) (interface{}, error) {
var v interface{}
switch vt := attr.Attr.(type) {
case *pb.CloudEventAttributeValue_CeBoolean:
v = vt.CeBoolean
case *pb.CloudEventAttributeValue_CeInteger:
v = vt.CeInteger
case *pb.CloudEventAttributeValue_CeString:
v = vt.CeString
case *pb.CloudEventAttributeValue_CeBytes:
v = vt.CeBytes
case *pb.CloudEventAttributeValue_CeUri:
uri, err := url.Parse(vt.CeUri)
if err != nil {
return nil, fmt.Errorf("failed to parse URI value %s: %s", vt.CeUri, err.Error())
}
v = uri
case *pb.CloudEventAttributeValue_CeUriRef:
uri, err := url.Parse(vt.CeUriRef)
if err != nil {
return nil, fmt.Errorf("failed to parse URIRef value %s: %s", vt.CeUriRef, err.Error())
}
v = types.URIRef{URL: *uri}
case *pb.CloudEventAttributeValue_CeTimestamp:
v = vt.CeTimestamp.AsTime()
default:
return nil, fmt.Errorf("unsupported attribute type: %T", vt)
}
return types.Validate(v)
}
// Convert from a protobuf variant into the generic, SDK event.
func FromProto(container *pb.CloudEvent) (*event.Event, error) {
e := event.New()
e.SetID(container.Id)
e.SetSource(container.Source)
e.SetSpecVersion(container.SpecVersion)
e.SetType(container.Type)
// NOTE: There are some issues around missing data content type values that
// are still unresolved. It is an optional field and if unset then it is
// implied that the encoding used for the envelope was also used for the
// data. However, there is no mapping that exists between data content types
// and the envelope content types. For example, how would this system know
// that receiving an envelope in application/cloudevents+protobuf know that
// the implied data content type if missing is application/protobuf.
//
// It is also not clear what should happen if the data content type is unset
// but it is known that the data content type is _not_ the same as the
// envelope. For example, a JSON encoded data value would be stored within
// the BinaryData attribute of the protobuf formatted envelope. Protobuf
// data values, however, are _always_ stored as a protobuf encoded Any type
// within the ProtoData field. Any use of the BinaryData or TextData fields
// means the value is _not_ protobuf. If content type is not set then have
// no way of knowing what the data encoding actually is. Currently, this
// code does not address this and only loads explicitly set data content
// type values.
contentType := ""
if container.Attributes != nil {
attr := container.Attributes[datacontenttype]
if attr != nil {
if stattr, ok := attr.Attr.(*pb.CloudEventAttributeValue_CeString); ok {
contentType = stattr.CeString
}
}
}
switch dt := container.Data.(type) {
case *pb.CloudEvent_BinaryData:
e.DataEncoded = dt.BinaryData
// NOTE: If we use SetData then the current implementation always sets
// the Base64 bit to true. Direct assignment appears to be the only way
// to set non-base64 encoded binary data.
// if err := e.SetData(contentType, dt.BinaryData); err != nil {
// return nil, fmt.Errorf("failed to convert binary type (%s) data: %s", contentType, err)
// }
case *pb.CloudEvent_TextData:
if err := e.SetData(contentType, dt.TextData); err != nil {
return nil, fmt.Errorf("failed to convert text type (%s) data: %s", contentType, err)
}
case *pb.CloudEvent_ProtoData:
e.SetDataContentType(ContentTypeProtobuf)
e.DataEncoded = dt.ProtoData.Value
}
for name, value := range container.Attributes {
v, err := valueFrom(value)
if err != nil {
return nil, fmt.Errorf("failed to convert attribute %s: %s", name, err)
}
switch name {
case datacontenttype:
vs, _ := v.(string)
e.SetDataContentType(vs)
case dataschema:
vs, _ := v.(string)
e.SetDataSchema(vs)
case subject:
vs, _ := v.(string)
e.SetSubject(vs)
case time:
vs, _ := v.(types.Timestamp)
e.SetTime(vs.Time)
default:
e.SetExtension(name, v)
}
}
return &e, nil
}