fluid icon indicating copy to clipboard operation
fluid copied to clipboard

DictionaryValue should return the item count for ToNumberValue()

Open deanebarker opened this issue 3 years ago • 9 comments

DictionaryValue.ToNumberValue() always returns 0.

https://github.com/sebastienros/fluid/blob/main/Fluid/Values/DictionaryValue.cs#L91

The underlying value is IFluidIndexable which has a Count property. It should return this from ToNumberValue(), to match the behavior of ArrayValue.

deanebarker avatar Feb 04 '22 23:02 deanebarker

DictionaryValue.ToNumberValue() always returns 0.

It's right, because Dictionary can't be converted to number, similarlly you will find ToStringValue() returns empty string

hishamco avatar Feb 05 '22 07:02 hishamco

If ArrayValue can convert to a number, why shouldn't DictionaryValue? They are both collections of items, one just happens to have keys.

deanebarker avatar Feb 05 '22 15:02 deanebarker

I disagree for ArrayValue too, @sebastienros I'm not sure why we should have array length

hishamco avatar Feb 05 '22 16:02 hishamco

For ArrayValue we could use the first element value, in case if the FluidValue is numeric

hishamco avatar Feb 05 '22 16:02 hishamco

In which context do you use this? I am trying to find the behavior in Shopify, but can't repro so far. I know I can trigger it in Fluid with {{ 1 | plus: my_array }}. In Shopify this just return NaN. Though both will work with {{ 1 | plus: my_array.size }} or {{ 1 | plus: my_array.length }}

sebastienros avatar Feb 06 '22 17:02 sebastienros

Also about ToStringValue() returning empty, this might be a bug actually. When using {{ _my_array | append: 'foo' }} it returns the same thing as {{ my_array }}foo so probably ToStringValue() should return the same thing as {{ my_dictionary }} or vice versa.

sebastienros avatar Feb 06 '22 17:02 sebastienros

I guess was just confused by the discrepancy. Why does ArrayValue return a number and DictionaryValue does not? Did we just reproduce some Shopify weirdness?

deanebarker avatar Feb 06 '22 17:02 deanebarker

If it's just the discrepancy, we might just want to fix the array one. Locally it doesn't break any tests which means there might have been no reason to return this number. However something like {{ my_array | plus: 1 }} return NaN in Shopify and LiquidJS se I wonder if we should do this or just assume 0 + 1 in this case as it's doing today.

sebastienros avatar Feb 06 '22 18:02 sebastienros

IMHO ToNumber(), ToString() should trying to convert the current value to their equivalent, no need to assume something that are not expected for the audience

hishamco avatar Feb 06 '22 19:02 hishamco