From e8ceec8c21d5aabd72ef5084fe1bee670b46698a Mon Sep 17 00:00:00 2001 From: Fernando Coronel Date: Wed, 5 Aug 2026 10:53:45 -0300 Subject: [PATCH] base64 stored data support add put_path/3 function to handle binary maps and improve error handling --- erlang_ls.config | 32 +++++++++++++++++++++++++++ src/maps_utils.erl | 54 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 erlang_ls.config diff --git a/erlang_ls.config b/erlang_ls.config new file mode 100644 index 0000000..507c651 --- /dev/null +++ b/erlang_ls.config @@ -0,0 +1,32 @@ +# erlang_ls.config +# Place this file in your project root directory + +# Points directly to root folders for flat projects +src_dirs: + - "src/*" + - "test/*" + +# Tracks your compiled external dependencies +deps_dirs: + - "_build/default/lib/*" + - "_build/test/lib/*" + +# Maps paths for your header files (.hrl) +include_dirs: + - "include" + - "_build/default/lib/*/include" + +# Real-time code analysis tools +diagnostics: + enabled: + - compiler + - crossref + disabled: + - dialyzer + - elvis + +# Inline editor actions +lenses: + enabled: + - ct-run-test + - suggest-spec diff --git a/src/maps_utils.erl b/src/maps_utils.erl index 028ae2b..45f7065 100644 --- a/src/maps_utils.erl +++ b/src/maps_utils.erl @@ -257,12 +257,9 @@ get_any(Keys, Map, Default) -> put_path([], _, _) -> error({badkey, []}); - put_path(Path, Value, Map) when is_list(Path) -> do_put_path(Path, Value, Map). - - %% ----------------------------------------------------------------------------- %% @doc %% @end @@ -629,11 +626,6 @@ validate_update(Map0, Changes, Spec, Opts) when is_map(Changes), is_map(Spec), i validate_update_error_list(Map0, Changes, Spec, Opts) when is_map(Changes), is_map(Spec), is_map(Opts) -> maps:fold(fun(K, V, Accum) -> Accum++validate_update_key(K, V, Map0, Changes, Opts) end, [], Spec). - - - - - %% ============================================================================= %% PRIVATE %% ============================================================================= @@ -670,9 +662,38 @@ maybe_get(Key, Map, Default) -> do_put_path(Keys, Value, undefined) -> do_put_path(Keys, Value, #{}); +do_put_path(Keys, Value, [undefined]) -> + do_put_path(Keys, Value, #{}); + do_put_path(Keys, Value, null) -> do_put_path(Keys, Value, #{}); +do_put_path(Keys, Value, [null]) -> + do_put_path(Keys, Value, #{}); + +do_put_path(Keys, Value, []) -> + do_put_path(Keys, Value, #{}); + +do_put_path(Path, Value, Bin) when is_binary(Bin) -> + try base64:decode(Bin) of + Decoded -> + case binary_to_term(Decoded) of + Map when is_map(Map) -> + do_put_path(Path, Value, Map); + L when is_list(L) -> + do_put_path(Path, Value, L); + _ -> + error({badmap, Bin}) + end + catch + _:_ -> + error({badmap, Bin}) + end; + +do_put_path(Path, Value, List ) when is_list(List) -> + Map = try maps:from_list(List) catch _:_ -> #{} end, + do_put_path(Path, Value, Map); + do_put_path([Key], Value, Map) -> maps:put(Key, Value, Map); @@ -688,6 +709,23 @@ do_put_path([], _, Map) -> do_put_path(Key, Value, Map) -> maps:put(Key, Value, Map). +-ifdef(TEST). + +-include_lib("eunit/include/eunit.hrl"). + +% Replicated this error +% 2026-05-06 12:30:01.569 [error] [<0.1430.0> magenta_riak_pool:transaction:51] error=error reason={badmap,<<"g2wAAAABZAAJdW5kZWZpbmVkag==">>} stacktrace=[{maps,put,[<<"enabled">>,true,<<"g2wAAAABZAAJdW5kZWZpbmVkag==">>],[]},{maps_utils,do_put_path,3,[{file,"/usr/src/app/_build/default/lib/maps_utils/src/maps_utils.erl"},{line,671}]},{maps_utils,do_put_path,3,[{file,"/usr/src/app/_build/default/lib/maps_utils/src/maps_utils.erl"},{line,674}]},{lists,foldl,3,[{file,"lists.erl"},{line,1267}]},{lists,map,2,[{file,"lists.erl"},{line,1243}]},{lists,map,2,[{file,"lists.erl"},{line,1243}]},{magenta_riak,handle_search,2,[{file,"/usr/src/app/_build/default/lib/magentalib/src/magenta_riak.erl"},{line,470}]},{magenta_riak_pool,transaction,1,[{file,"/usr/src/app/_build/default/lib/magentalib/src/magenta_riak_pool.erl"},{line,45}]}] +% + +put_path_list_test() -> + #{<<"enabled">> := true } = do_put_path([<<"enabled">>], true, []), + #{<<"enabled">> := true, <<"key">> := value } = do_put_path([<<"enabled">>], true, [{<<"key">>, value}]). +put_path_binary_map_test() -> + % Base64 enconded list [undefined] should be converted to a map and then the path should be set + #{<<"enabled">> := true } = do_put_path([<<"enabled">>], true, <<"g2wAAAABZAAJdW5kZWZpbmVkag==">>), + #{<<"enabled">> := true } = do_put_path([<<"enabled">>], true, <<"g2wAAAABZAAJdW5kZWZpbmVkag==">>). +-endif. + %% @private do_append_list_path(Keys, Value, undefined) ->