public PomonaQuery TransformRequest(PomonaContext context, StructuredType rootType, int? defaultTop = null)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
if (rootType == null)
throw new ArgumentNullException(nameof(rootType));
StructuredType ofType = null;
if (context.Query["$oftype"].HasValue)
ofType = (StructuredType)this.typeMapper.FromType((string)context.Query["$oftype"]);
var query = new PomonaQuery(rootType, ofType);
if (context.Query["$debug"].HasValue)
{
query.DebugInfoKeys =
new HashSet<string>(((string)context.Query["$debug"]).ToLower().Split(',').Select(x => x.Trim()));
}
string filter = null;
var top = defaultTop ?? 100;
var skip = 0;
if (context.Query["$totalcount"].HasValue && ((string)context.Query["$totalcount"]).ToLower() == "true")
query.IncludeTotalCount = true;
if (context.Query["$top"].HasValue)
top = int.Parse(context.Query["$top"]);
if (context.Query["$skip"].HasValue)
skip = int.Parse(context.Query["$skip"]);
if (context.Query["$filter"].HasValue)
filter = (string)context.Query["$filter"];
ParseFilterExpression(query, filter);
var selectSourceType = query.OfType.Type;
if (context.Query["$groupby"].HasValue)
{
var groupby = (string)context.Query["$groupby"];
ParseGroupByExpression(query, groupby);
selectSourceType =
typeof(IGrouping<,>).MakeGenericType(
query.GroupByExpression.ReturnType, selectSourceType);
}
if (context.Query["$projection"].HasValue)
{
var projectionString = (string)context.Query["$projection"];
query.Projection = projectionMap[projectionString];
QueryProjection projection;
if (!projectionMap.TryGetValue(projectionString, out projection))
{
throw new QueryParseException("\"" + projectionString +
"\" is not a valid value for query parameter $projection",
null,
QueryParseErrorReason.UnrecognizedProjection,
null);
}
query.Projection = projection;
}
else
{
query.Projection = QueryProjection.AsEnumerable;
}
if (context.Query["$select"].HasValue)
{
var select = (string)context.Query["$select"];
ParseSelect(query, select, selectSourceType);
}
if (context.Query["$orderby"].HasValue)
ParseOrderBy(query, (string)context.Query["$orderby"]);
query.Top = top;
query.Skip = skip;
if (context.Query["$expand"].HasValue)
{
// TODO: Translate expanded paths using TypeMapper
query.ExpandedPaths = ((string)context.Query["$expand"]);
}
else
query.ExpandedPaths = string.Empty;
query.Url = context.Url;
UpdateResultType(query);
return query;
}