/
data_chunk.go
87 lines (72 loc) · 1.67 KB
/
data_chunk.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
package core
import "errors"
var (
ErrDiscardedChunk = errors.New("chunk is discarded")
CHUNK_PROPNAMES = []string{"data"}
)
// A DataChunk represents a chunk of any kind of data, DataChunk implements Value.
type DataChunk struct {
data Value
read bool
discarded bool
getData func(c *DataChunk) (Value, error)
getElemCount func(c *DataChunk) int
merge func(c *DataChunk, other *DataChunk) error
//writeToWriter func(c *Chunk, writer *Writer) error
discard func(c *DataChunk) error
}
func (c *DataChunk) Data(ctx *Context) (Value, error) {
if c.discarded {
return nil, ErrDiscardedChunk
}
c.read = true
if c.getData == nil {
return c.data, nil
}
return c.getData(c)
}
func (c *DataChunk) ElemCount() int {
if c.getElemCount == nil {
return c.data.(Indexable).Len()
}
return c.getElemCount(c)
}
func (c *DataChunk) MergeWith(ctx *Context, other *DataChunk) error {
if c.discarded {
return ErrDiscardedChunk
}
return c.merge(c, other)
}
func (c *DataChunk) Discard(ctx *Context) error {
if c.read {
return nil
}
defer func() {
c.discarded = true
}()
return c.discard(c)
}
func (*DataChunk) GetGoMethod(name string) (*GoFunction, bool) {
return nil, false
}
func (c *DataChunk) Prop(ctx *Context, name string) Value {
switch name {
case "data":
data, err := c.Data(ctx)
if err != nil {
panic(err)
}
return data
}
method, ok := c.GetGoMethod(name)
if !ok {
panic(FormatErrPropertyDoesNotExist(name, c))
}
return method
}
func (*DataChunk) SetProp(ctx *Context, name string, value Value) error {
return ErrCannotSetProp
}
func (*DataChunk) PropertyNames(ctx *Context) []string {
return CHUNK_PROPNAMES
}