-
Notifications
You must be signed in to change notification settings - Fork 13
Prepare struct option #34
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
Conversation
Enhance the diff function to accept an `:ancestor_path` option, allowing users to specify a starting point for diffing nested maps and lists. Update related tests to verify functionality with various ancestor paths, including handling of escaped characters.
…uct handling Updated the diff function to accept a `:prepare_struct` option, allowing users to define a function for converting structs to maps. This change includes modifications to related functions and tests to ensure proper functionality with various struct scenarios.
…truct handling Added detailed documentation for the `:prepare_struct` option, which allows users to define a function for converting structs to maps during the diff process. Updated the default behavior to a no-op function for clarity.
|
@Valian looks good. But what do you think about a prepare_target and prepare_source in general and not only limited to structs? |
|
I agree we could make it work for any data. I suggested structs since they're easy to pattern match, and usually should be enough. Not sure about different transforms for source and destination. Do you have an use case for different transforms for source and destination? |
|
When someone is using regular maps which should be filtered for passwords or api tokens. Or someone wants a "ignore case" for strings and wants to lower case all strings before diffing. Do you think there is any disadvantage to allowing such functionality for any type?🤔 |
|
The only disadvantage I see is potentially a small performance hit - instead of doing it only for structs we would be doing it for each value, including strings, numbers etc. Unless I misunderstood and you'd like to do it only for maps and not for individual simple values (strings etc) inside maps?
I only expressed concern about two separate handlers, In other words, for me The difference is in:
I think library API should cover 90% of the most common, reasonable requirements. With After a lengthy response, here what I'd suggest:
What do you think @corka149? |
|
Sounds reasonable @Valian . I agree with your suggestions. Let's use |
|
Nice! Will adjust soon |
|
Sorry for it taking some time @corka149. New approach is almost ready. Just, I realized now that function has to almost always include a passthrough clause eg test "Create diff with prepare_map option using subset of fields" do
source = %TestStruct{
field1: "value1",
field2: "value2",
inner: %{nested: "old"},
field: "ignored"
}
destination = %TestStruct{
field1: "new_value",
field2: "value2",
inner: %{nested: "new"},
field: "also_ignored"
}
patches =
Jsonpatch.diff(source, destination,
prepare_map: fn
%TestStruct{field1: field1, inner: inner} -> %{field1: field1, inner: inner}
map -> map
end
)
expected_patches = [
%{op: "replace", path: "/field1", value: "new_value"},
%{op: "replace", path: "/inner/nested", value: "new"}
]
assert_equal_patches(patches, expected_patches)
endIf I'd write simply |
Implemented logic to create a replace operation when the type of the root value changes, such as setting a value to nil. Updated tests to verify this behavior, including cases with ancestor paths.
|
@corka149 I've added one more change here. When I was using Jsonpatch in live_vue, especially with You can check my latest commit, but in short I'm now generating a replace operation when type of root value was updated. I think it makes sense? According to AI, this is correct based on RFC. test "Create full replace operation when type of root value changes" do
assert [%{op: "replace", path: "", value: 1}] = Jsonpatch.diff("unexpected", 1)
end |
|
Thx for all the effort @Valian 🙏 |
|
no problem! Now onto rebasing #32 and hopefully merge & release <3 |
I've added a new option to diff letting to customize structs before diffing them. By default it does nothing, but can be used to nicely customize diff behaviour for your structs.
I've built that PR on top of #33, so that other one should be merged first.
Fixes #29