示例#1
0
        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));
        }
示例#2
0
        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));
        }
示例#3
0
        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()));
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        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))));
        }
示例#7
0
        public void HelloWorld()
        {
            var val    = "Hello World";
            var actual = DynamicRuntimeMacros.Val(OneCallContext.CreateEmpty(), "", val, null);

            Assert.True(actual.IsSuccess);
            Assert.Equal(val, actual.Value);
        }
示例#8
0
        public void InputNullWillBeEmpty()
        {
            string val    = null;
            var    actual = DynamicRuntimeMacros.Val(OneCallContext.CreateEmpty(), "", val, null);

            Assert.True(actual.IsSuccess);
            Assert.Equal(string.Empty, actual.Value);
        }
示例#9
0
文件: IifTest.cs 项目: sdcb/sdmap
        public void NotExistPropShouldFail()
        {
            var val = DynamicRuntimeMacros.Iif(OneCallContext.CreateEmpty(),
                                               "",
                                               new { A = new bool?() },
                                               new object[] { "B", "true", "false" });

            Assert.False(val.IsSuccess);
        }
示例#10
0
 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));
 }
示例#11
0
 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 }));
 }
示例#12
0
文件: IifTest.cs 项目: sdcb/sdmap
        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);
        }
示例#13
0
        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);
        }
示例#14
0
        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);
        }
示例#15
0
文件: IifTest.cs 项目: sdcb/sdmap
        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);
        }
示例#16
0
 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));
 }
示例#17
0
        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);
        }
示例#18
0
        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);
        }
示例#19
0
 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));
 }
示例#20
0
        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));
        }
示例#21
0
        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);
        }
示例#22
0
        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));
        }
示例#23
0
        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()));
            }
        }
示例#24
0
        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);
        }
示例#25
0
        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);
        }
示例#26
0
        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);
        }
示例#27
0
        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);
        }
示例#28
0
        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));
            }
        }
示例#29
0
 public static Result <string> DetectMe(OneCallContext context, string ns, object self, object[] arguments)
 {
     return(Result.Ok("Hello World"));
 }
示例#30
0
文件: IterateTest.cs 项目: sdcb/sdmap
 private Result<string> CallEach(object self, 
     string prop, string joiner, string text)
 {
     return DynamicRuntimeMacros.Each(OneCallContext.CreateEmpty(),
         "", self, new object[] { prop, joiner, text });
 }