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

Numerics shouldn't be tested for ".bytes" like suffix #23349

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jrafanie
Copy link
Member

Symbols or other non-strings should be converted to strings before testing the regex.

Fixes ManageIQ/manageiq-ui-classic#9357

@jrafanie jrafanie added the bug label Feb 20, 2025
@jrafanie jrafanie requested a review from Fryguy as a code owner February 20, 2025 20:16
@jrafanie jrafanie force-pushed the fix-expression-to-human-with-numeric-value branch from 07ca05e to 1e25ed3 Compare February 20, 2025 20:23
@jrafanie jrafanie force-pushed the fix-expression-to-human-with-numeric-value branch from 1e25ed3 to 5fe17ef Compare February 20, 2025 22:09
Symbols or other non-strings should be converted to strings before testing the regex.

Fixes ManageIQ/manageiq-ui-classic#9357
@jrafanie jrafanie force-pushed the fix-expression-to-human-with-numeric-value branch from 5fe17ef to 11a32fb Compare February 20, 2025 22:11
@@ -804,9 +804,10 @@ def self.quote(val, typ)
def self.quote_human(val, typ)
case typ&.to_sym
when :integer, :decimal, :fixnum, :float
return val if val.kind_of?(Numeric)
return val.to_i unless val.to_s.number_with_method? || typ == :float
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this line should have handled your specific case, so I'm not sure why it didn't.

Copy link
Member Author

Choose a reason for hiding this comment

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

unless or.. let me check it out... need a truth table

Copy link
Member Author

Choose a reason for hiding this comment

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

So, both examples are for columns that are floats columns so typ is float. I think I can just remove the || typ == :float and my first guard clause should handle it.
I believe all floats end up in on line 817/818:

else
  val
end

which should be the same as the added guard clause

Copy link
Member

@Fryguy Fryguy Feb 21, 2025

Choose a reason for hiding this comment

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

Maybe this line is just for strings, but not strings that are like 5.bytes e.g. "5". The || typ == :float hurts my brain, though.

Even so, then I'd expect the cases of "5" and 5 to be the same, returning val.to_i, and so that wouldn't fall through into the next line, so I'm confused.

@Fryguy Fryguy self-assigned this Feb 21, 2025
else
"#{val} #{sfx.titleize}"
end
/^([0-9.,]+)\.([a-z]+)$/.match(val.to_s)
Copy link
Member Author

Choose a reason for hiding this comment

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

Review this with whitespace removed. I think it simplifies it. I was surprised I could not use $1, $2 after line 808 (number_with_method?) since that returns the result of =~ anyway but alas, they were not set, perhaps when the scope returns out of that method. It feels redundant to use a nearly identical regex.

btw, any idea why they're different?
https://github.com/ManageIQ/more_core_extensions/blob/27d3e2086d26d7814a1fdeaf9ba7a7281344fab8/lib/more_core_extensions/core_ext/string/to_i_with_method.rb#L3C5-L3C64

number_with_method?

/^([0-9\.,]+)\.([a-z]+)$/

vs.

this code:

/^([0-9.,]+)\.([a-z]+)$/

Copy link
Member

Choose a reason for hiding this comment

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

Those 2 are identical. a . within [] is treated literally. Technically the former doesn't need to escape it, but I find it more explicit when it does, and is clearer to the reader, IMO.

Copy link
Member

@Fryguy Fryguy Feb 21, 2025

Choose a reason for hiding this comment

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

[1] pry(main)> "a" =~ /[.]/
=> nil
[2] pry(main)> "." =~ /[.]/
=> 0
[3] pry(main)> "a" =~ /[\.]/
=> nil
[4] pry(main)> "." =~ /[\.]/
=> 0

Copy link
Member

@Fryguy Fryguy Feb 21, 2025

Choose a reason for hiding this comment

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

I was surprised I could not use $1, $2 after line 808 (number_with_method?) since that returns the result of =~ anyway

Yeah the regex side effects are local scope, if I recall. Even so, number_with_method? should return boolean, and we should provide a separate method that returns the constituent parts (perhaps named number_with_method (no ?).

EDIT, or we can just use NUMBER_WITH_METHOD_REGEX directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants