Skip to content

Commit b4d2a23

Browse files
committed
update accepted typecode range to match msgpack specification of [-128,127]
1 parent 7cc8266 commit b4d2a23

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/msgpack_packer.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ pack_map(M, Opt)->
374374
-spec pack_ext(any(), msgpack_ext_packer(), msgpack:options()) -> {ok, binary()} | {error, any()}.
375375
pack_ext(Any, Packer, Opt) ->
376376
case Packer(Any, Opt) of
377-
{ok, {Type, Data}} when 0 < Type andalso Type < 16#100 ->
377+
{ok, {Type, Data}} when -16#80 =< Type andalso Type =< 16#7F ->
378378
Bin = case byte_size(Data) of
379379
1 -> <<16#D4, Type:8, Data/binary>>;
380380
2 -> <<16#D5, Type:8, Data/binary>>;

src/msgpack_term.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
pack_ext/2, unpack_ext/3]).
2222
-behaviour(msgpack_ext).
2323

24-
-define(ERLANG_TERM, 131).
24+
-define(ERLANG_TERM, 127).
2525
-define(TERM_OPTION, [{enable_str,true},{ext,?MODULE},{allow_atom,none}]).
2626

2727
%% @doc experimental

test/msgpack_ext_example_tests.erl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,28 @@ behaviour_test() ->
7070
Opt = [{ext, ?MODULE}],
7171
Term = {native, {self(), make_ref(), foobar, fun() -> ok end}},
7272
{ok, Term} = msgpack:unpack(msgpack:pack(Term, Opt), Opt).
73+
74+
75+
ext_typecode_range_test() ->
76+
%% typecode range from msgpack spec. [-128,-1] is the "reserved"
77+
%% range, [0,127] is the "user-defined" range.
78+
TypecodeMin = -128,
79+
TypecodeMax = 127,
80+
Packer = fun ({thing, N}, _) ->
81+
{ok, {N, msgpack:pack(N)}}
82+
end,
83+
Unpacker = fun(_N, Bin, _) ->
84+
msgpack:unpack(Bin)
85+
end,
86+
Opt = [{ext,{Packer,Unpacker}}],
87+
%% it should be possible to use an uncontroversial ext type code:
88+
Enc = msgpack:pack({thing,1}, Opt),
89+
?assertMatch({ok, 1}, msgpack:unpack(Enc, Opt)),
90+
%% it should be possible to use ext typecodes covering the entire
91+
%% range specified in the msgpack specification:
92+
[begin
93+
Encoded = msgpack:pack({thing, N}, Opt),
94+
Result = msgpack:unpack(Encoded, Opt),
95+
?assertMatch({ok, N}, Result)
96+
end || N <- lists:seq(TypecodeMin,TypecodeMax)],
97+
ok.

0 commit comments

Comments
 (0)