-
Notifications
You must be signed in to change notification settings - Fork 714
/
Copy pathplugin_spec.go
228 lines (194 loc) · 5.15 KB
/
plugin_spec.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package xfer
import (
"bytes"
"fmt"
"sort"
"github.com/davecgh/go-spew/spew"
"github.com/ugorji/go/codec"
"github.com/weaveworks/ps"
"github.com/weaveworks/scope/test/reflect"
)
// PluginSpec is shared between the Probe, App, and UI. It is the plugin's
// self-proclaimed description.
type PluginSpec struct {
ID string `json:"id"`
// Label is a human-readable name of the plugin
Label string `json:"label"`
Description string `json:"description,omitempty"`
// Interfaces is a list of things this plugin can be used for (e.g. "reporter")
Interfaces []string `json:"interfaces"`
APIVersion string `json:"api_version,omitempty"`
Status string `json:"status,omitempty"`
}
// PluginSpecs is a set of plugin specs keyed on ID. Clients must use
// the Add method to add plugin specs
type PluginSpecs struct {
psMap ps.Map
}
// EmptyPluginSpecs is the empty set of plugin specs.
var EmptyPluginSpecs = PluginSpecs{ps.NewMap()}
// MakePluginSpecs makes a new PluginSpecs with the given plugin specs.
func MakePluginSpecs(specs ...PluginSpec) PluginSpecs {
return EmptyPluginSpecs.Add(specs...)
}
// Add adds the specs to the PluginSpecs. Add is the only valid way to grow a
// PluginSpecs. Add returns the PluginSpecs to enable chaining.
func (n PluginSpecs) Add(specs ...PluginSpec) PluginSpecs {
result := n.psMap
if result == nil {
result = ps.NewMap()
}
for _, spec := range specs {
result = result.Set(spec.ID, spec)
}
return PluginSpecs{result}
}
// Merge combines the two PluginSpecss and returns a new result.
func (n PluginSpecs) Merge(other PluginSpecs) PluginSpecs {
nSize, otherSize := n.Size(), other.Size()
if nSize == 0 {
return other
}
if otherSize == 0 {
return n
}
result, iter := n.psMap, other.psMap
if nSize < otherSize {
result, iter = iter, result
}
iter.ForEach(func(key string, otherVal interface{}) {
result = result.Set(key, otherVal)
})
return PluginSpecs{result}
}
// Lookup the spec by 'key'
func (n PluginSpecs) Lookup(key string) (PluginSpec, bool) {
if n.psMap != nil {
value, ok := n.psMap.Lookup(key)
if ok {
return value.(PluginSpec), true
}
}
return PluginSpec{}, false
}
// Keys is a list of all the keys in this set.
func (n PluginSpecs) Keys() []string {
if n.psMap == nil {
return nil
}
k := n.psMap.Keys()
sort.Strings(k)
return k
}
// Size is the number of specs in the set
func (n PluginSpecs) Size() int {
if n.psMap == nil {
return 0
}
return n.psMap.Size()
}
// ForEach executes f for each spec in the set. Nodes are traversed in sorted
// order.
func (n PluginSpecs) ForEach(f func(PluginSpec)) {
for _, key := range n.Keys() {
if val, ok := n.psMap.Lookup(key); ok {
f(val.(PluginSpec))
}
}
}
// Copy is a noop
func (n PluginSpecs) Copy() PluginSpecs {
return n
}
func (n PluginSpecs) String() string {
keys := []string{}
if n.psMap == nil {
n = EmptyPluginSpecs
}
psMap := n.psMap
if psMap == nil {
psMap = ps.NewMap()
}
for _, k := range psMap.Keys() {
keys = append(keys, k)
}
sort.Strings(keys)
buf := bytes.NewBufferString("{")
for _, key := range keys {
val, _ := psMap.Lookup(key)
fmt.Fprintf(buf, "%s: %s, ", key, spew.Sdump(val))
}
fmt.Fprintf(buf, "}")
return buf.String()
}
// DeepEqual tests equality with other PluginSpecss
func (n PluginSpecs) DeepEqual(i interface{}) bool {
d, ok := i.(PluginSpecs)
if !ok {
return false
}
if n.Size() != d.Size() {
return false
}
if n.Size() == 0 {
return true
}
equal := true
n.psMap.ForEach(func(k string, val interface{}) {
if otherValue, ok := d.psMap.Lookup(k); !ok {
equal = false
} else {
equal = equal && reflect.DeepEqual(val, otherValue)
}
})
return equal
}
func (n PluginSpecs) toIntermediate() []PluginSpec {
intermediate := make([]PluginSpec, 0, n.Size())
n.ForEach(func(spec PluginSpec) {
intermediate = append(intermediate, spec)
})
return intermediate
}
func (n PluginSpecs) fromIntermediate(specs []PluginSpec) PluginSpecs {
return MakePluginSpecs(specs...)
}
// CodecEncodeSelf implements codec.Selfer
func (n *PluginSpecs) CodecEncodeSelf(encoder *codec.Encoder) {
if n.psMap != nil {
encoder.Encode(n.toIntermediate())
} else {
encoder.Encode(nil)
}
}
// CodecDecodeSelf implements codec.Selfer
func (n *PluginSpecs) CodecDecodeSelf(decoder *codec.Decoder) {
in := []PluginSpec{}
if err := decoder.Decode(&in); err != nil {
return
}
*n = PluginSpecs{}.fromIntermediate(in)
}
// MarshalJSON shouldn't be used, use CodecEncodeSelf instead
func (PluginSpecs) MarshalJSON() ([]byte, error) {
panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead")
}
// UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead
func (*PluginSpecs) UnmarshalJSON(b []byte) error {
panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead")
}
// PluginSpecsByID implements sort.Interface, so we can sort the specs by the
// ID field.
type PluginSpecsByID []PluginSpec
// Len is part of sort.Interface.
func (p PluginSpecsByID) Len() int {
return len(p)
}
// Swap is part of sort.Interface.
func (p PluginSpecsByID) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
// Less is part of sort.Interface.
func (p PluginSpecsByID) Less(i, j int) bool {
return p[i].ID < p[j].ID
}