Пример #1
0
        private static LiquidExpressionResult Convert <TDest>(LiquidNumeric num)
            where TDest : ILiquidValue
        {
            var destType = typeof(TDest);

            if (destType == typeof(LiquidDate))
            {
                var longVal  = num.LongValue;
                var dateTime = new DateTime(longVal);
                return(LiquidExpressionResult.Success(new LiquidDate(dateTime)));
            }

            return(LiquidExpressionResult.Success(LiquidNumeric.Create(0))); // Liquid seems to convert unknowns to numeric.
        }
Пример #2
0
        /// <summary>
        /// This applies the liquid casting rules, e.g. "null is zero when LiquidNumeric" or
        /// "null is empty string when LiquidString".
        /// </summary>
        /// <typeparam name="TDest"></typeparam>
        /// <returns></returns>
        public static LiquidExpressionResult ConvertFromNull <TDest>()
            where TDest : ILiquidValue
        {
            var destType = typeof(TDest);

            if (destType == typeof(LiquidNumeric))
            {
                return(LiquidExpressionResult.Success(new Some <ILiquidValue>(LiquidNumeric.Create(0))));
            }
            if (destType == typeof(LiquidString))
            {
                return(LiquidExpressionResult.Success(LiquidString.Create(String.Empty)));
            }
            return(LiquidExpressionResult.Success(new None <ILiquidValue>()));
        }
Пример #3
0
        // TODO: this is inefficient and ugly and duplicates much of LiquidCollection
        private LiquidExpressionResult DoLookup(ITemplateContext ctx, LiquidString str, ILiquidValue indexProperty)
        {
            var    strValues          = str.StringVal.ToCharArray().Select(ch => LiquidString.Create(ch.ToString()).ToOption()).ToList();
            String propertyNameString = ValueCaster.RenderAsString(indexProperty);
            int    index;

            if (propertyNameString.ToLower().Equals("first"))
            {
                index = 0;
            }
            else if (propertyNameString.ToLower().Equals("last"))
            {
                index = strValues.Count - 1;
            }
            else if (propertyNameString.ToLower().Equals("size"))
            {
                return(LiquidExpressionResult.Success(LiquidNumeric.Create(strValues.Count)));
            }
            else
            {
                //var maybeIndexResult = ValueCaster.Cast<ILiquidValue, LiquidNumeric>(indexProperty);
                var numericIndexProperty = indexProperty as LiquidNumeric;

                if (numericIndexProperty == null)
                {
                    return(ctx.Options.ErrorWhenValueMissing ?
                           LiquidExpressionResult.Error("invalid string index: '" + propertyNameString + "'") :
                           LiquidExpressionResult.Success(new None <ILiquidValue>()));
                }
                else
                {
                    index = numericIndexProperty.IntValue;
                }
            }

            if (strValues.Count == 0)
            {
                //return LiquidExpressionResult.Error("Empty string: " + propertyNameString);
                return(LiquidExpressionResult.Success(new None <ILiquidValue>())); // not an error in Ruby liquid.
            }
            return(LiquidExpressionResult.Success(CollectionIndexer.ValueAt(strValues, index)));
        }
Пример #4
0
        private LiquidExpressionResult DoLookup(ITemplateContext ctx, LiquidHash liquidHash, ILiquidValue indexProperty)
        {
            String propertyNameString = ValueCaster.RenderAsString(indexProperty);

            if (propertyNameString.ToLower().Equals("size"))
            {
                return(LiquidExpressionResult.Success(LiquidNumeric.Create(liquidHash.Keys.Count)));
            }

            var valueAt = liquidHash.ValueAt(indexProperty.Value.ToString());

            if (valueAt.HasValue)
            {
                return(LiquidExpressionResult.Success(valueAt));
            }
            else
            {
                return(LiquidExpressionResult.ErrorOrNone(ctx, indexProperty.ToString()));
            }
        }
Пример #5
0
        // ReSharper disable once UnusedParameter.Local
        private static LiquidExpressionResult Convert <TDest>(LiquidDate liquidDate)
            where TDest : ILiquidValue
        {
            var destType = typeof(TDest);

            if (destType == typeof(LiquidNumeric))
            {
                LiquidNumeric ticks;
                if (liquidDate == null || !liquidDate.DateTimeValue.HasValue)
                {
                    ticks = LiquidNumeric.Create(0L);
                }
                else
                {
                    ticks = LiquidNumeric.Create(liquidDate.DateTimeValue.Value.Ticks);
                }
                return(LiquidExpressionResult.Success(ticks));
            }

            return(LiquidExpressionResult.Error("Can't convert from date to " + destType));
        }
Пример #6
0
        private LiquidExpressionResult DoLookup(ITemplateContext ctx, LiquidCollection liquidCollection, ILiquidValue indexProperty)
        {
            bool errorOnEmpty = ctx.Options.ErrorWhenValueMissing && liquidCollection.Count == 0;



            String propertyNameString = ValueCaster.RenderAsString(indexProperty);
            int    index;

            if (propertyNameString.ToLower().Equals("first"))
            {
                if (errorOnEmpty)
                {
                    return(LiquidExpressionResult.Error("cannot dereference empty array"));
                }
                index = 0;
            }
            else if (propertyNameString.ToLower().Equals("last"))
            {
                if (errorOnEmpty)
                {
                    return(LiquidExpressionResult.Error("cannot dereference empty array"));
                }
                index = liquidCollection.Count - 1;
            }
            else if (propertyNameString.ToLower().Equals("size"))
            {
                return(LiquidExpressionResult.Success(LiquidNumeric.Create(liquidCollection.Count)));
            }
            else
            {
                var success = Int32.TryParse(propertyNameString, out index);
                //var maybeIndexResult = ValueCaster.Cast<ILiquidValue, LiquidNumeric>(indexProperty);

                if (!success)
                {
                    if (ctx.Options.ErrorWhenValueMissing)
                    {
                        return(LiquidExpressionResult.Error("invalid index: '" + propertyNameString + "'"));
                    }
                    else
                    {
                        return(LiquidExpressionResult.Success(new None <ILiquidValue>()));// liquid seems to return nothing when non-int index.
                    }
                }

//                if (maybeIndexResult.IsError || !maybeIndexResult.SuccessResult.HasValue)
//                {
//                    return LiquidExpressionResult.Error("invalid array index: " + propertyNameString);
//                }
//                else
//                {
//                    index = maybeIndexResult.SuccessValue<LiquidNumeric>().IntValue;
//                }
            }

            if (liquidCollection.Count == 0)
            {
                return(errorOnEmpty ?
                       LiquidExpressionResult.Error("cannot dereference empty array") :
                       LiquidExpressionResult.Success(new None <ILiquidValue>()));
            }
            var result = liquidCollection.ValueAt(index);

            return(LiquidExpressionResult.Success(result));
        }
Пример #7
0
 public LiquidRange(LiquidNumeric start, LiquidNumeric end)
 {
     _start = start;
     _end   = end;
 }
Пример #8
0
 private bool Equals(LiquidNumeric nv)
 {
     return(nv != null && nv.DecimalValue == DecimalValue);
 }