Use map format by default in 18.0 or over

This commit is contained in:
Yuki Ito 2015-07-21 20:24:57 +09:00
parent e84e60dbff
commit beb231cb71
4 changed files with 60 additions and 19 deletions

View File

@ -55,17 +55,27 @@
{ext, {msgpack_ext_packer(),msgpack_ext_unpacker()} | module()} {ext, {msgpack_ext_packer(),msgpack_ext_unpacker()} | module()}
]. ].
-ifdef(default_map).
-define(DEFAULT_MAP_FORMAT, map).
-define(DEFAULT_MAP_UNPACKER_FUN, fun msgpack_unpacker:unpack_map/3).
-else.
-define(DEFAULT_MAP_FORMAT, jiffy).
-define(DEFAULT_MAP_UNPACKER_FUN, fun msgpack_unpacker:unpack_map_jiffy/3).
-endif.
-record(options_v1, { -record(options_v1, {
interface = jiffy :: jiffy | jsx, interface = ?DEFAULT_MAP_FORMAT :: jiffy | jsx,
map_unpack_fun = fun msgpack_unpacker:unpack_map_jiffy/3 :: map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
msgpack_map_unpacker(),
impl = erlang :: erlang | nif impl = erlang :: erlang | nif
}). }).
-record(options_v2, { -record(options_v2, {
interface = jiffy :: jiffy | jsx, interface = ?DEFAULT_MAP_FORMAT :: jiffy | jsx,
map_unpack_fun = fun msgpack_unpacker:unpack_map_jiffy/3 :: map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
msgpack_map_unpacker(),
impl = erlang :: erlang | nif, impl = erlang :: erlang | nif,
allow_atom = none :: none | pack, %% allows atom when packing allow_atom = none :: none | pack, %% allows atom when packing
enable_str = false :: boolean(), %% true for new spec enable_str = false :: boolean(), %% true for new spec
@ -80,10 +90,10 @@
-type msgpack_option() :: #options_v2{}. -type msgpack_option() :: #options_v2{}.
-else. -else.
-record(options_v3, { -record(options_v3, {
interface = jiffy :: format_type(), interface = ?DEFAULT_MAP_FORMAT :: format_type(),
map_unpack_fun = fun msgpack_unpacker:unpack_map_jiffy/3 :: map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
msgpack_map_unpacker(),
impl = erlang :: erlang | nif, impl = erlang :: erlang | nif,
allow_atom = none :: none | pack, %% allows atom when packing allow_atom = none :: none | pack, %% allows atom when packing
enable_str = false :: boolean(), %% true for new spec enable_str = false :: boolean(), %% true for new spec

View File

@ -10,7 +10,16 @@ case erlang:system_info(otp_release) of
CONFIG ++ [{erl_opts, HashDefine}] CONFIG ++ [{erl_opts, HashDefine}]
end; end;
%% "17", our future with map %% In 17, maps are experimental
"17" ->
CONFIG;
_ -> _ ->
CONFIG HashDefine = [{d,default_map}],
case lists:keysearch(erl_opts, 1, CONFIG) of
{value, {erl_opts, Opts}} ->
lists:keyreplace(erl_opts,1,CONFIG,{erl_opts,Opts++HashDefine});
false ->
CONFIG ++ [{erl_opts, HashDefine}]
end
end. end.

View File

@ -165,6 +165,24 @@ parse_options([{ext, {Packer,Unpacker}}|TL], Opt0) when
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-ifdef(default_map).
test_data()->
[true, false, nil,
0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF,
-1, -23, -512, -1230, -567898, -16#FFFFFFFFFF,
-16#80000001,
123.123, -234.4355, 1.0e-34, 1.0e64,
[23, 234, 0.23],
<<"hogehoge">>, <<"243546rf7g68h798j", 0, 23, 255>>,
<<"hoasfdafdas][">>,
[0,42, <<"sum">>, [1,2]], [1,42, nil, [3]],
-234, -40000, -16#10000000, -16#100000000,
42
].
-else.
test_data()-> test_data()->
[true, false, null, [true, false, null,
0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF, 0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF,
@ -179,6 +197,8 @@ test_data()->
42 42
]. ].
-endif.
enable_str_test() -> enable_str_test() ->
?assertEqual(<<167:8, (<<"saitama">>)/binary >>, ?assertEqual(<<167:8, (<<"saitama">>)/binary >>,
msgpack:pack(<<"saitama">>, [{enable_str, false}])), msgpack:pack(<<"saitama">>, [{enable_str, false}])),

View File

@ -23,6 +23,8 @@
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-include("msgpack.hrl").
-ifdef(DO_MSGPACK_CROSSLANG_TEST). -ifdef(DO_MSGPACK_CROSSLANG_TEST).
test_data() -> test_data() ->
@ -148,25 +150,25 @@ issue_27_test_() ->
[ [
%% null(jiffy) => nil(msgpack) => null(jsx) %% null(jiffy) => nil(msgpack) => null(jsx)
?_assertEqual({ok, null}, ?_assertEqual({ok, null},
msgpack:unpack(msgpack:pack(null), [{format,jsx}])), msgpack:unpack(msgpack:pack(null, [{format,jiffy}]), [{format,jsx}])),
%% null(jiffy) => nil(msgpack) => null(jiffy) %% null(jiffy) => nil(msgpack) => null(jiffy)
?_assertEqual({ok, null}, ?_assertEqual({ok, null},
msgpack:unpack(msgpack:pack(null, [{format,jiffy}]))), msgpack:unpack(msgpack:pack(null, [{format,jiffy}]), [{format,jiffy}])),
%% null(jsx) => nil(msgpack) => null(jiffy) %% null(jsx) => nil(msgpack) => null(jiffy)
?_assertEqual({ok, null}, ?_assertEqual({ok, null},
msgpack:unpack(msgpack:pack(null, [{format,jsx}]))), msgpack:unpack(msgpack:pack(null, [{format,jsx}]), [{format,jiffy}])),
%% nil(jiffy-atom) => <<nil>>(msgpack-binary) => <<"nil">> %% nil(jiffy-atom) => <<nil>>(msgpack-binary) => <<"nil">>
?_assertEqual({ok, <<"nil">>}, ?_assertEqual({ok, <<"nil">>},
msgpack:unpack(msgpack:pack(nil, [{allow_atom,pack}]))), msgpack:unpack(msgpack:pack(nil, [{allow_atom,pack}, {format,jiffy}]), [{format,jiffy}])),
%% nil(jsx-atom) => <<nil>>(msgpack-binary) => <<"nil">> %% nil(jsx-atom) => <<nil>>(msgpack-binary) => <<"nil">>
?_assertEqual({ok, <<"nil">>}, ?_assertEqual({ok, <<"nil">>},
msgpack:unpack(msgpack:pack(nil, msgpack:unpack(msgpack:pack(nil,
[{format,jsx},{allow_atom,pack}])))]. [{format,jsx},{allow_atom,pack}]), [{format,jiffy}]))].
string_test() -> string_test() ->
{ok, CWD} = file:get_cwd(), {ok, CWD} = file:get_cwd(),
@ -181,13 +183,13 @@ default_test_() ->
{"pack", {"pack",
fun() -> fun() ->
Map = {[{1,2}]}, Map = {[{1,2}]},
?assertEqual(pack(Map, [{format, jiffy}]), pack(Map)) ?assertEqual(pack(Map, [{format, ?DEFAULT_MAP_FORMAT}]), pack(Map))
end}, end},
{"unpack", {"unpack",
fun() -> fun() ->
Map = {[{1,2}]}, Map = {[{1,2}]},
Binary = pack(Map, [{format, jiffy}]), Binary = pack(Map, [{format, ?DEFAULT_MAP_FORMAT}]),
?assertEqual(unpack(Binary, [{format, jiffy}]), unpack(Binary)) ?assertEqual(unpack(Binary, [{format, ?DEFAULT_MAP_FORMAT}]), unpack(Binary))
end} end}
]. ].