Permalink
Browse files

Merge

Merge branch 'protobuf_defaults_support'

Conflicts:
	test/protobuffs_tests.erl
  • Loading branch information...
esstrifork committed Jul 6, 2011
2 parents 4bfdd7a + fd19193 commit 132d91a211d023f2c563b5704ead8a1a420e523b
Showing with 27 additions and 14 deletions.
  1. +21 −14 src/protobuffs_compile.erl
  2. +6 −0 test/protobuffs_tests.erl
View
@@ -210,8 +210,14 @@ filter_forms(Msgs, Enums, [{attribute,L,export,[{encode_pikachu,1},{decode_pikac
filter_forms(Msgs, Enums, [{attribute,L,record,{pikachu,_}}|Tail], Basename, Acc) ->
Records = [begin
- OutFields = [string:to_lower(A) || {_, _, _, A, _} <- lists:keysort(1, Fields)],
- Frm_Fields = [{record_field,L,{atom,L,list_to_atom(OutField)}}|| OutField <- OutFields],
+ OutFields = [{string:to_lower(A), default_for(K,D)} || {_, K, _, A, D} <- lists:keysort(1, Fields)],
+ Frm_Fields = [begin
+ RF = {record_field,L,{atom,L,list_to_atom(OutFieldName)}},
+ if Default == none -> RF;
+ true -> erlang:append_element(RF, erl_parse:abstract(Default))
+ end
+ end
+ || {OutFieldName,Default} <- OutFields],
{attribute, L, record, {atomize(Name), Frm_Fields}}
end || {Name, Fields} <- Msgs],
filter_forms(Msgs, Enums, Tail, Basename, Records ++ Acc);
@@ -502,20 +508,21 @@ write_header_include_file(Basename, Messages) ->
%% @hidden
generate_field_definitions(Fields) ->
- generate_field_definitions(Fields, []).
+ [lists:flatten(generate_field_definition(Field)) || Field <- Fields].
%% @hidden
-generate_field_definitions([], Acc) ->
- lists:reverse(Acc);
-generate_field_definitions([{Name, required, _} | Tail], Acc) ->
- Head = lists:flatten(io_lib:format("~s = erlang:error({required, ~s})", [Name, Name])),
- generate_field_definitions(Tail, [Head | Acc]);
-generate_field_definitions([{Name, _, none} | Tail], Acc) ->
- Head = lists:flatten(io_lib:format("~s", [Name])),
- generate_field_definitions(Tail, [Head | Acc]);
-generate_field_definitions([{Name, optional, Default} | Tail], Acc) ->
- Head = lists:flatten(io_lib:format("~s = ~p", [Name, Default])),
- generate_field_definitions(Tail, [Head | Acc]).
+generate_field_definition({Name, required, _}) ->
+ io_lib:format("~s = erlang:error({required, ~s})", [Name, Name]);
+generate_field_definition({Name, Kind, Default}) ->
+ case default_for(Kind, Default) of
+ none -> io_lib:format("~s", [Name]);
+ DefaultValue -> io_lib:format("~s = ~p", [Name, DefaultValue])
+ end.
+
+default_for(repeated, none) -> [];
+default_for(repeated_packed, none) -> [];
+default_for(optional, Default) -> Default;
+default_for(_,_) -> none.
%% @hidden
atomize(String) ->
@@ -262,6 +262,12 @@ should_encode_to_empty_test_() ->
[?_assertEqual(<<>>, hasdefault_pb:encode_optionalwithdefault(R))
|| R <- [EncodeToEmpty1 | EncodeToEmpties]].
+absent_list_should_decode_to_nil_test_() ->
+ DataDir = "../test/erlang_protobuffs_SUITE_data",
+ Path = filename:absname(filename:join([DataDir,"addressbook.proto"])),
+ ok = protobuffs_compile:scan_file(Path),
+
+ [?_assertEqual({addressbook, []}, addressbook_pb:decode(addressbook, <<>>))].
%%--------------------------------------------------------------------
%% Help functions

0 comments on commit 132d91a

Please sign in to comment.