-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
174 lines (141 loc) · 3.32 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package dirsyn
/*
Boolean wraps a pointer to bool to implement the ASN.1 BOOLEAN type.
From [§ 3.3.3 of RFC 4517]
Boolean = "TRUE" / "FALSE"
A zero instance of this type is equivalent to "UNDEFINED" (neither TRUE
nor FALSE).
[§ 3.3.3 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.3
*/
type Boolean struct {
*bool
}
/*
Boolean returns an instance of [Boolean] alongside an error.
Valid input types are native Go Booleans, string representations of
Booleans and nil.
If the input is a Go Boolean, true is equal to "TRUE" in the context of
directory values, while false is equal to "FALSE". The return error instance
shall always be nil.
If the input is a string, case is not significant in the matching process.
A value of "TRUE" returns a Go Boolean of true, while "FALSE" returns false.
Any other string value results in an error.
If the input is nil, the return is a zero instance of [Boolean].
All other input types return an error.
From [§ 3.3.3 of RFC 4517]:
Boolean = "TRUE" / "FALSE"
[§ 3.3.3 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.3
*/
func (r RFC4517) Boolean(x any) (b Boolean, err error) {
b, err = assertBoolean(x)
return
}
func boolean(x any) (result Boolean) {
_, err := assertBoolean(x)
result.Set(err == nil)
return
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r Boolean) IsZero() bool {
return r.bool == nil
}
/*
Undefined wraps [Boolean.IsZero] and indicates a state that is neither
true nor false.
*/
func (r Boolean) Undefined() bool { return r.IsZero() }
/*
True returns a Boolean value indicative of a true receiver state.
*/
func (r Boolean) True() (v bool) {
if !r.IsZero() {
v = (*r.bool) == true
}
return
}
/*
False returns a Boolean value indicative of a false receiver state.
*/
func (r Boolean) False() (v bool) {
if !r.IsZero() {
v = (*r.bool) != true
}
return
}
/*
Set assigns the indicated truthy input value to the receiver instance.
Valid input types are string, *bool, bool or nil. Case is not significant
in the matching process involving strings.
*/
func (r *Boolean) Set(b any) {
switch tv := b.(type) {
case *bool:
r.bool = tv
case bool:
r.bool = &tv
case nil:
r.bool = nil
case string:
var t bool
switch uc(tv) {
case `TRUE`:
t = true
r.bool = &t
case `FALSE`:
r.bool = &t
}
}
}
func (r Boolean) String() (s string) {
s = "UNDEFINED"
if !r.IsZero() {
s = "FALSE"
if *r.bool {
s = "TRUE"
}
}
return
}
func assertBoolean(x any) (b Boolean, err error) {
switch tv := x.(type) {
case nil:
case Boolean:
b = tv
case bool:
b = Boolean{&tv}
case string:
if !(streqf(tv, `TRUE`) || streqf(tv, `FALSE`)) {
err = errorTxt("Invalid Boolean " + tv)
} else {
_b := uc(tv) == `TRUE`
b = Boolean{&_b}
}
default:
err = errorBadType("Boolean")
}
return
}
/*
booleanMatch implements [§ 4.2.2 of RFC 4517].
OID: 2.5.13.13.
[§ 4.2.2 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.2
*/
func booleanMatch(a, b any) (result Boolean, err error) {
var A, B Boolean
if A, err = assertBoolean(a); err != nil {
return
}
if B, err = assertBoolean(b); err != nil {
return
}
if A.True() {
result.Set(B.True())
} else if A.False() {
result.Set(B.False())
} else if A.Undefined() {
result.Set(B.Undefined())
}
return
}