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

Merge nil values or keep the original via an option #33

Merged
merged 1 commit into from
Apr 26, 2017

Conversation

jrafanie
Copy link
Contributor

For some people, it's unexpected that explicitly merging in a nil will
not overwrite an existing value.
DeepMerge::deep_merge({:a => nil}, {:a => 1})

Currently, we retain the 1 value.
My expectation is we'd get a nil value.

Since changing this is a change in behavior, and possibly not desirable,
I'm exposing an option to opt-in to this new behavior.

Note, this is related to #20 and can be confusing to see the difference.
This commit handles merging a nil value into an existing destination via
an option.
PR #20 handles NOT merging a value into an already nil destination.

@Fryguy
Copy link
Collaborator

Fryguy commented Apr 21, 2017

This change looks good to me. Not sure on the option name nor description as they don't seem to describe properly what this does, but we can debate that. @jrafanie, I believe you are ultimately trying to get this into railsconfig/config, so I would like some input from @pkuczynski here as well. @pkuczynski thoughts? Here is some WIP code from jrafanie on how it might work in the config gem: jrafanie/config@b8af322

See also rubyconfig/config#13

@pkuczynski
Copy link

@Fryguy I think the name of the option is not the best choice, although thinking about it for a while, I am not sure how could it be named differently (prosed default value maintains current behavior)? Any other suggestions?

  • override_with_nil (default: false)
  • nil_overrides (false)
  • skip_nil (true)
  • skip_nill_values (true)
  • discard_nil_values (true)

This PR is also missing documentation update (README.md).

Other than that it is indeed required feature for config gem (I made a mistake saying that #20 solves the problem) and I would welcome it very much!

@jrafanie any chance to make updates? @danielsdeleo any chance to get that merged?

@jrafanie
Copy link
Contributor Author

Yes @pkuczynski I can make those changes.

From your suggestions, I think I like skip_nil_values (true) the most although naming is hard and I'll change my mind tomorrow. I think that describes more what a consumer of this method would expect the interface to look like:

hash_src = {"item" => nil}
hash_dst = {"item" => 'existing'}
DeepMerge::deep_merge!(hash_src, hash_dst, {:skip_nil_values => false})

assert_equal({"item" => nil}, hash_dst)

If you agree, I'll update the PR and add some doc. Thanks for the review.

@pkuczynski
Copy link

Yeah, naming always puzzles my head too :)

I think the best name for an option is when it is human readable. So in my mind, I would rather expect that non-default some_option_name = true would turn on an additional feature. That's why I am in favor of positive names like nil_should_override, instead of skip, discard, which sound like double negation.

But in this case it is very hard to come up with a name for me too, so I guess we can proceed with skip_nil_values

@pkuczynski
Copy link

After reading what I wrote above, I think skip_nil_values is the best option, unless someone thinks otherwise?

@jrafanie
Copy link
Contributor Author

@pkuczynski I agree on usually preferring the positive form instead of double negation...

the only positive I can come up with is:
DeepMerge::deep_merge!(hash_src, hash_dst, {:merge_nil_values => true})

Again, naming...

@pkuczynski
Copy link

:merge_nil_values sounds good...

@jrafanie jrafanie force-pushed the discard_nil_value_option branch 2 times, most recently from b47e089 to df851ce Compare April 25, 2017 20:06
For some people, it's unexpected that explicitly merging in a nil will
not overwrite an existing value.
`DeepMerge::deep_merge({:a => nil}, {:a => 1})`

Currently, we retain the 1 value.
My expectation is we'd get a nil value.

Since changing this is a change in behavior, and possibly not desirable,
I'm exposing an option to opt-in to this new behavior.

Note, this is related to danielsdeleo#20 and can be confusing to see the difference.
This commit handles merging a nil value into an existing destination via
an option.
PR danielsdeleo#20 handles NOT merging a value into an already nil destination.
@jrafanie jrafanie force-pushed the discard_nil_value_option branch from df851ce to e0e2c7e Compare April 25, 2017 20:08
@jrafanie
Copy link
Contributor Author

Ok, @pkuczynski updated

@jrafanie
Copy link
Contributor Author

Updated the WIP my railsconfig branch to show how it would be used there: jrafanie/config@fe672d2

@Fryguy
Copy link
Collaborator

Fryguy commented Apr 26, 2017

LGTM

@Fryguy Fryguy merged commit 1ed5af9 into danielsdeleo:master Apr 26, 2017
@Fryguy
Copy link
Collaborator

Fryguy commented Apr 26, 2017

@jrafanie I just realized there's another section in the README that needs an update...can you open another PR? See https://github.com/danielsdeleo/deep_merge#options

jrafanie added a commit to jrafanie/deep_merge that referenced this pull request Apr 26, 2017
jrafanie added a commit to jrafanie/deep_merge that referenced this pull request Apr 26, 2017
@jrafanie jrafanie deleted the discard_nil_value_option branch April 26, 2017 14:14
@jrafanie
Copy link
Contributor Author

Thanks @pkuczynski @Fryguy for the fast review and merge!

Once #33 is merged, a tag is created and pushed to rubygems.org, I can open the PR on on the config gem that will look something like this. We can discuss there if this change in behavior should be optional via an opt-in or opt-out.

Does that sound good @pkuczynski @Fryguy?

@pkuczynski
Copy link

Sounds god to me! I won't be able to do any work on config gem for the next two weeks, but please create PR after deep_merge is released and I will review it as soon as I can...

Thanks for your contribution @jrafanie!

Fryguy added a commit that referenced this pull request May 1, 2017
…lues

Add missing doc for #33, merge_nil_values
jrafanie added a commit to jrafanie/config that referenced this pull request Jan 31, 2018
jrafanie added a commit to jrafanie/config that referenced this pull request Jan 31, 2018
jrafanie added a commit to jrafanie/config that referenced this pull request Jan 31, 2018
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
jrafanie added a commit to jrafanie/config that referenced this pull request Feb 7, 2018
Previously, if you merged in {a: nil}, it would not overwrite an
existing value.  In rubyconfig#13, this was found to be an undesirable default
behavior.

Now, we will merge nil values by default. You can retain the old
behavior via `config.merge_nil_values = false` in your Config initializer.

This required changes to deep merge, found in: danielsdeleo/deep_merge#33
This was released in deep_merge 1.2.0+.

Fixes rubyconfig#13
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.

3 participants