-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_registry.go
More file actions
408 lines (360 loc) · 11.5 KB
/
event_registry.go
File metadata and controls
408 lines (360 loc) · 11.5 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package schema
import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"
"sync"
)
// EventSchema describes the expected shape of event data for a specific event type and version.
type EventSchema struct {
Type string `json:"type" yaml:"type"` // e.g., "order.created"
Version string `json:"version" yaml:"version"` // semver
Description string `json:"description" yaml:"description"` // human-readable description
Fields map[string]FieldDef `json:"fields" yaml:"fields"` // field name -> definition
Required []string `json:"required" yaml:"required"` // required field names
Examples []map[string]any `json:"examples,omitempty" yaml:"examples,omitempty"` // example data payloads
}
// FieldDef describes a single field within an event data payload.
type FieldDef struct {
Type string `json:"type" yaml:"type"` // string, number, boolean, object, array
Description string `json:"description" yaml:"description"` // human-readable description
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"` // allowed values (if constrained)
Format string `json:"format,omitempty" yaml:"format,omitempty"` // email, uri, date-time, uuid
}
// EventSchemaRegistry stores and validates event schemas keyed by "type:version".
type EventSchemaRegistry struct {
mu sync.RWMutex
schemas map[string]*EventSchema // key: "type:version"
}
// EventValidationError represents a single event data validation failure.
type EventValidationError struct {
Field string
Message string
}
func (e *EventValidationError) Error() string {
if e.Field != "" {
return fmt.Sprintf("field %q: %s", e.Field, e.Message)
}
return e.Message
}
// EventValidationErrors collects multiple event validation failures.
type EventValidationErrors []*EventValidationError
func (ve EventValidationErrors) Error() string {
msgs := make([]string, len(ve))
for i, e := range ve {
msgs[i] = e.Error()
}
return fmt.Sprintf("event validation failed with %d error(s):\n - %s",
len(ve), strings.Join(msgs, "\n - "))
}
// Pre-compiled format validation regexes.
var (
emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
uuidRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
dateTimeRegex = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}`)
uriRegex = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+.\-]*://`)
)
// NewEventSchemaRegistry creates a new empty event schema registry.
func NewEventSchemaRegistry() *EventSchemaRegistry {
return &EventSchemaRegistry{
schemas: make(map[string]*EventSchema),
}
}
// schemaKey returns the composite key for an event schema.
func schemaKey(eventType, version string) string {
return eventType + ":" + version
}
// Register validates and stores an event schema. It returns an error if a
// schema with the same type and version is already registered, or if the
// schema is missing required metadata.
func (r *EventSchemaRegistry) Register(schema *EventSchema) error {
if schema == nil {
return fmt.Errorf("event schema must not be nil")
}
if schema.Type == "" {
return fmt.Errorf("event schema type must not be empty")
}
if schema.Version == "" {
return fmt.Errorf("event schema version must not be empty")
}
// Validate that required fields reference defined fields
for _, req := range schema.Required {
if _, ok := schema.Fields[req]; !ok {
return fmt.Errorf("required field %q is not defined in fields", req)
}
}
// Validate field definitions
validTypes := map[string]bool{
"string": true, "number": true, "boolean": true, "object": true, "array": true,
}
for name, field := range schema.Fields {
if !validTypes[field.Type] {
return fmt.Errorf("field %q has invalid type %q (must be string, number, boolean, object, or array)", name, field.Type)
}
}
key := schemaKey(schema.Type, schema.Version)
r.mu.Lock()
defer r.mu.Unlock()
if _, exists := r.schemas[key]; exists {
return fmt.Errorf("event schema %q version %q is already registered", schema.Type, schema.Version)
}
r.schemas[key] = schema
return nil
}
// Get retrieves an event schema by type and version. Returns nil and false if
// not found.
func (r *EventSchemaRegistry) Get(eventType, version string) (*EventSchema, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
s, ok := r.schemas[schemaKey(eventType, version)]
return s, ok
}
// GetLatest returns the latest version of the schema for a given event type,
// determined by lexicographic comparison of semantic version strings. Returns
// nil and false if no schema exists for the type.
func (r *EventSchemaRegistry) GetLatest(eventType string) (*EventSchema, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
var latest *EventSchema
for _, s := range r.schemas {
if s.Type == eventType {
if latest == nil || compareSemver(s.Version, latest.Version) > 0 {
latest = s
}
}
}
if latest == nil {
return nil, false
}
return latest, true
}
// Validate validates the given data map against the latest schema for the
// specified event type. Returns nil if valid, or an EventValidationErrors
// containing all failures.
func (r *EventSchemaRegistry) Validate(eventType string, data map[string]any) error {
schema, ok := r.GetLatest(eventType)
if !ok {
return fmt.Errorf("no schema registered for event type %q", eventType)
}
return validateEventData(schema, data)
}
// ValidateVersion validates data against a specific version of the schema.
func (r *EventSchemaRegistry) ValidateVersion(eventType, version string, data map[string]any) error {
schema, ok := r.Get(eventType, version)
if !ok {
return fmt.Errorf("no schema registered for event type %q version %q", eventType, version)
}
return validateEventData(schema, data)
}
// List returns all registered schemas as a slice, sorted by type then version.
func (r *EventSchemaRegistry) List() []*EventSchema {
r.mu.RLock()
defer r.mu.RUnlock()
out := make([]*EventSchema, 0, len(r.schemas))
for _, s := range r.schemas {
out = append(out, s)
}
sort.Slice(out, func(i, j int) bool {
if out[i].Type != out[j].Type {
return out[i].Type < out[j].Type
}
return out[i].Version < out[j].Version
})
return out
}
// ListTypes returns all unique registered event types, sorted alphabetically.
func (r *EventSchemaRegistry) ListTypes() []string {
r.mu.RLock()
defer r.mu.RUnlock()
typeSet := make(map[string]bool)
for _, s := range r.schemas {
typeSet[s.Type] = true
}
types := make([]string, 0, len(typeSet))
for t := range typeSet {
types = append(types, t)
}
sort.Strings(types)
return types
}
// Remove deletes a schema identified by event type and version. Returns true
// if the schema was found and removed, false otherwise.
func (r *EventSchemaRegistry) Remove(eventType, version string) bool {
key := schemaKey(eventType, version)
r.mu.Lock()
defer r.mu.Unlock()
if _, exists := r.schemas[key]; !exists {
return false
}
delete(r.schemas, key)
return true
}
// validateEventData performs full validation of data against a schema.
func validateEventData(schema *EventSchema, data map[string]any) error {
var errs EventValidationErrors
// Check required fields
for _, req := range schema.Required {
if _, ok := data[req]; !ok {
errs = append(errs, &EventValidationError{
Field: req,
Message: "required field is missing",
})
}
}
// Validate each field present in data that has a schema definition
for name, value := range data {
fieldDef, ok := schema.Fields[name]
if !ok {
// Fields not in the schema are allowed (open content model)
continue
}
// Type check
if err := checkFieldType(name, fieldDef.Type, value); err != nil {
errs = append(errs, err)
continue // skip further checks if type is wrong
}
// Enum check
if len(fieldDef.Enum) > 0 {
if err := checkEnum(name, fieldDef.Enum, value); err != nil {
errs = append(errs, err)
}
}
// Format check
if fieldDef.Format != "" {
if err := checkFormat(name, fieldDef.Format, value); err != nil {
errs = append(errs, err)
}
}
}
if len(errs) > 0 {
return errs
}
return nil
}
// checkFieldType validates that value matches the expected schema type.
func checkFieldType(field, expectedType string, value any) *EventValidationError {
if value == nil {
return nil // nil is accepted for any type (absence is handled by required check)
}
switch expectedType {
case "string":
if _, ok := value.(string); !ok {
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("expected type string, got %s", reflect.TypeOf(value).Kind()),
}
}
case "number":
switch value.(type) {
case float64, float32, int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64:
// valid numeric types
default:
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("expected type number, got %s", reflect.TypeOf(value).Kind()),
}
}
case "boolean":
if _, ok := value.(bool); !ok {
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("expected type boolean, got %s", reflect.TypeOf(value).Kind()),
}
}
case "object":
if _, ok := value.(map[string]any); !ok {
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("expected type object (map), got %s", reflect.TypeOf(value).Kind()),
}
}
case "array":
v := reflect.ValueOf(value)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("expected type array (slice), got %s", v.Kind()),
}
}
}
return nil
}
// checkEnum validates that value is one of the allowed enum values.
func checkEnum(field string, allowed []string, value any) *EventValidationError {
str, ok := value.(string)
if !ok {
return &EventValidationError{
Field: field,
Message: "enum validation requires a string value",
}
}
for _, a := range allowed {
if str == a {
return nil
}
}
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("value %q is not in allowed enum values %v", str, allowed),
}
}
// checkFormat validates that a string value matches the expected format.
func checkFormat(field, format string, value any) *EventValidationError {
str, ok := value.(string)
if !ok {
return nil // format checks only apply to strings
}
var valid bool
switch format {
case "email":
valid = emailRegex.MatchString(str)
case "uuid":
valid = uuidRegex.MatchString(str)
case "date-time":
valid = dateTimeRegex.MatchString(str)
case "uri":
valid = uriRegex.MatchString(str)
default:
return nil // unknown formats are silently accepted
}
if !valid {
return &EventValidationError{
Field: field,
Message: fmt.Sprintf("value %q does not match format %q", str, format),
}
}
return nil
}
// compareSemver compares two semantic version strings. It returns:
//
// -1 if a < b, 0 if a == b, 1 if a > b
//
// Versions are expected in "major.minor.patch" format. Non-parseable versions
// fall back to lexicographic comparison.
func compareSemver(a, b string) int {
aParts := strings.Split(a, ".")
bParts := strings.Split(b, ".")
maxLen := len(aParts)
if len(bParts) > maxLen {
maxLen = len(bParts)
}
for i := 0; i < maxLen; i++ {
var aNum, bNum int
if i < len(aParts) {
_, _ = fmt.Sscanf(aParts[i], "%d", &aNum)
}
if i < len(bParts) {
_, _ = fmt.Sscanf(bParts[i], "%d", &bNum)
}
if aNum < bNum {
return -1
}
if aNum > bNum {
return 1
}
}
return 0
}