This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
keptn_context_extended_c_e.go
148 lines (125 loc) · 3.94 KB
/
keptn_context_extended_c_e.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
package models
import (
"encoding/json"
"errors"
"fmt"
"time"
)
// KeptnContextExtendedCE keptn context extended CloudEvent
type KeptnContextExtendedCE struct {
// contenttype
Contenttype string `json:"contenttype,omitempty"`
// data
// Required: true
Data interface{} `json:"data"`
// extensions
Extensions interface{} `json:"extensions,omitempty"`
// id
ID string `json:"id,omitempty"`
// shkeptncontext
Shkeptncontext string `json:"shkeptncontext,omitempty"`
// shkeptnspecversion
Shkeptnspecversion string `json:"shkeptnspecversion,omitempty"`
// source
// Required: true
Source *string `json:"source"`
// specversion
Specversion string `json:"specversion,omitempty"`
// time
// Format: date-time
Time time.Time `json:"time,omitempty"`
// triggeredid
Triggeredid string `json:"triggeredid,omitempty"`
// gitcommitid
GitCommitID string `json:"gitcommitid,omitempty"`
// type
// Required: true
Type *string `json:"type"`
}
// DataAs attempts to populate the provided data object with the event payload.
// data should be a pointer type.
func (ce *KeptnContextExtendedCE) DataAs(out interface{}) error {
bytes, err := json.Marshal(ce.Data)
if err != nil {
return err
}
return json.Unmarshal(bytes, out)
}
// Validate checks whether the required properties 'time', 'type', 'id' and 'source' are defined and non-empty
func (ce *KeptnContextExtendedCE) Validate() error {
if ce.Time.IsZero() {
return errors.New("time must be specified")
}
if ce.Type == nil || *ce.Type == "" {
return errors.New("type must be specified")
}
if ce.ID == "" {
return errors.New("id must be specified")
}
if ce.Source == nil || *ce.Source == "" {
return errors.New("source must be specified")
}
return nil
}
// TemporaryData represents additional (temporary) data to be added
// to the data section of a keptn event
type TemporaryData interface{}
// AddTemporaryDataOptions are used to modify the behavior of adding temporary
// data to a keptn event
type AddTemporaryDataOptions struct {
// OverwriteIfExisting indicates, that the data will be overwritten
// in case a key for that data already exists
OverwriteIfExisting bool
}
const temporaryDataRootKey = "temporaryData"
// AddTemporaryData adds further (temporary) properties to the data section of the keptn event
func (ce *KeptnContextExtendedCE) AddTemporaryData(key string, tmpData TemporaryData, opts AddTemporaryDataOptions) error {
eventData := map[string]interface{}{}
if ce.Data != nil {
err := ce.DataAs(&eventData)
if err != nil {
return err
}
}
if temporaryData, found := eventData[temporaryDataRootKey]; found {
if _, kfound := temporaryData.(map[string]interface{})[key]; kfound {
if !opts.OverwriteIfExisting {
return fmt.Errorf("Key %s already exists", key)
}
temporaryData.(map[string]interface{})[key] = tmpData
}
} else {
eventData[temporaryDataRootKey] = map[string]interface{}{key: tmpData}
}
ce.Data = eventData
return nil
}
// GetTemporaryData returns the (temporary) data eventually stored in the event
func (ce *KeptnContextExtendedCE) GetTemporaryData(key string, tmpdata interface{}) error {
eventData := map[string]interface{}{}
if err := ce.DataAs(&eventData); err != nil {
return err
}
if temporaryData, found := eventData[temporaryDataRootKey]; found {
if keyData, kfound := temporaryData.(map[string]interface{})[key]; kfound {
if marshalledKeyData, err := json.Marshal(keyData); err == nil {
return json.Unmarshal(marshalledKeyData, tmpdata)
}
}
return fmt.Errorf("temporary data with key %s not found", key)
}
return fmt.Errorf("temporary data with key %s not found", key)
}
// ToJSON converts object to JSON string
func (ce *KeptnContextExtendedCE) ToJSON() ([]byte, error) {
return json.Marshal(ce)
}
// FromJSON converts JSON string to object
func (ce *KeptnContextExtendedCE) FromJSON(b []byte) error {
var res KeptnContextExtendedCE
if err := json.Unmarshal(b, &res); err != nil {
return err
}
*ce = res
return nil
}