protoavro

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2023 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package protoavro provides functions for converting between Protocol Buffers and Avro.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func InferSchema

func InferSchema(desc protoreflect.MessageDescriptor) (avro.Schema, error)

InferSchema returns the Avro schema, with default SchemaOptions, for the protobuf message descriptor.

Example
msg := &library.Book{}
schema, err := protoavro.SchemaOptions{}.InferSchema(msg.ProtoReflect().Descriptor())
if err != nil {
	panic(err)
}
expected := avro.Nullable(avro.Record{
	Type:      avro.RecordType,
	Name:      "Book",
	Namespace: "google.example.library.v1",
	Fields: []avro.Field{
		{Name: "name", Type: avro.Nullable(avro.String())},
		{Name: "author", Type: avro.Nullable(avro.String())},
		{Name: "title", Type: avro.Nullable(avro.String())},
		{Name: "read", Type: avro.Nullable(avro.Boolean())},
	},
})
fmt.Println(cmp.Equal(expected, schema))
Output:

true

Types

type Marshaler

type Marshaler struct {
	// contains filtered or unexported fields
}

Marshaler encodes and writes Avro binary encoded messages.

Example
var msg library.Book
var b bytes.Buffer
marshaller, err := protoavro.NewMarshaler(msg.ProtoReflect().Descriptor(), &b)
if err != nil {
	panic(err)
}
if err := marshaller.Marshal(
	&library.Book{
		Name:   "shelves/1/books/1",
		Title:  "Harry Potter",
		Author: "J. K. Rowling",
	},
); err != nil {
	panic(err)
}
Output:

func NewMarshaler

func NewMarshaler(descriptor protoreflect.MessageDescriptor, writer io.Writer) (*Marshaler, error)

NewMarshaler returns a new marshaler, with default SchemaOptions, that writes protobuf messages to writer in Avro binary format.

func (*Marshaler) Append added in v0.6.0

func (m *Marshaler) Append(messages interface{}) error

Append writes the messages to the writer.

func (*Marshaler) Marshal

func (m *Marshaler) Marshal(messages ...proto.Message) error

Marshal encodes and writes messages to the writer.

type SchemaOptions added in v0.6.0

type SchemaOptions struct {
	OmitRootElement bool
}

SchemaOptions contains configuration options for Avro schema inference. OmitRootElement is used to determine whether the root element of a message should be omitted, when writing to Avro.

func (SchemaOptions) Encode added in v0.6.0

func (o SchemaOptions) Encode(message proto.Message) (interface{}, error)

Encode encodes the message.

func (SchemaOptions) InferSchema added in v0.6.0

InferSchema returns the Avro schema for the protobuf message descriptor.

func (SchemaOptions) NewMarshaler added in v0.6.0

func (o SchemaOptions) NewMarshaler(descriptor protoreflect.MessageDescriptor, writer io.Writer) (*Marshaler, error)

NewMarshaler returns a new marshaler that writes protobuf messages to writer in Avro binary format.

func (SchemaOptions) NewUnmarshaler added in v0.6.0

func (o SchemaOptions) NewUnmarshaler(reader io.Reader) (*Unmarshaler, error)

NewUnmarshaler returns a new unmarshaler that reads protobuf messages from reader in Avro binary format.

type Unmarshaler

type Unmarshaler struct {
	// contains filtered or unexported fields
}

Unmarshaler reads and decodes Avro binary encoded messages.

func NewUnmarshaler

func NewUnmarshaler(reader io.Reader) (*Unmarshaler, error)

NewUnmarshaler returns a new unmarshaler that reads protobuf messages from reader in Avro binary format.

func (*Unmarshaler) Scan

func (m *Unmarshaler) Scan() bool

Scan returns true when there is at least one more message to be read. Scan should be called prior to calling Unmarshal.

func (*Unmarshaler) Unmarshal

func (m *Unmarshaler) Unmarshal(message proto.Message) error

Unmarshal consumes one message from the reader and places it in message.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL