-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.go
110 lines (96 loc) · 3.15 KB
/
list.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
package coltype
import (
"fmt"
"strings"
"github.com/go-sif/sif"
)
// listType is a FixedWidthColumnType which stores a fixed number of fixed-width type items
type listType[T any] struct {
ItemType sif.GenericFixedWidthColumnType[T]
Length int
}
// Size in bytes of a listType
func (c *listType[T]) Size() int {
return c.Length * c.ItemType.Size()
}
// ToStringT produces a string representation of a value of a listType value
func (c *listType[T]) ToStringT(v []T) string {
var res strings.Builder
fmt.Fprint(&res, "[")
i := 0
for _, val := range v {
// don't print more than 5 entries
if i > 5 {
fmt.Fprintf(&res, "... %d more", c.Length-5)
break
}
fmt.Fprintf(&res, "%x", c.ItemType.ToString(val))
i++
}
fmt.Fprint(&res, "]")
return res.String()
}
// ToString produces a string representation of a value of a listType value
func (c *listType[T]) ToString(v interface{}) string {
vl := v.([]T)
return c.ToStringT(vl)
}
// SerializeFixedT defines how values of this type are serialized into raw bytes
func (c *listType[T]) SerializeFixedT(v []T, dest []byte) error {
fixedType := c.ItemType.(sif.GenericFixedWidthColumnType[T])
start := 0
for _, val := range v {
err := c.ItemType.SerializeFixedT(val, dest[start:start+fixedType.Size()])
if err != nil {
return err
}
start += fixedType.Size()
}
return nil
}
// SerializeFixed defines how values of this type are serialized into raw bytes
func (c *listType[T]) SerializeFixed(v interface{}, dest []byte) error {
vl, ok := v.([]T)
if !ok {
return fmt.Errorf("value %v is not a []T", v)
}
return c.SerializeFixedT(vl, dest)
}
// DeserializeT defines how values of this type are deserialized from raw bytes
func (c *listType[T]) DeserializeT(sv []byte) ([]T, error) {
res := make([]T, 0, c.Length)
for start := 0; start < c.Size(); start += c.ItemType.Size() {
dval, err := c.ItemType.DeserializeT(sv[start : start+c.ItemType.Size()])
if err != nil {
return nil, err
}
res = append(res, dval)
}
return res, nil
}
// Deserialize defines how values of this type are deserialized from raw bytes
func (c *listType[T]) Deserialize(sv []byte) (interface{}, error) {
return c.DeserializeT(sv)
}
// GetLength is an accessor for listType's set Length
func (c *listType[T]) GetLength() int {
return c.Length
}
// GetItemType is an accessor for listType's set ItemType
func (c *listType[T]) GetItemType() sif.GenericFixedWidthColumnType[T] {
return c.ItemType
}
// ListColumnType provides access to the Length and ItemType set for a GenericColumnType[T]
type ListColumnType[T any] interface {
sif.ColumnType
GetLength() int
GetItemType() sif.GenericFixedWidthColumnType[T]
}
// List returns a GenericColumnAccessor for a FixedWidthColumnType which stores a fixed number of fixed-width type items
func List[T any](colName string, itemType sif.GenericColumnAccessor[T], length int) sif.GenericColumnAccessor[[]T] {
if !sif.IsFixedWidth(itemType.Type()) {
panic(fmt.Sprintf("provided itemType for List(%s) is not a fixed-width column type", colName))
}
fixedType := itemType.Type().(sif.GenericFixedWidthColumnType[T])
return sif.CreateColumnAccessor[[]T](&listType[T]{fixedType, length}, colName)
}