Git Product home page Git Product logo

exprotoc's Introduction

Exprotoc

Elixir protocol buffers compiler

Mix project options

To use exprotoc, first include exprotoc in your mixfile as a dependency. Then, in the project property of your mix project, add :exprotoc to the list of compilers (e.g. compilers: [:exprotoc, :elixir, :app).

To configure the :exprotoc compiler prepass, there are three exposed options: proto_out, proto_files, and proto_path.

proto_out

Binary string that represents the directory generated code should be output to. Defaults to the lib folder.

proto_files

This should be a list of all the proto files you wish to turn into elixir code.

proto_path

List of directories in the order exprotoc should visit to look for proto files and imports.

Example mix.exs file

defmodule Example.Mixfile do
  use Mix.Project

  def project do
    [ app: :example,
      version: "0.0.1",
      elixir: "~> 0.12.5",
      compilers: [:exprotoc, :elixir, :app],
      proto_files: ["example.proto"],
      proto_path: ["priv"],
      deps: deps ]
  end

  defp deps do
    [{ :exprotoc, github: "jeremyong/exprotoc" }]
  end
end

This will compile the Example application with the generated code for the proto file example.proto. Because no proto_out option is specified, the generated code will be output to the lib folder.

Usage

Just access your message modules as you would a standard Elixir dict.

For example, with the following proto message:

package Example;
message Foo {
  enum Bar {
    Zap = 150;
  }
  required Foo a = 1;
  required uint32 b = 2;
}

exprotoc will create a module Example.Foo and submodule Example.Foo.Bar.

You can create a new message simply with Example.Foo.new or Example.Foo.new a: Example.Foo.Bar.zap.

You can access your message with the access protocol. For example:

message = Example.Foo.new a: Example.Foo.Bar.zap
IO.inspect message[:a] # Will output { Example.Foo.Bar, :zap }
IO.inspect message[:b] # Will output nil
message = Example.Foo.put message, :b, 150
IO.inspect message[:b] # Will output 150

All message modules export the encode and decode functions to turn a message into an iolist or turn binary into a message.

Default values work as expected.

Repeated fields are represented as lists.

exprotoc's People

Contributors

eliaw avatar jeremyong avatar pma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

exprotoc's Issues

Parser isn't working properly

I'm writing a new RethinkDB client driver, and am running into an issue with the parser:

** (MatchError) no match of right hand side value: {:error, {10, :proto_grammar, ['syntax error before: ', 'x3f61ba36']}}
    lib/exprotoc/parser.ex:36: Exprotoc.Parser.parse/1
    lib/exprotoc.ex:5: Exprotoc.compile/3
    (elixir) lib/enum.ex:534: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:534: Enum.each/2
    (mix) lib/mix/tasks/compile.ex:68: anonymous fn/2 in Mix.Tasks.Compile.run/1
    (elixir) lib/enum.ex:962: Enum."-map/2-lc$^0/1-0-"/2
    (mix) lib/mix/tasks/compile.ex:67: Mix.Tasks.Compile.run/1
    (mix) lib/mix/cli.ex:64: Mix.CLI.run_task/2

The .proto file I'm attempting to parse is RethinkDB's ql2.proto, which can be found here. I also maintain a fork of elixir-protobuf, and haven't had an issue with parsing this file. I'm looking to deprecate elixir-protobuf and use exprotoc, otherwise I'd just use that. If I get some time tonight or tomorrow, I'll take a look and see if I can find the source of the problem, but I'm sure you'll know where to look much faster than I will.

Support message with no fields

The grammar won't accept a message with no fields. Example (see Derived2):

message Base {
  enum MsgType {
    Derived1 = 1;
    Derived2 = 2;
  }

  message Derived1 {
    required uint32 extra1 = 1;
  }

  message Derived2 {
  }

  required MsgType type = 1;
  optional Derived1 derived1 = 2;
  optional Derived2 derived2 = 3;
}

Tried with other libs including gpb (erlang), protoc (python), protobuf-j2me (java) and they manage to parse it.

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.