You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When manually initializing Config with a YAML source, and then adding a hash source via .add_source!(), the Settings.reload!() method causes the hash source to be clobbered. Reversing the order of the sources by using prepend_source!() instead causes the YAML file to be totally clobbered leaving only the contents of the hash instead.
The reasons seems to be related to the fact that on line 43 of config/options.rb, we perform a DeepMerge of conf and source_conf. In this case, conf is an object of type Config::Options containing the configuration so far at that point in the reload process. The hash source, which is an object of type Config::Sources::HashSource, returns a plain hash when calling .load().
So source_conf (which is set by calling source.load()) is a plain Hash, and conf is a Config::Options.
Per the DeepMerge documentation:
The deep_merge! method permits merging of arbitrary child elements. The two top level elements must be hashes.
When I changed line 43 of config/options.rb like this, it worked:
43conf=DeepMerge.deep_merge!(# Manually set "conf" to this since the destination wouldn't stick44source_conf,45conf.to_hash,# Make sure this is also a hash46preserve_unmergeables: false,47knockout_prefix: Config.knockout_prefix,48overwrite_arrays: Config.overwrite_arrays,49merge_nil_values: Config.merge_nil_values,50merge_hash_arrays: Config.merge_hash_arrays51)
Alternatively you could just change line 40-41, I think:
40ifconf.empty?41conf=source_conf.to_hash
Once I made this change, I got the expected behaviour: the parameters in my hash were loaded after those in my YAML file, and merged together with priority given to the hash which is last in the sources list.
Happy to PR either of those methods, but wanted to make sure I wasn't doing something wrong or missing an unintended consequence of those changes.
The text was updated successfully, but these errors were encountered:
Have you tried running the test after making any of the changes? This might give you an answer if anything gets broken. Feel free to do a PR and it will test it automatically across different frameworks.
When manually initializing Config with a YAML source, and then adding a hash source via
.add_source!()
, theSettings.reload!()
method causes the hash source to be clobbered. Reversing the order of the sources by usingprepend_source!()
instead causes the YAML file to be totally clobbered leaving only the contents of the hash instead.The reasons seems to be related to the fact that on line 43 of
config/options.rb
, we perform a DeepMerge ofconf
andsource_conf
. In this case,conf
is an object of typeConfig::Options
containing the configuration so far at that point in the reload process. The hash source, which is an object of typeConfig::Sources::HashSource
, returns a plain hash when calling.load()
.So
source_conf
(which is set by callingsource.load()
) is a plainHash
, andconf
is aConfig::Options
.Per the DeepMerge documentation:
When I changed line 43 of
config/options.rb
like this, it worked:Before:
After:
Alternatively you could just change line 40-41, I think:
Once I made this change, I got the expected behaviour: the parameters in my hash were loaded after those in my YAML file, and merged together with priority given to the hash which is last in the sources list.
Happy to PR either of those methods, but wanted to make sure I wasn't doing something wrong or missing an unintended consequence of those changes.
The text was updated successfully, but these errors were encountered: