This is an automated email from the ASF dual-hosted git repository. nickva pushed a commit to tag 2.0.0 in repository https://gitbox.apache.org/repos/asf/couchdb-jiffy.git
commit 465193c6cdaf9abb1608fe5d8c3ce5c0c6c05f18 Author: Gaspar Chilingarov <[email protected]> AuthorDate: Thu Jun 29 15:12:38 2017 +0300 Allow number encoding as keys Co-author: Nick Vatamaniuc <[email protected]> --- README.md | 6 +++++- c_src/encoder.c | 29 +++++++++++++++++++---------- test/jiffy_07_compound_tests.erl | 23 ++++++++++++++++++++++- test/jiffy_12_error_tests.erl | 9 +-------- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ed84d6e..5572a4b 100644 --- a/README.md +++ b/README.md @@ -106,10 +106,14 @@ Data Format [true, 1.0] -> [true, 1.0] -> [true, 1.0] {[]} -> {} -> {[]} {[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]} + {[{123, bar}]} -> {"123": "bar"} -> {[{<<"123">>, <<"bar">>}]} + {[{1.5, bar}]} -> {"1.5": "bar"} -> {[{<<"1.5">>, <<"bar">>}]} {[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]} #{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>} + #{123 => <<"bar">>} -> {"123": "bar"} -> #{<<"123">> => <<"bar">>} + #{1.5 => <<"bar">>} -> {"1.5": "bar"} -> #{<<"1.5">> => <<"bar">>} -N.B. The last entry in this table is only valid for VM's that support +N.B. The last three entries in this table are only valid for VM's that support the `maps` data type (i.e., 17.0 and newer) and client code must pass the `return_maps` option to `jiffy:decode/2`. diff --git a/c_src/encoder.c b/c_src/encoder.c index 731b78b..ca1689e 100644 --- a/c_src/encoder.c +++ b/c_src/encoder.c @@ -492,16 +492,6 @@ enc_string(Encoder* e, ERL_NIF_TERM val) return enc_quoted(e, bin.data, bin.size, 0); } -static inline int -enc_object_key(ErlNifEnv *env, Encoder* e, ERL_NIF_TERM val) -{ - if(enif_is_atom(env, val)) { - return enc_atom(e, val); - } - - return enc_string(e, val); -} - // From https://www.slideshare.net/andreialexandrescu1/three-optimization-tips-for-c-15708507 #define P01 10 @@ -620,6 +610,25 @@ enc_char(Encoder* e, char c) return 1; } +static inline int +enc_object_key(ErlNifEnv *env, Encoder* e, ERL_NIF_TERM val) +{ + ErlNifSInt64 ival; + double dval; + + if(enif_is_atom(env, val)) { + return enc_atom(e, val); + } + if(enif_get_int64(env, val, &ival)) { + return enc_char(e, '"') && enc_long(e, ival) && enc_char(e, '"'); + } + if(enif_get_double(env, val, &dval)) { + return enc_char(e, '"') && enc_double(e, dval) && enc_char(e, '"'); + } + + return enc_string(e, val); +} + static int enc_shift(Encoder* e) { int i; diff --git a/test/jiffy_07_compound_tests.erl b/test/jiffy_07_compound_tests.erl index 45fd383..39e4941 100644 --- a/test/jiffy_07_compound_tests.erl +++ b/test/jiffy_07_compound_tests.erl @@ -9,7 +9,8 @@ compound_success_test_() -> - [gen(ok, Case) || Case <- cases(ok)]. + [gen(ok, Case) || Case <- cases(ok)] ++ + [gen(number_key_encoding, Case) || Case <- cases(number_key_encoding)]. compound_failure_test_() -> @@ -24,6 +25,11 @@ gen(ok, {J1, E, J2}) -> {"Encode", ?_assertEqual(J2, enc(E))} ]}; +gen(number_key_encoding, {J, E}) -> + {msg("~s", [J]), [ + {"Encode", ?_assertEqual(J, enc(E))} + ]}; + gen(error, J) -> {msg("Error: ~s", [J]), [ ?_assertError(_, dec(J)) @@ -53,6 +59,21 @@ cases(ok) -> } ]; +cases(number_key_encoding) -> + [ + % Integer keys + {<<"{\"123\":\"foo\"}">>, {[{123, <<"foo">>}]}}, + {<<"{\"-1\":\"n\"}">>, {[{-1, <<"n">>}]}}, + {<<"{\"123\":\"a\",\"456\":\"b\"}">>, {[{123, <<"a">>}, {456, <<"b">>}]}}, + {<<"{\"123\":\"v\"}">>, #{123 => <<"v">>}}, + % These could change if we swtich away from Ryu + {<<"{\"1.5\":\"x\"}">>, {[{1.5, <<"x">>}]}}, + {<<"{\"-2.5\":\"x\"}">>, {[{-2.5, <<"x">>}]}}, + {<<"{\"0.0\":\"x\"}">>, {[{0.0, <<"x">>}]}}, + {<<"{\"1.5\":\"a\",\"2.0\":\"b\"}">>, {[{1.5, <<"a">>}, {2.0, <<"b">>}]}}, + {<<"{\"1.5\":\"v\"}">>, #{1.5 => <<"v">>}} + ]; + cases(error) -> [ <<"[{}">>, diff --git a/test/jiffy_12_error_tests.erl b/test/jiffy_12_error_tests.erl index 7a1ed9f..7c2c2b3 100644 --- a/test/jiffy_12_error_tests.erl +++ b/test/jiffy_12_error_tests.erl @@ -68,17 +68,10 @@ enc_invalid_object_member_arity_test_() -> enc_invalid_object_member_key_test_() -> Type = invalid_object_member_key, - E1 = {1, true}, {"invalid_object_member_key", [ {"Bad string", enc_error(Type, <<143>>, {[{<<143>>, true}]})}, - {"Basic", enc_error(Type, 1, {[{1, true}]})}, {"Basic", enc_error(Type, [1], {[{[1], true}]})}, - {"Basic", enc_error(Type, {[{foo,bar}]}, {[{{[{foo,bar}]}, true}]})}, - {"Second", enc_error(Type, 1, {[{bar, baz}, E1]})}, - {"Nested", enc_error(Type, 1, {[{bar,{[E1]}}]})}, - {"Nested", enc_error(Type, 1, {[{bar,{[{baz, 1}, E1]}}]})}, - {"In List", enc_error(Type, 1, [{[E1]}])}, - {"In List", enc_error(Type, 1, [{[{bang, true}, E1]}])} + {"Basic", enc_error(Type, {[{foo,bar}]}, {[{{[{foo,bar}]}, true}]})} ]}.
