-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
bool.go
executable file
·96 lines (82 loc) · 1.73 KB
/
bool.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package types
import (
"encoding/json"
)
type BoolValue struct {
BaseAttribute
value bool
}
func (b BoolValue) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"value": b.value,
"metadata": b.metadata,
})
}
func (b *BoolValue) UnmarshalJSON(data []byte) error {
var keys map[string]any
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
if keys["value"] != nil {
b.value = keys["value"].(bool)
}
if keys["metadata"] != nil {
raw, err := json.Marshal(keys["metadata"])
if err != nil {
return err
}
var m Metadata
if err := json.Unmarshal(raw, &m); err != nil {
return err
}
b.metadata = m
}
return nil
}
func Bool(value bool, metadata Metadata) BoolValue {
return BoolValue{
value: value,
BaseAttribute: BaseAttribute{metadata: metadata},
}
}
func BoolTest(value bool) BoolValue {
return Bool(value, NewTestMetadata())
}
func BoolDefault(value bool, metadata Metadata) BoolValue {
b := Bool(value, metadata)
b.BaseAttribute.metadata.isDefault = true
return b
}
func BoolUnresolvable(m Metadata) BoolValue {
b := Bool(false, m)
b.BaseAttribute.metadata.isUnresolvable = true
return b
}
func BoolExplicit(value bool, metadata Metadata) BoolValue {
b := Bool(value, metadata)
b.BaseAttribute.metadata.isExplicit = true
return b
}
func (b BoolValue) Value() bool {
return b.value
}
func (b BoolValue) GetRawValue() any {
return b.value
}
func (b BoolValue) IsTrue() bool {
if b.metadata.isUnresolvable {
return false
}
return b.Value()
}
func (b BoolValue) IsFalse() bool {
if b.metadata.isUnresolvable {
return false
}
return !b.Value()
}
func (s BoolValue) ToRego() any {
m := s.metadata.ToRego().(map[string]any)
m["value"] = s.Value()
return m
}