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

Update item_property to recognize integers #7878

Merged
merged 4 commits into from Dec 10, 2019

Conversation

summerisgone
Copy link
Contributor

@summerisgone summerisgone commented Oct 26, 2019

This is a 🐛 bug fix

  • I've added tests

  • no need to update documentation

  • The test suite passes locally

Summary

Method item_property aggressively parses floats from values. In order to properly render integers I added integer check before float check. I guess better solution would be type check, but I'm afraid it might break other things.

Context

Fixes #7827

--
P.S.
Thanks for wonderful software, glad to bring my 5 cents!

Copy link
Member

@ashmaroli ashmaroli left a comment

Choose a reason for hiding this comment

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

Couple of recommendations:

  • The two regexes would best be defined as a pair of private_constant instead.
  • Prefer stashing the stringified property in a local variable instead of opening chances for multiple stringifications (e.g. when the first branch is going to evaluate to a false)
  • Prefer using String#match? over String#=~ when the logic doesn't involve reading the matchdata.

@ashmaroli ashmaroli dismissed their stale review November 1, 2019 03:51

Concerns addressed..

@ashmaroli ashmaroli requested a review from a team November 1, 2019 03:52
@parkr
Copy link
Member

parkr commented Nov 4, 2019

Two quick questions:

  1. Does the Liquid library have a method for casting to numbers that we could use instead of our own implementation?
  2. Are the Windows failures expected?

@ashmaroli
Copy link
Member

  1. Are the Windows failures expected?

Windows failures are only on GH Actions: There's a PR to patch that: #7885

@ashmaroli
Copy link
Member

  1. Does the Liquid library have a method for casting to numbers that we could use instead of our own implementation?

Liquid has a utility function:
https://github.com/Shopify/liquid/blob/57c9cf64ebc777fe5e92d4408d31a911f087eeb4/lib/liquid/utils.rb#L48-L63

    def self.to_number(obj)
      case obj
      when Float
        BigDecimal(obj.to_s)
      when Numeric
        obj
      when String
        /\A-?\d+\.\d+\z/.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
      else
        if obj.respond_to?(:to_number)
          obj.to_number
        else
          0
        end
      end
    end

@summerisgone
Copy link
Contributor Author

summerisgone commented Nov 6, 2019

@parkr @ashmaroli

Does the Liquid library have a method for casting to numbers that we could use instead of our own implementation?

Actually it is not required to cast to string or to any type at all, since data comes in typed manner from yaml files. But item_property function is used for filtering, sorting and grouping. I think in-place solution might fit better then moving cast to templates, since it would have less side effects.

@ashmaroli
Copy link
Member

ashmaroli commented Nov 6, 2019

it is not required to cast to string or to any type at all

@summerisgone The reason #parse_sort_input exists is to allow sorting of stringified integers.
For example, Ruby would sort ["15", "20", "10", "120"] into ["10", "120", "15", "20"] which is not acceptable. But if we were to cast those strings to Integer type and then sort the array, the result would be [10, 15, 20, 120], which would in turn result in the expected results for filtering, sorting or grouping.

@summerisgone
Copy link
Contributor Author

@ashmaroli what is your suggestion, to import to_number from Liquid utils instead of parse_sort_input? Shall I update the PR?
Consider it won't parse spaces, but I think it might be OK 🤷‍♂️

@ashmaroli
Copy link
Member

@summerisgone Its a public method. So you can call it directly. For example:

Liquid::Utils.to_number(input)

However, you may need to add additional tests to ensure expected results for various objects.

@summerisgone
Copy link
Contributor Author

@ashmaroli I tried to use Liquid::Utils.to_number, but it does convert all non-numbers to 0, which is not desired when sorting strings. I think it is not drop-in replacement for parse_sort_input unfortunately

@summerisgone
Copy link
Contributor Author

@ashmaroli what else blocks the merge?

@ashmaroli
Copy link
Member

@summerisgone The change looks okay.
I'm just wondering if there's a way to go about this without stringifying property..
But since the diff shows that property is already being stringified on master, you can rest assured that this will get merged.
Optimizations if any will be via a separate pull request.

@mattr-
Copy link
Member

mattr- commented Dec 10, 2019

@jekyllbot: merge +minor

@jekyllbot jekyllbot merged commit eb81dc0 into jekyll:master Dec 10, 2019
jekyllbot added a commit that referenced this pull request Dec 10, 2019
@DirtyF DirtyF added the fix label Apr 27, 2020
ashmaroli pushed a commit to ashmaroli/jekyll that referenced this pull request May 6, 2020
Update item_property to recognize integers
This backports eb81dc0 to 4.0-stable
ashmaroli added a commit that referenced this pull request May 6, 2020
Update item_property to recognize integers
This backports eb81dc0 to 4.0-stable

Co-authored-by: Ivan Gromov <summer.is.gone@gmail.com>
@jekyll jekyll locked and limited conversation to collaborators Apr 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Number-like strings are converted to floats by certain Jekyll Filters
7 participants