Refactor #184 for less redundant code
Project: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/commit/f6e40f70 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/tree/f6e40f70 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/diff/f6e40f70 Branch: refs/heads/master Commit: f6e40f70094bdaa002f2b70bf989808dd948f169 Parents: 6384fa9 Author: Bob Ippolito <[email protected]> Authored: Mon Dec 19 10:24:37 2016 -0800 Committer: Bob Ippolito <[email protected]> Committed: Mon Dec 19 10:34:30 2016 -0800 ---------------------------------------------------------------------- rebar.config | 8 ++++---- src/mochijson2.erl | 54 ++++++++++++++----------------------------------- 2 files changed, 19 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/f6e40f70/rebar.config ---------------------------------------------------------------------- diff --git a/rebar.config b/rebar.config index 492832e..8fc1a14 100644 --- a/rebar.config +++ b/rebar.config @@ -1,9 +1,9 @@ % -*- mode: erlang -*- {erl_opts, [debug_info, - {platform_define, "R15", 'gen_tcp_r15b_workaround'}, - {platform_define, "(R14|R15|R16B-)", 'crypto_compatibility'}, - {platform_define, "(R14|R15|R16B|17)", 'rand_mod_unavailable'}, - {platform_define, "(17|18|19)", 'map_available'}]}. + {platform_define, "^R15", 'gen_tcp_r15b_workaround'}, + {platform_define, "^(R14|R15|R16B-)", 'crypto_compatibility'}, + {platform_define, "^(R14|R15|R16B|17)", 'rand_mod_unavailable'}, + {platform_define, "^(R14|R15|R16)", 'map_unavailable'}]}. {cover_enabled, true}. {eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}. {dialyzer_opts, [{warnings, [no_return, http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/f6e40f70/src/mochijson2.erl ---------------------------------------------------------------------- diff --git a/src/mochijson2.erl b/src/mochijson2.erl index 7c63777..e39c522 100644 --- a/src/mochijson2.erl +++ b/src/mochijson2.erl @@ -82,6 +82,12 @@ -define(IS_WHITESPACE(C), (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)). +-ifdef(map_unavailable). +-define(IS_MAP(_), false). +-else. +-define(IS_MAP(X), is_map(X)). +-endif. + %% @type json_string() = atom | binary() %% @type json_number() = integer() | float() %% @type json_array() = [json_term()] @@ -150,7 +156,6 @@ parse_decoder_options([{format, Format} | Rest], State) parse_decoder_options(Rest, State#decoder{object_hook=Format}). --ifdef(map_available). json_encode(true, _State) -> <<"true">>; json_encode(false, _State) -> @@ -177,7 +182,7 @@ json_encode(Array, State) when is_list(Array) -> json_encode_array(Array, State); json_encode({array, Array}, State) when is_list(Array) -> json_encode_array(Array, State); -json_encode(M, State) when is_map(M) -> +json_encode(M, State) when ?IS_MAP(M) -> json_encode_map(M, State); json_encode({json, IoList}, _State) -> IoList; @@ -185,40 +190,6 @@ json_encode(Bad, #encoder{handler=null}) -> exit({json_encode, {bad_term, Bad}}); json_encode(Bad, State=#encoder{handler=Handler}) -> json_encode(Handler(Bad), State). --else. -json_encode(true, _State) -> - <<"true">>; -json_encode(false, _State) -> - <<"false">>; -json_encode(null, _State) -> - <<"null">>; -json_encode(I, _State) when is_integer(I) -> - integer_to_list(I); -json_encode(F, _State) when is_float(F) -> - mochinum:digits(F); -json_encode(S, State) when is_binary(S); is_atom(S) -> - json_encode_string(S, State); -json_encode([{K, _}|_] = Props, State) when (K =/= struct andalso - K =/= array andalso - K =/= json) -> - json_encode_proplist(Props, State); -json_encode({struct, Props}, State) when is_list(Props) -> - json_encode_proplist(Props, State); -json_encode({Props}, State) when is_list(Props) -> - json_encode_proplist(Props, State); -json_encode({}, State) -> - json_encode_proplist([], State); -json_encode(Array, State) when is_list(Array) -> - json_encode_array(Array, State); -json_encode({array, Array}, State) when is_list(Array) -> - json_encode_array(Array, State); -json_encode({json, IoList}, _State) -> - IoList; -json_encode(Bad, #encoder{handler=null}) -> - exit({json_encode, {bad_term, Bad}}); -json_encode(Bad, State=#encoder{handler=Handler}) -> - json_encode(Handler(Bad), State). --endif. json_encode_array([], _State) -> <<"[]">>; @@ -240,7 +211,11 @@ json_encode_proplist(Props, State) -> [$, | Acc1] = lists:foldl(F, "{", Props), lists:reverse([$\} | Acc1]). --ifdef(map_available). +-ifdef(map_unavailable). +json_encode_map(Bad, _State) -> + %% IS_MAP definition guarantees that this branch is dead + exit({json_encode, {bad_term, Bad}}). +-else. json_encode_map(Map, _State) when map_size(Map) =:= 0 -> <<"{}">>; json_encode_map(Map, State) -> @@ -990,11 +965,12 @@ utf8_non_character_test_() -> [{"roundtrip escaped", ?_assertEqual(S, decode(encode(S)))}, {"roundtrip utf8", ?_assertEqual(S, decode((encoder([{utf8, true}]))(S)))}]. -% iolist_to_binary(mochijson2:encode(#{a => 1, b => #{ c => 2}})). --ifdef(map_available). +-ifndef(map_unavailable). + encode_map_test() -> M = <<"{\"a\":1,\"b\":{\"c\":2}}">>, ?assertEqual(M, iolist_to_binary(encode(#{a => 1, b => #{ c => 2}}))). + encode_empty_map_test() -> ?assertEqual(<<"{}">>, encode(#{})).
