Permalink
Browse files

Add bson encoding of null values.

undefined value have been already implemented, but doesn't follow the specifications.

Signed-off-by: François de Metz <fdemetz@af83.com>
  • Loading branch information...
1 parent f111d51 commit 9fac3480d66927975d4bb17def54a56cfadcc3f3 François de Metz committed Oct 6, 2011
Showing with 24 additions and 4 deletions.
  1. +12 −3 src/emongo_bson.erl
  2. +12 −1 t/002-bson.t
View
@@ -84,6 +84,11 @@ encode_key_value(Key, {binary, SubType, Val}) when is_integer(SubType), is_binar
encode_key_value(Key, {oid, HexString}) when is_list(HexString) ->
encode_key_value(Key, {oid, emongo:hex2dec(HexString)});
+%% UNDEFINED
+encode_key_value(Key, undefined) ->
+ Key1 = encode_key(Key),
+ <<6, Key1/binary, 0>>;
+
encode_key_value(Key, {oid, OID}) when is_binary(OID) ->
Key1 = encode_key(Key),
<<7, Key1/binary, 0, OID/binary>>;
@@ -114,8 +119,8 @@ encode_key_value(Key, {datetime, Val}) ->
encode_key_value(Key, {{Year, Month, Day}, {Hour, Min, Secs}}) when is_integer(Year), is_integer(Month), is_integer(Day), is_integer(Hour), is_integer(Min), is_integer(Secs) ->
encode_key_value(Key, {datetime, {{Year, Month, Day}, {Hour, Min, Secs}}});
-%% VOID
-encode_key_value(Key, undefined) ->
+%% NULL
+encode_key_value(Key, null) ->
Key1 = encode_key(Key),
<<10, Key1/binary, 0>>;
@@ -206,6 +211,10 @@ decode_value(5, <<_Size:32/little-signed, 2:8/little, BinSize:32/little-signed,
decode_value(5, <<Size:32/little-signed, SubType:8/little, BinData:Size/binary-little-unit:8, Tail/binary>>) ->
{{binary, SubType, BinData}, Tail};
+%% VOID
+decode_value(6, Tail) ->
+ {undefined, Tail};
+
%% OID
decode_value(7, <<OID:12/binary, Tail/binary>>) ->
{{oid, OID}, Tail};
@@ -227,7 +236,7 @@ decode_value(9, <<MSecs:64/little-signed, Tail/binary>>) ->
%% VOID
decode_value(10, Tail) ->
- {undefined, Tail};
+ {null, Tail};
%% INT
decode_value(16, <<Int:32/little-signed, Tail/binary>>) ->
View
@@ -63,6 +63,17 @@ main(_) ->
ok
end)(),
+ %% 6) data_undefined
+ (fun() ->
+ Val = undefined,
+ BinVal = <<>>,
+ Size = size(BinVal) + 8,
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 6, 97, 0, BinVal/binary, 0>>, "data_undefined encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_null decodes ok"),
+ ok
+ end)(),
+
%% 7) data_oid
(fun() ->
Val1 = {oid, <<255,255,255,255,255,255,255,255,255,255,255,255>>},
@@ -108,7 +119,7 @@ main(_) ->
%% 10) data_null
(fun() ->
- Val = undefined,
+ Val = null,
BinVal = <<>>,
Size = size(BinVal) + 8,
Encoded = emongo_bson:encode([{<<"a">>, Val}]),

0 comments on commit 9fac348

Please sign in to comment.