Giter Site home page Giter Site logo

erlang_protobuffs's Introduction

README

Build Status

This module is a composite of other open source modules and original code to make interfacing with the Protocol Buffers protocol easy.

Encode / Decode

Encoding is simple.

1> protobuffs:encode(1, 1, uint32).
<<8,1>>
2> erlang:iolist_to_binary([
    protobuffs:encode(1, <<"Nick">>, string),
    protobuffs:encode(2, 25, uint32)
]).
<<10,4,78,105,99,107,16,25>>

Decoding is simple too.

1> protobuffs:decode(<<8, 1>>, uint32).
{{1, 1}, <<>>}
2> protobuffs:decode(<<10,4,78,105,99,107,16,25>>, bytes).
{{1, <<"Nick">>}, <<16,25>>}
3> protobuffs:decode(<<16,25>>, bytes).
{{2, 25}, <<>>}

Using .proto Files

The main objective of this module is to allow developers to use .proto files easily. This module provides very basic functionality to do so.

Consider the t/simple.proto file.

message Person {
	required string name = 1;
	required string address = 2;
	required string phone_number = 3;
	required int32 age = 4;
}

From that file we can create an Erlang module that can encode and decode the Person message into records.

1> protobuffs_compile:scan_file("simple.proto").
ok
2> simple_pb:decode_person(<<10,4,78,105,99,107,18,13,77,111,...>>).
{person,<<"Nick">>,<<"Mountain View">>, <<"+1 (000) 555-1234">>,25}
3> simple_pb:encode_person({person, <<"Nick">>, <<"Mountain View">>,
    <<"+1 (000) 555-1234">>,25}).
<<10,4,78,105,99,107,18,13,77,111,117,110,116,97,105,110,32,86,105,...>>

How cool is that? From .proto files, we create modules that export encode and decode functions for the messages defined.

no_debug_info

The protobuffs_compile module relies on the pokemon_pb module being compiled with debug info. This is because pokemon_pb serves as a template for generated _pb modules. Running protobuffs_compile:scan_file/1 reads the erlang forms from the pokemon_pb.beam file and expands and alters those forms to create the generated module.

Building with rebar

To compile %>./rebar compile

To run all tests %>./rebar eunit %>./rebar ct

Se rebar doc for more information.

CREDITS

Some of the protobuffs.erl module came from code written by Brian Buchanan.

Some of the protobuffs_compile.erl module came from code written by Tim Fletcher.

The rest of it and it's test suite was written by Nick Gerakines. Major contributions have been made by Jacob Vorreuter.

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.