Last active
April 4, 2017 12:09
-
-
Save tiagopog/a82e9cae369c7c75478ecf53d7ea6368 to your computer and use it in GitHub Desktop.
Exprotobuf - Prototyping the use of :gpb.compile/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Protobuf do | |
# alias Protobuf.Parser | |
# alias Protobuf.Builder | |
# alias Protobuf.Config | |
# alias Protobuf.ConfigError | |
# alias Protobuf.Field | |
# alias Protobuf.OneOfField | |
# alias Protobuf.Utils | |
alias Mix.Compilers.Erlang | |
defmacro __using__(opts) do | |
path = Path.expand(opts[:path]) | |
if File.exists?(path) do | |
IO.puts "Compiling proto from proto: #{path}" | |
IO.inspect :gpb_compile.file('user.proto', [{:i, '/app/lib'}, :mapfields_as_maps]) | |
# erl_file = Erlang.to_erl_file("/app/user.erl") | |
# IO.inspect erl_file | |
compiled = :compile.file('/app/user.erl', [{:i, '_build/dev/lib/gpb/include'}]) | |
IO.inspect compiled | |
end | |
quote do | |
require Record | |
import Record, only: [defrecord: 2, extract: 2] | |
defrecord :user, extract(:User, from: "user.hrl") | |
def encode(data), do: :user.encode_msg(data) | |
def decode(bin), do: :user.decode_msg(bin, :User) | |
end | |
end | |
end | |
# Teste: | |
User.encode({:User, "abc", %{"def" => "x123", "ghi" => "y456"}}) |> User.decode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment