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

ignore_keys ignores only common keys #88

Open
rellampec opened this issue Jan 24, 2024 · 1 comment
Open

ignore_keys ignores only common keys #88

rellampec opened this issue Jan 24, 2024 · 1 comment

Comments

@rellampec
Copy link

rellampec commented Jan 24, 2024

require 'hashdiff'
Hashdiff::VERSION
#=> "1.1.0"

The following snippet fails to ignore top level key :a

a = { a: 4, b: {a: 5, c: 6} }
b = {       b: {a: 7, c: 3} }

diff = Hashdiff.diff(a, b, ignore_keys: :a)
#=> [["-", "a", 4], ["~", "b.c", 6, 3]]

Shouldn't diff be expected to be the below?

[["~", "b.c", 6, 3]]

The documentation on ignore_keys states that:

The :ignore_keys option allows you to specify one or more keys to ignore, which defaults to [] (none). Ignored keys are ignored at all levels.

Here line where :ignore_keys happens (in Hashdiff::ComparableHashes::call):

opts[:ignore_keys].each { |k| common_keys.delete k }

added_keys and deleted_keys don't seem to have the exclusion applied.

@rellampec rellampec changed the title ignore_keys only ignores common keys ignore_keys ignores only common keys Jan 24, 2024
@rellampec
Copy link
Author

a = { a: 4, b: {a: 5, c: 6}         }
b = {       b: {a: 7, c: 3},  d: 8 }

(1) ignore_keys: keep backwards compatibility

diff = Hashdiff.diff(a, b, ignore_keys: [:a, :d])
#=> [["-", "a", 4], ["~", "b.a", 5, 7], ["~", "b.c", 6, 3], ["+", "d", 8]]
  • ignores change of b.a but includes :a removal and :d addition.

(2) new ignore_added_keys: only affects added keys

diff = Hashdiff.diff(a, b, ignore_keys: [:a, :d])
#=> [["-", "a", 4], ["~", "b.a", 5, 7], ["~", "b.c", 6, 3]]
  • like ignore_keys but also ignoring added key :d

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

No branches or pull requests

1 participant