Beispiel #1
0
        public static Result <string> Iterate(SdmapCompilerContext 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()));
        }
Beispiel #2
0
        public static Result <string> IsNotEqual(SdmapCompilerContext 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]);
            var compare = arguments[1];

            if (!IsEqual(val, compare))
            {
                return(MacroUtil.EvalToString(arguments[2], context, self));
            }

            return(Empty);
        }
Beispiel #3
0
        public static Result <string> Iif(SdmapCompilerContext 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, arguments[0]) ?? false;

            return(test ?
                   MacroUtil.EvalToString(arguments[1], context, self) :
                   MacroUtil.EvalToString(arguments[2], context, self));
        }
Beispiel #4
0
        public void LiteralTest(string input, bool expected)
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression(input, ctx);
            var actual = func(ctx, null);

            Assert.Equal(expected, actual);
        }
Beispiel #5
0
        public void NsSyntax4Test()
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression("A.B.C.D", ctx);
            var actual = func(ctx, new { A = new { B = new { C = new { D = false } } } });

            Assert.Equal(false, actual);
        }
Beispiel #6
0
        public static Result <string> Include(SdmapCompilerContext context,
                                              string ns, object self, object[] arguments)
        {
            var contextId = (string)arguments[0];

            return(context.TryGetEmiter(contextId, ns)
                   .OnSuccess(emiter => emiter.Emit(self, context)));
        }
Beispiel #7
0
        public void AndTest(string code, bool expected)
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression(code, ctx);
            var actual = func(new OneCallContext(ctx, null));

            Assert.Equal(expected, actual);
        }
Beispiel #8
0
        public void IsNullTest(string propValue, string op, bool expected)
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression($"A {op}", ctx);
            var result = func(new OneCallContext(ctx, new { A = propValue }));

            Assert.Equal(expected, result);
        }
Beispiel #9
0
        public void ScalarTest(bool expected)
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression("A", ctx);
            var actual = func(new OneCallContext(ctx, new { A = expected }));

            Assert.Equal(expected, actual);
        }
Beispiel #10
0
        public void IsEmptyArray(string code, bool emptyArray, bool expected)
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression(code, ctx);
            var actual = func(ctx, new { A = emptyArray ? new int[] { } : new[] { 1 } });

            Assert.Equal(expected, actual);
        }
Beispiel #11
0
        public void IsNotEmptyTest()
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression("isNotEmpty(A)", ctx);
            var actual = func(ctx, new { A = new[] { 1 } });

            Assert.Equal(true, actual);
        }
Beispiel #12
0
        public void BraceTest()
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression("(A)", ctx);
            var actual = func(new OneCallContext(ctx, new { A = true }));

            Assert.Equal(true, actual);
        }
Beispiel #13
0
        public void HelloWorld()
        {
            var val    = "Hello World";
            var actual = CommonMacros.Val(SdmapCompilerContext.CreateEmpty(), "", val, null);

            Assert.True(actual.IsSuccess);
            Assert.Equal(val, actual.Value);
        }
Beispiel #14
0
        public void InputNullWillBeEmpty()
        {
            string val    = null;
            var    actual = CommonMacros.Val(SdmapCompilerContext.CreateEmpty(), "", val, null);

            Assert.True(actual.IsSuccess);
            Assert.Equal(string.Empty, actual.Value);
        }
Beispiel #15
0
        public void NsSyntax2Test()
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression("A.B", ctx);
            var actual = func(new OneCallContext(ctx, new { A = new { B = true } }));

            Assert.Equal(true, actual);
        }
Beispiel #16
0
        public void IsEmptyString(string code, string value, bool expected)
        {
            var ctx    = SdmapCompilerContext.CreateEmpty();
            var func   = CompileExpression(code, ctx);
            var actual = func(new OneCallContext(ctx, new { A = value }));

            Assert.Equal(expected, actual);
        }
Beispiel #17
0
        public void NotExistPropShouldFail()
        {
            var val = CommonMacros.Iif(SdmapCompilerContext.CreateEmpty(),
                                       "",
                                       new { A = new bool?() },
                                       new object[] { "B", "true", "false" });

            Assert.False(val.IsSuccess);
        }
