Permalink
Browse files

Module-based encoding: Supporting protobuf-style defaults.

(Only Erlang-style defaults - replacing 'undefined' with the default value - used to be supported.
Protobuf-style default - eliding optional fields when they have a certain value - is a different thing.)
  • Loading branch information...
1 parent e66c821 commit 4b087bdebfe4d05c974a8962deef85547357b1cf @esstrifork esstrifork committed Jun 30, 2011
Showing with 7 additions and 6 deletions.
  1. +4 −4 src/pokemon_pb.erl
  2. +3 −2 src/protobuffs_compile.erl
View
@@ -38,11 +38,11 @@ encode(pikachu, Record) ->
iolist_to_binary(iolist(pikachu, Record)).
iolist(pikachu, Record) ->
- [pack(1, required, with_default(Record#pikachu.abc, none), string, [])].
+ [pack(1, required, with_default(Record#pikachu.abc, none, required), string, [])].
-with_default(undefined, none) -> undefined;
-with_default(undefined, Default) -> Default;
-with_default(Val, _) -> Val.
+with_default(Value, Default, optional) when Value==Default -> undefined; % PB default.
+with_default(undefined, Default, required) when Default/=none -> Default; % Erlang default.
+with_default(Value, _, _) -> Value.
pack(_, optional, undefined, _, _) -> [];
@@ -177,7 +177,7 @@ tokenize_file(InFile, Acc) ->
{ok,Token,_EndLine} ->
tokenize_file(InFile,[Token | Acc]);
{error,token} ->
- error(scanning_error);
+ error(scanning_error);
{eof,_} ->
lists:reverse(Acc)
end.
@@ -280,7 +280,8 @@ filter_iolist_clause({MsgName, Fields}, {clause,L,_Args,Guards,_Content}) ->
[{record_field,L,
{var,L,'Record'},atomize(MsgName),
{atom,L,atomize(SName)}},
- erl_parse:abstract(Default)]},
+ erl_parse:abstract(Default),
+ {atom,L,Tag}]},
{atom,L,atomize(SType)},
{nil,L}]},
Acc}

0 comments on commit 4b087bd

Please sign in to comment.