protected void AddMap <TSource>(string counter, Expression <Func <IEnumerable <CounterEntry>, IEnumerable> > map)
        {
            if (string.IsNullOrWhiteSpace(counter))
            {
                throw new ArgumentException("Counter name cannot be null or whitespace.", nameof(counter));
            }
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            _maps.Add(() =>
            {
                var querySource = (typeof(TSource) == typeof(object))
                    ? "counters"
                    : IndexDefinitionHelper.GetQuerySource(Conventions, typeof(TSource), IndexSourceType.Counters);

                if (StringExtensions.IsIdentifier(counter))
                {
                    querySource = $"{querySource}.{counter}";
                }
                else
                {
                    querySource = $"{querySource}[@\"{counter.Replace("\"", "\"\"")}\"]";
                }

                return(IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TSource, TReduceResult>(map, Conventions, querySource, translateIdentityProperty: true));
            });
        }
Exemplo n.º 2
0
        public void WillTranslateReferenceToIdTo__document_id()
        {
            Expression <Func <IEnumerable <Nestable>, IEnumerable> > map = nests => from nestable in nests
                                                                           select new { nestable.Id };
            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Nestable, Nestable>(map, new DocumentConvention(), "docs", true);

            Assert.Contains("Id = nestable.__document_id", code);
        }
Exemplo n.º 3
0
        public void WillNotTranslateIdTo__document_idIfNotOnRootEntity()
        {
            Expression <Func <IEnumerable <Nestable>, IEnumerable> > map = nests => from nestable in nests
                                                                           from child in nestable.Children
                                                                           select new { child.Id };
            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Nestable, Nestable>(map, new DocumentConvention(), "docs", true);

            Assert.Contains("Id = child.Id", code);
        }
Exemplo n.º 4
0
        public void WillTranslateProperlyBothRootAndChild()
        {
            Expression <Func <IEnumerable <Nestable>, IEnumerable> > map = nests => from nestable in nests
                                                                           from child in nestable.Children
                                                                           select new { child.Id, Id2 = nestable.Id };
            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Nestable, Nestable>(map, new DocumentConvention(), "docs", true);

            Assert.Equal("docs\r\n\t.SelectMany(nestable => nestable.Children, (nestable, child) => new {Id = child.Id, Id2 = nestable.__document_id})", code);
        }
Exemplo n.º 5
0
        public void WillTranslateAnonymousArray()
        {
            Expression <Func <IEnumerable <Nestable>, IEnumerable> > map = nests => from nestable in nests
                                                                           let elements = new[] { new { Id = nestable.Id }, new { Id = nestable.Id } }
            from element in elements
                                                                     select new { Id = element.Id };
            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Nestable, Nestable>(map, new DocumentConvention(), "docs", true);

            Assert.Contains("new[]", code);
        }
        public void WillTranslateProperlyBothRootAndChild()
        {
            Expression <Func <IEnumerable <Nestable>, IEnumerable> > map = nests => from nestable in nests
                                                                           from child in nestable.Children
                                                                           select new { child.Id, Id2 = nestable.Id };
            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Nestable, Nestable>(map, new DocumentConventions(), "docs", true);

            Assert.Contains("Id = child.Id", code);
            Assert.Contains("Id2 = Id(nestable)", code);
        }
Exemplo n.º 7
0
        public void WillTranslateAnonymousArray()
        {
            Expression <Func <IEnumerable <Nestable>, IEnumerable> > map = nests => from nestable in nests
                                                                           let elements = new[] { new { Id = nestable.Id }, new { Id = nestable.Id } }
            from element in elements
                                                                     select new { Id = element.Id };
            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Nestable, Nestable>(map, new DocumentConvention(), "docs", true);

            Assert.Equal("docs\r\n\t.Select(nestable => new {nestable = nestable, elements = new []{new {Id = nestable.Id}, new {Id = nestable.Id}}})\r\n" +
                         "\t.SelectMany(__h__TransparentIdentifier2 => __h__TransparentIdentifier2.elements, (__h__TransparentIdentifier2, element) => new {Id = element.Id})", code);
        }
        public void WillProperlyCompileWhenUsingToString()
        {
            Expression <Func <IEnumerable <Coin>, IEnumerable <object> > > query = x => from y in x
                                                                                   group y by y.Denomination
                                                                                   into g
                                                                                   select
                                                                                   new
            {
                Denomination = g.Key,
                Cost         = g.First().Cost.ToString()
            };


            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Coin, Coin>(query, new DocumentConvention(), "docs", false);

            Assert.Equal("docs\r\n\t.GroupBy(y => y.Denomination)\r\n\t.Select(g => new {Denomination = g.Key, Cost = ((double)g.First().Cost).ToString()})", code);
        }
        public void WillGenerateDecimalCast()
        {
            Expression <Func <IEnumerable <Coin>, IEnumerable <object> > > query = x => from y in x
                                                                                   group y by y.Denomination
                                                                                   into g
                                                                                   select
                                                                                   new
            {
                Denomination = g.Key,
                Cost         = g.Sum(z => z.Cost)
            };


            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Coin, Coin>(query, new DocumentConvention(), "docs", false);

            Assert.Equal("docs\r\n\t.GroupBy(y => y.Denomination)\r\n\t.Select(g => new {Denomination = g.Key, Cost = g.Sum(z => ((double)z.Cost))})", code);
        }
        public void WillProperlyCompileWhenUsingToString()
        {
            Expression <Func <IEnumerable <Coin>, IEnumerable <object> > > query = x => from y in x
                                                                                   group y by y.Denomination
                                                                                   into g
                                                                                   select
                                                                                   new
            {
                Denomination = g.Key,
                Cost         = g.First().Cost.ToString()
            };


            var code = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <Coin, Coin>(query, new DocumentConventions(), "docs", false);

            Assert.Equal(LinuxTestUtils.Dos2Unix(@"docs.GroupBy(y => y.Denomination).Select(g => new {
    Denomination = g.Key,
    Cost = (DynamicEnumerable.FirstOrDefault(g)).Cost.ToString()
})"), code);
        }