Packages
avrora
0.1.0-beta
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/aurora/schema.ex
defmodule Avrora.Schema do
@moduledoc """
A wrapper struct around AvroEx.Schema and Confluent Schema Registry for more
convinient use.
"""
defstruct [:id, :version, :ex_schema, :raw_schema]
@type t :: %__MODULE__{
id: nil | integer(),
version: nil | integer(),
ex_schema: AvroEx.Schema.t(),
raw_schema: map()
}
@doc """
Parses a json payload and converts it to the schema with id, AvroEx representation
and Map representation.
## Examples
iex> json = ~s({"namespace":"io.confluent","type":"record","name":"Payment","fields":[{"name":"id","type":"string"},{"name":"amount","type":"double"}]})
iex> {:ok, avro} = Avrora.Schema.parse(json)
iex> avro.ex_schema.schema.qualified_names
["io.confluent.Payment"]
iex> Map.get(avro.raw_schema, "name")
"Payment"
"""
@spec parse(String.t()) :: {:ok, t()} | {:error, term()}
def parse(payload) when is_binary(payload) do
with {:ok, raw_schema} <- Jason.decode(payload), do: parse(raw_schema)
end
@doc """
Parses a Map payload and converts it to the schema with id, AvroEx representation
and Map representation.
## Examples
iex> payload = %{"namespace" => "io.confluent", "type" => "record", "name" => "Payment", "fields" => [%{"name" => "id", "type" => "string"}, %{"name" => "amount", "type" => "double"}]}
iex> {:ok, avro} = Avrora.Schema.parse(payload)
iex> avro.ex_schema.schema.qualified_names
["io.confluent.Payment"]
iex> Map.get(avro.raw_schema, "name")
"Payment"
"""
@spec parse(map()) :: {:ok, t()} | {:error, term()}
def parse(payload) when is_map(payload) do
with {:ok, schema} <- AvroEx.Schema.cast(payload),
{:ok, schema} <- AvroEx.Schema.namespace(schema),
{:ok, context} <- AvroEx.Schema.expand(schema, %AvroEx.Schema.Context{}) do
{
:ok,
%__MODULE__{
id: nil,
version: nil,
ex_schema: %AvroEx.Schema{schema: schema, context: context},
raw_schema: payload
}
}
end
end
end