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

WIP: Enum new PUP-10944 #8710

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

WIP: Enum new PUP-10944 #8710

wants to merge 1 commit into from

Conversation

nod0n
Copy link

@nod0n nod0n commented Jul 23, 2021

@puppetlabs-jenkins
Copy link
Collaborator

Can one of the admins verify this patch?

@CLAassistant
Copy link

CLAassistant commented Jul 23, 2021

CLA assistant check
All committers have signed the CLA.

pick one item of an enum. A boolean or integer value is accepted and a
string is returned.
@nod0n nod0n marked this pull request as ready for review August 19, 2021 11:40
@nod0n nod0n requested review from a team August 19, 2021 11:40
@joshcooper
Copy link
Contributor

Hi @nod0n, thanks for your contribution! I read through the slack conversation, about wanting something like bool2str from stdlib. If I understand correctly, you want to take a boolean (or integer) as input and have it select among multiple boolean-like values (yes, no, etc), for example:

notice(convert(['no', 'yes'], true))

If so I'm not sure adding Enum.new will do that, but I may be misunderstanding. For example, I took your PR and added the following:

       end
 
       dispatch :from_args do
+        param          'Array', :values
         param          'Selector', :selector      # selects one of the Enum's strings
         optional_param 'Format',   :string_format # how should the string being formatted?
       end
 
-      def from_args(selector, format = :default)
+      def from_args(values, selector, format = :default)
         string = case selector
-          when true then @values[1]
-          when false then @values[0]
-          when Integer then @values[selector]
+          when true then values[1]
+          when false then values[0]
+          when Integer then values[selector]
           end
 
         StringConverter.singleton.convert(string, format)

However that fails, because Enum.new is supposed to return an instance of an Enum, not a String:

$ bx puppet apply -e 'notice(Enum.new(["no", "yes"], true))'
Error: Evaluation Error: Error while evaluating a Method call, Converted value from Enum.new() has wrong type, expects a match for Enum, got 'yes' (line: 1, column: 16) on node localhost

Based on your original comments in slack:

but is it also possible to set custom value for true , like with bool2str

makes me think you really do want to implement String.new(["no", "yes"], true)? Thoughts @hlindberg?

@hlindberg
Copy link
Contributor

hlindberg commented Sep 1, 2021 via email

@joshcooper
Copy link
Contributor

An ”instance of Enum” is actually a ruby string so that should work

Ahh, ok I'll take a look

@joshcooper joshcooper added the enhancement New feature or request label Nov 15, 2023
when true then @values[1]
when false then @values[0]
when Integer then @values[selector]
end
Copy link
Contributor

@joshcooper joshcooper Mar 13, 2024

Choose a reason for hiding this comment

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

When the selector is an Integer we should assert that it's between 0 <= selector < @values.length and raise ArgumentError if it's not. Similarly, we should have an else clause and raise if since it's not supported. We do something similar when creating floats:

raise ArgumentError, _("Illegal radix: %{radix}, expected 2, 8, 10, 16, or default") % { radix: radix }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants