Packages
avrora
0.29.0
0.30.2
0.30.1
0.30.0
0.29.2
0.29.1
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.2
0.24.1
0.24.0
0.23.0
0.22.0
0.21.1
0.21.0
0.20.0
0.19.0
0.18.1
0.18.0
0.17.0
0.16.0
0.15.0
0.14.1
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.4.0
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.1-beta
0.1.0-beta
An Elixir library for working with Avro messages conveniently. It supports local schema files and Confluent® schema registry.
Current section
Files
Jump to
Current section
Files
lib/avrora/codec.ex
defmodule Avrora.Codec do
@moduledoc """
A behaviour for encoding/decoding Avro messages.
"""
@doc """
Check either a given binary Avro message is decodable with a specific codec or not.
NOTE: It's not guaranteed that a decodable binary message will be successfuly decoded.
## Examples
...> Avrora.Codec.Plain.is_decodable(<<1, 2, 3>>)
true
...> Avrora.Codec.SchemaRegistry.is_decodable(<<1, 2, 3>>)
false
"""
@callback is_decodable(payload :: binary()) :: boolean
@doc """
Extract schema from the binary Avro message.
## Examples
...> payload = <<0, 0, 0, 0, 42, 72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48,
48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
...> {:ok, schema} = Avrora.Codec.SchemaRegistry.extract_schema(payload)
...> schema.id
42
...> schema.full_name
"io.acme.Payment"
"""
@callback extract_schema(payload :: binary()) :: {:ok, result :: Avrora.Schema.t()} | {:error, reason :: term()}
@doc """
Decode a binary Avro message into the Elixir data.
## Examples
...> payload = <<0, 0, 0, 0, 8, 72, 48, 48, 48, 48, 48, 48, 48, 48, 45,
48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
...> Avrora.Codec.SchemaRegistry.decode(payload)
{:ok, %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}}
"""
@callback decode(payloadd :: binary()) :: {:ok, result :: map() | list(map())} | {:error, reason :: term()}
@doc """
Decode a binary Avro message into the Elixir data with given schema.
## Examples
...> payload = <<72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48, 48, 48, 48,
45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
...> {:ok, schema} = Avrora.Resolver.resolve("io.acme.Payment")
...> Avrora.Codec.Plain.decode(payload, schema: schema)
{:ok, %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}}
"""
@callback decode(payload :: binary(), options :: keyword(Avrora.Schema.t())) ::
{:ok, result :: map() | list(map())} | {:error, reason :: term()}
@doc """
Encode the Elixir data into a binary Avro message.
## Examples
...> payload = %{"id" => "00000000-0000-0000-0000-000000000000", "amount" => 15.99}
...> {:ok, schema} = Avrora.Resolver.resolve("io.acme.Payment")
...> Avrora.Codec.Plain.encode(payload, schema: schema)
<<72, 48, 48, 48, 48, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48, 48, 45, 48, 48, 48,
48, 45, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 123, 20, 174, 71, 225, 250, 47, 64>>
"""
@callback encode(payload :: binary() | map(), options :: keyword(Avrora.Schema.t())) ::
{:ok, result :: binary()} | {:error, reason :: term()}
end