Skip to content

Commit c3aed39

Browse files
authored
Merge pull request #73 from rbkmoney/master
Explicit tagged list
2 parents 7e3334d + 9d56647 commit c3aed39

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

src/msgpack.erl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
-export_type([object/0, msgpack_map/0, options/0, ext_packer/0, ext_unpacker/0]).
4949
-type object() :: msgpack_term().
5050

51-
-type options() ::
51+
-type options() ::
5252
[{spec, new|old} |
5353
{allow_atom, none|pack} |
5454
{known_atoms, [atom()]} |
55-
{unpack_str, as_binary|as_list} |
55+
{unpack_str, as_binary|as_list|as_tagged_list} |
5656
{validate_string, boolean()} |
57-
{pack_str, from_binary|from_list|none} |
57+
{pack_str, from_binary|from_list|from_tagged_list|none} |
5858
{map_format, map|jiffy|jsx} |
5959
{ext, {msgpack:ext_packer(), msgpack:ext_unpacker()} | module()}].
6060

@@ -86,10 +86,10 @@ binary_to_term(Bin, Opt) ->
8686

8787
%% @doc Encode an erlang term into an msgpack binary.
8888
%% Returns {error, {badarg, term()}} if the input is illegal.
89-
-spec pack(msgpack:object()) -> binary() | {error, {badarg, term()}}.
89+
-spec pack(msgpack:object()) -> binary() | {error, _}.
9090
pack(Term) -> msgpack:pack(Term, []).
9191

92-
-spec pack(msgpack:object(), msgpack:options()) -> binary().
92+
-spec pack(msgpack:object(), msgpack:options()) -> binary() | {error, _}.
9393
pack(Term, Opts) ->
9494
Option = parse_options(Opts),
9595
try
@@ -158,14 +158,14 @@ parse_options([{allow_atom,Type}|T], Opt0) ->
158158
parse_options([{known_atoms, Atoms}|T], Opt0) when is_list(Atoms) ->
159159
parse_options(T, Opt0?OPTION{known_atoms=Atoms});
160160

161-
parse_options([{unpack_str, As}|T], Opt0) when As =:= as_binary orelse As =:= as_list ->
161+
parse_options([{unpack_str, As}|T], Opt0) when As =:= as_binary orelse As =:= as_list orelse As =:= as_tagged_list ->
162162
parse_options(T, Opt0?OPTION{unpack_str=As});
163163

164164
parse_options([{validate_string, Bool}|T], Opt) when is_boolean(Bool) ->
165165
parse_options(T, Opt?OPTION{validate_string=Bool});
166166

167167
parse_options([{pack_str, From}|T], Opt)
168-
when From =:= from_binary orelse From =:= from_list orelse From =:= none ->
168+
when From =:= from_binary orelse From =:= from_list orelse From =:= from_tagged_list orelse From =:= none ->
169169
parse_options(T, Opt?OPTION{pack_str=From});
170170

171171
parse_options([{map_format,Type}|T], Opt0)

src/msgpack.hrl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
%% Erlang representation of msgpack data.
2828
-type msgpack_term() :: [msgpack_term()] | msgpack_map() |
29-
integer() | float() | boolean() | binary().
29+
integer() | float() | boolean() | binary() | string() | {string, string()}.
3030

3131
-type format_type() :: jsx|jiffy|map.
3232

@@ -37,9 +37,9 @@
3737
spec = new :: new | old,
3838
allow_atom = pack :: none | pack, %% allows atom when packing
3939
known_atoms = [] :: [atom()],
40-
unpack_str = as_list :: as_binary | as_list,
40+
unpack_str = as_list :: as_binary | as_list | as_tagged_list,
4141
validate_string = false :: boolean(),
42-
pack_str = from_list :: from_binary | from_list | none,
42+
pack_str = from_list :: from_binary | from_list | from_tagged_list | none,
4343
map_format = ?DEFAULT_MAP_FORMAT :: format_type(),
4444
map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
4545
ext_packer = undefined :: msgpack:ext_packer() | undefined,

src/msgpack_packer.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ pack(Map, Opt = ?OPTION{map_format=jsx}) when Map =:= [{}]->
6161
pack([{_,_}|_] = Map, Opt = ?OPTION{map_format=jsx}) ->
6262
pack_map(Map, Opt);
6363

64+
pack({string, String}, ?OPTION{spec=new, pack_str=from_tagged_list}=Opt) ->
65+
case pack_string(String, Opt) of
66+
{error, _} -> throw({badarg, String});
67+
Bin when is_binary(Bin) -> Bin
68+
end;
6469
pack(List, ?OPTION{spec=new, pack_str=from_list}=Opt) when is_list(List) ->
6570
try
6671
case lists:all(fun is_integer/1, List) of

src/msgpack_unpacker.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ unpack_str_or_raw(V, ?OPTION{spec=new,
233233
{case UnpackStr of
234234
as_binary when ValidateString -> unpack_str(V), maybe_bin(V, Opt);
235235
as_binary -> maybe_bin(V, Opt);
236-
as_list -> unpack_str(V)
236+
as_list -> unpack_str(V);
237+
as_tagged_list -> {string, unpack_str(V)}
237238
end, Rest}.
238239

239240
maybe_bin(Bin, _) ->

test/msgpack_tests.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,17 @@ new_spec_pack_test_() ->
568568
?_assertMatch(<<16#DD, 0, 1, 0, 0, _:65536/binary>>, %% 0xDD, four bytes, N objects
569569
msgpack:pack(list_minus_one(65536), [{spec,new},{pack_str,PackStr}]))]
570570
|| PackStr <- [from_list, from_binary, none]]
571+
},
572+
{"pack_str from_tagged_list",
573+
[?_assertEqual(<<2#101:3, 3:5, 97,97,97>>,
574+
msgpack:pack({string, "aaa"}, [{spec,new},{pack_str,from_tagged_list}])),
575+
?_assertMatch(<<16#D9, 32, _:32/binary>>,
576+
msgpack:pack({string, list_a(32)}, [{spec,new},{pack_str,from_tagged_list}])),
577+
?_assertMatch(<<16#DA, 1, 0, _:256/binary>>,
578+
msgpack:pack({string, list_a(256)}, [{spec,new},{pack_str,from_tagged_list}])),
579+
?_assertMatch(<<16#DB, 0, 1, 0, 0, _:65536/binary>>,
580+
msgpack:pack({string, list_a(65536)}, [{spec,new},{pack_str,from_tagged_list}]))
581+
]
571582
}].
572583

573584
new_spec_unpack_test_() ->
@@ -603,7 +614,9 @@ new_spec_unpack_test_() ->
603614
?_assertEqual({ok, list_a(256)},
604615
msgpack:unpack(<<16#DA, 1,0, (binary_a(256))/binary>>, [{spec,new},{unpack_str,as_list}])),
605616
?_assertEqual({ok, list_a(65536)},
606-
msgpack:unpack(<<16#DB, 0,1,0,0, (binary_a(65536))/binary>>, [{spec,new},{unpack_str,as_list}]))
617+
msgpack:unpack(<<16#DB, 0,1,0,0, (binary_a(65536))/binary>>, [{spec,new},{unpack_str,as_list}])),
618+
?_assertEqual({ok, {string, "aaa"}},
619+
msgpack:unpack(<<2#101:3, 3:5, 97,97,97>>, [{spec,new},{unpack_str,as_tagged_list}]))
607620
]}].
608621

609622
unpack_str_validation_test_() ->

0 commit comments

Comments
 (0)