public static Result <string> Iif(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(RequirePropNotNull(arguments[0])); } if (prop.PropertyType != typeof(bool) && prop.PropertyType != typeof(bool?)) { return(RequirePropType(prop, "bool")); } var test = (bool?)GetPropValue(self, (string)arguments[0]) ?? false; return(test ? MacroUtil.EvalToString(arguments[1], context, self) : MacroUtil.EvalToString(arguments[2], context, self)); }
public static Result <string> CombineDeps(OneCallContext ctx) { bool shouldContinue; do { shouldContinue = false; foreach (SegmentDef def in ctx.Defs) { if (def.Enabled) { continue; } if (!ctx.Deps.Contains(def.Id)) { continue; } var result = def.TurnOn(ctx); if (result.IsFailure) { return(result.Map(r => r.Sql)); } if (result.Value.HasMore) { shouldContinue = true; } } } while (shouldContinue); return(CombineStrings(ctx)); }
public static Result <string> Iterate(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } if (!(self is IEnumerable)) { return(RequireType(self.GetType(), "IEnumerable")); } var result = new StringBuilder(); foreach (var newSelf in self as IEnumerable) { if (result.Length > 0) { result.Append(arguments[0]); } var one = MacroUtil.EvalToString(arguments[1], context, newSelf); if (one.IsFailure) { return(one); } result.Append(one.Value); } return(Result.Ok(result.ToString())); }
public static Result <string> IsNotEqual(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(Empty); } string syntax = (string)arguments[0]; var val = GetPropValue(self, syntax); var compare = arguments[1]; if (!IsEqual(val, compare)) { return(MacroUtil.EvalToString(arguments[2], context, self)); } return(Empty); }
public static Result <string> IsGreaterEqual(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(RequirePropNotNull(arguments[0])); } string syntax = (string)arguments[0]; var val = GetPropValue(self, syntax); var compare = Convert.ToDouble(arguments[1]); if (Convert.ToDouble(val) >= compare) { return(MacroUtil.EvalToString(arguments[2], context, self)); } return(Empty); }
public static Result <string> Include(OneCallContext context, string ns, object self, object[] arguments) { var contextId = (string)arguments[0]; return(context.Compiler.TryGetEmiter(contextId, ns) .OnSuccess(emiter => emiter.Emit(context.Dig(self)))); }
public void HelloWorld() { var val = "Hello World"; var actual = DynamicRuntimeMacros.Val(OneCallContext.CreateEmpty(), "", val, null); Assert.True(actual.IsSuccess); Assert.Equal(val, actual.Value); }
public void InputNullWillBeEmpty() { string val = null; var actual = DynamicRuntimeMacros.Val(OneCallContext.CreateEmpty(), "", val, null); Assert.True(actual.IsSuccess); Assert.Equal(string.Empty, actual.Value); }
public void NotExistPropShouldFail() { var val = DynamicRuntimeMacros.Iif(OneCallContext.CreateEmpty(), "", new { A = new bool?() }, new object[] { "B", "true", "false" }); Assert.False(val.IsSuccess); }
public static Result <string> Deps(OneCallContext context, string ns, object self, object[] arguments) { foreach (var dep in arguments.Select(x => (string)x)) { context.Deps.Add(dep); } return(Result.Ok(string.Empty)); }
private Result <string> CallIsNotEqual(object self, string prop, object val, string result) { if (val is int) { val = Convert.ToDouble(val); } return(DynamicRuntimeMacros.IsNotEqual(OneCallContext.CreateEmpty(), "", self, new object[] { prop, val, result })); }
public void EmptyNullShouldTreatFalsy() { var val = DynamicRuntimeMacros.Iif(OneCallContext.CreateEmpty(), "", new { A = new bool?() }, new object[] { "A", "true", "false" }); Assert.True(val.IsSuccess); Assert.Equal("false", val.Value); }
public void WillEmitPropNameWhenFail() { var val = DynamicRuntimeMacros.IsNotEmpty(OneCallContext.CreateEmpty(), "", new { A = DateTime.Now }, new object[] { "ThePropertyName", "Ok" }); Assert.True(val.IsFailure); Assert.Contains("ThePropertyName", val.Error); }
public void ValueIsNotEmpty() { var val = DynamicRuntimeMacros.IsNotEmpty(OneCallContext.CreateEmpty(), "", new { A = DateTime.Now }, new object[] { "A", "Ok" }); Assert.True(val.IsSuccess); Assert.Equal("Ok", val.Value); }
public void Smoke() { var val = DynamicRuntimeMacros.Iif(OneCallContext.CreateEmpty(), "", new { A = true }, new object[] { "A", "true", "false" }); Assert.True(val.IsSuccess); Assert.Equal("true", val.Value); }
public static Result <string> EvalToString(object value, OneCallContext context, object self) { if (value is string) { return(Result.Ok((string)value)); } if (value is EmitFunction) { return(((EmitFunction)value)(context.DigNewFragments(self))); } throw new ArgumentOutOfRangeException(nameof(value)); }
public void HelloWorld() { var code = "sql v1{Hello World}"; var parseTree = GetParseTree(code); var result = SqlEmiterUtil.CompileNamed( SdmapCompilerContext.CreateEmpty(), parseTree.namedSql()[0]); Assert.True(result.IsSuccess); var function = result.Value; var output = function(OneCallContext.CreateEmpty()); Assert.Equal("Hello World", output.Value); }
public void SqlInNamespaceTest() { var sql = "SELECT * FROM `client_WOReactive`;"; var code = "sql v1{" + sql + "}"; var parseTree = GetParseTree(code); var result = SqlEmiterUtil.CompileNamed( SdmapCompilerContext.CreateEmpty(), parseTree.namedSql()[0]); Assert.True(result.IsSuccess); var function = result.Value; var output = function(OneCallContext.CreateEmpty()); Assert.Equal(sql, output.Value); }
public static Result <string> EvalToString(object value, OneCallContext context, object self) { if (value is string str) { return(Result.Ok(str)); } if (value is EmitFunction func) { return(func(context.DigNewFragments(self))); } else if (value is Result <string> res) { return(res); } throw new ArgumentOutOfRangeException(nameof(value)); }
public static Result <string> Execute(OneCallContext context, string name, string ns, object self, object[] arguments) { if (!context.Compiler.MacroManager.Methods.TryGetValue(name, out Macro macro)) { return(Result.Fail <string>($"Macro: '{name}' cannot be found.")); } var rtCheck = RuntimeCheck(arguments, macro); if (rtCheck.IsFailure) { return(Result.Fail <string>(rtCheck.Error)); } return(macro.Method(context, ns, self, arguments)); }
public static Result <string> HasProp(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop != null) { return(MacroUtil.EvalToString(arguments[1], context, self)); } return(Empty); }
public static Result <string> Prop(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(RequirePropNotNull(arguments[0])); } return(Result.Ok(GetPropValue(self, arguments[0])?.ToString() ?? string.Empty)); }
public static Result <string> Each(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(RequirePropNotNull(arguments[0])); } string syntax = (string)arguments[0]; var val = GetPropValue(self, syntax); if (val == null) { return(Empty); } else if (!(val is IEnumerable)) { return(RequirePropType(prop, "IEnumerable")); } else { var result = new StringBuilder(); foreach (var newSelf in val as IEnumerable) { if (result.Length > 0) { result.Append(arguments[1]); } var one = MacroUtil.EvalToString(arguments[2], context, newSelf); if (one.IsFailure) { return(one); } result.Append(one.Value); } return(Result.Ok(result.ToString())); } }
public void MultiLineTest() { var sql = "SELECT \r\n" + " * \r\n" + "FROM \r\n" + " `client_WOReactive`; \r\n"; var code = $"sql v1{{{sql}}}"; var parseTree = GetParseTree(code); var result = SqlEmiterUtil.CompileNamed( SdmapCompilerContext.CreateEmpty(), parseTree.namedSql()[0]); Assert.True(result.IsSuccess); var function = result.Value; var output = function(OneCallContext.CreateEmpty()); Assert.Equal(sql, output.Value); }
public static Result <string> IsNotLike(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(Empty); } var val = GetPropValue(self, arguments[0]) as string; if (!Regex.IsMatch(val, (string)arguments[1])) { return(MacroUtil.EvalToString(arguments[2], context, self)); } return(Empty); }
public static Result <string> IsEmpty(OneCallContext context, string ns, object self, object[] arguments) { if (self == null) { return(RequireNotNull()); } var prop = GetProp(self, arguments[0]); if (prop == null) { return(RequirePropNotNull(arguments[0])); } string syntax = (string)arguments[0]; if (IsEmpty(GetPropValue(self, syntax))) { return(MacroUtil.EvalToString(arguments[1], context, self)); } return(Empty); }
public static Result <string> Def(OneCallContext context, string ns, object self, object[] arguments) { var id = (string)arguments[0]; if (arguments[1] is string sql) { var fragment = new RawSegmentDef(id, sql); context.Fragments.Add(fragment); context.Defs.Add(fragment); } else if (arguments[1] is EmitFunction emiter) { var fragment = new EmiterSegmentDef(id, emiter); context.Fragments.Add(fragment); context.Defs.Add(fragment); } else { return(Result.Fail <string>("argument 1 must be string or sql statement.")); } return(Empty); }
private Result <string> CallCompare(object self, string op, string prop, int val, string result) { switch (op) { case ">": return(DynamicRuntimeMacros.IsGreaterThan(OneCallContext.CreateEmpty(), "", self, new object[] { prop, val, result })); case "<": return(DynamicRuntimeMacros.IsLessThan(OneCallContext.CreateEmpty(), "", self, new object[] { prop, val, result })); case ">=": return(DynamicRuntimeMacros.IsGreaterEqual(OneCallContext.CreateEmpty(), "", self, new object[] { prop, val, result })); case "<=": return(DynamicRuntimeMacros.IsLessEqual(OneCallContext.CreateEmpty(), "", self, new object[] { prop, val, result })); default: throw new ArgumentOutOfRangeException(nameof(op)); } }
public static Result <string> DetectMe(OneCallContext context, string ns, object self, object[] arguments) { return(Result.Ok("Hello World")); }
private Result<string> CallEach(object self, string prop, string joiner, string text) { return DynamicRuntimeMacros.Each(OneCallContext.CreateEmpty(), "", self, new object[] { prop, joiner, text }); }