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

Resolve merge issue with json-config vs thor defaults #2377

Merged
merged 2 commits into from
Dec 6, 2017

Conversation

jquick
Copy link
Contributor

@jquick jquick commented Dec 6, 2017

When testing the backend_cache via json-config we noticed that the Thor defaults were overriding the json-config settings. This was due to a hash merge using the wrong hash as the source. Rearranging the hash merge and using the json-config options as the override hash fixes this issue.

Along with this change it was requested for debug lines to ensure backend_cache was enabled or not.

Signed-off-by: Jared Quick [email protected]

@jquick jquick requested a review from a team as a code owner December 6, 2017 18:21
@jquick jquick force-pushed the jq/merge_json_cli_options branch from e69ee75 to 54e9f61 Compare December 6, 2017 18:24
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

Hi @jquick! I think this change will introduce a problem. Details below.

@@ -120,7 +120,7 @@ def opts

def merged_opts
# argv overrides json
Thor::CoreExt::HashWithIndifferentAccess.new(options_json.merge(options))
Thor::CoreExt::HashWithIndifferentAccess.new(options.merge(options_json))
Copy link
Contributor

Choose a reason for hiding this comment

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

This will fix the "Thor defaults incorrectly overriding the JSON options" problem, but it introduces the "JSON options overriding user-provided Thor CLI flags" problem. And the comment on line 122 indicates the intention - flags on the CLI should override the JSON.

I think we may have to remove default assignments in Thor and handle them on our own:

  • remove all defaults in Thor option declarations
  • build a hash of all the options that have defaults, and assign their default values
  • merge in the JSON options
  • merge in the Thor options


describe 'merge_options' do
it 'json options override cli' do
json = { "backend_cache":true }
Copy link
Contributor

Choose a reason for hiding this comment

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

read_config doesn't return JSON but rather parsed JSON. Can we change the variable name to parsed_json so it's clear? initially I thought you were trying to represent actual JSON here and thought you forgot the quotes around the whole string. 🙂

Also, this Hash has a key of a symbol named :backend_cache with the way you've written it. JSON.parse won't return it like that, so we should probably change this to:

parsed_json = { 'backend_cache' => true }

json = { "backend_cache":true }
cli_options = { 'json_config' => 'dummy', 'backend_cache' => false }
cli.instance_variable_set(:@options, cli_options)
cli.expects(:read_config).returns(json)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a .with to this so we show how the options plumb all the way to the config reading?

cli.expects(:read_config).with('dummy').returns(json)

@jquick jquick force-pushed the jq/merge_json_cli_options branch from 55fcbb6 to 5c7b178 Compare December 6, 2017 20:54
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

Looks great. I especially like how you appease my OCD with line spaces.

Copy link
Contributor

@arlimus arlimus left a comment

Choose a reason for hiding this comment

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

Awesome fix and coding, thank you @jquick !!

@arlimus arlimus merged commit 4c592f4 into master Dec 6, 2017
@arlimus arlimus deleted the jq/merge_json_cli_options branch December 6, 2017 21:22
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