-
Notifications
You must be signed in to change notification settings - Fork 67
/
array.go
61 lines (53 loc) · 1.02 KB
/
array.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
package zed
import (
"fmt"
"strings"
"github.com/brimdata/zed/zcode"
)
type TypeArray struct {
id int
Type Type
}
func NewTypeArray(id int, typ Type) *TypeArray {
return &TypeArray{id, typ}
}
func (t *TypeArray) ID() int {
return t.id
}
func (t *TypeArray) String() string {
return fmt.Sprintf("[%s]", t.Type)
}
func (t *TypeArray) Marshal(zv zcode.Bytes) (interface{}, error) {
// start out with zero-length container so we get "[]" instead of nil
vals := []Value{}
it := zv.Iter()
for !it.Done() {
val, _, err := it.Next()
if err != nil {
return nil, err
}
vals = append(vals, Value{t.Type, val})
}
return vals, nil
}
func (t *TypeArray) Format(zv zcode.Bytes) string {
var b strings.Builder
sep := ""
b.WriteByte('[')
it := zv.Iter()
for !it.Done() {
val, _, err := it.Next()
if err != nil {
return badZng(err, t, zv)
}
b.WriteString(sep)
if val == nil {
b.WriteString("null")
} else {
b.WriteString(t.Type.Format(val))
}
sep = ","
}
b.WriteByte(']')
return b.String()
}