-
Notifications
You must be signed in to change notification settings - Fork 70
/
ternary.go
48 lines (38 loc) · 1.06 KB
/
ternary.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
package config
import (
"fmt"
"strings"
)
// Ternary is like a bool, but includes the unknown state
type Ternary string
// TernaryValues provides the set of Ternary values
var TernaryValues = struct {
// Allow the option
Allow Ternary
// Disallow the option
Disallow Ternary
// Unknown is the unknown state
Unknown Ternary
}{
Allow: "ALLOW",
Disallow: "DISALLOW",
Unknown: "NOT_ASKED",
}
// Valid returns true for a valid value, false otherwise
func (t Ternary) Valid() error {
val := string(t)
if strings.EqualFold(val, string(TernaryValues.Allow)) ||
strings.EqualFold(val, string(TernaryValues.Disallow)) ||
strings.EqualFold(val, string(TernaryValues.Unknown)) {
return nil
}
return fmt.Errorf("\"%s\" is not a valid value; Please use one of: %s", val, TernaryValues)
}
// Bool returns true if the ternary is set and contains the true value
func (t Ternary) Bool() bool {
return strings.EqualFold(t.String(), TernaryValues.Allow.String())
}
// String returns the string value of the ternary
func (t Ternary) String() string {
return string(t)
}