-
Notifications
You must be signed in to change notification settings - Fork 1
/
volume.go
54 lines (47 loc) · 1.09 KB
/
volume.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
package types
import (
"encoding/json"
"errors"
)
// Volume encapsulates a volume which should be mounted into the filesystem
// of all apps in a ContainerRuntimeManifest
type Volume struct {
Kind string `json:"kind"`
Fulfills []ACName `json:"fulfills"`
// currently used only by "host"
// TODO(jonboulle): factor out?
Source string `json:"source,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
}
type volume Volume
func (v Volume) assertValid() error {
switch v.Kind {
case "empty":
return nil
case "host":
if v.Source == "" {
return errors.New("source for host volume cannot be empty")
}
return nil
default:
return errors.New(`unrecognized volume type: should be one of "empty", "host"`)
}
}
func (v *Volume) UnmarshalJSON(data []byte) error {
var vv volume
if err := json.Unmarshal(data, &vv); err != nil {
return err
}
nv := Volume(vv)
if err := nv.assertValid(); err != nil {
return err
}
*v = nv
return nil
}
func (v Volume) MarshalJSON() ([]byte, error) {
if err := v.assertValid(); err != nil {
return nil, err
}
return json.Marshal(volume(v))
}