public override void Run(Context context) { if (Content == null) { context.Error("Content required for Add"); context.Current = null; return; } var key = context.Current.ToString(); // does it exist? if (!context.Data.ContainsKey(key)) context.Data[key] = new List<object>(); var list = context.Data[key] as IList<object>; if (list == null) throw new NotSupportedException("Target must be a list to add to"); var sb = new StringBuilder(); var newContext = new Context(context.Data, s => sb.Append(s)); Content.Run(newContext); list.Add(sb.ToString()); context.Current = null; }
public override bool Test(Context context) { if (context.Current is bool) return ((bool)context.Current); context.Error("Current passed to True wasn't boolean"); context.Current = null; return false; }
public override bool Test(Context context) { if (Params.Count != 1) { context.Error("StartsWith requires a single parameter"); context.Current = null; return false; } var newContext = new Context(context); Params.First().Run(newContext); if (context.Current is string && newContext.Current is string) return (context.Current as string).StartsWith(newContext.Current as string); context.Error("StartsWith compares two strings"); context.Current = null; return false; }
public override void Run(Context context) { if (Params != null && Params.Count != 0) { context.Error("Params given to constant"); context.Current = null; return; } context.Current = Value; }
public override bool Test(Context context) { var type = context.Current.GetType(); if (type.IsImplementationOf(typeof(IEnumerable<>))) return ReflectUtil.GetListCount(context.Current) == 0; context.Error("Current passed to Empty wasn't Enumerable"); context.Current = null; return false; }
public override void Run(Context context) { if (Params.Count != 1) { context.Error("Each requires a single parameter"); context.Current = null; return; } if (Content == null) { context.Error("Content required for Each"); context.Current = null; return; } // key is optional var keyContext = new Context(context); Params.First().Run(keyContext); var key = keyContext.Current.ToString(); //todo: how to get IEnumerable where we don't care what it contains? if (context.Current is IEnumerable) { var list = (context.Current as IEnumerable); var index = 0; foreach (var value in list) { var newContext = new Context(context); newContext.Data[key] = value; newContext.Data[key + "_index"] = index; Content.Run(newContext); index++; } context.Current = null; return; } context.Error("Unable to enumerate " + context.Current.ToString()); context.Current = null; }
public override void Run(Context context) { if (context.Current is IAction) { var action = context.Current as IAction; var newContext = new Context(context); action.Run(newContext); context.Current = null; } if (context.Current is string) { //todo: boot up another parser and execute this string? context.Error("Executing arbitary string not supported"); context.Current = null; return; } context.Error("Execute doesn't understand " + context.Current.ToString()); context.Current = null; }
public override bool Test(Context context) { if (Params.Count != 1) { context.Error("Equal requires a single parameter"); context.Current = null; return false; } var newContext = new Context(context); Params.First().Run(newContext); var type = context.Current.GetType(); if (type.IsImplementationOf(typeof(IEnumerable<>))) return ReflectUtil.ListContains(context.Current, newContext.Current); context.Error("Current passed to Contains wasn't Enumerable"); context.Current = null; return false; }
public override void Run(Context context) { var type = context.Current.GetType(); if (!type.IsImplementationOf(typeof(IEnumerable<>))) { context.Error("Current passed to First wasn't Enumerable"); context.Current = null; return; } context.Current = ReflectUtil.GetListLast(context.Current); }
public override bool Test(Context context) { if (Params.Count != 1) { context.Error("GreaterThanOrEqual requires a single parameter"); context.Current = null; return false; } var newContext = new Context(context); Params.First().Run(newContext); var decimal1 = context.Current.ToString().ToDecimal(); var decimal2 = newContext.Current.ToString().ToDecimal(); if (decimal1.HasValue && decimal2.HasValue) return decimal1.Value >= decimal2.Value; context.Error("GreaterThanOrEqual compares two numbers"); context.Current = null; return false; }
public override bool Test(Context context) { if (Params.Count != 1) { context.Error("Equal requires a single parameter"); context.Current = null; return false; } var newContext = new Context(context); Params.First().Run(newContext); return context.Current.Equals(newContext.Current); }
public override void Run(Context context) { if (Content == null) { context.Error("Content required for Define"); context.Current = null; return; } var key = context.Current.ToString(); context.Data[key] = Content; context.Current = null; }
public override void Run(Context context) { if (Content == null) { context.Error("Content required for Assign"); context.Current = null; return; } var key = context.Current.ToString(); var sb = new StringBuilder(); var newContext = new Context(context.Data, s => sb.Append(s)); Content.Run(newContext); context.Data[key] = sb.ToString(); context.Current = null; }
public override void Run(Context context) { if (Params.Count != 1) { context.Error("Get requires a single parameter"); context.Current = null; return; } var newContext = new Context(context); Params.First().Run(newContext); var value = newContext.Current; if (value == null) { context.Error("Param executed by Get ended up null " + Params.First()); context.Current = null; return; } var type = context.Current.GetType(); if (type.IsImplementationOf(typeof(IDictionary<,>))) { try { context.Current = ReflectUtil.GetDictionaryValue(context.Current, value); return; } catch (Exception e) { context.Error(string.Format("Get could not index {0} using {1}. Exception: {2}", context.Current, value, e.Message)); context.Current = null; return; } } if (type.IsImplementationOf(typeof(IEnumerable<>))) { try { context.Current = ReflectUtil.GetListIndex(context.Current, value.ToString().ToInt().Value); return; } catch (Exception) { // continue, perhaps we are really looking for a property or method } } var propertyInfo = type.GetProperty(value.ToString()); if (propertyInfo != null) { try { context.Current = propertyInfo.GetValue(context.Current, null); return; } catch (Exception e) { context.Error(string.Format("Get could not access {0} with property {1}. Exception {2}", context.Current, propertyInfo.Name, e.Message)); context.Current = null; return; } } var fieldInfo = type.GetField(value.ToString()); if (fieldInfo != null) { try { context.Current = fieldInfo.GetValue(context.Current); return; } catch (Exception e) { context.Error(string.Format("Get could not access {0} with field {1}. Exception {2}", context.Current, fieldInfo.Name, e.Message)); context.Current = null; return; } } var methodInfo = type.GetMethod( value.ToString(), new Type[] {}); if (methodInfo != null) { try { context.Current = methodInfo.Invoke(context.Current, null); return; } catch (Exception e) { context.Error(string.Format("Get could not access {0} with method {1}. Exception {2}", context.Current, methodInfo.Name, e.Message)); context.Current = null; return; } } context.Error(string.Format("Could not 'Get' {0} from {1}", value, context.Current)); context.Current = null; }