-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_bool.go
83 lines (70 loc) · 1.09 KB
/
null_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
package nullany
import "errors"
/*
@see https://github.com/xin-tsla/nullany/blob/main/README.md
*/
type NullBool uint8
const (
Null NullBool = iota
False
True
)
func NewNullBool() NullBool {
return Null
}
func NewNullBoolVal(b bool) NullBool {
if b {
return True
}
return False
}
func (n NullBool) IsNull() bool {
return n == Null
}
func (n NullBool) Get() (bool, error) {
switch n {
case False:
return false, nil
case True:
return true, nil
default:
return false, errors.New("null")
}
}
func (n NullBool) Must() bool {
if n == True {
return true
}
return false
}
type op uint8
const (
and op = iota
or
)
func handleBinaryOp(a, b NullBool, o op) NullBool {
if a == Null && b == Null {
return Null
}
if a == Null {
return b
}
if b == Null {
return a
}
l := a.Must()
r := b.Must()
switch o {
case and:
return NewNullBoolVal(l && r)
case or:
return NewNullBoolVal(l || r)
}
return Null
}
func (n NullBool) And(nb NullBool) NullBool {
return handleBinaryOp(n, nb, and)
}
func (n NullBool) Or(nb NullBool) NullBool {
return handleBinaryOp(n, nb, or)
}