From 3afabba001ef877b118752b4375fbcbc629370bf Mon Sep 17 00:00:00 2001 From: Zhuang Sirui Date: Thu, 20 Aug 2015 17:48:36 +0800 Subject: [PATCH] Support key as integer for the maps put. Add test case at `bson_tests:maps_put_test/0`. --- src/bson_binary.erl | 4 +++- test/bson_tests.erl | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bson_binary.erl b/src/bson_binary.erl index 49e3719..1a9efe8 100644 --- a/src/bson_binary.erl +++ b/src/bson_binary.erl @@ -44,6 +44,8 @@ get_map(<>) -> %% @private put_field_accum(Label, Value, Bin) when is_atom(Label) -> <>; +put_field_accum(Label, Value, Bin) when is_integer(Label) -> + <>; put_field_accum(Label, Value, Bin) when is_binary(Label) -> <>. @@ -225,4 +227,4 @@ put_oid(<>) -> Oid. %% @private -spec get_oid(binary()) -> {<<_:96>>, binary()}. -get_oid(<>) -> {Oid, Bin}. \ No newline at end of file +get_oid(<>) -> {Oid, Bin}. diff --git a/test/bson_tests.erl b/test/bson_tests.erl index 5fbc64b..03f9119 100644 --- a/test/bson_tests.erl +++ b/test/bson_tests.erl @@ -104,10 +104,10 @@ utf8_test() -> ?assertEqual(<<"test">>, bson:utf8("test")). maps_put_test() -> - SimpleMap = #{<<"map">> => true, <<"simple">> => <<"very">>, atom => key, array => [1, 2, 3, 4]}, + SimpleMap = #{<<"map">> => true, <<"simple">> => <<"very">>, atom => key, array => [1, 2, 3, 4], 1024 => <<"integer value">>}, Encoded1 = bson_binary:put_document(SimpleMap), {Decoded1, <<>>} = bson_binary:get_document(Encoded1), - ?assertEqual({<<"array">>, [1, 2, 3, 4], <<"atom">>, key, <<"map">>, true, <<"simple">>, <<"very">>}, Decoded1), + ?assertEqual({<<"1024">>, <<"integer value">>, <<"array">>, [1, 2, 3, 4], <<"atom">>, key, <<"map">>, true, <<"simple">>, <<"very">>}, Decoded1), MapWithMap = #{<<"map">> => true, <<"simple">> => <<"not">>, <<"why">> => #{<<"because">> => <<"with map">>, ok => true}}, Encoded2 = bson_binary:put_document(MapWithMap), {Decoded2, <<>>} = bson_binary:get_document(Encoded2), @@ -145,4 +145,4 @@ maps_flattering_test() -> <<"personal.country.domen">> => <<"su">>, <<"email">> => <<"test@test.su">> }, - ?assertEqual(FlattenMap, Result). \ No newline at end of file + ?assertEqual(FlattenMap, Result).