Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore_invalid_paths to patch #21

Merged
merged 1 commit into from Feb 28, 2024
Merged

Conversation

tulinmola
Copy link
Contributor

Sometimes it's necessary to apply a patch to a JSON that may lack certain operation paths. In this scenario, it would be great to have an option to ignore the invalid paths. I provide the following example:

      iex> # Patch will succeed, not applying invalid path operations.
      iex> patch = [
      ...> %{op: "replace", path: "/name", value: "Alice"},
      ...> %{op: "replace", path: "/age", value: 42}
      ...> ]
      iex> target = %{"name" => "Bob"} # No age in target
      iex> Jsonpatch.apply_patch(patch, target, ignore_invalid_paths: true)
      {:ok, %{"name" => "Alice"}}

Thanks for this great library. Simple and useful!
Tulo

@corka149
Copy link
Owner

corka149 commented Sep 3, 2023

Hi Tulo. Thanks four your PR! It looks good to me. Could you fix the Credo issue? Then I can merge.

Copy link
Owner

@corka149 corka149 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As credo reported:

Function body is nested too deep (max depth is 2, was 3).

error = %Jsonpatch.Error{patch: patch, patch_index: patch_index, reason: reason}
{:halt, {:error, error}}
{:error, {error, _} = reason} ->
if ignore_invalid_paths && error == :invalid_path do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One option would be to extract the whole block as own function to avoid deep nesting.

@corka149 corka149 merged commit 2671eec into corka149:master Feb 28, 2024
7 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants