avro

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedType     = errors.New("unsupported avro type")
	ErrSchemaValueMismatch = errors.New("avro schema doesn't match supplied value")
)

Functions

This section is empty.

Types

type Builder

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

Builder builds avro.RecordSchema instances and marshals them into JSON. Builder accepts arguments for creating fields and creates them internally (i.e. a user doesn't need to create the fields). All errors will be returned as a joined error when marshaling the schema to JSON.

Example
package main

import (
	"fmt"

	"github.com/goccy/go-json"
	"github.com/hamba/avro/v2"
)

func main() {
	enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"})
	if err != nil {
		panic(err)
	}
	bytes, err := NewBuilder("schema_name", "schema_namespace").
		AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)).
		AddField("enum_field", enumSchema).
		MarshalJSON()
	if err != nil {
		panic(err)
	}

	prettyPrint(bytes)
}

func prettyPrint(bytes []byte) {
	m := map[string]interface{}{}
	err := json.Unmarshal(bytes, &m)
	if err != nil {
		panic(err)
	}

	pretty, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(pretty))
}
Output:

{
  "fields": [
    {
      "default": 100,
      "name": "int_field",
      "type": "int"
    },
    {
      "name": "enum_field",
      "type": {
        "name": "enum_namespace.enum_schema",
        "symbols": [
          "val1",
          "val2",
          "val3"
        ],
        "type": "enum"
      }
    }
  ],
  "name": "schema_namespace.schema_name",
  "type": "record"
}

func NewBuilder

func NewBuilder(name, namespace string) *Builder

NewBuilder constructs a new Builder and initializes it with the given name and namespace.

func (*Builder) AddField

func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOption) *Builder

AddField adds a new field with the given name, schema and schema options. If creating the field returns an error, the error is saved, joined with other errors (if any), and returned when marshaling to JSON.

func (*Builder) Build

func (b *Builder) Build() (*avro.RecordSchema, error)

Build builds the underlying schema. Errors that occurred while creating fields or constructing the schema will be returned as a joined error.

func (*Builder) MarshalJSON

func (b *Builder) MarshalJSON() ([]byte, error)

MarshalJSON marshals the underlying schema to JSON. Errors that occurred while creating fields, constructing the schema or marshaling it will be returned as a joined error.

type Serde

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

Serde represents an Avro schema. It exposes methods for marshaling and unmarshalling data.

func Parse

func Parse(text []byte) (*Serde, error)

Parse parses a schema byte slice.

func SerdeForType

func SerdeForType(v any) (*Serde, error)

SerdeForType uses reflection to extract an Avro schema from v. Maps are regarded as structs.

func (*Serde) Marshal

func (s *Serde) Marshal(v any) ([]byte, error)

Marshal returns the Avro encoding of v. Note that this function may mutate v. Limitations: - Map keys need to be of type string, - Array values need to be of type uint8 (byte).

func (*Serde) String

func (s *Serde) String() string

String returns the canonical form of the schema.

func (*Serde) Unmarshal

func (s *Serde) Unmarshal(b []byte, v any) 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. Note that arrays and maps are unmarshalled into slices and maps with untyped values (i.e. []any and map[string]any). This is a limitation of the Avro library used for encoding/decoding the payload.

Jump to

Keyboard shortcuts

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