Skip to content

Commit

Permalink
Merge pull request #43 from nalundgaard/issue_42
Browse files Browse the repository at this point in the history
Patch jsn:delete to delete objects at end of path
  • Loading branch information
nalundgaard authored Mar 29, 2022
2 parents 99352c8 + 41b4220 commit 606c63d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/jsn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,8 @@ keys_set(Keys, {P}, Value, Empty) when is_list(P) ->
keys_set([Key | Rest], Object, Value, Empty)
when is_binary(Key), (is_list(Object) orelse is_map(Object)) ->
case key_get(Key, Object, jsn__undefined) of
E when Value =:= jsn__delete, (E =:= jsn__undefined orelse E =:= Empty) ->
E when Value =:= jsn__delete, Rest /= [],
(E =:= jsn__undefined orelse E =:= Empty) ->
return_if_object(Object, Empty);
E when E =:= jsn__undefined; E =:= Empty ->
key_set(Key, Object, keys_set(Rest, Empty, Value, Empty));
Expand Down
6 changes: 6 additions & 0 deletions test/jsn_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ delete_test_() ->
?_assertEqual(#{}, jsn:delete(Path3, #{})),
?_assertEqual([], jsn:delete(<<"foo">>, [{<<"foo">>, <<"bar">>}])),
?_assertEqual(#{}, jsn:delete(<<"foo">>, #{<<"foo">> => <<"bar">>})),
?_assertEqual(#{}, jsn:delete(<<"foo">>, #{<<"foo">> => #{}})),
?_assertEqual(#{}, jsn:delete(<<"foo">>, #{<<"foo">> => []})),
?_assertEqual(#{<<"foo">> => #{}},
jsn:delete(<<"foo.bar">>, #{<<"foo">> => #{<<"bar">> => #{}}})),
?_assertEqual(#{<<"foo">> => #{<<"bar">> => #{}}},
jsn:delete(<<"foo.baz">>, #{<<"foo">> => #{<<"bar">> => #{}}})),
?_assertEqual(jsn:new({Path2, [1,2,3]}, [{format, proplist}]),
jsn:delete(foo, Object1Plist)),
?_assertEqual(jsn:new({Path2, [1,2,3]}, [{format, map}]),
Expand Down

0 comments on commit 606c63d

Please sign in to comment.