-
Notifications
You must be signed in to change notification settings - Fork 15
/
manager.go
86 lines (70 loc) · 2.07 KB
/
manager.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
package metadata
import (
"strings"
"github.com/jexia/semaphore/pkg/broker"
"github.com/jexia/semaphore/pkg/broker/logger"
"github.com/jexia/semaphore/pkg/references"
"github.com/jexia/semaphore/pkg/specs"
"github.com/jexia/semaphore/pkg/specs/template"
"go.uber.org/zap"
)
// NewManager constructs a new metadata manager for the given resource.
func NewManager(ctx *broker.Context, resource string, params specs.Header) *Manager {
keys := make(map[string]struct{}, len(params))
for key := range params {
keys[strings.ToLower(key)] = struct{}{}
}
return &Manager{
Context: ctx,
Resource: template.JoinPath(resource, template.HeaderResource),
Params: params,
Keys: keys,
}
}
// Manager represents a metadata manager for a given resource
type Manager struct {
Context *broker.Context
Resource string
Params specs.Header
Keys map[string]struct{}
}
// Marshal attempts to marshal the given metadata specs from the given refs store
func (manager *Manager) Marshal(store references.Store) MD {
if manager == nil || manager.Params == nil {
return make(MD, 0)
}
result := make(MD, len(manager.Params))
for key, property := range manager.Params {
if property.Scalar == nil {
continue
}
value := property.Scalar.Default
if property.Reference != nil {
ref := store.Load(property.Reference.String())
if ref != nil {
value = ref.Value
}
}
logger.Debug(manager.Context, "Marshalling header property", zap.String("key", key))
if value == nil {
logger.Debug(manager.Context, "Header property is empty", zap.String("key", key))
continue
}
result[key] = value.(string)
}
return result
}
// Unmarshal unmarshals the given transport metadata into the given reference store
func (manager *Manager) Unmarshal(metadata MD, store references.Store) {
for key, value := range metadata {
_, has := manager.Keys[key]
if !has {
continue
}
ref := &references.Reference{
Value: value,
}
logger.Debug(manager.Context, "Unmarshalling header property", zap.String("key", key))
store.Store(manager.Resource+":"+key, ref)
}
}