Introduce an array_path option#34
Conversation
The patches suggested in the README only work when you have string keys on the hash.
|
Oh wow, sorry I really don't know how I missed that other PR in my reading up 🤦♂️ Would you prefer it if I closed this one and we work at getting that one over the line?
We technically don't, but I don't think this actually causes us a problem. I actually started by making this clearer by storing array indexes in arrays e.g If we say have two hashes that look similar but is comparing a hash and an array, the diff includes the type difference: It could potentially catch you out on a I'm assuming that's not a big deal as people probably have a reasonable idea of what their structure is like that they're diffing. For But there's all sorts of ways you can cause problems by changing the input object for |
|
@kevindew Thanks a lot for the clarification. I think as long as the users are warned, it's fine to keep it as it is. Could you please add a special comment to mention the tricky issue? Otherwise, LGTM. |
Codecov Report
@@ Coverage Diff @@
## master #34 +/- ##
==========================================
+ Coverage 99.21% 99.27% +0.05%
==========================================
Files 10 10
Lines 637 688 +51
==========================================
+ Hits 632 683 +51
Misses 5 5
Continue to review full report at Codecov.
|
This introduces an array_path option that can be used when generating a
diff. This represents the path to aspects of the diff as an array rather
than a string.
eg.
```
x = {'a' => 1}
y = {'a' => 2}
HashDiff.diff(x, y)
=> [["~", "a", 1, 2]]h
HashDiff.diff(x, y, :array_path => true)
=> [["~", ["a"], 1, 2]]
```
This allows there to be more flexibility with the types used as keys in
a hash. Allowing workarounds for issues such as:
liufengyun#25
eg
```
x = {'a'=>1}
y = {:a=>1}
HashDiff.diff(x, y)
=> [["-", "a", 1], ["+", "a", 1]]
HashDiff.diff(x, y, :array_path => true)
=> [["-", ["a"], 1], ["+", [:a], 1]]
```
And improved ability to patch hashes with keys:
eg
```
x = {a: {b: :c}}
y = {a: {b: :d}}
diff = HashDiff.diff(x, y)
=> [["~", "a.b", :c, :d]]
HashDiff.patch!(x, diff)
NoMethodError: undefined method `[]=' for nil:NilClass
diff = HashDiff.diff(x, y, array_path: true)
=> [["~", [:a, :b], :c, :d]]
HashDiff.patch!(x, diff)
=> {:a=>{:b=>:d}}
```
This updates the `patch!` and `unpatch!` methods to accept diffs with
either paths as strings or as arrays.
|
@liufengyun Sure thing - I've added additional documentation to it. Oh and also caught a typo in the readme. |
|
LGTM, thanks a lot @kevindew 👍 |
Hello 👋, thanks for your work on this gem. We've found it useful.
This PR introduces an array_path option that can be used when generating a diff. This represents the path to aspects of the diff as an array rather than a string.
eg.
This allows there to be more flexibility with the types used as keys in a hash. Allowing workarounds for issues such as: #25
eg
And improved ability to patch hashes with keys:
eg
This updates the
patch!andunpatch!methods to accept diffs with either paths as strings or as arrays.