-
Notifications
You must be signed in to change notification settings - Fork 224
/
format.go
105 lines (83 loc) · 2.87 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
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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package format
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/cloudevents/sdk-go/v2/event"
)
// Format marshals and unmarshals structured events to bytes.
type Format interface {
// MediaType identifies the format
MediaType() string
// Marshal event to bytes
Marshal(*event.Event) ([]byte, error)
// Unmarshal bytes to event
Unmarshal([]byte, *event.Event) error
}
// Prefix for event-format media types.
const Prefix = "application/cloudevents"
// IsFormat returns true if mediaType begins with "application/cloudevents"
func IsFormat(mediaType string) bool { return strings.HasPrefix(mediaType, Prefix) }
// JSON is the built-in "application/cloudevents+json" format.
var JSON = jsonFmt{}
type jsonFmt struct{}
func (jsonFmt) MediaType() string { return event.ApplicationCloudEventsJSON }
func (jsonFmt) Marshal(e *event.Event) ([]byte, error) { return json.Marshal(e) }
func (jsonFmt) Unmarshal(b []byte, e *event.Event) error {
return json.Unmarshal(b, e)
}
// JSONBatch is the built-in "application/cloudevents-batch+json" format.
var JSONBatch = jsonBatchFmt{}
type jsonBatchFmt struct{}
func (jb jsonBatchFmt) MediaType() string {
return event.ApplicationCloudEventsBatchJSON
}
// Marshal will return an error for jsonBatchFmt since the Format interface doesn't support batch Marshalling, and we
// know it's structured batch json, we'll go direct to the json.UnMarshall() (see `ToEvents()`) since that is the best
// way to support batch operations for now.
func (jb jsonBatchFmt) Marshal(e *event.Event) ([]byte, error) {
return nil, errors.New("not supported for batch events")
}
func (jb jsonBatchFmt) Unmarshal(b []byte, e *event.Event) error {
return errors.New("not supported for batch events")
}
// built-in formats
var formats map[string]Format
func init() {
formats = map[string]Format{}
Add(JSON)
Add(JSONBatch)
}
// Lookup returns the format for contentType, or nil if not found.
func Lookup(contentType string) Format {
i := strings.IndexRune(contentType, ';')
if i == -1 {
i = len(contentType)
}
contentType = strings.TrimSpace(strings.ToLower(contentType[0:i]))
return formats[contentType]
}
func unknown(mediaType string) error {
return fmt.Errorf("unknown event format media-type %#v", mediaType)
}
// Add a new Format. It can be retrieved by Lookup(f.MediaType())
func Add(f Format) { formats[f.MediaType()] = f }
// Marshal an event to bytes using the mediaType event format.
func Marshal(mediaType string, e *event.Event) ([]byte, error) {
if f := formats[mediaType]; f != nil {
return f.Marshal(e)
}
return nil, unknown(mediaType)
}
// Unmarshal bytes to an event using the mediaType event format.
func Unmarshal(mediaType string, b []byte, e *event.Event) error {
if f := formats[mediaType]; f != nil {
return f.Unmarshal(b, e)
}
return unknown(mediaType)
}