/
type.go
50 lines (42 loc) · 1.22 KB
/
type.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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package resource
import (
"github.com/juju/errors"
)
// These are the valid resource types (except for unknown).
const (
typeUnknown Type = iota
TypeFile
TypeContainerImage
)
var types = map[Type]string{
TypeFile: "file",
TypeContainerImage: "oci-image",
}
// Type enumerates the recognized resource types.
type Type int
// ParseType converts a string to a Type. If the given value does not
// match a recognized type then an error is returned.
func ParseType(value string) (Type, error) {
for rt, str := range types {
if value == str {
return rt, nil
}
}
return typeUnknown, errors.Errorf("unsupported resource type %q", value)
}
// String returns the printable representation of the type.
func (rt Type) String() string {
return types[rt]
}
// Validate ensures that the type is valid.
func (rt Type) Validate() error {
// Ideally, only the (unavoidable) zero value would be invalid.
// However, typedef'ing int means that the use of int literals
// could result in invalid Type values other than the zero value.
if _, ok := types[rt]; !ok {
return errors.NewNotValid(nil, "unknown resource type")
}
return nil
}