-
Notifications
You must be signed in to change notification settings - Fork 1
/
internal_schema_registry.go
88 lines (78 loc) · 2.52 KB
/
internal_schema_registry.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
package gluon
import (
"errors"
"reflect"
"strings"
"sync"
)
var (
// ErrMessageNotRegistered The message type was not found on the message registry
ErrMessageNotRegistered = errors.New("gluon: The specified message type is not present on the message registry")
)
// MessageMetadata Is a set of definitions to describe a specific message schema.
type MessageMetadata struct {
Topic string
Source string
SchemaName string
SchemaVersion int
SchemaInternalType reflect.Type
}
// internalSchemaRegistry Is a concurrent-safe internal database which relations message concrete types to useful metadata.
//
// The metadata is composed by the message's topic name, source, data schema URI and subject. Most of the previous
// fields come from the CloudEvents specification.
//
// It is widely recommended to use specific types rather than use primitives as this agent uses the message type
// to make the relation to the specified metadata.
//
// For more information about most of the metadata fields, check: https://github.com/cloudevents/spec/blob/master/spec.md
type internalSchemaRegistry struct {
mu sync.RWMutex
registry map[string]*MessageMetadata // Key: struct type, Val: topic
}
func newInternalSchemaRegistry() *internalSchemaRegistry {
return &internalSchemaRegistry{
mu: sync.RWMutex{},
registry: map[string]*MessageMetadata{},
}
}
func (r *internalSchemaRegistry) register(schema interface{}, meta MessageMetadata) {
r.mu.Lock()
defer r.mu.Unlock()
schemaType := reflect.TypeOf(schema)
if _, ok := r.registry[schemaType.String()]; ok {
return
}
meta.SchemaInternalType = schemaType
r.registry[schemaType.String()] = &meta
}
func (r *internalSchemaRegistry) get(schema interface{}) (*MessageMetadata, error) {
r.mu.RLock()
defer r.mu.RUnlock()
typeStr := reflect.TypeOf(schema).String()
// Note: remove references to a native type
// this helps marshalers which depend on the internal schema registry as they receive pointers when decoding
typeStr = strings.Replace(typeStr, "*", "", 1)
if meta, ok := r.registry[typeStr]; ok {
return meta, nil
}
return nil, ErrMessageNotRegistered
}
func (r *internalSchemaRegistry) getByKey(k string) (*MessageMetadata, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if meta, ok := r.registry[k]; ok {
return meta, nil
}
return nil, ErrMessageNotRegistered
}
func (r *internalSchemaRegistry) getByTopic(t string) *MessageMetadata {
r.mu.RLock()
defer r.mu.RUnlock()
for _, v := range r.registry {
if v.Topic == t {
return v
}
}
return nil
}