/
rule_type.go
56 lines (48 loc) · 1.14 KB
/
rule_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
51
52
53
54
55
56
package storage
import (
"fmt"
"strings"
)
// RuleType is an enum of the types a project rule can be.
type RuleType int
const (
Node RuleType = iota
Event
)
func (r RuleType) String() string {
if r < Node || r > Event {
panic(fmt.Sprintf("unknown value from iota RuleType on String() conversion: %d", r))
}
return ruleTypeStringValues()[r]
}
// NewRuleType converts a string to a RuleType or returns an error.
func NewRuleType(in string) (RuleType, error) {
switch in {
case "node":
return Node, nil
case "event":
return Event, nil
default:
return Node,
fmt.Errorf("rule type must be one of %q, you passed %s",
ruleTypeStringValues(), in)
}
}
// UnmarshalJSON implements json unmarshalling for an RuleType reference
// so we can pull them out of the database directly as the correct type.
func (r *RuleType) UnmarshalJSON(b []byte) error {
// After byte conversion, things coming out of db as
// '"node"' and '"event"'.
result, err := NewRuleType(strings.Trim(string(b), "\""))
if err != nil {
return err
}
*r = result
return nil
}
func ruleTypeStringValues() [2]string {
return [...]string{
"node",
"event",
}
}