avro

package module
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2023 License: Apache-2.0 Imports: 3 Imported by: 9

README

avro

avro for go

Install

go get github.com/aacfactory/avro

Usage

Set avro tag.

type Anonymous struct {
	Anonymous string `avro:"anonymous"`
}

type Bar struct {
	String string `avro:"string"`
	Next   *Bar   `avro:"next"`
}

type Foo struct {
	Anonymous
	String  string         `avro:"string"`
	Boolean bool           `avro:"boolean"`
	Int     int            `avro:"int"`
	Long    int64          `avro:"long"`
	Float   float32        `avro:"float"`
	Double  float64        `avro:"double"`
	Uint    uint64         `avro:"uint"`
	Time    time.Time      `avro:"time"`
	Dur     time.Duration  `avro:"dur"`
	Byte    byte           `avro:"byte"`
	Bytes   []byte         `avro:"bytes"`
	Bar     Bar            `avro:"bar"`
	Baz     *Bar           `avro:"baz"`
	Bars    []Bar          `avro:"bars"`
	Map     map[string]Bar `avro:"map"`
}

Marshal.

    foo := Foo{
		Anonymous: Anonymous{
			Anonymous: "Anonymous",
		},
		String:  "foo",
		Boolean: true,
		Int:     1,
		Long:    2,
		Float:   3.3,
		Double:  4.4,
		Uint:    uint64(5),
		Time:    time.Now(),
		Dur:     10 * time.Hour,
		Byte:    'B',
		Bytes:   []byte("bytes"),
		Bar: Bar{
			String: "bar",
			Next: &Bar{
				String: "Bar-Next",
				Next:   nil,
			},
		},
		Baz:  nil,
		Bars: []Bar{{String: "bar-1"}},
		Map:  map[string]Bar{"bar2": {String: "bar-2"}},
	}

	p, encodeErr := base.Marshal(s, foo)

Unmarshal

    r := Foo{}
	decodeErr := base.Unmarshal(s, p, &r)
	if decodeErr != nil {
		t.Error(decodeErr)
		return
	}

Note:

interface is not supported.

Benchmark

avro

BenchmarkAvro-20         1000000              1054 ns/op             626 B/op	9 allocs/op

encoding/json

BenchmarkJson-20          260964              4495 ns/op            1218 B/op   29 allocs/op

json-iterator/go

BenchmarkJson-20          591304              1953 ns/op             889 B/op   19 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v any) (p []byte, err error)

func MustMarshal added in v1.2.3

func MustMarshal(v any) (p []byte)

func MustUnmarshal added in v1.2.10

func MustUnmarshal(p []byte, v any)

func Register added in v1.1.0

func Register(v any)

func SchemaOf added in v1.1.1

func SchemaOf(v any) (p []byte, err error)

func Unmarshal

func Unmarshal(p []byte, v any) (err error)

Types

type Marshaler added in v1.2.0

type Marshaler interface {
	MarshalAvro() ([]byte, error)
}

type RawMessage added in v1.2.3

type RawMessage []byte

func (RawMessage) MarshalAvro added in v1.2.3

func (r RawMessage) MarshalAvro() ([]byte, error)

func (*RawMessage) UnmarshalAvro added in v1.2.3

func (r *RawMessage) UnmarshalAvro(p []byte) error

type Unmarshaler added in v1.2.0

type Unmarshaler interface {
	UnmarshalAvro(p []byte) error
}

Directories

Path Synopsis
internal
hashs/mmhash
Package mmhash export runtime.memhash
Package mmhash export runtime.memhash

Jump to

Keyboard shortcuts

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