forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
49 lines (40 loc) · 1.21 KB
/
decoder.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
package avro
import (
"io"
)
// Decoder reads and decodes Avro values from an input stream.
type Decoder struct {
s Schema
r *Reader
}
// NewDecoder returns a new decoder that reads from reader r using schema s.
func NewDecoder(s string, r io.Reader) (*Decoder, error) {
sch, err := Parse(s)
if err != nil {
return nil, err
}
return NewDecoderForSchema(sch, r), nil
}
// NewDecoderForSchema returns a new decoder that reads from r using schema.
func NewDecoderForSchema(schema Schema, reader io.Reader) *Decoder {
return DefaultConfig.NewDecoder(schema, reader)
}
// Decode reads the next Avro encoded value from its input and stores it in the value pointed to by v.
func (d *Decoder) Decode(v any) error {
if d.r.head == d.r.tail && d.r.reader != nil {
if !d.r.loadMore() {
return io.EOF
}
}
d.r.ReadVal(d.s, v)
//nolint:errorlint // Only direct EOF errors should be discarded.
if d.r.Error == io.EOF {
return nil
}
return d.r.Error
}
// Unmarshal parses the Avro encoded data and stores the result in the value pointed to by v.
// If v is nil or not a pointer, Unmarshal returns an error.
func Unmarshal(schema Schema, data []byte, v any) error {
return DefaultConfig.Unmarshal(schema, data, v)
}