-
Notifications
You must be signed in to change notification settings - Fork 583
Add Ruby function stdlib::cmp_hash #1376
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
Conversation
Hello reviewers, I need some help with a specific spec test:
My intention is to make sure that an exception is thrown when the lambda supplied to
Does Once this (hopefully final) issue is resolved, I'll squash-rebase the new commits for this pull request. |
Hi all, I won't be able to react to any updates till July 17th. Therefor I've disabled the troublesome spec test and squashed all my commits for this PR. The CI workflow I ran manually still fails in the Acceptance phase though, which I don't see related to my changes. Again, I need assistance from somebody else who is more experienced with the automated tests. Regarding the pending signature of the CLA, my employer has no decisive opinion on that, so I'm still trying to get a final decision. In case that is a strong requirement before the merge, I'll continue that when I've returned. |
Hum 🤔 $ puppet apply -te 'warning({ a => { b => c }} == { a => { b => c }})'
Warning: Scope(Class[main]): true
[...]
$ puppet apply -te 'warning({ a => { b => c }} == { a => { b => ce }})'
Warning: Scope(Class[main]): false
[...] Do you have a specific use case in mind? |
@smortex, sorry for the unclear phrasing. Puppet can tell whether two Hashes are identical or not, but it cannot sort them (lesser and greater relations are undefined for Hashes). Even the build-in
|
@XMol, oh! I understand! But still it does not make sense to me, do you have a practical use cases for this? Because I can't imagine a situation where we may want to turn an array of hash into another like this 🤔 :
Ruby Hash does not implement |
Admittedly @smortex, our use-case was quite niche. If it wasn't, I'm sure Puppet or stdlib would provide some method that would accommodate it in some way. Yes, using
Sure, Puppet then can implement these operators for Hashes. In my opinion, the proposed function is distinctly different from them. And if you wanted to know whether a Hash is a subset of another, the I also don't see a reason why you would want to reorder your example like that. But I did find a way to accomplish it! 😄
Another example from my work environment would be data input from Puppet's ENC, which actually are Arrays of Hashes, like the designated configuration of the network interfaces of the node. Or the actual use-case we had, which is the configuration of multipath. For a different contrived example, maybe someone wanted to order the mounted devices according to their used space?
It is perfectly fine for me to dismiss the pull request with the reasoning that nobody can envision a high demand for it. I felt the same, which is why I had published this function only as a community snippet at first. |
Non-important details about by weird example
Oh, I did not intended to sort like that, I was not understanding where this was going so I tried with some sample data and this was the result of my experiment. Since it did not make more sense, I was still confused… hence the request for real world examples 😀
Hum, I still don't see how this help: sorting arrays of hashes is already straightforward: $data = [
{ size => 3 },
{ size => 1 },
]
$data.sort |$a, $b| { compare($a.dig('size'), $b.dig('size')) } #=> [{size => 1}, {size => 3}] Your comparison function avoid the code duplication, let's put this on the side for now and I will write about it at the end 😉
Unfortunately I have no experience with this and can't imagine the issue you want to solve 😢.
Ah! This example makes sense to me!!! 😀 Thank you! Then adding support for Hash to puppet's # Proposal 1
$facts['mountpoints'].sort |$a, $b| { compare($a.dig(1, 'used_bytes'), $b.dig(1, 'used_bytes')) } This also work without a block and sort hashes by key: # Proposal 1
{
a => 1,
c => 2,
b => 3,
}.sort #=> {a => 1, b => 3, c => 2} At this point, the main difference with this and the function you provide is that yours does not do the actual comparison, but yield to a value that is compared internally. This is equivalent to the # Proposal 2
$facts['mountpoints'].stdlib::sort_by |$m| { $m.dig(1, 'used_bytes') } I think we are on the good way, did I still completely fail to understand the problem, or do the above proposals (add Hash support to |
Hi @smortex, sometimes going through longer discussions can be quite enlightening - for both parties! 😃
That's a great solution with current methods that Puppet offers! It actually does the very same as I left out all the details regarding configuration of multipath, because that is completely homebrew. I.e. we could avoid the need for comparing Hashes by rewriting other bits of code. So that is a weak argument, but explains our motivation. Enabling Agreed, |
Closing this pull request, in favor of a new one that wants to add Implementing that new function taught me quite a lot of things!
Now I'm struggling with the spec tests again, but that can be discussed in the upcoming new PR. |
Summary
Add a new function for comparing two (Puppet) Hashes.
Additional Context
To the best of my knowledge, Puppet has no means to compare two Hashes to each other. That is, not unless additional steps are taken to transform them into something that Puppet actually can compare directly. The new function
stdlib::cmp_hash
should simplify that.Checklist
puppet apply
for version 7.23.0.