private static LiquidExpressionResult Convert <TDest>(LiquidHash liquidHash) where TDest : ILiquidValue { var destType = typeof(TDest); // So, according to https://github.com/Shopify/liquid/wiki/Liquid-for-Designers, a hash value will be iterated // as an array with two indices. if (destType == typeof(LiquidCollection)) { var newArray = new LiquidCollection(); var dictarray = liquidHash.Keys.Select( k => (Option <ILiquidValue>) new Some <ILiquidValue>(new LiquidCollection { LiquidString.Create(k), liquidHash[k] })).ToList(); foreach (var item in dictarray) { newArray.Add(item); } return(LiquidExpressionResult.Success(newArray)); } // TODO: Should this return the default value for whatever TDest is requested? return(LiquidExpressionResult.Error("Can't convert from a LiquidHash to " + destType)); }
private static bool CheckIsEmpty(LiquidCollection val) { return(!val.Any()); }
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)); }