/
condition_attribute.go
76 lines (68 loc) · 1.72 KB
/
condition_attribute.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
package storage
import (
"fmt"
"strings"
)
// ConditionAttribute is an enum of attributes a project rule condition can be.
type ConditionAttribute int
const (
ChefRole ConditionAttribute = iota
ChefServer
ChefTag
Environment
Organization
PolicyGroup
PolicyName
)
func (c ConditionAttribute) String() string {
if c < ChefRole || c > PolicyName {
panic(fmt.Sprintf("unknown value from iota ConditionAttribute on String() conversion: %d", c))
}
return conditionAttributeStringValues()[c]
}
// NewConditionAttribute converts a string to a ConditionAttribute or returns an error.
func NewConditionAttribute(in string) (ConditionAttribute, error) {
switch in {
case "organization":
return Organization, nil
case "chef-server":
return ChefServer, nil
case "environment":
return Environment, nil
case "role":
return ChefRole, nil
case "chef-tag":
return ChefTag, nil
case "policy-name":
return PolicyName, nil
case "policy-group":
return PolicyGroup, nil
default:
return Organization,
fmt.Errorf("condition attribute type must be one of %q, you passed %s",
conditionAttributeStringValues(), in)
}
}
// UnmarshalJSON implements json unmarshalling for a ConditionAttribute reference
// so we can pull them out of the database directly as the correct type.
func (c *ConditionAttribute) UnmarshalJSON(b []byte) error {
// After byte conversion, things coming out of db as
// '"node"' and '"event"'.
result, err := NewConditionAttribute(strings.Trim(string(b), "\""))
if err != nil {
return err
}
*c = result
return nil
}
func conditionAttributeStringValues() [7]string {
return [...]string{
"role",
"chef-server",
"chef-tag",
"environment",
"organization",
"policy-group",
"policy-name",
}
}