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 by default #196

Merged

Conversation

jrafanie
Copy link
Contributor

@jrafanie jrafanie commented Jan 31, 2018

Previously, if you merged in {a: nil}, it would not overwrite an
existing value. In #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 #13

@jrafanie jrafanie force-pushed the use_discard_nil_value_to_keep_nils branch 2 times, most recently from d2eba65 to 50d3f80 Compare January 31, 2018 19:44
@@ -127,6 +127,7 @@ def merge!(hash)
current = to_hash
DeepMerge.deep_merge!(hash.dup,
current,
merge_nil_values: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note, this is a bit controversial because it's changing behavior. If needed, this could be optional, defaulted to false (to maintain current behavior) and you can enable it via Config.merge_nil_values similar to the overwrite_arrays option.

so, the question is:

  1. should this be optional
  2. if so, what should be the default

I can change the PR based on this feedback. Note, this is to fix #13. Whatever is decided, we can update that issue with the right way solve that issue.

@pkuczynski
Copy link
Member

Looks good, thanks! We would need few additional things before this can be merged:

  • option to set merge_nil_values in Config.setup, so users can decide about this behavior
  • documentation in README
  • entry in CHANGELOG
  • fix codeclimate issues

@jrafanie do you think you can work on this?

@jrafanie
Copy link
Contributor Author

jrafanie commented Feb 2, 2018

option to set merge_nil_values in Config.setup, so users can decide about this behavior

sure @pkuczynski, what do you want the default to be? If we change behavior to default to merge_vil_values: true, we'll need to bump versioning to account for this. If we default to false, and expose it as an option, we won't break anyone depending on the current behavior.

@pkuczynski
Copy link
Member

Make the default true. We will bump a version and publish a warning in changelog :)

@jrafanie
Copy link
Contributor Author

jrafanie commented Feb 7, 2018

I got it @pkuczynski. I'll push those changes.

@jrafanie jrafanie force-pushed the use_discard_nil_value_to_keep_nils branch from 0b2bbe1 to 73ad458 Compare February 7, 2018 22:10
@jrafanie jrafanie changed the title Changes to work with deep_merge so we merge in nil values Merge nil values by default Feb 7, 2018
@jrafanie jrafanie force-pushed the use_discard_nil_value_to_keep_nils branch 4 times, most recently from f1caeb1 to 56afa61 Compare February 7, 2018 22:45
@@knockout_prefix = nil
@@merge_nil_values = true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't fix this cop offense to match the style of the existing code and thereby not change style in this feature.

Copy link
Member

Choose a reason for hiding this comment

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

👍

CHANGELOG.md Outdated
@@ -2,6 +2,7 @@

## Unreleased

* **WARNING:** nil values will now overwrite an existing value. This change of behavior can be reverted via `config.merge_nil_values = false` in your Config initializer ([#196](https://github.com/railsconfig/config/pull/196))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I retained the format of the rest of the file here and didn't fix the line length cop.

Copy link
Member

Choose a reason for hiding this comment

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

👍

@jrafanie jrafanie force-pushed the use_discard_nil_value_to_keep_nils branch from 56afa61 to 3ccbc79 Compare February 7, 2018 22:49
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 jrafanie force-pushed the use_discard_nil_value_to_keep_nils branch from 3ccbc79 to 6d3b140 Compare February 7, 2018 22:52
@jrafanie
Copy link
Contributor Author

jrafanie commented Feb 7, 2018

@pkuczynski Please take a look. I have added your suggestions and fixed all "reasonable" cops without changing the existing style. See my comments above.

@pkuczynski
Copy link
Member

Looks good! Thanks for all your work on this. I will release this as 1.7.0

@pkuczynski pkuczynski added this to the 1.6.2 milestone Feb 8, 2018
@pkuczynski pkuczynski merged commit b75f4c6 into rubyconfig:master Feb 8, 2018
@jrafanie jrafanie deleted the use_discard_nil_value_to_keep_nils branch February 8, 2018 16:02
@jrafanie
Copy link
Contributor Author

jrafanie commented Feb 8, 2018

Thanks @pkuczynski 👍

@pkuczynski
Copy link
Member

Done: https://rubygems.org/gems/config/versions/1.7.0

@jrafanie
Copy link
Contributor Author

jrafanie commented Feb 9, 2018

@pkuczynski FYI, I started playing with this change more in depth in a rails app and it works! But, I did notice a minor change in behavior: we had multiple configurations sources with our "last" source coming from our monolith and defining a "ems" key that other sources would populate. Because the monolith specified the key but with no value, since those would be set by sub-sources, we were overriding the values with nil. I think that's the right behavior and we should just validate our yml so we don't accidentally squash values from other sources.

See: ManageIQ/manageiq#16982

@pkuczynski
Copy link
Member

Well, that's the risk. Your monolith should not define null key or should come first. Other than that I am glad it worked and you like my gem :)

@jeremywadsack
Copy link

Just in case anyone else comes across this, this breaking changed tripped my up because it's not just explicit nil but implicit ones:

# settings.yml
section:
  area:
    setting: nil

# development.yml
section:
  area:
#    setting: your-email-here

This lead to an error undefined method 'setting' for nil:NilClass. We had used this pattern in the past to "document" settings or sections for settings. Or perhaps we were just lazy and left empty YAML "sections" defined which makes them nil instead of a hash.

The solution was to comment out (for documentation) or remove the sections that have no defined values:

# development.yml

# section:
#  area:
#    setting: your-email-here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants