Beispiel #1
0
        internal Expression Include(string templateName, string indent)
        {
            if (_includeLevel >= IncludeLimit)
            {
                throw new NustacheException(
                          string.Format("You have reached the include limit of {0}. Are you trying to render infinitely recursive templates or data?", IncludeLimit));
            }

            _includeLevel++;

            var oldIndent = _indent;

            _indent = (_indent ?? "") + (indent ?? "");

            Expression compiled = null;

            TemplateDefinition templateDefinition = GetTemplateDefinition(templateName);

            if (_includedTemplates.Contains(templateName))
            {
                throw new NustacheException("Unsupported: Compiled recursive templates will be supported in a later release");
            }
            _includedTemplates.Add(templateName);

            if (templateDefinition != null)
            {
                compiled = templateDefinition.Compile(this);
            }
            else if (templateLocator != null)
            {
                var template = templateLocator(templateName);

                if (template != null)
                {
                    compiled = template.Compile(this);
                }
            }
            _indent = oldIndent;

            _includeLevel--;

            return(compiled);
        }
Beispiel #2
0
        public void TestNestedResultsMappingAnonymousType()
        {
            TemplateDefinition nestedResultsMapping = Define.Template(
                new
            {
                Version = "2.0",
                Results =
                    Define.QueryWithNestedResults("cid",
                                                  @"select c.id as cid, sname,'2017-03-01' as LastUpdate, fname, BDAY, ADR_STREET, ADR_CITY, i.id as iid, i.inv_date, i.amount, o.article, o.id as oid
                                                    from customer c
                                                    left join orders o on c.id = o.customer_id
                                                    left join invoices i on c.id = i.customer_id
                                                    where i.amount > ${context:min_amount}"
                                                  ,
                                                  new
                {
                    Id         = Define.Column("cid"),
                    SName      = Define.Column(),
                    LastUpdate = Define.Column(),
                    Birthday   = Define.Column("BDAY"),
                    Address    = new
                    {
                        Street = Define.Column("ADR_STREET"),
                        City   = Define.Column("ADR_CITY"),
                    },
                    Invoices = Define.NestedResults("iid",
                                                    new
                    {
                        InvoiceId   = Define.Column("iid"),
                        Inv_Date    = Define.Column(),
                        TotalAmount = Define.Column("amount"),
                    }
                                                    ),
                    Orders = Define.NestedResults("oid",
                                                  new
                    {
                        OrderId     = Define.Column("oid"),
                        ArticleName = Define.Column("article"),
                    }
                                                  )
                }
                                                  )
            });

            // Compile Mapping

            var compiledMapping = nestedResultsMapping.Compile();

            // Execute Mapping

            using (var engine = new MappingEngine(() => new SqliteConnection("Data Source=Test.db"), compiledMapping))

                using (var ms = new MemoryStream())
                {
                    var context = new Dictionary <string, object>();
                    context.Add("min_amount", 100);
                    engine.ExecuteMapping(ms, context, false);
                    var json = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                    Assert.Equal(nestedResultsGeneratedJson, json);
                }
        }