Skip to content

Commit ba4803e

Browse files
committed
R16 or earlier with compile option
1 parent 3a20095 commit ba4803e

6 files changed

Lines changed: 100 additions & 18 deletions

File tree

include/msgpack.hrl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121

2222
-type msgpack_map_jiffy() :: {[{msgpack_term(), msgpack_term()}]}.
2323

24-
-type msgpack_map() :: msgpack_map_jsx() | msgpack_map_jiffy()
25-
| map().
24+
-ifdef(without_map).
25+
-type msgpack_map() :: msgpack_map_jsx() | msgpack_map_jiffy().
26+
-else.
27+
-type msgpack_map() :: msgpack_map_jsx() | msgpack_map_jiffy() | map().
28+
-endif.
2629

2730
-type msgpack_map_unpacker() ::
2831
fun((binary(), non_neg_integer(), msgpack_map(), msgpack_option()) ->
@@ -71,6 +74,12 @@
7174
original_list = [] :: msgpack_list_options()
7275
}).
7376

77+
-ifdef(without_map).
78+
79+
-define(OPTION, #options_v2).
80+
-type msgpack_option() :: #options_v2{}.
81+
82+
-else.
7483
-record(options_v3, {
7584
interface = map :: format_type(),
7685
map_unpack_fun = fun msgpack_unpacker:unpack_map/3 ::
@@ -85,3 +94,6 @@
8594

8695
-define(OPTION, #options_v3).
8796
-type msgpack_option() :: #options_v3{}.
97+
98+
-endif.
99+

rebar.config.script

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
case erlang:system_info(otp_release) of
2+
3+
%% Rxx, before R16
4+
[$R|_] ->
5+
HashDefine = [{d,without_map}],
6+
case lists:keysearch(erl_opts, 1, CONFIG) of
7+
{value, {erl_opts, Opts}} ->
8+
lists:keyreplace(erl_opts,1,CONFIG,{erl_opts,Opts++HashDefine});
9+
false ->
10+
CONFIG ++ [{erl_opts, HashDefine}]
11+
end;
12+
13+
%% "17", our future with map
14+
_ ->
15+
CONFIG
16+
end.

src/msgpack.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ enable_str_test() ->
183183
?assertEqual(<<167:8, (<<"saitama">>)/binary >>,
184184
msgpack:pack(<<"saitama">>, [{enable_str, false}])),
185185
?assertEqual(<<196,7,115,97,105,116,97,109,97>>,
186-
msgpack:pack(<<"saitama">>, [{enable_str, true}])).
186+
msgpack:pack(<<"saitama">>, [{enable_str, true}])).
187187

188188
basic_test()->
189189
Tests = test_data(),

src/msgpack_packer.erl

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,16 @@ pack(true, _) ->
4040
<< 16#C3:8 >>;
4141

4242
pack(Bin, Opt) when is_binary(Bin) ->
43-
case Opt of
44-
#options_v3{enable_str=true} = Opt -> pack_raw2(Bin);
45-
#options_v3{enable_str=false} = Opt -> pack_raw(Bin);
46-
#options_v2{enable_str=true} = Opt -> pack_raw2(Bin);
47-
#options_v2{enable_str=false} = Opt -> pack_raw(Bin);
48-
#options_v1{} = Opt -> pack_raw(Bin)
49-
end;
43+
handle_binary(Bin, Opt);
5044

5145
pack(Atom, #options_v2{allow_atom=pack} = Opt) when is_atom(Atom) ->
5246
pack(erlang:atom_to_binary(Atom, unicode), Opt);
5347

54-
%% map interface
55-
pack(Map, Opt = ?OPTION{interface=map}) when is_map(Map) ->
56-
pack_map(maps:to_list(Map), Opt);
48+
%% -ifndef(without_map).
49+
%% %% map interface
50+
%% pack(Map, Opt = ?OPTION{interface=map}) when is_map(Map) ->
51+
%% pack_map(maps:to_list(Map), Opt);
52+
%% -endif.
5753

5854
%% jiffy interface
5955
pack({Map}, Opt = ?OPTION{interface=jiffy}) when is_list(Map) ->
@@ -87,18 +83,62 @@ pack(List, ?OPTION{enable_str=true}=Opt) when is_list(List) ->
8783
pack(List, Opt) when is_list(List) ->
8884
pack_array(List, Opt);
8985

86+
pack(Other, Opt) ->
87+
handle_ext(Other, Opt).
88+
89+
-ifdef(without_map).
90+
91+
handle_binary(Bin, Opt) ->
92+
case Opt of
93+
#options_v2{enable_str=true} = Opt -> pack_raw2(Bin);
94+
#options_v2{enable_str=false} = Opt -> pack_raw(Bin);
95+
#options_v1{} = Opt -> pack_raw(Bin)
96+
end.
97+
9098
%% Packing ext type with user defined packer function
91-
pack(Any, _Opt = ?OPTION{ext_packer=Packer, original_list=Orig})
99+
handle_ext(Any, _Opt = ?OPTION{ext_packer=Packer,
100+
original_list=Orig,
101+
interface=Interface})
102+
when is_function(Packer) andalso Interface =/= map ->
103+
104+
case pack_ext(Any, Packer, Orig) of
105+
{ok, Binary} -> Binary;
106+
{error, E} -> throw({error, E})
107+
end;
108+
109+
handle_ext(Other, _) ->
110+
throw({badarg, Other}).
111+
112+
-else.
113+
114+
handle_binary(Bin, Opt) ->
115+
case Opt of
116+
#options_v3{enable_str=true} = Opt -> pack_raw2(Bin);
117+
#options_v3{enable_str=false} = Opt -> pack_raw(Bin);
118+
#options_v2{enable_str=true} = Opt -> pack_raw2(Bin);
119+
#options_v2{enable_str=false} = Opt -> pack_raw(Bin);
120+
#options_v1{} = Opt -> pack_raw(Bin)
121+
end.
122+
123+
%% %% map interface
124+
handle_ext(Map, Opt = ?OPTION{interface=map}) when is_map(Map) ->
125+
pack_map(maps:to_list(Map), Opt);
126+
127+
handle_ext(Any, _Opt = ?OPTION{ext_packer=Packer,
128+
original_list=Orig,
129+
interface=Interface})
92130
when is_function(Packer) ->
93131

94132
case pack_ext(Any, Packer, Orig) of
95133
{ok, Binary} -> Binary;
96134
{error, E} -> throw({error, E})
97135
end;
98136

99-
pack(Other, _) ->
137+
handle_ext(Other, _) ->
100138
throw({badarg, Other}).
101139

140+
-endif.
141+
102142
-spec pack_int(integer()) -> binary().
103143
%% negative fixnum
104144
pack_int(N) when N >= -32->

src/msgpack_unpacker.erl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
-include("msgpack.hrl").
2424
-include_lib("eunit/include/eunit.hrl").
2525

26-
-export([unpack_map/3, unpack_map_jiffy/3, unpack_map_jsx/3]).
26+
27+
-ifndef(without_map).
28+
-export([unpack_map/3]).
29+
-endif.
30+
31+
-export([unpack_map_jiffy/3, unpack_map_jsx/3]).
2732

2833
%% unpack them all
2934
-spec unpack_stream(Bin::binary(), msgpack_option()) -> {msgpack:object(), binary()} | no_return().
@@ -166,14 +171,23 @@ unpack_array(Bin, Len, Acc, Opt) ->
166171
{Term, Rest} = unpack_stream(Bin, Opt),
167172
unpack_array(Rest, Len-1, [Term|Acc], Opt).
168173

174+
-ifdef(without_map).
175+
map_unpacker(jiffy) ->
176+
fun ?MODULE:unpack_map_jiffy/3;
177+
map_unpacker(jsx) ->
178+
fun ?MODULE:unpack_map_jsx/3.
179+
-else.
169180
map_unpacker(map) ->
170181
fun ?MODULE:unpack_map/3;
171182
map_unpacker(jiffy) ->
172183
fun ?MODULE:unpack_map_jiffy/3;
173184
map_unpacker(jsx) ->
174185
fun ?MODULE:unpack_map_jsx/3.
186+
-endif.
187+
175188

176189

190+
-ifndef(without_map).
177191
-spec unpack_map(binary(), non_neg_integer(), msgpack_option()) ->
178192
{map(), binary()} | no_return().
179193
unpack_map(Bin, Len, Opt) ->
@@ -186,6 +200,7 @@ unpack_map(Bin, Len, Opt) ->
186200
%% {Key, Rest} = unpack_stream(Bin, Opt),
187201
%% {Value, Rest2} = unpack_stream(Rest, Opt),
188202
%% unpack_map(Rest2, Len-1, maps:put(Key, Value, Acc), Opt).
203+
-endif.
189204

190205
%% Users SHOULD NOT send too long list: this uses lists:reverse/1
191206
-spec unpack_map_jiffy(binary(), non_neg_integer(), msgpack_option()) ->

test/msgpack_test.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ issue_jsx_5_test() ->
118118
Bin0 = <<130,196,4,116,121,112,101,196,7,119,111,114,107,101,114,115,
119119
196,4,100,97,116,97,145,130,196,8,119,111,114,107,101,114,105,100,
120120
196,5,115,116,100,46,49,196,5,115,108,111,116,115,160>>,
121-
?debugFmt("~w", [binary_to_list(Bin0)]),
122-
?debugFmt("~w", [binary_to_list(Encoded)]),
121+
123122
?assertEqual(Bin0, Encoded),
124123

125124
{ok, Decoded} = msgpack:unpack(Bin0, [{format,jsx}, {enable_str,true}]),

0 commit comments

Comments
 (0)