diff --git a/src/gpb_compile.erl b/src/gpb_compile.erl index 351934c8..1975ffd1 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,20 +6187,31 @@ 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, + 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(Field, Defs, Opts)]), CommaSep = if I < LastIndex -> ","; true -> "" %% last entry @@ -6318,7 +6331,8 @@ 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(#?gpb_field{type=Type, occurrence=Occurrence}, + Defs, Opts) -> OrUndefined = case get_mapping_and_unset_by_opts(Opts) of records -> " | undefined"; {maps, present_undefined} -> " | undefined"; @@ -6385,7 +6399,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()".