/
manager.go
112 lines (84 loc) · 1.93 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
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
package properties
import (
"encoding/json"
"fmt"
"sync"
"code.cloudfoundry.org/garden"
)
type Manager struct {
propMutex sync.RWMutex
prop map[string]map[string]string
}
func NewManager() *Manager {
return &Manager{
prop: make(map[string]map[string]string),
}
}
func (m *Manager) DestroyKeySpace(handle string) error {
m.propMutex.Lock()
defer m.propMutex.Unlock()
delete(m.prop, handle)
return nil
}
func (m *Manager) MarshalJSON() ([]byte, error) {
return json.Marshal(m.prop)
}
func (m *Manager) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &(m.prop))
}
func (m *Manager) Set(handle string, name string, value string) {
m.propMutex.Lock()
defer m.propMutex.Unlock()
if _, ok := m.prop[handle]; !ok {
m.prop[handle] = make(map[string]string)
}
m.prop[handle][name] = value
}
func (m *Manager) All(handle string) (garden.Properties, error) {
m.propMutex.RLock()
defer m.propMutex.RUnlock()
return m.prop[handle], nil
}
func (m *Manager) Get(handle string, name string) (string, bool) {
var (
prop string
exists bool
)
m.propMutex.RLock()
defer m.propMutex.RUnlock()
prop, exists = m.prop[handle][name]
return prop, exists
}
func (m *Manager) Remove(handle string, name string) error {
m.propMutex.Lock()
defer m.propMutex.Unlock()
if _, exists := m.prop[handle][name]; !exists {
return NoSuchPropertyError{
Message: fmt.Sprintf("cannot Remove %s:%s", handle, name),
}
}
delete(m.prop[handle], name)
return nil
}
func (m *Manager) MatchesAll(handle string, props garden.Properties) bool {
m.propMutex.RLock()
defer m.propMutex.RUnlock()
for key, val := range props {
if m.prop[handle][key] != val {
return false
}
}
return true
}
type NoSuchPropertyError struct {
Message string
}
func (e NoSuchPropertyError) Error() string {
return e.Message
}
type NoSuchKeySpaceError struct {
Message string
}
func (e NoSuchKeySpaceError) Error() string {
return e.Message
}