From 7af4aeab1c176531874e38cb801ae28e8fa954c7 Mon Sep 17 00:00:00 2001 From: Evan Vigil-McClanahan Date: Thu, 17 Nov 2016 10:38:41 -0800 Subject: [PATCH 1/2] adjust proto3 initializers --- src/gpb_compile.erl | 66 ++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/gpb_compile.erl b/src/gpb_compile.erl index 351934c8..a005525a 100644 --- a/src/gpb_compile.erl +++ b/src/gpb_compile.erl @@ -6089,7 +6089,8 @@ format_record_typespec(Msg, Fields, Defs, Opts) -> ?f("-type ~p() ::~n" " #{~s~n" " }.", - [Msg, outdent_first(format_hfields(7 + 1, Fields, Opts, Defs))]) + [Msg, outdent_first(format_hfields(Msg, 7 + 1, + Fields, Opts, Defs))]) end. format_export_types(Defs, Opts) -> @@ -6152,12 +6153,13 @@ format_msg_record(Msg, Fields, Opts, Defs) -> ?f("-define(~p, true).~n", [Def]), ?f("-record(~p,~n", [Msg]), ?f(" {"), - outdent_first(format_hfields(8+1, Fields, Opts, Defs)), + outdent_first(format_hfields(Msg, 8+1, Fields, Opts, Defs)), "\n", ?f(" }).~n"), ?f("-endif.~n")]. -format_hfields(Indent, Fields, Opts, Defs) -> +format_hfields(MsgName, Indent, Fields, Opts, Defs) -> + IsProto3 = gpb:is_msg_proto3(MsgName, Defs), TypeSpecs = get_type_specs_by_opts(Opts), MapsOrRecords = get_records_or_maps_by_opts(Opts), MappingAndUnset = get_mapping_and_unset_by_opts(Opts), @@ -6176,7 +6178,7 @@ format_hfields(Indent, Fields, Opts, Defs) -> string:join( lists:map( fun({I, #?gpb_field{name=Name, fnum=FNum, opts=FOpts, - occurrence=Occur}=Field}) -> + type=Type, occurrence=Occur}=Field}) -> TypeSpecifierSep = calc_field_type_sep(Field, Opts), LineLead = if MappingAndUnset == {maps, omitted}, Occur == optional, @@ -6185,21 +6187,32 @@ format_hfields(Indent, Fields, Opts, Defs) -> true -> "" end, - DefaultStr = case proplists:get_value(default, FOpts, '$no') of - '$no' -> - case {Occur, MapsOrRecords} of - {repeated, records} -> ?f(" = []"); - _ -> "" - end; - Default -> - case MapsOrRecords of - records -> - ?f(" = ~p", [Default]); - maps -> - "" - end - end, - TypeStr = ?f("~s", [type_to_typestr(Field, Defs, Opts)]), + DefaultStr = + case proplists:get_value(default, FOpts, '$no') of + '$no' -> + case {Occur, MapsOrRecords} of + {repeated, records} -> ?f(" = []"); + {_, records} -> + case IsProto3 of + true -> + Default = + proto3_type_default(Type, + Defs, + Opts), + ?f(" = ~p", [Default]); + false -> "" + end; + _ -> "" + end; + Default -> + case MapsOrRecords of + records -> + ?f(" = ~p", [Default]); + maps -> + "" + end + end, + TypeStr = ?f("~s", [type_to_typestr(MsgName, Field, Defs, Opts)]), CommaSep = if I < LastIndex -> ","; true -> "" %% last entry end, @@ -6229,7 +6242,7 @@ format_hfields(Indent, Fields, Opts, Defs) -> true -> "" end, - TypeStr = ?f("~s", [type_to_typestr(Field, Defs, Opts)]), + TypeStr = ?f("~s", [type_to_typestr(MsgName, Field, Defs, Opts)]), CommaSep = if I < LastIndex -> ","; true -> "" %% last entry end, @@ -6318,9 +6331,14 @@ mandatory_map_item_type_sep(Opts) -> can_specify_map_item_presence_in_typespecs(Opts) -> is_target_major_version_at_least(19, Opts). -type_to_typestr(#?gpb_field{type=Type, occurrence=Occurrence}, Defs, Opts) -> +type_to_typestr(MsgName, #?gpb_field{type=Type, occurrence=Occurrence}, + Defs, Opts) -> OrUndefined = case get_mapping_and_unset_by_opts(Opts) of - records -> " | undefined"; + records -> + case gpb:is_msg_proto3(MsgName, Defs) of + true -> ""; + _ -> " | undefined" + end; {maps, present_undefined} -> " | undefined"; {maps, omitted} -> "" end, @@ -6335,7 +6353,7 @@ type_to_typestr(#?gpb_field{type=Type, occurrence=Occurrence}, Defs, Opts) -> optional -> type_to_typestr_2(Type, Defs, Opts) ++ OrUndefined end; -type_to_typestr(#gpb_oneof{fields=OFields}, Defs, Opts) -> +type_to_typestr(_, #gpb_oneof{fields=OFields}, Defs, Opts) -> OrUndefined = case get_mapping_and_unset_by_opts(Opts) of records -> ["undefined"]; {maps, present_undefined} -> ["undefined"]; @@ -6385,7 +6403,7 @@ msg_to_typestr(M, Opts) -> %% when the strings_as_binaries option is requested the corresponding %% typespec should be spec'ed string_to_typestr(true) -> - "binary() | iolist()"; + "iodata()"; string_to_typestr(false) -> "iolist()". From fef099a84ca421c2ac1463e163f020dcfa46a831 Mon Sep 17 00:00:00 2001 From: Drew Varner Date: Wed, 25 Jan 2017 01:06:00 -0500 Subject: [PATCH 2/2] Add undefined to records typespec --- src/gpb_compile.erl | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/gpb_compile.erl b/src/gpb_compile.erl index a005525a..1975ffd1 100644 --- a/src/gpb_compile.erl +++ b/src/gpb_compile.erl @@ -6212,7 +6212,7 @@ format_hfields(MsgName, Indent, Fields, Opts, Defs) -> "" end end, - TypeStr = ?f("~s", [type_to_typestr(MsgName, Field, Defs, Opts)]), + TypeStr = ?f("~s", [type_to_typestr(Field, Defs, Opts)]), CommaSep = if I < LastIndex -> ","; true -> "" %% last entry end, @@ -6242,7 +6242,7 @@ format_hfields(MsgName, Indent, Fields, Opts, Defs) -> true -> "" end, - TypeStr = ?f("~s", [type_to_typestr(MsgName, Field, Defs, Opts)]), + TypeStr = ?f("~s", [type_to_typestr(Field, Defs, Opts)]), CommaSep = if I < LastIndex -> ","; true -> "" %% last entry end, @@ -6331,14 +6331,10 @@ mandatory_map_item_type_sep(Opts) -> can_specify_map_item_presence_in_typespecs(Opts) -> is_target_major_version_at_least(19, Opts). -type_to_typestr(MsgName, #?gpb_field{type=Type, occurrence=Occurrence}, +type_to_typestr(#?gpb_field{type=Type, occurrence=Occurrence}, Defs, Opts) -> OrUndefined = case get_mapping_and_unset_by_opts(Opts) of - records -> - case gpb:is_msg_proto3(MsgName, Defs) of - true -> ""; - _ -> " | undefined" - end; + records -> " | undefined"; {maps, present_undefined} -> " | undefined"; {maps, omitted} -> "" end, @@ -6353,7 +6349,7 @@ type_to_typestr(MsgName, #?gpb_field{type=Type, occurrence=Occurrence}, optional -> type_to_typestr_2(Type, Defs, Opts) ++ OrUndefined end; -type_to_typestr(_, #gpb_oneof{fields=OFields}, Defs, Opts) -> +type_to_typestr(#gpb_oneof{fields=OFields}, Defs, Opts) -> OrUndefined = case get_mapping_and_unset_by_opts(Opts) of records -> ["undefined"]; {maps, present_undefined} -> ["undefined"];