A rebar plugin generates protocolbuf description file to an erlang file.
Add the plugin to your rebar config:
{plugins, [ { rebar3_pb_msgdesc, {git, "https://github.com/xlkness/rebar3_pb_msgdesc", {branch, "master"}}} ]}.
Configuration in rebar.config
{msgdesc_opt, [ {desc_file, "./test.csv"}, {out_put_file, "test_pb_desc.erl"} ]}.
{provider_hooks, [ {pre, [ {compile, {msgdesc, compile}} ]} ]}.
Add some protocolbuf descriptions content to a file, format type_id,type_name,module
, i.e:
10000,Request,test_proto 10001,Response,test_proto
Then just call your plugin directly in an existing application:
rebar3 msgdesc compile
And you can find an out_file_name
file in the out_dir
, the content are as follows:
msg_type(10000) -> Request; msg_type(10001) -> Response; msg_type(Other) -> {error, msgdesc, msg_type, Other}. msg_code('Request') -> 10000; msg_code('Response') -> 10001; msg_code(Other) -> {error, msgdesc, msg_code, Other). decode_for(10000) -> test_proto; decode_for(10001) -> test_proto; decode_for(Other) -> {error, msgdesc, decode_for, Other).
A simple plugin, but i use lexer and yecc syntax tool to accomplish. Just for studying.
File rebar3_pb_msgdesc_lexer.xrl
and rebar3_pb_msgdesc_parser.yrl
are the most important files.