Beispiel #18
0
        public static Result <EmitFunction> CompileCore(
            CoreSqlContext coreSql,
            SdmapCompilerContext context,
            string functionName)
        {
            var visitor = new CoreSqlVisitor(context);

            return(visitor.Process(coreSql, functionName)
                   .OnSuccess(() => visitor.Function));
        }
Beispiel #19
0
        public void ValueIsNotEmpty()
        {
            var val = CommonMacros.IsNotEmpty(SdmapCompilerContext.CreateEmpty(),
                                              "",
                                              new { A = DateTime.Now },
                                              new object[] { "A", "Ok" });

            Assert.True(val.IsSuccess);
            Assert.Equal("Ok", val.Value);
        }
Beispiel #20
0
        public void Smoke()
        {
            var val = CommonMacros.Iif(SdmapCompilerContext.CreateEmpty(),
                                       "",
                                       new { A = true },
                                       new object[] { "A", "true", "false" });

            Assert.True(val.IsSuccess);
            Assert.Equal("true", val.Value);
        }
Beispiel #21
0
        public void EmptyNullShouldTreatFalsy()
        {
            var val = CommonMacros.Iif(SdmapCompilerContext.CreateEmpty(),
                                       "",
                                       new { A = new bool?() },
                                       new object[] { "A", "true", "false" });

            Assert.True(val.IsSuccess);
            Assert.Equal("false", val.Value);
        }
        public void SubsqlCanCompile()
        {
            var ctx      = SdmapCompilerContext.CreateEmpty();
            var compiler = new SdmapCompiler(ctx);

            compiler.AddSourceCode("sql v1{#isNull<A, sql{test}}");

            var ok = compiler.EnsureCompiled();

            Assert.True(ok.IsSuccess);
            Assert.Equal(2, ctx.Emiters.Count);
        }
Beispiel #23
0
 public static Result <string> EvalToString(object value, SdmapCompilerContext context, object self)
 {
     if (value is string)
     {
         return(Result.Ok((string)value));
     }
     if (value is EmitFunction)
     {
         return(((EmitFunction)value)(context, self));
     }
     throw new ArgumentOutOfRangeException(nameof(value));
 }
        public void ReallyCompiled()
        {
            var ctx      = SdmapCompilerContext.CreateEmpty();
            var compiler = new SdmapCompiler(ctx);

            compiler.AddSourceCode("sql v1{Hello World}");

            Assert.True(ctx.Emiters["v1"].Emiter == null);
            var ok = compiler.EnsureCompiled();

            Assert.True(ctx.Emiters["v1"].Emiter != null);
        }
        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(SdmapCompilerContext.CreateEmpty(), null);

            Assert.Equal("Hello World", output.Value);
        }
Beispiel #26
0
        private BoolVisitorDelegate CompileExpression(string code, SdmapCompilerContext ctx)
        {
            var dm = new DynamicMethod(
                "test",
                typeof(bool),
                new[] { typeof(OneCallContext) });
            var il = dm.GetILGenerator();

            var visitOk = new BoolVisitor(il).Visit(GetParser(code).boolExpression());

            Assert.True(visitOk.IsSuccess);

            il.Emit(OpCodes.Ret);
            return((BoolVisitorDelegate)dm.CreateDelegate(typeof(BoolVisitorDelegate)));
        }
Beispiel #27
0
        public void OrShortCircuit()
        {
            var ctx  = SdmapCompilerContext.CreateEmpty();
            var func = CompileExpression("A.Value || B.Value", ctx);
            var obj  = new
            {
                A = new BoolWithAccessCount(true),
                B = new BoolWithAccessCount(false)
            };
            var actual = func(new OneCallContext(ctx, obj));

            Assert.True(actual);
            Assert.Equal(0, obj.B.AccessCount);
            Assert.Equal(1, obj.A.AccessCount);
        }
        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(SdmapCompilerContext.CreateEmpty(), null);

            Assert.Equal(sql, output.Value);
        }
Beispiel #29
0
        public static Result <string> Prop(SdmapCompilerContext 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));
        }
Beispiel #30
0
        public static Result <string> HasProp(SdmapCompilerContext 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);
        }