Permalink
Browse files

adding distinction between int and long in emongo_bson

  • Loading branch information...
1 parent 521c7f5 commit a3e5c24536a3b05e2507fe8015d572a60a792906 Jacob Vorreuter committed Nov 3, 2009
Showing with 67 additions and 15 deletions.
  1. +10 −1 src/emongo_bson.erl
  2. +57 −14 t/002-bson.t
View
@@ -119,10 +119,15 @@ encode_key_value(Key, {regexp, Regexp, Options}) ->
<<11, Key1/binary, 0, RegexpBin/binary, 0, OptionsBin/binary, 0>>;
% INT
-encode_key_value(Key, Val) when is_integer(Val) ->
+encode_key_value(Key, Val) when is_integer(Val), Val =< 2147483647, Val >= -2147483648 ->
Key1 = encode_key(Key),
<<16, Key1/binary, 0, Val:32/little-signed>>;
+% LONG
+encode_key_value(Key, Val) when is_integer(Val) ->
+ Key1 = encode_key(Key),
+ <<18, Key1/binary, 0, Val:64/little-signed>>;
+
encode_key_value(Key, Val) ->
exit({oh_balls, Key, Val}).
@@ -220,5 +225,9 @@ decode_value(10, Tail) ->
decode_value(16, <<Int:32/little-signed, Tail/binary>>) ->
{Int, Tail};
+%% LONG
+decode_value(18, <<Int:64/little-signed, Tail/binary>>) ->
+ {Int, Tail};
+
decode_value(_, _) ->
exit(oh_fuck).
View
@@ -12,7 +12,9 @@ main(_) ->
Val = 1.1,
BinVal = <<Val:64/little-signed-float>>,
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 1, 97, 0, BinVal/binary, 0>>, "data_number encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 1, 97, 0, BinVal/binary, 0>>, "data_number encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_number decodes ok"),
ok
end)(),
@@ -22,7 +24,9 @@ main(_) ->
Val1 = unicode:characters_to_binary(Val),
BinVal = <<(byte_size(Val1)+1):32/little-signed, Val1/binary, 0:8>>,
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 2, 97, 0, BinVal/binary, 0>>, "data_string encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 2, 97, 0, BinVal/binary, 0>>, "data_string encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, list_to_binary(Val)}], "data_string decodes ok"),
ok
end)(),
@@ -31,7 +35,9 @@ main(_) ->
Val = [{"b", "c"}],
BinVal = emongo_bson:encode(Val),
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 3, 97, 0, BinVal/binary, 0>>, "data_object encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 3, 97, 0, BinVal/binary, 0>>, "data_object encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, [{<<"b">>, <<"c">>}]}], "data_object decodes ok"),
ok
end)(),
@@ -40,7 +46,9 @@ main(_) ->
Val = {array, ["a", "b", "c"]},
BinVal = emongo_bson:encode([{0, "a"}, {1, "b"}, {2, "c"}]),
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 4, 97, 0, BinVal/binary, 0>>, "data_array encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 4, 97, 0, BinVal/binary, 0>>, "data_array encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, {array, [<<"a">>, <<"b">>, <<"c">>]}}], "data_array decodes ok"),
ok
end)(),
@@ -49,21 +57,28 @@ main(_) ->
Val = {binary, 2, <<"abc">>},
BinVal = <<7:32/little-signed, 2:8, 3:32/little-signed, <<"abc">>/binary>>,
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 5, 97, 0, BinVal/binary, 0>>, "data_binary encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 5, 97, 0, BinVal/binary, 0>>, "data_binary encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_binary decodes ok"),
ok
end)(),
%% 7) data_oid
(fun() ->
- Val1 = {oid, "ffffffffffffffffffffffff"},
- BinVal1 = emongo:hex2dec("ffffffffffffffffffffffff"),
+ Val1 = {oid, <<255,255,255,255,255,255,255,255,255,255,255,255>>},
+ BinVal1 = <<255,255,255,255,255,255,255,255,255,255,255,255>>,
Size1 = size(BinVal1) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val1}]), <<Size1:32/little-unsigned, 7, 97, 0, BinVal1/binary, 0>>, "data_oid hex encodes ok"),
+ Encoded1 = emongo_bson:encode([{<<"a">>, Val1}]),
+ etap:is(Encoded1, <<Size1:32/little-unsigned, 7, 97, 0, BinVal1/binary, 0>>, "data_oid dec encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded1)), [{<<"a">>, Val1}], "data_oid decodes ok"),
- Val2 = {oid, <<255,255,255,255,255,255,255,255,255,255,255,255>>},
- BinVal2 = <<255,255,255,255,255,255,255,255,255,255,255,255>>,
+ Val2 = {oid, "ffffffffffffffffffffffff"},
+ BinVal2 = emongo:hex2dec("ffffffffffffffffffffffff"),
Size2 = size(BinVal2) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val2}]), <<Size2:32/little-unsigned, 7, 97, 0, BinVal2/binary, 0>>, "data_oid dec encodes ok"),
+ Encoded2 = emongo_bson:encode([{<<"a">>, Val2}]),
+ etap:is(Encoded2, <<Size2:32/little-unsigned, 7, 97, 0, BinVal2/binary, 0>>, "data_oid hex encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded2)), [{<<"a">>, Val1}], "data_oid decodes ok"),
+
ok
end)(),
@@ -72,7 +87,9 @@ main(_) ->
Val = true,
BinVal = <<1:8>>,
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 8, 97, 0, BinVal/binary, 0>>, "data_boolean encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 8, 97, 0, BinVal/binary, 0>>, "data_boolean encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_boolean decodes ok"),
ok
end)(),
@@ -83,7 +100,9 @@ main(_) ->
Epoch = Secs1 * 1000 + trunc(MicroSecs / 1000),
BinVal = <<Epoch:64/little-signed>>,
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 9, 97, 0, BinVal/binary, 0>>, "data_date encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 9, 97, 0, BinVal/binary, 0>>, "data_date encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, {MegaSecs,Secs,erlang:trunc(MicroSecs / 1000) * 1000}}], "data_date decodes ok"),
ok
end)(),
@@ -92,7 +111,31 @@ main(_) ->
Val = undefined,
BinVal = <<>>,
Size = size(BinVal) + 8,
- etap:is(emongo_bson:encode([{<<"a">>, Val}]), <<Size:32/little-unsigned, 10, 97, 0, BinVal/binary, 0>>, "data_null encodes ok"),
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 10, 97, 0, BinVal/binary, 0>>, "data_null encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_null decodes ok"),
+ ok
+ end)(),
+
+ %% 16) data_int
+ (fun() ->
+ Val = 11,
+ BinVal = <<Val:32/little-signed>>,
+ Size = size(BinVal) + 8,
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 16, 97, 0, BinVal/binary, 0>>, "data_int encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_int decodes ok"),
+ ok
+ end)(),
+
+ %% 18) data_long
+ (fun() ->
+ Val = 5275387038659964208,
+ BinVal = <<Val:64/little-signed>>,
+ Size = size(BinVal) + 8,
+ Encoded = emongo_bson:encode([{<<"a">>, Val}]),
+ etap:is(Encoded, <<Size:32/little-unsigned, 18, 97, 0, BinVal/binary, 0>>, "data_number encodes ok"),
+ etap:is(hd(emongo_bson:decode(Encoded)), [{<<"a">>, Val}], "data_number decodes ok"),
ok
end)(),

0 comments on commit a3e5c24

Please sign in to comment.