Skip to content

An Avro Library that emphasizes testability and ease of use.

Notifications You must be signed in to change notification settings

euranova/avro_ex

This branch is 82 commits behind beam-community/avro_ex:master.

Folders and files

NameName
Last commit message
Last commit date
Mar 10, 2020
Sep 14, 2017
Sep 29, 2020
Sep 29, 2020
Mar 10, 2020
Nov 28, 2019
Oct 3, 2018
Sep 14, 2017
Nov 28, 2019
Mar 10, 2020
Mar 10, 2020

Repository files navigation

AvroEx

A pure-elixir avro encoding/decoding library.

Documentation

The docs can be found on hex.pm

Installation

def deps do
  [{:avro_ex, "~> 1.0"}]
end

Usage

Decoding

If you have a worker which receives a raw avro message and some kind of repository where you're storing your schemas, you might do something like this:

defmodule MyWorker do
  alias AvroEx.Schema

  def start_link() do
    WorkerLib.start_link(__MODULE__)
  end

  @schema_id "some_schema"

  def handle_message(message) do
    {:ok, decoded_message} =
      @schema_id
      |> SchemaRepository.fetch_schema
      |> AvroEx.parse_schema!
      |> AvroEx.decode(message)

    # And do things with the message
  end
end

Encoding

Let's say you have a LinkedList with the following schema:

{
  "type": "record",
  "name": "LinkedList",
  "fields": [
    {"name": "value", "type": "int"},
    {"name": "next", "type": ["null", "LinkedList"]}
  ]
}

If you wanted to encode it, you would do something like:

def my_function(schema) do
  list =
    %{
      "value" => 9001,
      "next" => %{
        "value" => 42,
        "next" => nil
      }
    }

  {:ok, encoded_avro} = AvroEx.encode(schema, list)
  # Do something with encoded avro
end

Testing

If you're unit testing your code and you want to ensure that your code builds a structure that is encodable using the given schema:

defmodule MyModule.Test do
  use ExUnit.Case

  setup do
    data = ...
    schema = ...
    {:ok, %{data: data, schema: schema}}
  end

  describe "my_function/1" do
    test "builds a structure that can be encoded with our avro schema", context do
      result = MyModule.my_function(context.data)

      assert AvroEx.encodable?(context.schema, result)
    end
  end
end

About

An Avro Library that emphasizes testability and ease of use.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 100.0%