-
Notifications
You must be signed in to change notification settings - Fork 3
/
bool.go
55 lines (44 loc) · 1.46 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
package coltype
import (
"fmt"
"github.com/go-sif/sif"
)
// boolType is a FixedWidthColumnType which stores a boolean value
type boolType struct{}
// Size in bytes of a boolType value
func (c *boolType) Size() int {
return 1
}
// ToStringT produces a string representation of a boolType value
func (c *boolType) ToStringT(v bool) string {
return fmt.Sprintf("%t", v)
}
// ToString produces a string representation of a boolType value
func (c *boolType) ToString(v interface{}) string {
return c.ToStringT(v.(bool))
}
// SerializeFixedT defines how values of this type are serialized into raw bytes
func (c *boolType) SerializeFixedT(v bool, dest []byte) error {
if v {
dest[0] = 1
} else {
dest[0] = 0
}
return nil
}
// SerializeFixed defines how values of this type are serialized into raw bytes
func (c *boolType) SerializeFixed(v interface{}, dest []byte) error {
return c.SerializeFixedT(v.(bool), dest)
}
// DeserializeT defines how values of this type are deserialized from raw bytes
func (c *boolType) DeserializeT(sv []byte) (bool, error) {
return sv[0] > 0, nil
}
// Deserialize defines how values of this type are deserialized from raw bytes
func (c *boolType) Deserialize(sv []byte) (interface{}, error) {
return c.DeserializeT(sv)
}
// Bool returns a GenericColumnAccessor for a FixedWidthColumnType which stores a boolean value
func Bool(colName string) sif.GenericColumnAccessor[bool] {
return sif.CreateColumnAccessor[bool](&boolType{}, colName)
}