forked from cloudfoundry/rep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootfs_providers.go
159 lines (122 loc) · 3.26 KB
/
rootfs_providers.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
package rep
import (
"encoding/json"
"net/url"
)
type RootFSProvider interface {
Type() RootFSProviderType
Match(url.URL) bool
}
type RootFSProviderType string
const (
RootFSProviderTypeArbitrary RootFSProviderType = "arbitrary"
RootFSProviderTypeFixedSet RootFSProviderType = "fixed_set"
)
type RootFSProviders map[string]RootFSProvider
func (p RootFSProviders) Copy() RootFSProviders {
pCopy := RootFSProviders{}
for key := range p {
pCopy[key] = p[key]
}
return pCopy
}
func (p RootFSProviders) Match(rootFS url.URL) bool {
provider, ok := p[rootFS.Scheme]
if !ok {
return false
}
return provider.Match(rootFS)
}
func (providers *RootFSProviders) UnmarshalJSON(payload []byte) error {
var providerEnvelope map[string]json.RawMessage
err := json.Unmarshal(payload, &providerEnvelope)
if err != nil {
return err
}
*providers = RootFSProviders{}
for key, value := range providerEnvelope {
provider, err := unmarshalRootFSProvider(value)
if err != nil {
return err
}
(*providers)[key] = provider
}
return nil
}
type rootFSProviderEnvelope struct {
Type RootFSProviderType `json:"type"`
}
func unmarshalRootFSProvider(payload []byte) (RootFSProvider, error) {
var envelope rootFSProviderEnvelope
err := json.Unmarshal(payload, &envelope)
if err != nil {
return nil, err
}
switch envelope.Type {
case RootFSProviderTypeArbitrary:
return ArbitraryRootFSProvider{}, nil
case RootFSProviderTypeFixedSet:
var provider FixedSetRootFSProvider
err := provider.UnmarshalJSON(payload)
return provider, err
}
return nil, nil
}
type ArbitraryRootFSProvider struct{}
func (ArbitraryRootFSProvider) Type() RootFSProviderType { return RootFSProviderTypeArbitrary }
func (ArbitraryRootFSProvider) Match(url.URL) bool { return true }
func (provider ArbitraryRootFSProvider) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{"type": string(provider.Type())})
}
type FixedSetRootFSProvider struct {
FixedSet StringSet
}
func NewFixedSetRootFSProvider(rootfses ...string) FixedSetRootFSProvider {
return FixedSetRootFSProvider{
FixedSet: NewStringSet(rootfses...),
}
}
func (FixedSetRootFSProvider) Type() RootFSProviderType { return RootFSProviderTypeFixedSet }
func (provider FixedSetRootFSProvider) Match(rootfs url.URL) bool {
return provider.FixedSet.Contains(rootfs.Opaque)
}
func (provider FixedSetRootFSProvider) MarshalJSON() ([]byte, error) {
setPayload, err := json.Marshal(provider.FixedSet)
if err != nil {
return nil, err
}
typePayload, err := json.Marshal(provider.Type())
if err != nil {
return nil, err
}
setValue := json.RawMessage(setPayload)
typeValue := json.RawMessage(typePayload)
return json.Marshal(map[string]*json.RawMessage{
"type": &typeValue,
"set": &setValue,
})
}
func (provider *FixedSetRootFSProvider) UnmarshalJSON(payload []byte) error {
type fixed struct {
Set StringSet `json"set"`
}
var f fixed
err := json.Unmarshal(payload, &f)
if err != nil {
return err
}
provider.FixedSet = f.Set
return nil
}
type StringSet map[string]struct{}
func NewStringSet(entries ...string) StringSet {
set := StringSet{}
for _, entry := range entries {
set[entry] = struct{}{}
}
return set
}
func (set StringSet) Contains(candidate string) bool {
_, ok := set[candidate]
return ok
}