-
Notifications
You must be signed in to change notification settings - Fork 67
/
bool.go
70 lines (57 loc) · 1.12 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
package zed
import (
"github.com/brimdata/zed/zcode"
)
type TypeOfBool struct{}
var False = Value{TypeBool, []byte{0}}
var True = Value{TypeBool, []byte{1}}
func IsTrue(zv zcode.Bytes) bool {
return zv[0] != 0
}
// Not returns the inverse Value of the Boolean-typed bytes value of zb.
func Not(zb zcode.Bytes) Value {
if IsTrue(zb) {
return False
}
return True
}
func NewBool(b bool) Value {
return Value{TypeBool, EncodeBool(b)}
}
func AppendBool(zb zcode.Bytes, b bool) zcode.Bytes {
if b {
return append(zb, 1)
}
return append(zb, 0)
}
func EncodeBool(b bool) zcode.Bytes {
return AppendBool(nil, b)
}
func DecodeBool(zv zcode.Bytes) (bool, error) {
if zv == nil {
return false, nil
}
if zv[0] != 0 {
return true, nil
}
return false, nil
}
func (t *TypeOfBool) ID() int {
return IDBool
}
func (t *TypeOfBool) String() string {
return "bool"
}
func (t *TypeOfBool) Marshal(zv zcode.Bytes) (interface{}, error) {
return DecodeBool(zv)
}
func (t *TypeOfBool) Format(zv zcode.Bytes) string {
b, err := DecodeBool(zv)
if err != nil {
return badZng(err, t, zv)
}
if b {
return "true"
}
return "false"
}