summaryrefslogtreecommitdiff
path: root/vendor/github.com/hamba/avro/v2/encoder.go
blob: faa285e674139f91aa4a7dab1f450d8815d697a3 (plain)
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)
}