-
Notifications
You must be signed in to change notification settings - Fork 3
/
metadata.go
108 lines (89 loc) · 2.73 KB
/
metadata.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
// Copyright 2022 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eventsourcing
import (
"context"
"encoding/json"
"fmt"
)
// metadataKey is the key type for metadata stored in context.
type metadataKeyType struct {
}
/*
MetadataManager is an interface for a storage of metadata.
It can be used to easily store any metadata in the context of a call.
*/
type MetadataManager interface {
// GetContext returns a new context enriched with the metadata of this manager.
GetContext() context.Context
// GetMetadata returns the metadata of this manager.
GetMetadata() map[string]string
// SetMetadata sets the metadata of this manager.
SetMetadata(map[string]string) MetadataManager
// Get returns the information stored for the key.
Get(string) (string, bool)
GetObject(string, interface{}) error
// Set stores information for the key.
Set(string, string) MetadataManager
SetObject(string, interface{}) error
}
type metadataManager struct {
ctx context.Context
data map[string]string
}
func NewMetadataManagerFromContext(ctx context.Context) MetadataManager {
m := &metadataManager{
ctx: ctx,
data: make(map[string]string),
}
d, ok := ctx.Value(metadataKeyType{}).(map[string]string)
if ok {
m.data = d
}
return m
}
func (m *metadataManager) GetMetadata() map[string]string {
return m.data
}
func (m *metadataManager) SetMetadata(metadata map[string]string) MetadataManager {
m.data = metadata
return m
}
func (m *metadataManager) Get(key string) (string, bool) {
v, ok := m.data[key]
return v, ok
}
func (m *metadataManager) Set(key string, value string) MetadataManager {
m.data[key] = value
return m
}
func (m *metadataManager) SetObject(key string, v interface{}) error {
bytes, err := json.Marshal(v)
if err != nil {
return err
}
m.Set(key, string(bytes))
return nil
}
func (m *metadataManager) GetObject(key string, v interface{}) error {
jsonString, found := m.Get(key)
if !found {
return fmt.Errorf("metadata for key %s not found", key)
}
return json.Unmarshal([]byte(jsonString), v)
}
// GetContext returns a new context enriched with the metadata of this manager.
func (m *metadataManager) GetContext() context.Context {
return context.WithValue(m.ctx, metadataKeyType{}, m.data)
}