From 32fb700185a2f2783f8c7e8f3f02c9d8c5d6c4cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Rubinos=20Rodr=C3=ADguez?= Date: Sun, 24 Mar 2024 10:44:58 +0100 Subject: [PATCH] feat: better specs. Now it has full specs and errors. --- src/nbson.erl | 7 ++++--- src/nbson_encoder.erl | 16 +++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/nbson.erl b/src/nbson.erl index 82e5cf7..a81e23b 100644 --- a/src/nbson.erl +++ b/src/nbson.erl @@ -19,7 +19,7 @@ %%% TYPES -type map_document() :: #{key() => value()}. -type proplist_document() :: [{key(), value()}]. --type document() :: map_document() | proplist_document() | [{}]. +-type document() :: map_document() | proplist_document(). -type document_path() :: [key()]. -type key() :: binary(). -type regex_arg() :: unicode:latin1_chardata() | unicode:chardata() | unicode:external_chardata(). @@ -31,7 +31,8 @@ | null | min_key | max_key - | boolean() + | true + | false | atom() | nbson:document() | [value()] @@ -73,7 +74,7 @@ %%% EXTERNAL EXPORTS %%%----------------------------------------------------------------------------- -spec encode(Data) -> Result when - Data :: undefined | map_document() | proplist_document() | [document()], + Data :: undefined | [{}] | map_document() | proplist_document() | [document()], Result :: {ok, BSON} | {error, encode_error_reason()}, BSON :: binary(). encode(Data) -> diff --git a/src/nbson_encoder.erl b/src/nbson_encoder.erl index baab8a3..f2c3c3c 100644 --- a/src/nbson_encoder.erl +++ b/src/nbson_encoder.erl @@ -26,7 +26,8 @@ %%% EXTERNAL EXPORTS %%%----------------------------------------------------------------------------- -spec encode(Data) -> Result when - Data :: undefined | nbson:document() | [nbson:document()], + Data :: + undefined | [{}] | nbson:map_document() | nbson:proplist_document() | [nbson:document()], Result :: {ok, BSON} | {error, nbson:encode_error_reason()}, BSON :: binary(). encode(undefined) -> @@ -42,7 +43,7 @@ encode(Document) when is_map(Document) -> end; encode([{}]) -> {ok, ?EMPTY_DOC}; -encode(Data) when is_list(Data), is_tuple(hd(Data)), size(hd(Data)) == 2 -> +encode([{K, _V} | _Rest] = Data) when is_binary(K) -> case encode_proplist(Data) of {error, _Reason} = Error -> Error; @@ -184,6 +185,11 @@ encode_value(false) -> {?BOOLEAN_TYPE, <>}; encode_value(true) -> {?BOOLEAN_TYPE, <>}; +%max_key and min_key should be the last and ordered by Type value. But gradualizer doesn't like that +encode_value(max_key) -> + {?MAXKEY_TYPE, <<>>}; +encode_value(min_key) -> + {?MINKEY_TYPE, <<>>}; encode_value({Mega, Sec, Micro}) when is_integer(Mega), is_integer(Sec), is_integer(Micro) -> {?DATETIME_TYPE, <>}; encode_value(null) -> @@ -218,11 +224,7 @@ encode_value({timestamp, Inc, Time}) -> encode_value(V) when is_integer(V), -16#8000000000000000 =< V, V =< 16#7fffffffffffffff -> {?INT64_TYPE, <>}; encode_value(V) when is_integer(V) -> - {error, {integer_too_large, V}}; -encode_value(max_key) -> - {?MAXKEY_TYPE, <<>>}; -encode_value(min_key) -> - {?MINKEY_TYPE, <<>>}. + {error, {integer_too_large, V}}. -spec foldwhile(F, AccIn, List) -> Result when F :: fun((term(), AccIn) -> AccOut),