-
Notifications
You must be signed in to change notification settings - Fork 67
/
deserializer.go
41 lines (34 loc) · 971 Bytes
/
deserializer.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
package zngbytes
import (
"io"
"github.com/brimdata/zed"
"github.com/brimdata/zed/zio/zngio"
"github.com/brimdata/zed/zson"
)
type Deserializer struct {
reader *zngio.Reader
unmarshaler *zson.UnmarshalZNGContext
}
func NewDeserializer(reader io.Reader, templates []interface{}) *Deserializer {
return NewDeserializerWithContext(zed.NewContext(), reader, templates)
}
func NewDeserializerWithContext(zctx *zed.Context, reader io.Reader, templates []interface{}) *Deserializer {
u := zson.NewZNGUnmarshaler()
u.Bind(templates...)
return &Deserializer{
reader: zngio.NewReader(zctx, reader),
unmarshaler: u,
}
}
func (d *Deserializer) Close() error { return d.reader.Close() }
func (d *Deserializer) Read() (interface{}, error) {
rec, err := d.reader.Read()
if err != nil || rec == nil {
return nil, err
}
var action interface{}
if err := d.unmarshaler.Unmarshal(*rec, &action); err != nil {
return nil, err
}
return action, nil
}