Skip to content

Commit 76454e5

Browse files
san650ericmj
andauthored
Create maps recursively for "extra" metadata (#163)
Co-authored-by: Eric Meadows-Jönsson <[email protected]>
1 parent b42c56d commit 76454e5

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/hex_tarball.erl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ characters_to_list(Binary) ->
457457
normalize_metadata(Metadata1) ->
458458
Metadata2 = maybe_update_with(<<"requirements">>, fun normalize_requirements/1, Metadata1),
459459
Metadata3 = maybe_update_with(<<"links">>, fun try_into_map/1, Metadata2),
460-
Metadata4 = maybe_update_with(<<"extra">>, fun try_into_map/1, Metadata3),
460+
Metadata4 = maybe_update_with(<<"extra">>, fun try_into_nested_map/1, Metadata3),
461461
guess_build_tools(Metadata4).
462462

463463
%% @private
@@ -665,14 +665,29 @@ try_into_map(List) ->
665665

666666
%% @private
667667
try_into_map(Fun, Input) ->
668-
case
669-
is_list(Input) andalso
670-
lists:all(fun(E) -> is_tuple(E) andalso (tuple_size(E) == 2) end, Input)
671-
of
668+
case has_map_shape(Input) of
672669
true -> maps:from_list(lists:map(Fun, Input));
673670
false -> Input
674671
end.
675672

673+
%% @private
674+
try_into_nested_map(List) ->
675+
try_into_nested_map(fun(X) -> X end, List).
676+
677+
%% @private
678+
try_into_nested_map(Fun, Input) ->
679+
case has_map_shape(Input) of
680+
true -> maps:from_list(lists:map(fun({Key, Value}) ->
681+
Fun({Key, try_into_nested_map(Fun, Value)})
682+
end, Input));
683+
false -> Input
684+
end.
685+
686+
%% @private
687+
has_map_shape(Input) ->
688+
is_list(Input) andalso
689+
lists:all(fun(E) -> is_tuple(E) andalso (tuple_size(E) == 2) end, Input).
690+
676691
%% @private
677692
encode_base16(Binary) ->
678693
<<X:256/big-unsigned-integer>> = Binary,

test/hex_tarball_SUITE.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ memory_test(_Config) ->
5757
<<"name">> => <<"foo">>,
5858
<<"version">> => <<"1.0.0">>,
5959
<<"maintainers">> => [<<"José">>],
60-
<<"build_tool">> => <<"rebar3">>
60+
<<"build_tool">> => <<"rebar3">>,
61+
<<"extra">> => [{<<"foo">>, [{<<"bar">>, <<"baz">>}]}]
6162
},
6263
Contents = [{"src/foo.erl", <<"-module(foo).">>}],
6364
{ok, #{tarball := Tarball, inner_checksum := InnerChecksum, outer_checksum := OuterChecksum}} = hex_tarball:create(
6465
Metadata, Contents
6566
),
67+
Metadata1 = maps:put(<<"extra">>, #{<<"foo">> => #{<<"bar">> => <<"baz">>}}, Metadata),
6668
{ok, #{
6769
inner_checksum := InnerChecksum,
6870
outer_checksum := OuterChecksum,
6971
contents := Contents,
70-
metadata := Metadata
72+
metadata := Metadata1
7173
}} = hex_tarball:unpack(Tarball, memory),
7274
ok.
7375

0 commit comments

Comments
 (0)