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/storage/file.ex
defmodule Avrora.Storage.File do
@moduledoc """
Reads and parse schema from the disk with conventional name resoltion.
For instance schema name `<namespace>.<namespace>.<name>` will be converted to
`namespace/namespace/name.avsc` path.
"""
require Logger
alias Avrora.{Name, Schema}
@behaviour Avrora.Storage
@extension ".avsc"
@doc """
Reads the schema from the disk by it's full name.
Given name should contain a namespace and the schemas should be placed by a
convention – namespace segment is a folder name, hence a schema with a
full name: `io.confluent.Payment` should be placed like this
.
├── lib/
├── priv/
│ ├── ...
│ └── schemas/
│ └── io/
│ └── confluent/
│ └── Payment.avsc
└── ...
## Examples
iex> {:ok, avro} = Avrora.Storage.File.get("io.confluent.examples.Payment")
iex> avro.schema.schema.qualified_names
["io.confluent.examples.Payment"]
"""
def get(key) when is_binary(key) do
with {:ok, schema_name} <- Name.parse(key),
filepath <- Path.join(schemas_path(), name_to_filename(schema_name.name)),
{:ok, body} <- File.read(filepath) do
unless is_nil(schema_name.version) do
Logger.warn(
"file reading schema with version is not allowed, `#{schema_name.name}` used instead"
)
end
Schema.parse(body)
end
end
@doc false
def get(key) when is_integer(key), do: {:error, :unsupported}
@doc false
def put(_key, _value), do: {:error, :unsupported}
defp name_to_filename(name), do: String.replace(name, ".", "/") <> @extension
defp schemas_path, do: Application.get_env(:avrora, :schemas_path)
end