View Source AvroEx (AvroEx v2.0.0)

AvroEx is a library for encoding and decoding data with Avro schemas. Supports parsing schemas, encoding data, and decoding data.

For encoding and decoding, the following type chart should be referenced:

Avro TypesElixir Types
booleanboolean
integerinteger
longinteger
floatdecimal
doubledecimal
bytesbinary
stringString.t, atom
nullnil
Recordmap
EnumString.t, atom (corresponding to the enum's symbol list)

Link to this section Summary

Functions

Given an encoded message and its accompanying schema, decodes the message.

Same as decode/2, but returns raw decoded value.

Given an Elixir or JSON-encoded schema, parses the schema and returns a AvroEx.Schema.t/0 struct representing the schema.

Same as AvroEx.decode_schema/1, but raises an exception on failure instead of returning an error tuple.

Checks to see if the given data is encodable using the given schema. Helpful for unit testing.

Given AvroEx.Schema.t/0 and term(), takes the data and encodes it according to the schema.

Same as encode/2, but returns the encoded value directly.

Encodes the given schema to JSON

parse_schema(json) deprecated
parse_schema!(json) deprecated

Link to this section Types

Specs

encoded_avro() :: binary()

Link to this section Functions

Specs

decode(AvroEx.Schema.t(), encoded_avro()) ::
  {:ok, term()} | {:error, AvroEx.DecodeError.t()}

Given an encoded message and its accompanying schema, decodes the message.

iex> schema = AvroEx.decode_schema!("boolean")
iex> AvroEx.decode(schema, <<1>>)
{:ok, true}
Link to this function

decode!(schema, message)

View Source

Specs

decode!(AvroEx.Schema.t(), encoded_avro()) :: term()

Same as decode/2, but returns raw decoded value.

Raises AvroEx.DecodeError.t/0 on error.

examples

Examples

iex> schema = AvroEx.decode_schema!("string")
iex> encoded = AvroEx.encode!(schema, "hello")
iex> AvroEx.decode!(schema, encoded)
"hello"
Link to this function

decode_schema(schema, opts \\ [])

View Source

Specs

decode_schema(term(), Keyword.t()) ::
  {:ok, AvroEx.Schema.t()} | {:error, AvroEx.Schema.DecodeError.t()}

Given an Elixir or JSON-encoded schema, parses the schema and returns a AvroEx.Schema.t/0 struct representing the schema.

Errors for invalid JSON, invalid schemas, and bad name references.

options

Options

  • :strict - whether to strictly validate the schema, defaults to false. Recommended to turn this on for locally owned schemas, but not for interop with external schemas.

examples

Examples

iex> AvroEx.decode_schema("string")
{:ok, %AvroEx.Schema{schema: %AvroEx.Schema.Primitive{type: :string}}}

iex> json= ~S({"fields":[{"name":"a","type":"string"}],"name":"my_type","type":"record"})
iex> {:ok, %Schema{schema: record}} = AvroEx.decode_schema(json)
iex> match?(%Record{}, record)
true
Link to this function

decode_schema!(schema, opts \\ [])

View Source

Specs

decode_schema!(term(), Keyword.t()) :: AvroEx.Schema.t()

Same as AvroEx.decode_schema/1, but raises an exception on failure instead of returning an error tuple.

examples

Examples

iex> AvroEx.decode_schema!("int")
%AvroEx.Schema{schema: %AvroEx.Schema.Primitive{type: :int}}
Link to this function

encodable?(schema, data)

View Source

Specs

encodable?(AvroEx.Schema.t(), any()) :: boolean()

Checks to see if the given data is encodable using the given schema. Helpful for unit testing.

iex> AvroEx.encodable?(%Schema{schema: %Primitive{type: :string}}, "wut")
true

iex> AvroEx.encodable?(%Schema{schema: %Primitive{type: :string}}, 12345)
false

Specs

encode(AvroEx.Schema.t(), term()) ::
  {:ok, encoded_avro()} | {:error, AvroEx.EncodeError.t() | Exception.t()}

Given AvroEx.Schema.t/0 and term(), takes the data and encodes it according to the schema.

examples

Examples

iex> schema = AvroEx.decode_schema!("int")
iex> AvroEx.encode(schema, 1234)
{:ok, <<164, 19>>}

Specs

encode!(AvroEx.Schema.t(), term()) :: encoded_avro()

Same as encode/2, but returns the encoded value directly.

Raises AvroEx.EncodeError.t/0 on error.

examples

Examples

iex> schema = AvroEx.decode_schema!("boolean")
iex> AvroEx.encode!(schema, true)
<<1>>
Link to this function

encode_schema(schema, opts \\ [])

View Source

Specs

encode_schema(AvroEx.Schema.t(), Keyword.t()) :: String.t()

Encodes the given schema to JSON

options

Options

examples

Examples

iex> schema = AvroEx.decode_schema!(%{"type" => "int", "logicalType" => "date"})
iex> AvroEx.encode_schema(schema)
~S({"type":"int","logicalType":"date"})

iex> schema = AvroEx.decode_schema!(%{"type" => "int", "logicalType" => "date"})
iex> AvroEx.encode_schema(schema, canonical: true)
~S("int")
Link to this function

named_type(name, context)

View Source
This function is deprecated. Use AvroEx.Schema.Context.lookup/2.

Specs

This function is deprecated. Use AvroEx.decode_schema/1 instead.

Specs

parse_schema(AvroEx.Schema.json_schema()) ::
  {:ok, AvroEx.Schema.t()} | {:error, AvroEx.Schema.DecodeError.t()}
This function is deprecated. Use AvroEx.decode_schema!/1 instead.

Specs