Git Product home page Git Product logo

avro_ex's Introduction

AvroEx

A pure-elixir avro encoding/decoding library.

Documentation

The docs can be found on hex.pm

Installation

def deps do
  [{:avro_ex, "~> 0.1.0-beta.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

avro_ex's People

Contributors

cjpoll avatar

Watchers

 avatar

Forkers

rentpath

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.