Coder Social home page Coder Social logo

protobuf-elixir's Introduction

protobuf-elixir

Hex.pm Build Status

A pure Elixir implementation of Google Protobuf

Why this instead of exprotobuf(gpb)?

It has some must-have and other cool features like:

  1. A protoc plugin to generate Elixir code just like what other official libs do, which is powerful and reliable.
  2. Generate simple and explicit code with the power of Macro. (see test/support/test_msg.ex)
  3. Plugins support. Only grpc is supported now.
  4. Use structs for messages instead of Erlang records.
  5. Support Typespec in generated code.

Installation

The package can be installed by adding protobuf to your list of dependencies in mix.exs:

def deps do
  [
    {:protobuf, "~> 0.5.3"},
    # Only for files generated from Google's protos.
    # Can be ignored if you don't use Google's protos.
    {:google_protos, "~> 0.1"}
  ]
end

Features

  • Define messages with DSL
  • Decode basic messages
  • Skip unknown fields
  • Decode embedded messages
  • Decode packed and repeated fields
  • Encode messages
  • protoc plugin
  • map
  • Support default values
  • Validate values
  • Generate typespecs
  • oneof

Usage

Generate Elixir code

  1. Install protoc(cpp) here or brew install protobuf on MacOS.
  2. Install protoc plugin protoc-gen-elixir for Elixir . NOTE: You have to make sure protoc-gen-elixir(this name is important) is in your PATH.
$ mix escript.install hex protobuf
  1. Generate Elixir code using protoc
$ protoc --elixir_out=./lib helloword.proto
  1. Files helloworld.pb.ex will be generated, like:
defmodule Helloworld.HelloRequest do
  use Protobuf, syntax: :proto3

  @type t :: %__MODULE__{
    name: String.t
  }
  defstruct [:name]

  field :name, 1, type: :string
end

defmodule Helloworld.HelloReply do
  use Protobuf, syntax: :proto3

  @type t :: %__MODULE__{
    message: String.t
  }
  defstruct [:message]

  field :message, 1, type: :string
end

Encode and decode in your code

struct = Foo.new(a: 3.2, c: Foo.Bar.new())
encoded = Foo.encode(struct)
struct = Foo.decode(encoded)

Note:

  • You should use YourModule.new instead of using the struct directly because default values will be set for all fields.
  • Default values will be set by default in decode, which can be changed by :use_default option.
  • Validation is done in encode. An error will be raised if the struct is invalid(like type is not matched).

gRPC Support

If you write services in protobuf, you can generate gRPC code by passing plugins=grpc in --go_out:

$ protoc --elixir_out=plugins=grpc:./lib/ *.proto

Tips for protoc

  • Custom protoc-gen-elixir name or path using --plugin
$ protoc --elixir_out=./lib --plugin=./protoc-gen-elixir *.proto
  • Pass -I argument if you import other protobuf files
$ protoc -I protos --elixir_out=./lib protos/hello.proto

Sponsors

This project is being sponsored by Tubi. Thank you!

Acknowledgements

Many thanks to gpb and golang/protobuf as good examples of writing Protobuf decoder/encoder.

protobuf-elixir's People

Contributors

amatalai avatar ch4s3 avatar cjab avatar falood avatar princemaple avatar qinix avatar tony612 avatar zentetsukenz avatar

Watchers

 avatar  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.