mirror of
https://github.com/valitydev/msgpack-erlang.git
synced 2024-11-06 00:35:24 +00:00
Use map format by default in 18.0 or over
This commit is contained in:
parent
e84e60dbff
commit
beb231cb71
@ -55,17 +55,27 @@
|
||||
{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, {
|
||||
interface = jiffy :: jiffy | jsx,
|
||||
map_unpack_fun = fun msgpack_unpacker:unpack_map_jiffy/3 ::
|
||||
msgpack_map_unpacker(),
|
||||
interface = ?DEFAULT_MAP_FORMAT :: jiffy | jsx,
|
||||
map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
|
||||
impl = erlang :: erlang | nif
|
||||
}).
|
||||
|
||||
-record(options_v2, {
|
||||
interface = jiffy :: jiffy | jsx,
|
||||
map_unpack_fun = fun msgpack_unpacker:unpack_map_jiffy/3 ::
|
||||
msgpack_map_unpacker(),
|
||||
interface = ?DEFAULT_MAP_FORMAT :: jiffy | jsx,
|
||||
map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
|
||||
impl = erlang :: erlang | nif,
|
||||
allow_atom = none :: none | pack, %% allows atom when packing
|
||||
enable_str = false :: boolean(), %% true for new spec
|
||||
@ -80,10 +90,10 @@
|
||||
-type msgpack_option() :: #options_v2{}.
|
||||
|
||||
-else.
|
||||
|
||||
-record(options_v3, {
|
||||
interface = jiffy :: format_type(),
|
||||
map_unpack_fun = fun msgpack_unpacker:unpack_map_jiffy/3 ::
|
||||
msgpack_map_unpacker(),
|
||||
interface = ?DEFAULT_MAP_FORMAT :: format_type(),
|
||||
map_unpack_fun = ?DEFAULT_MAP_UNPACKER_FUN :: msgpack_map_unpacker(),
|
||||
impl = erlang :: erlang | nif,
|
||||
allow_atom = none :: none | pack, %% allows atom when packing
|
||||
enable_str = false :: boolean(), %% true for new spec
|
||||
|
@ -10,7 +10,16 @@ case erlang:system_info(otp_release) of
|
||||
CONFIG ++ [{erl_opts, HashDefine}]
|
||||
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.
|
||||
|
@ -165,6 +165,24 @@ parse_options([{ext, {Packer,Unpacker}}|TL], Opt0) when
|
||||
-ifdef(TEST).
|
||||
-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()->
|
||||
[true, false, null,
|
||||
0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF,
|
||||
@ -179,6 +197,8 @@ test_data()->
|
||||
42
|
||||
].
|
||||
|
||||
-endif.
|
||||
|
||||
enable_str_test() ->
|
||||
?assertEqual(<<167:8, (<<"saitama">>)/binary >>,
|
||||
msgpack:pack(<<"saitama">>, [{enable_str, false}])),
|
||||
|
@ -23,6 +23,8 @@
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-include("msgpack.hrl").
|
||||
|
||||
-ifdef(DO_MSGPACK_CROSSLANG_TEST).
|
||||
|
||||
test_data() ->
|
||||
@ -148,25 +150,25 @@ issue_27_test_() ->
|
||||
[
|
||||
%% null(jiffy) => nil(msgpack) => null(jsx)
|
||||
?_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)
|
||||
?_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)
|
||||
?_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">>
|
||||
?_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">>
|
||||
?_assertEqual({ok, <<"nil">>},
|
||||
msgpack:unpack(msgpack:pack(nil,
|
||||
[{format,jsx},{allow_atom,pack}])))].
|
||||
[{format,jsx},{allow_atom,pack}]), [{format,jiffy}]))].
|
||||
|
||||
string_test() ->
|
||||
{ok, CWD} = file:get_cwd(),
|
||||
@ -181,13 +183,13 @@ default_test_() ->
|
||||
{"pack",
|
||||
fun() ->
|
||||
Map = {[{1,2}]},
|
||||
?assertEqual(pack(Map, [{format, jiffy}]), pack(Map))
|
||||
?assertEqual(pack(Map, [{format, ?DEFAULT_MAP_FORMAT}]), pack(Map))
|
||||
end},
|
||||
{"unpack",
|
||||
fun() ->
|
||||
Map = {[{1,2}]},
|
||||
Binary = pack(Map, [{format, jiffy}]),
|
||||
?assertEqual(unpack(Binary, [{format, jiffy}]), unpack(Binary))
|
||||
Binary = pack(Map, [{format, ?DEFAULT_MAP_FORMAT}]),
|
||||
?assertEqual(unpack(Binary, [{format, ?DEFAULT_MAP_FORMAT}]), unpack(Binary))
|
||||
end}
|
||||
].
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user