/// <summary> /// Evaluate object[index] /// </summary> /// <returns></returns> public override object DoEvaluate() { var ndxVal = IndexExp.Evaluate(); this.ListObject = VariableExp.Evaluate(); // Check for empty objects. ExceptionHelper.NotNull(this, this.ListObject, "indexing"); ExceptionHelper.NotNull(this, ndxVal, "indexing"); var lobj = (LObject)this.ListObject; // CASE 1. Access // e.g. Array: users[0] // e.g. Map: users['total'] if (!this.IsAssignment) { var result = EvalHelper.AccessIndex(this.Ctx.Methods, this, lobj, (LObject)ndxVal); return(result); } // CASE 2. Assignment // e.g. Array: users[0] = 'john' // e.g. Map: users['total'] = 200 // NOTE: In this case of assignment, return back a MemberAccess object descripting what is assign var indexAccess = new IndexAccess(); indexAccess.Instance = lobj; indexAccess.MemberName = (LObject)ndxVal; return(indexAccess); }
public void DoConvert(ParamConvertConfig c, IContext context) { var arg = c.Args.First(); var delegte1 = EvalHelper.GetDelegate(context, arg); ParamConvertUtil.StoreToParams(context, c, delegte1.DynamicInvoke()); }
public bool Continue(Context context) { var conditionSetting = GetConditionModel(context); var @delegate = EvalHelper.GetDelegate(context, conditionSetting.Eval); return((bool)@delegate.DynamicInvoke()); }
/// <summary> /// Evaluate > >= != == less less than /// </summary> /// <returns></returns> public override object DoEvaluate() { object result = null; var left = (LObject)Left.Evaluate(); var right = (LObject)Right.Evaluate(); // Both double if (left.Type == LTypes.Number && right.Type == LTypes.Number) { result = EvalHelper.CompareNumbers(this, (LNumber)left, (LNumber)right, Op); } // Both strings else if (left.Type == LTypes.String && right.Type == LTypes.String) { result = EvalHelper.CompareStrings(this, (LString)left, (LString)right, Op); } // Both bools else if (left.Type == LTypes.Bool && right.Type == LTypes.Bool) { result = EvalHelper.CompareBools(this, (LBool)left, (LBool)right, Op); } // Both dates else if (left.Type == LTypes.Date && right.Type == LTypes.Date) { result = EvalHelper.CompareDates(this, (LDate)left, (LDate)right, Op); } // Both Timespans else if (left.Type == LTypes.Time && right.Type == LTypes.Time) { result = EvalHelper.CompareTimes(this, (LTime)left, (LTime)right, Op); } // 1 or both null else if (left == LObjects.Null || right == LObjects.Null) { result = EvalHelper.CompareNull(left, right, Op); } // Day of week ? else if (left.Type == LTypes.DayOfWeek || right.Type == LTypes.DayOfWeek) { result = EvalHelper.CompareDays(this, left, right, Op); } // Units //else if (left.Type == LTypes.Unit || right.Type == LTypes.Unit) else if (left.Type.Name == "LUnit" || right.Type.Name == "LUnit") { result = EvalHelper.CompareUnits(this, (LUnit)((LClass)left).Value, (LUnit)((LClass)right).Value, Op); } return(result); }
public bool Checked(UpdateContext context, ColumnRule mc, object data, string key, string realKey) { var eval = mc.Value.ToString(); var action = context.ContentParams.CreateOrGet <string, object, Delegate>(eval, expression => { return(EvalHelper.GetDelegate(context, expression, data)); }); return((bool)action.DynamicInvoke(data)); }
public bool WhetheResultStop(Context context, object result) { var conditionSetting = GetConditionModel(context); if (string.IsNullOrEmpty(conditionSetting.ResultEval)) { return(false); } var @delegate = EvalHelper.GetDelegate(context, conditionSetting.ResultEval, result); return((bool)@delegate.DynamicInvoke(result)); }
/// <summary> /// Evaluates the binding returns a value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="binding">The binding.</param> /// <returns></returns> public static T Eval <T>(this BindingBase binding) { ArgumentValidator.NotNull(binding, "binding"); var helper = new EvalHelper(); BindingOperations.SetBinding(helper, EvalHelper.ValueProperty, binding); T result = (T)helper.GetValue(EvalHelper.ValueProperty); BindingOperations.ClearBinding(helper, EvalHelper.ValueProperty); return(result); }
internal static string BuildSql(IContext context, string oldSql, Segment segment) { var eval = EvalHelper.GetDelegate(context, segment.ArgContext); var result = (bool)eval.DynamicInvoke(); if (!result) { return(string.Empty); } var content = SegmentUtil.GetContent(oldSql, segment); return(SegmentUtil.BuildContent(context, oldSql, content, segment)); }
/// <summary> /// Evaluates the binding returns a value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="binding">The binding.</param> /// <returns></returns> public static T Eval <T>(BindingBase binding) { Contract.Requires(binding != null); var helper = new EvalHelper(); BindingOperations.SetBinding(helper, EvalHelper.ValueProperty, binding); var result = (T)helper.Value; BindingOperations.ClearBinding(helper, EvalHelper.ValueProperty); return(result); }
/// <summary> /// Returns calculated price and rounded price /// </summary> /// <param name="productID"></param> /// <param name="formula"></param> /// <param name="connectorID"></param> /// <returns></returns> public KeyValuePair <decimal, decimal> RoundPrice(int productID, string formula, int connectorID) { var f = new PriceRoundingFormulaParser(formula); var connector = _scope.Repository <Connector>().GetSingle(c => c.ConnectorID == connectorID); var calculatedPrice = CalculatePrice(productID, 1, connector, PriceRuleType.UnitPrice); var costPrice = CalculatePrice(productID, 1, connector, PriceRuleType.CostPrice); decimal price = calculatedPrice; if (costPrice == 0) { var vendorPrices = _scope.Repository <VendorAssortment>().GetSingle(c => c.ProductID == productID).VendorPrices; vendorPrices.ThrowIfNull("This product contains no vendor prices."); costPrice = vendorPrices.FirstOrDefault(c => c.MinimumQuantity == 0 || c.MinimumQuantity == 1).Price.Try(c => c.Value, 0); } costPrice.ThrowIf(c => c == 0, "The cost price of this product is 0"); if (f.IsMargin) { var margin = (calculatedPrice - costPrice) / (costPrice / 100); //apply formula and round var condition = f.Formula.Replace("m", margin.ToString()); var isTrue = (Boolean)EvalHelper.Eval(condition); if (isTrue) { //round price int p = (int)calculatedPrice; price = (decimal)p + f.RoundValue; } } else { //apply formula to fixed value && round var condition = f.Formula.Replace("x", calculatedPrice.ToString()); condition = condition.Replace("*", ((int)calculatedPrice).ToString()); //replace * with whole number var isTrue = (Boolean)EvalHelper.Eval(condition); if (isTrue) { //round price int p = (int)calculatedPrice; price = (decimal)p + f.RoundValue; } } return(new KeyValuePair <decimal, decimal>(calculatedPrice, price)); }
protected static IEnumerable<object> GetDatas(UpdateContext context, UpdateConfig config, object complexData) { string field = string.Empty; if (config.Config != null && config.Config[BatchFieldPath] != null) { field = config.Config[BatchFieldPath].ToSingleData<string>(string.Empty); } bool isArray; object inputData; if (string.IsNullOrEmpty(field)) { isArray = ReflectUtil.ReflectUtil.IsArray(complexData); inputData = complexData; } else { var array = field.Split('.'); var valueInfo = ValueGetter.GetValue(array, complexData); isArray = valueInfo.IsArray; inputData = valueInfo.Data; } IEnumerable<object> results; if (!isArray) { results = new object[] { inputData }; } else { results = (IEnumerable<object>)inputData; } var filterEval = config.Config[FilterEval].ToSingleData<string>(string.Empty); if (string.IsNullOrEmpty(filterEval) || results.Any() == false) { return results; } var action = context.ContentParams.CreateOrGet<string, object, Delegate>(filterEval, eval => { return EvalHelper.GetDelegate(context, filterEval, results.First()); }); results = results.Where(r => (bool)action.DynamicInvoke(r)).ToArray(); return results; }
private LNumber IncrementNumber(LNumber val) { this.DataType = typeof(double); var inc = this.Increment == 0 ? 1 : this.Increment; if (this.Expression != null) { var incval = this.Expression.Evaluate(); // TODO: Check if null and throw langexception? inc = ((LNumber)incval).Value; } // 1. Calculate the unary value val = EvalHelper.CalcUnary(val, Op, inc); // 2. Set the value back into scope this.Value = val; this.Ctx.Memory.SetValue(this.Name, val); return(val); }
private static void EnsureAllTypesAreHandled(IEnumerable <object[]> supportedResults, IEnumerable <object[]> unsupportedTypes, string operatorName) { var data = supportedResults.Concat(unsupportedTypes); var knownTypes = TypeCoercer.SignedIntegerLengthByType.Keys .Concat(TypeCoercer.UnsignedIntegerLengthByType.Keys) .Concat(TypeCoercer.FloatIntegerLengthByType.Keys).ToList(); var seenTypeCombinations = new HashSet <(Type, Type)>(); foreach (object[] objects in data) { Type leftType = EvalHelper.Eval((string)objects[0]).GetType(); Type rightType = EvalHelper.Eval((string)objects[1]).GetType(); seenTypeCombinations.Add((leftType, rightType)); } var unhandledTypeCombinations = new HashSet <(Type LeftType, Type RightType)>(); foreach (Type knownLeftType in knownTypes) { foreach (Type knownRightType in knownTypes) { (Type, Type)knownTypesTuple = (knownLeftType, knownRightType); if (!seenTypeCombinations.Contains(knownTypesTuple)) { unhandledTypeCombinations.Add(knownTypesTuple); } } } unhandledTypeCombinations.Should() .BeEmpty($"'{operatorName}' operator test data contains all possible type combinations"); }
public Task <ActionResult> EvalAsync( [Remainder, Description("The C# code to execute. Can be in a codeblock with C# highlighting if you want.")] string code) => Ok(async() => await EvalHelper.EvaluateAsync(Context, code), false);
/// <summary> /// Evaluate * / + - % /// </summary> /// <returns></returns> public override object DoEvaluate() { // Validate object result = 0; var left = (LObject)Left.Evaluate(); var right = (LObject)Right.Evaluate(); // Case 1: Both numbers if (this.IsTypeMatch(LTypes.Number, left, right)) { result = EvalHelper.CalcNumbers(this, (LNumber)left, (LNumber)right, Op); } // Case 2: Both times else if (this.IsTypeMatch(LTypes.Time, left, right)) { result = EvalHelper.CalcTimes(this, (LTime)left, (LTime)right, Op); } // Case 3: Both dates else if (this.IsTypeMatch(LTypes.Date, left, right)) { result = EvalHelper.CalcDates(this, (LDate)left, (LDate)right, Op); } // Case 4: Both strings. else if (this.IsTypeMatch(LTypes.String, left, right)) { var strleft = ((LString)left).Value; var strright = ((LString)right).Value; // Check string limit. Ctx.Limits.CheckStringLength(this, strleft, strright); result = new LString(strleft + strright); } // MIXED TYPES // TODO: Needs to be improved with new code for types. // Case 5 : Double and Bool else if (left.Type == LTypes.Number && right.Type == LTypes.Bool) { var r = ((LBool)right).Value; var rval = r ? 1 : 0; result = EvalHelper.CalcNumbers(this, (LNumber)left, new LNumber(rval), Op); } // Bool Double else if (left.Type == LTypes.Bool && right.Type == LTypes.Number) { var l = ((LBool)left).Value; var lval = l ? 1 : 0; result = EvalHelper.CalcNumbers(this, new LNumber(lval), (LNumber)right, Op); } // Append as strings. else if (left.Type == LTypes.String && right.Type == LTypes.Bool) { var st1 = ((LString)left).Value + ((LBool)right).Value.ToString().ToLower(); result = new LString(st1); } // Append as strings. else if (left.Type == LTypes.Bool && right.Type == LTypes.String) { var st2 = ((LBool)left).Value.ToString().ToLower() + ((LString)right).Value; result = new LString(st2); } // TODO: Need to handle LUnit and LVersion better //else if (left.Type == LTypes.Unit && right.Type == LTypes.Unit) else if (left.Type.Name == "LUnit" && right.Type.Name == "LUnit") { result = EvalHelper.CalcUnits(this, (LUnit)((LClass)left).Value, (LUnit)((LClass)right).Value, Op, Ctx.Units); } else { var st3 = left.GetValue().ToString() + right.GetValue().ToString(); result = new LString(st3); } return(result); }