Packages
protox
1.0.0
2.0.9
2.0.8
2.0.7
2.0.6
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-dev
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
retired
1.7.3
1.7.2
1.7.1
1.7.0
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.2
1.3.1
1.3.0
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0
0.25.0
0.24.0
0.23.1
0.23.0
0.22.0
0.21.0
0.20.0
0.19.1
0.19.0
0.18.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.2
0.15.1
0.15.0
0.14.0
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.1
0.9.0
0.8.0
0.7.1
0.7.0
A fast, easy to use and 100% conformant Elixir library for Google Protocol Buffers (aka protobuf)
Current section
Files
Jump to
Current section
Files
lib/protox.ex
defmodule Protox do
@moduledoc ~S'''
Use this module to generate the Elixir modules from a set of protobuf definitions:
defmodule Foo do
use Protox, files: [
"./defs/foo.proto",
"./defs/bar.proto",
"./defs/baz/fiz.proto",
]
end
It's also possible to directly give a schema:
defmodule Bar do
use Protox, schema: """
syntax = "proto3";
package fiz;
message Baz {
}
message Foo {
map<int32, Baz> b = 2;
}
"""
end
The generated modules respect the package declaration. For instance, in the above example,
both the `Fiz.Baz` and `Fiz.Foo` modules will be generated.
See https://github.com/ahamez/protox/blob/master/README.md for detailed instructions.
'''
defmacro __using__(args) do
{args, _} = Code.eval_quoted(args)
namespace =
case Keyword.get(args, :namespace) do
nil -> nil
n -> n
end
path =
case Keyword.get(args, :path) do
nil -> nil
p -> Path.expand(p)
end
files =
case Keyword.drop(args, [:namespace, :path]) do
schema: <<text::binary>> ->
filename = "#{__CALLER__.module}_#{:sha |> :crypto.hash(text) |> Base.encode16()}.proto"
filepath = [Mix.Project.build_path(), filename] |> Path.join() |> Path.expand()
File.write!(filepath, text)
[filepath]
files: files ->
Enum.map(files, &Path.expand/1)
end
{:ok, file_descriptor_set} = Protox.Protoc.run(files, path)
{enums, messages} = Protox.Parse.parse(file_descriptor_set, namespace)
quote do
unquote(make_external_resources(files))
unquote(Protox.Define.define(enums, messages))
end
end
def generate_code(files, include_path \\ nil) do
path =
case include_path do
nil -> nil
_ -> Path.expand(include_path)
end
{:ok, file_descriptor_set} =
files
|> Enum.map(&Path.expand/1)
|> Protox.Protoc.run(path)
{enums, messages} = Protox.Parse.parse(file_descriptor_set, nil)
code = quote do: unquote(Protox.Define.define(enums, messages))
code_str = Macro.to_string(code)
["#", " credo:disable-for-this-file\n", code_str]
end
defp make_external_resources(files) do
Enum.map(files, fn file -> quote(do: @external_resource(unquote(file))) end)
end
end