-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathresmap.go
174 lines (138 loc) · 3.65 KB
/
resmap.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
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
package zebra
import (
"encoding/json"
)
type ResourceList struct {
ctr TypeConstructor
Resources []Resource
}
func NewResourceList(ctr TypeConstructor) *ResourceList {
return &ResourceList{
ctr: ctr,
Resources: []Resource{},
}
}
func (r *ResourceList) Add(res Resource) error {
r.Resources = append(r.Resources, res)
return nil
}
func (r *ResourceList) Delete(res Resource) error {
listLen := len(r.Resources)
for i, val := range r.Resources {
if val.GetMeta().ID == res.GetMeta().ID {
r.Resources[i] = r.Resources[listLen-1]
r.Resources = r.Resources[:listLen-1]
return nil
}
}
return ErrNotFound
}
func CopyResourceList(dest *ResourceList, src *ResourceList) {
if dest == nil || src == nil {
return
}
dest.ctr = src.ctr
dest.Resources = make([]Resource, len(src.Resources))
copy(dest.Resources, src.Resources)
}
func (r *ResourceList) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Resources)
}
func (r *ResourceList) UnmarshalJSON(data []byte) error {
// unmarshal the data as a list of maps with string as key so that
// we can then create a resource object and parse each resource.
values := []json.RawMessage{}
if e := json.Unmarshal(data, &values); e != nil {
return e
}
// Convert each value back into a byte array. Use the type to create the
// actual resource object and then unmarshal the byte array into that
// resource object.
for _, value := range values {
resource := r.ctr()
// We have the []byte representation and we have the target object
// so now can do the final unmarshal and add this object into the
// resource list
if e := json.Unmarshal(value, resource); e != nil {
return e
}
r.Resources = append(r.Resources, resource)
}
return nil
}
type ResourceMap struct {
factory ResourceFactory
Resources map[string]*ResourceList
}
func NewResourceMap(f ResourceFactory) *ResourceMap {
return &ResourceMap{
factory: f,
Resources: map[string]*ResourceList{},
}
}
func CopyResourceMap(dest *ResourceMap, src *ResourceMap) {
if dest == nil || src == nil {
return
}
dest.factory = src.factory
dest.Resources = make(map[string]*ResourceList)
for key, val := range src.Resources {
ctr, _ := src.factory.Constructor(key)
dest.Resources[key] = NewResourceList(ctr)
CopyResourceList(dest.Resources[key], val)
}
}
func (r *ResourceMap) Factory() ResourceFactory {
return r.factory
}
func (r *ResourceMap) Add(res Resource) error {
key := res.GetMeta().Type.Name
rl := r.Resources[key]
if rl == nil {
if ctr, ok := r.factory.Constructor(key); ok {
rl = NewResourceList(ctr)
r.Resources[key] = rl
} else {
return ErrTypeEmpty
}
}
return rl.Add(res)
}
func (r *ResourceMap) Delete(res Resource) error {
key := res.GetMeta().Type.Name
if r.Resources[key] == nil {
return ErrNotFound
}
if err := r.Resources[key].Delete(res); err != nil {
return err
}
if len(r.Resources[key].Resources) == 0 {
delete(r.Resources, key)
}
return nil
}
func (r *ResourceMap) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Resources)
}
func (r *ResourceMap) UnmarshalJSON(data []byte) error {
// unmarshal the data as a map[string][]byte to extract each resourcelist
// against a type.
values := map[string]json.RawMessage{}
if e := json.Unmarshal(data, &values); e != nil {
return e
}
// for each type create the resource list and parse the resource list
for vType, rData := range values {
ctr, ok := r.factory.Constructor(vType)
if !ok {
return ErrTypeEmpty
}
rList := NewResourceList(ctr)
if e := json.Unmarshal(rData, rList); e != nil {
return e
}
// Add this list to the Resource map
r.Resources[vType] = rList
}
return nil
}