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

Fixes #3309 - Support deep merging of hash and array structures in smart class parameters #1715

Closed
wants to merge 1 commit into from

Conversation

orrabin
Copy link
Member

@orrabin orrabin commented Aug 27, 2014

This relates to feature 3636 which states a couple of changes to the way lookup_keys work
I'm creating a PR to get comments of this part of the change and to see that I'm in the right direction

@orrabin
Copy link
Member Author

orrabin commented Sep 2, 2014

tests are failing on purpose for now

else
values[key_id][name][:value].deep_merge!(value[:value])
values[key_id][name][:element] << value[:element]
# This was an attempt to handle element to only show the elements that won in the merge
Copy link
Member Author

Choose a reason for hiding this comment

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

@ohadlevy here is the problem I had with element
I have a problem predicting what the deep_merge will eliminate and finding which element had an impact on the final hash

@orrabin orrabin changed the title DO NOT MERGE: fixes #3848: Matcher value for smart variables against array Fixes #3848, #3309: Support deep merging of hash and array structures in smart class parameters Sep 3, 2014
@orrabin
Copy link
Member Author

orrabin commented Oct 5, 2014

[test]

1 similar comment
@orrabin
Copy link
Member Author

orrabin commented Oct 6, 2014

[test]

@mbacovsky
Copy link
Member

I'm a bit confused about bugs related to this PR.

Are all this issues going to be addressed in this PR?
I was reviewing #3309 related stuff so far...

@orrabin
Copy link
Member Author

orrabin commented Oct 7, 2014

@mbacovsky #3636 is not addressed in this PR, this is only #3848 and #3309 which are the same bug but one is for arrays and one for hashes.

@mbacovsky
Copy link
Member

@orrabin, with #3636 I see now, I was confused with the branch name. With the #3848 and #3309 I'm not sure though. From the bug descriptions it seems that the former is about matchers (hostgroup=[HG1,HG2]) and the later is about values and its deep merging (covering both hashes and array type values). So different issues unless I'm missing something. Makes sense?

@@ -35,6 +35,7 @@ def possible_value_orders

def values_hash(options = {})
Copy link
Member

Choose a reason for hiding this comment

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

This method is hard to read (not you fault) and you're adding more complexity. Therefore I'd consider doing some refactorings. First - you can extract values[key_id][name] into some variable so you don't have to repeat it on every line.

Then you can use hash with default values values = Hash.new {|h, k| h[k] = {}; values[:new_key][name] = triplet so you avoid hash[key_id] ||= {} lines.

Furthermore you don't have to store values_for_deep_merge in this complex hash format. I'd suggest using array OpenStruct or Struct instance for keeping value, index, element and element_name. Then calling hash_deep_merge that will generate values in desired hash structure. The benefit is that hash_deep_merge will be much easier to read. Ideally I'd use OpenStruct in whole values_hash method and convert the result into hash at the end (maybe some convert method).

Also I think you should compute values inside when "hash" branch, not looking at values_for_deep_merge outside of hash context in unless condition (this will also eliminate the need of values_for_deep_merge being global to the whole method).

Last but not least, I'd split this method so the actual evaluation is separated per type so we'd have methods like update_array_matcher, update_hash_matcher, update_generic_matcher which can be tested separately.

Let me know if I should elaborate any of previous refactorings. This file would deserves many other improvements but these are not related to the PR.

@ares
Copy link
Member

ares commented Oct 14, 2014

From user perspective I found one usability issue. When you set a overriden parameter to be an array or hash, Continue looking checkbox is not enabled so I have to save the parameter and then reopen the edit form to be able to check it. Same applies to Avoid duplicates which I can use after I save Continue looking checkbox value. This in combination with forgetting search filter after form submit is really annoying.

:override => true, :key_type => 'array', :merge_overrides => true,
:default_value => [], :path => "organization\nlocation", :avoid_duplicates => true,
:puppetclass => puppetclasses(:one))
host = hosts(:one)
Copy link
Member

Choose a reason for hiding this comment

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

Could you please also use FactoryGirl to create a host? This will help us when we rebase the host factory PR.

@orrabin
Copy link
Member Author

orrabin commented Oct 14, 2014

@ares thanks for all the comments I will start refactoring :)
regarding the usability issue, did you check the latest version? the checkboxes are enabled for me once I change the parameter type (it should be handled by keyTypeChange in lookup_keys.js)
also, the checkbox for Continue looking is now called Merge overrides

@ares
Copy link
Member

ares commented Oct 14, 2014

Few more nitpicks based on our PR rules,

@ares
Copy link
Member

ares commented Oct 14, 2014

Just for the record, you're right, I had the older commit. JS now works just as I would expect 👍 .

@orrabin orrabin changed the title Fixes #3309: Support deep merging of hash and array structures in smart class parameters Fixes #3309 - Support deep merging of hash and array structures in smart class parameters Oct 20, 2014
@orrabin
Copy link
Member Author

orrabin commented Oct 20, 2014

@ares I refactored values_hash in a second commit so it would be easier to review.
Can you take another look at this?

@ares
Copy link
Member

ares commented Oct 21, 2014

Apart from few nitpicks the refactoring is very nice.

@orrabin
Copy link
Member Author

orrabin commented Oct 21, 2014

@ares I fixed your comments, rebased and squashed commits :)

@ares
Copy link
Member

ares commented Oct 22, 2014

Merged as ea4eec4, thanks @orrabin!

@ares ares closed this Oct 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants