-
Notifications
You must be signed in to change notification settings - Fork 21
/
event.go
66 lines (57 loc) · 1.77 KB
/
event.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
// Copyright (c) 2022 Silverton Data, Inc.
// You may use, distribute, and modify this code under the terms of the AGPLv3 license, a copy of
// which may be found at https://github.com/silverton-io/honeypot/blob/main/LICENSE
package cloudevents
import (
"database/sql/driver"
"encoding/json"
"time"
)
type CloudEvent struct { // https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/cloudevents.json
Id string `json:"id"`
Source string `json:"source"`
SpecVersion string `json:"specversion"`
Type string `json:"type"`
DataContentType string `json:"datacontenttype"`
DataSchema string `json:"dataschema"`
Subject *string `json:"subject"`
Time time.Time `json:"time"`
Data map[string]interface{} `json:"data"`
Datab64 string `json:"datab64"`
}
func (e CloudEvent) SchemaName() *string {
return &e.DataSchema
}
func (e CloudEvent) PayloadAsByte() ([]byte, error) {
eBytes, err := json.Marshal(e.Data)
if err != nil {
return nil, err
}
return eBytes, nil
}
func (e CloudEvent) AsByte() ([]byte, error) {
eBytes, err := json.Marshal(e)
if err != nil {
return nil, err
}
return eBytes, nil
}
func (e CloudEvent) AsMap() (map[string]interface{}, error) {
var event map[string]interface{}
cByte, err := e.AsByte()
if err != nil {
return nil, err
}
err = json.Unmarshal(cByte, &event)
if err != nil {
return nil, err
}
return event, nil
}
func (e CloudEvent) Value() (driver.Value, error) {
b, err := json.Marshal(e)
return string(b), err
}
func (e CloudEvent) Scan(input interface{}) error {
return json.Unmarshal(input.([]byte), &e)
}