コード例 #1
0
        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.GroupBy(y => y.Denomination).Select(g => new {
    Denomination = g.Key,
    Cost = Enumerable.Sum(g, z => ((double)((double) z.Cost)))
})", code);
        }
コード例 #2
0
        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.GroupBy(y => y.Denomination).Select(g => new {
    Denomination = g.Key,
    Cost = DynamicEnumerable.FirstOrDefault(g).Cost.ToString()
})", code);
        }
コード例 #3
0
        public async Task Format()
        {
            using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                var json = await context.ReadForMemoryAsync(RequestBodyStream(), "studio-tasks/format");

                if (json == null)
                {
                    throw new BadRequestException("No JSON was posted.");
                }

                if (json.TryGet(nameof(FormattedExpression.Expression), out string expressionAsString) == false)
                {
                    throw new BadRequestException("'Expression' property was not found.");
                }

                if (string.IsNullOrWhiteSpace(expressionAsString))
                {
                    NoContentStatus();
                    return;
                }

                var type = IndexDefinitionHelper.DetectStaticIndexType(expressionAsString, reduce: null);

                FormattedExpression formattedExpression;
                switch (type)
                {
                case IndexType.Map:
                case IndexType.MapReduce:
                    using (var workspace = new AdhocWorkspace())
                    {
                        var expression = SyntaxFactory
                                         .ParseExpression(expressionAsString)
                                         .NormalizeWhitespace();

                        var result = Formatter.Format(expression, workspace);

                        if (result.ToString().IndexOf("Could not format:", StringComparison.Ordinal) > -1)
                        {
                            throw new BadRequestException();
                        }

                        formattedExpression = new FormattedExpression
                        {
                            Expression = result.ToString()
                        };
                    }
                    break;

                case IndexType.JavaScriptMap:
                case IndexType.JavaScriptMapReduce:
                    formattedExpression = new FormattedExpression
                    {
                        Expression = JSBeautify.Apply(expressionAsString)
                    };
                    break;

                default:
                    throw new NotSupportedException($"Unknown index type '{type}'.");
                }

                await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    context.Write(writer, formattedExpression.ToJson());
                }
            }
        }
コード例 #4
0
ファイル: RavenDB-13100.cs プロジェクト: radtek/ravendb
        public void CanDetectTimeSeriesIndexSourceLinqSyntaxCanStripWhiteSpace()
        {
            string map = "\t\t  \t from    ts  \t \t in  \t \t timeSeries.Users";

            Assert.Equal(IndexSourceType.TimeSeries, IndexDefinitionHelper.DetectStaticIndexSourceType(map));
        }
コード例 #5
0
ファイル: RavenDB-13100.cs プロジェクト: radtek/ravendb
        public void CanDetectTimeSeriesIndexSourceLinqSyntaxSingleTs()
        {
            string map = "from ts in timeSeries.Users";

            Assert.Equal(IndexSourceType.TimeSeries, IndexDefinitionHelper.DetectStaticIndexSourceType(map));
        }
コード例 #6
0
ファイル: RavenDB-13100.cs プロジェクト: radtek/ravendb
        public void CanDetectDocumentsIndexSourceMethodSyntax()
        {
            string map = "docs.Users.OrderBy(user => user.Id).Select(user => new { user.Name })";

            Assert.Equal(IndexSourceType.Documents, IndexDefinitionHelper.DetectStaticIndexSourceType(map));
        }