-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
95 lines (83 loc) · 1.51 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
package convpb
import (
"database/sql"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type BoolWrapper interface {
Commoner[BoolWrapper]
ToBoolValue() *wrapperspb.BoolValue
}
type boolWrapper struct {
value *bool
}
func (b *boolWrapper) IsNil() bool {
return b == nil || b.value == nil
}
func (b *boolWrapper) Clone() BoolWrapper {
if b.IsNil() {
return &boolWrapper{}
}
cop := *b.value
return &boolWrapper{value: &cop}
}
func (b *boolWrapper) EmptySetNil() BoolWrapper {
if b.IsNil() {
return b
}
if !*b.value {
b.value = nil
}
return b
}
func (b *boolWrapper) ToBoolValue() *wrapperspb.BoolValue {
if b.IsNil() {
return nil
}
return wrapperspb.Bool(*b.value)
}
type BoolValuer interface {
Commoner[BoolValuer]
ToBoolRef() *bool
ToBool() bool
ToSQLNullBool() sql.NullBool
}
type boolValuer struct {
value *wrapperspb.BoolValue
}
func (b *boolValuer) IsNil() bool {
return b == nil || b.value == nil
}
func (b *boolValuer) Clone() BoolValuer {
if b.IsNil() {
return &boolValuer{}
}
return &boolValuer{value: wrapperspb.Bool(b.value.Value)}
}
func (b *boolValuer) EmptySetNil() BoolValuer {
if b.IsNil() {
return b
}
if !b.value.Value {
b.value = nil
}
return b
}
func (b *boolValuer) ToBoolRef() *bool {
if b.IsNil() {
return nil
}
v := b.value.Value
return &v
}
func (b *boolValuer) ToBool() bool {
if b.IsNil() {
return false
}
return b.value.Value
}
func (b *boolValuer) ToSQLNullBool() sql.NullBool {
return sql.NullBool{
Bool: b.ToBool(),
Valid: !b.IsNil(),
}
}