forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
37 lines (31 loc) · 842 Bytes
/
encoder.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
package avro
import (
"io"
)
// Encoder writes Avro values to an output stream.
type Encoder struct {
s Schema
w *Writer
}
// NewEncoder returns a new encoder that writes to w using schema s.
func NewEncoder(s string, w io.Writer) (*Encoder, error) {
sch, err := Parse(s)
if err != nil {
return nil, err
}
return NewEncoderForSchema(sch, w), nil
}
// NewEncoderForSchema returns a new encoder that writes to w using schema.
func NewEncoderForSchema(schema Schema, w io.Writer) *Encoder {
return DefaultConfig.NewEncoder(schema, w)
}
// Encode writes the Avro encoding of v to the stream.
func (e *Encoder) Encode(v any) error {
e.w.WriteVal(e.s, v)
_ = e.w.Flush()
return e.w.Error
}
// Marshal returns the Avro encoding of v.
func Marshal(schema Schema, v any) ([]byte, error) {
return DefaultConfig.Marshal(schema, v)
}