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); }
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); }
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()); } } }
public void CanDetectTimeSeriesIndexSourceLinqSyntaxCanStripWhiteSpace() { string map = "\t\t \t from ts \t \t in \t \t timeSeries.Users"; Assert.Equal(IndexSourceType.TimeSeries, IndexDefinitionHelper.DetectStaticIndexSourceType(map)); }
public void CanDetectTimeSeriesIndexSourceLinqSyntaxSingleTs() { string map = "from ts in timeSeries.Users"; Assert.Equal(IndexSourceType.TimeSeries, IndexDefinitionHelper.DetectStaticIndexSourceType(map)); }
public void CanDetectDocumentsIndexSourceMethodSyntax() { string map = "docs.Users.OrderBy(user => user.Id).Select(user => new { user.Name })"; Assert.Equal(IndexSourceType.Documents, IndexDefinitionHelper.DetectStaticIndexSourceType(map)); }