-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_header.go
251 lines (222 loc) · 6.96 KB
/
event_header.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
// Code generated by github.com/actgardner/gogen-avro/v8. DO NOT EDIT.
/*
* SOURCES:
* EventHeader.avsc
* RobTest.avsc
*/
package my_group_my_artifact4
import (
"encoding/json"
"fmt"
"io"
"github.com/actgardner/gogen-avro/v9/compiler"
"github.com/actgardner/gogen-avro/v9/vm"
"github.com/actgardner/gogen-avro/v9/vm/types"
)
var _ = fmt.Printf
// The below fields include header information and should be included on every event in the DESP. Inspired by: https://github.com/cloudevents/spec/blob/v0.2/spec.md
type EventHeader struct {
// A unique identifier of the event - for example, a randomly generated GUID
Id string `json:"id"`
// Time the event occurred in milliseconds since epoch, UTC timezone.
Time int64 `json:"time"`
// Type of occurrence which has happened. Reference the domain.event registered in schema-registry.
Type string `json:"type"`
// Service that produced the event. Future: reference to producer registry.
Source string `json:"source"`
// Service that produced the event. Future: reference to producer registry.
Source3 string `json:"source3"`
}
const EventHeaderAvroCRC64Fingerprint = "E\xf6\n\xf8\x112!\xb3"
func NewEventHeader() EventHeader {
r := EventHeader{}
return r
}
func DeserializeEventHeader(r io.Reader) (EventHeader, error) {
t := NewEventHeader()
deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema()))
if err != nil {
return t, err
}
err = vm.Eval(r, deser, &t)
return t, err
}
func DeserializeEventHeaderFromSchema(r io.Reader, schema string) (EventHeader, error) {
t := NewEventHeader()
deser, err := compiler.CompileSchemaBytes([]byte(schema), []byte(t.Schema()))
if err != nil {
return t, err
}
err = vm.Eval(r, deser, &t)
return t, err
}
func writeEventHeader(r EventHeader, w io.Writer) error {
var err error
err = vm.WriteString(r.Id, w)
if err != nil {
return err
}
err = vm.WriteLong(r.Time, w)
if err != nil {
return err
}
err = vm.WriteString(r.Type, w)
if err != nil {
return err
}
err = vm.WriteString(r.Source, w)
if err != nil {
return err
}
err = vm.WriteString(r.Source3, w)
if err != nil {
return err
}
return err
}
func (r EventHeader) Serialize(w io.Writer) error {
return writeEventHeader(r, w)
}
func (r EventHeader) Schema() string {
return "{\"doc\":\"The below fields include header information and should be included on every event in the DESP. Inspired by: https://github.com/cloudevents/spec/blob/v0.2/spec.md\",\"fields\":[{\"doc\":\"A unique identifier of the event - for example, a randomly generated GUID\",\"name\":\"id\",\"type\":{\"avro.java.string\":\"String\",\"type\":\"string\"}},{\"doc\":\"Time the event occurred in milliseconds since epoch, UTC timezone.\",\"name\":\"time\",\"type\":\"long\"},{\"doc\":\"Type of occurrence which has happened. Reference the domain.event registered in schema-registry.\",\"name\":\"type\",\"type\":{\"avro.java.string\":\"String\",\"type\":\"string\"}},{\"doc\":\"Service that produced the event. Future: reference to producer registry.\",\"name\":\"source\",\"type\":{\"avro.java.string\":\"String\",\"type\":\"string\"}},{\"doc\":\"Service that produced the event. Future: reference to producer registry.\",\"name\":\"source3\",\"type\":{\"avro.java.string\":\"String\",\"type\":\"string\"}}],\"name\":\"com.kroger.desp.commons.desp.healthcheck.rph.EventHeader\",\"type\":\"record\"}"
}
func (r EventHeader) SchemaName() string {
return "com.kroger.desp.commons.desp.healthcheck.rph.EventHeader"
}
func (_ EventHeader) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ EventHeader) SetInt(v int32) { panic("Unsupported operation") }
func (_ EventHeader) SetLong(v int64) { panic("Unsupported operation") }
func (_ EventHeader) SetFloat(v float32) { panic("Unsupported operation") }
func (_ EventHeader) SetDouble(v float64) { panic("Unsupported operation") }
func (_ EventHeader) SetBytes(v []byte) { panic("Unsupported operation") }
func (_ EventHeader) SetString(v string) { panic("Unsupported operation") }
func (_ EventHeader) SetUnionElem(v int64) { panic("Unsupported operation") }
func (r *EventHeader) Get(i int) types.Field {
switch i {
case 0:
return &types.String{Target: &r.Id}
case 1:
return &types.Long{Target: &r.Time}
case 2:
return &types.String{Target: &r.Type}
case 3:
return &types.String{Target: &r.Source}
case 4:
return &types.String{Target: &r.Source3}
}
panic("Unknown field index")
}
func (r *EventHeader) SetDefault(i int) {
switch i {
}
panic("Unknown field index")
}
func (r *EventHeader) NullField(i int) {
switch i {
}
panic("Not a nullable field index")
}
func (_ EventHeader) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ EventHeader) AppendArray() types.Field { panic("Unsupported operation") }
func (_ EventHeader) Finalize() {}
func (_ EventHeader) AvroCRC64Fingerprint() []byte {
return []byte(EventHeaderAvroCRC64Fingerprint)
}
func (r EventHeader) MarshalJSON() ([]byte, error) {
var err error
output := make(map[string]json.RawMessage)
output["id"], err = json.Marshal(r.Id)
if err != nil {
return nil, err
}
output["time"], err = json.Marshal(r.Time)
if err != nil {
return nil, err
}
output["type"], err = json.Marshal(r.Type)
if err != nil {
return nil, err
}
output["source"], err = json.Marshal(r.Source)
if err != nil {
return nil, err
}
output["source3"], err = json.Marshal(r.Source3)
if err != nil {
return nil, err
}
return json.Marshal(output)
}
func (r *EventHeader) UnmarshalJSON(data []byte) error {
var fields map[string]json.RawMessage
if err := json.Unmarshal(data, &fields); err != nil {
return err
}
var val json.RawMessage
val = func() json.RawMessage {
if v, ok := fields["id"]; ok {
return v
}
return nil
}()
if val != nil {
if err := json.Unmarshal([]byte(val), &r.Id); err != nil {
return err
}
} else {
return fmt.Errorf("no value specified for id")
}
val = func() json.RawMessage {
if v, ok := fields["time"]; ok {
return v
}
return nil
}()
if val != nil {
if err := json.Unmarshal([]byte(val), &r.Time); err != nil {
return err
}
} else {
return fmt.Errorf("no value specified for time")
}
val = func() json.RawMessage {
if v, ok := fields["type"]; ok {
return v
}
return nil
}()
if val != nil {
if err := json.Unmarshal([]byte(val), &r.Type); err != nil {
return err
}
} else {
return fmt.Errorf("no value specified for type")
}
val = func() json.RawMessage {
if v, ok := fields["source"]; ok {
return v
}
return nil
}()
if val != nil {
if err := json.Unmarshal([]byte(val), &r.Source); err != nil {
return err
}
} else {
return fmt.Errorf("no value specified for source")
}
val = func() json.RawMessage {
if v, ok := fields["source3"]; ok {
return v
}
return nil
}()
if val != nil {
if err := json.Unmarshal([]byte(val), &r.Source3); err != nil {
return err
}
} else {
return fmt.Errorf("no value specified for source3")
}
return nil
}