Git Product home page Git Product logo

eflatbuffers's Introduction

Eflatbuffers

This is a flatbuffers implementation in Elixir.

In contrast to existing implementations there is no need to compile code from a schema. Instead, data and schemas are processed dynamically at runtime, offering greater flexibility.

Using Eflatbuffers

Schema file:

table Root {
  foreground:Color;
  background:Color;
}

table Color {
  red:   ubyte;
  green: ubyte;
  blue:  ubyte;
}
root_type Root;

Parsing the schema:

iex(1)> schema = File.read!(path_to_schema) |> Eflatbuffers.Schema.parse!
{%{Color: {:table,
    %{fields: [red: {:ubyte, %{default: 0}}, green: {:ubyte, %{default: 0}},
       blue: {:ubyte, %{default: 0}}],
      indices: %{blue: {2, {:ubyte, %{default: 0}}},
        green: {1, {:ubyte, %{default: 0}}},
        red: {0, {:ubyte, %{default: 0}}}}}},
   Root: {:table,
    %{fields: [foreground: {:table, %{name: :Color}},
       background: {:table, %{name: :Color}}],
      indices: %{background: {1, {:table, %{name: :Color}}},
        foreground: {0, {:table, %{name: :Color}}}}}}}, %{root_type: :Root}}

Serializing data:

iex(2)> color_scheme = %{foreground: %{red: 128, green: 20, blue: 255}, background: %{red: 0, green: 100, blue: 128}}
iex(3)> color_scheme_fb = Eflatbuffers.write!(color_scheme, schema)
<<16, 0, 0, 0, 0, 0, 0, 0, 8, 0, 12, 0, 4, 0, 8, 0, 8, 0, 0, 0, 18, 0, 0, 0, 31,
  0, 0, 0, 10, 0, 7, 0, 4, 0, 5, 0, 6, 0, 10, 0, 0, 0, 128, 20, 255, 10, 0, 6,
  0, 0, ...>>

So we can read the whole thing which converts it back into a map:

iex(4)> Eflatbuffers.read!(color_scheme_fb, schema)
%{background: %{blue: 128, green: 100, red: 0},
  foreground: %{blue: 255, green: 20, red: 128}}

Or we can get a portion with means it seeks into the flatbuffer and only deserializes the part below the path:

iex(5)> Eflatbuffers.get!(color_scheme_fb, [:background], schema)
%{blue: 128, green: 100, red: 0}
iex(6)> Eflatbuffers.get!(color_scheme_fb, [:background, :green], schema)
100

Comparing Eflatbufers to flatc

features both in Eflatbufers and flatc

  • tables
  • scalars
  • strings
  • vectors
  • unions
  • enums
  • defaults
  • json to fb
  • fb to json
  • file identifier + validation
  • random access
  • validate file identifiers

features only in Eflatbuffers

  • vectors of enums

features only in flatc

  • shared strings
  • shared vtables
  • includes
  • alignment
  • additional attributes
  • structs

deviation of Eflatbuffers from flatc

  • default values are written to json

eflatbuffers's People

Contributors

eproxus avatar reimerei avatar

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.