Exemplo n.º 1
0
        public void CanImplementDepInMacro()
        {
            var code = "sql v1{#def<B, 'Nice'>#isNotEmptyWithDeps<A, sql {ABCDEFG}, B>}";
            var rt   = new SdmapCompiler();

            rt.AddSourceCode(code);
            string id = "isNotEmptyWithDeps";

            rt.AddMacro(id, (context, ns, self, arguments) =>
            {
                if (self == null)
                {
                    return(Result.Fail <string>($"Query requires not null in macro '{id}'."));
                }
                ;

                var prop = self.GetType().GetProperty((string)arguments[0]);
                if (prop == null)
                {
                    return(Result.Fail <string>($"Query requires property '{prop}' in macro '{id}'."));
                }

                if (!RuntimeMacros.IsEmpty(RuntimeMacros.GetPropValue(self, (string)arguments[0])))
                {
                    foreach (var dep in arguments.Skip(2).OfType <string>())
                    {
                        context.Deps.Add(dep);
                    }
                    return(((EmitFunction)arguments[1])(context.DupNewFragments()));
                }
                return(Result.Ok(string.Empty));
            });
            var result = rt.Emit("v1", new { A = new[] { 1, 2, 3 } });

            Assert.Equal("NiceABCDEFG", result);
        }
Exemplo n.º 2
0
 public static bool IsEmpty(object obj)
 => RuntimeMacros.IsEmpty(obj);