Skip to content

Commit

Permalink
Expose UriQueryExpressionParser.ParseFilter as a public API (#805)
Browse files Browse the repository at this point in the history
Expose UriQueryExpressionParser and QueryToken object model publicly
  • Loading branch information
m-abdelmonem authored and robward-ms committed May 2, 2017
1 parent 170827a commit 0b54111
Show file tree
Hide file tree
Showing 46 changed files with 853 additions and 97 deletions.
3 changes: 2 additions & 1 deletion src/Microsoft.OData.Client/ALinq/ResourceBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace Microsoft.OData.Client
using Microsoft.OData;
using Microsoft.OData.UriParser;
using Microsoft.OData.Edm;

using PathSegmentToken = Microsoft.OData.Client.ALinq.UriParser.PathSegmentToken;
using NonSystemToken = Microsoft.OData.Client.ALinq.UriParser.NonSystemToken;
#endregion Namespaces

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\SyntacticAst\QueryToken.cs">
<Link>ALinq\UriParser\SyntacticAst\QueryToken.cs</Link>
</Compile>
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\TreeNodeKinds\QueryTokenKind.cs">
<Link>ALinq\UriParser\TreeNodeKinds\QueryTokenKind.cs</Link>
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\SyntacticAst\QueryTokenKind.cs">
<Link>ALinq\UriParser\SyntacticAst\QueryTokenKind.cs</Link>
</Compile>
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\SyntacticAst\RangeVariableToken.cs">
<Link>ALinq\UriParser\SyntacticAst\RangeVariableToken.cs</Link>
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OData.Client/Microsoft.OData.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\SyntacticAst\QueryToken.cs">
<Link>ALinq\UriParser\SyntacticAst\QueryToken.cs</Link>
</Compile>
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\TreeNodeKinds\QueryTokenKind.cs">
<Link>ALinq\UriParser\TreeNodeKinds\QueryTokenKind.cs</Link>
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\SyntacticAst\QueryTokenKind.cs">
<Link>ALinq\UriParser\SyntacticAst\QueryTokenKind.cs</Link>
</Compile>
<Compile Include="$(EnlistmentRoot)\src\Microsoft.OData.Core\UriParser\SyntacticAst\RangeVariableToken.cs">
<Link>ALinq\UriParser\SyntacticAst\RangeVariableToken.cs</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,9 @@
<Compile Include="$(ODataCrossTargettingSourcePath)\UriParser\SyntacticAst\QueryToken.cs">
<Link>Microsoft\OData\Core\UriParser\SyntacticAst\QueryToken.cs</Link>
</Compile>
<Compile Include="$(ODataCrossTargettingSourcePath)\UriParser\SyntacticAst\QueryTokenKind.cs">
<Link>Microsoft\OData\Core\UriParser\SyntacticAst\QueryTokenKind.cs</Link>
</Compile>
<Compile Include="$(ODataCrossTargettingSourcePath)\UriParser\SyntacticAst\RangeVariableToken.cs">
<Link>Microsoft\OData\Core\UriParser\SyntacticAst\RangeVariableToken.cs</Link>
</Compile>
Expand Down Expand Up @@ -1339,9 +1342,6 @@
<Compile Include="$(ODataCrossTargettingSourcePath)\UriParser\TreeNodeKinds\QueryNodeKind.cs">
<Link>Microsoft\OData\Core\UriParser\TreeNodeKinds\QueryNodeKind.cs</Link>
</Compile>
<Compile Include="$(ODataCrossTargettingSourcePath)\UriParser\TreeNodeKinds\QueryTokenKind.cs">
<Link>Microsoft\OData\Core\UriParser\TreeNodeKinds\QueryTokenKind.cs</Link>
</Compile>
<Compile Include="$(ODataCrossTargettingSourcePath)\UriParser\TreeNodeKinds\RequestTargetKind.cs">
<Link>Microsoft\OData\Core\UriParser\TreeNodeKinds\RequestTargetKind.cs</Link>
</Compile>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/Microsoft.OData.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@
<Compile Include="UriParser\TreeNodeKinds\BinaryOperatorKind.cs" />
<Compile Include="UriParser\TreeNodeKinds\ExpressionTokenKind.cs" />
<Compile Include="UriParser\TreeNodeKinds\QueryNodeKind.cs" />
<Compile Include="UriParser\TreeNodeKinds\QueryTokenKind.cs" />
<Compile Include="UriParser\SyntacticAst\QueryTokenKind.cs" />
<Compile Include="UriParser\TreeNodeKinds\RequestTargetKind.cs" />
<Compile Include="UriParser\TreeNodeKinds\UnaryOperatorKind.cs" />
<Compile Include="UriParser\TypePromotionUtils.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ namespace Microsoft.OData.UriParser.Aggregation
#endif
using Microsoft.OData.UriParser;

internal sealed class AggregateExpressionToken : QueryToken
/// <summary>
/// Query token representing an Aggregate expression.
/// </summary>
public sealed class AggregateExpressionToken : QueryToken
{
private readonly QueryToken expression;

Expand All @@ -24,6 +27,12 @@ internal sealed class AggregateExpressionToken : QueryToken

private readonly string alias;

/// <summary>
/// Create an AggregateExpressionToken.
/// </summary>
/// <param name="expression">The aggregate expression.</param>
/// <param name="method">The aggregation method.</param>
/// <param name="alias">The alias for this query token.</param>
public AggregateExpressionToken(QueryToken expression, AggregationMethod method, string alias)
{
ExceptionUtils.CheckArgumentNotNull(expression, "expression");
Expand All @@ -34,32 +43,53 @@ public AggregateExpressionToken(QueryToken expression, AggregationMethod method,
this.alias = alias;
}

/// <summary>
/// Create an AggregateExpressionToken.
/// </summary>
/// <param name="expression">The aggregate expression.</param>
/// <param name="methodDefinition">The aggregate method definition.</param>
/// <param name="alias">The alias for this query token.</param>
public AggregateExpressionToken(QueryToken expression, AggregationMethodDefinition methodDefinition, string alias)
: this(expression, methodDefinition.MethodKind, alias)
{
this.methodDefinition = methodDefinition;
}

/// <summary>
/// Gets the kind of this token.
/// </summary>
public override QueryTokenKind Kind
{
get { return QueryTokenKind.AggregateExpression; }
}

/// <summary>
/// Gets the AggregationMethod of this token.
/// </summary>
public AggregationMethod Method
{
get { return this.method; }
}

/// <summary>
/// Gets the aggregate method definition.
/// </summary>
public AggregationMethodDefinition MethodDefinition
{
get { return this.methodDefinition; }
}

/// <summary>
/// Gets the expression.
/// </summary>
public QueryToken Expression
{
get { return this.expression; }
}

/// <summary>
/// Gets the alias.
/// </summary>
public string Alias
{
get { return this.alias; }
Expand Down
15 changes: 14 additions & 1 deletion src/Microsoft.OData.Core/UriParser/Aggregation/AggregateToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,34 @@ namespace Microsoft.OData.UriParser.Aggregation
using System.Collections.Generic;
using Microsoft.OData.UriParser;

internal sealed class AggregateToken : ApplyTransformationToken
/// <summary>
/// Query token representing an Aggregate token.
/// </summary>
public sealed class AggregateToken : ApplyTransformationToken
{
private readonly IEnumerable<AggregateExpressionToken> expressions;

/// <summary>
/// Create an AggregateToken.
/// </summary>
/// <param name="expressions">The list of AggregateExpressionToken.</param>
public AggregateToken(IEnumerable<AggregateExpressionToken> expressions)
{
ExceptionUtils.CheckArgumentNotNull(expressions, "expressions");
this.expressions = expressions;
}

/// <summary>
/// Gets the kind of this token.
/// </summary>
public override QueryTokenKind Kind
{
get { return QueryTokenKind.Aggregate; }
}

/// <summary>
/// Gets the list of AggregateExpressionToken.
/// </summary>
public IEnumerable<AggregateExpressionToken> Expressions
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.OData.UriParser.Aggregation
/// <summary>
/// Base class for Apply transformation tokens
/// </summary>
internal abstract class ApplyTransformationToken : QueryToken
public abstract class ApplyTransformationToken : QueryToken
{
}
}
18 changes: 16 additions & 2 deletions src/Microsoft.OData.Core/UriParser/Aggregation/GroupByToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ namespace Microsoft.OData.UriParser.Aggregation
using System.Collections.Generic;
using Microsoft.OData.UriParser;

internal sealed class GroupByToken : ApplyTransformationToken
/// <summary>
/// Query token representing a GroupBy token.
/// </summary>
public sealed class GroupByToken : ApplyTransformationToken
{
private readonly IEnumerable<EndPathToken> properties;

private readonly ApplyTransformationToken child;

/// <summary>
/// Create a GroupByToken.
/// </summary>
/// <param name="properties">The list of group by properties.</param>
/// <param name="child">The child of this token.</param>
public GroupByToken(IEnumerable<EndPathToken> properties, ApplyTransformationToken child)
{
ExceptionUtils.CheckArgumentNotNull(properties, "properties");
Expand All @@ -28,18 +36,24 @@ public GroupByToken(IEnumerable<EndPathToken> properties, ApplyTransformationTok
}

/// <summary>
/// The kind of the query token.
/// Gets the kind of this token.
/// </summary>
public override QueryTokenKind Kind
{
get { return QueryTokenKind.AggregateGroupBy; }
}

/// <summary>
/// Gets the list of group by properties.
/// </summary>
public IEnumerable<EndPathToken> Properties
{
get { return this.properties; }
}

/// <summary>
/// Gets the child of this token.
/// </summary>
public ApplyTransformationToken Child
{
get { return this.child; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ExpandToken NormalizePaths(ExpandToken treeToInvert)
foreach (ExpandTermToken term in treeToInvert.ExpandTerms)
{
PathReverser pathReverser = new PathReverser();
PathSegmentToken reversedPath = term.PathToNavProp.Accept(pathReverser);
PathSegmentToken reversedPath = term.PathToNavigationProp.Accept(pathReverser);

// we also need to call the select token normalizer for this level to reverse the select paths
SelectToken newSelectToken = term.SelectOption;
Expand Down Expand Up @@ -86,7 +86,7 @@ public ExpandToken CombineTerms(ExpandToken treeToCollapse)
{
ExpandToken newSubExpand = CombineTerms(termToken.ExpandOption);
finalTermToken = new ExpandTermToken(
termToken.PathToNavProp,
termToken.PathToNavigationProp,
termToken.FilterOption,
termToken.OrderByOptions,
termToken.TopOption,
Expand All @@ -112,12 +112,12 @@ public ExpandToken CombineTerms(ExpandToken treeToCollapse)
/// <returns>the combined token, or, if the two are mutually exclusive, the same tokens</returns>
public ExpandTermToken CombineTerms(ExpandTermToken existingToken, ExpandTermToken newToken)
{
Debug.Assert(new PathSegmentTokenEqualityComparer().Equals(existingToken.PathToNavProp, newToken.PathToNavProp), "Paths should be equal.");
Debug.Assert(new PathSegmentTokenEqualityComparer().Equals(existingToken.PathToNavigationProp, newToken.PathToNavigationProp), "Paths should be equal.");

List<ExpandTermToken> childNodes = CombineChildNodes(existingToken, newToken).ToList();
SelectToken combinedSelects = CombineSelects(existingToken, newToken);
return new ExpandTermToken(
existingToken.PathToNavProp,
existingToken.PathToNavigationProp,
existingToken.FilterOption,
existingToken.OrderByOptions,
existingToken.TopOption,
Expand Down Expand Up @@ -177,13 +177,13 @@ private void AddChildOptionsToDictionary(ExpandTermToken newToken, Dictionary<Pa
private void AddOrCombine(IDictionary<PathSegmentToken, ExpandTermToken> combinedTerms, ExpandTermToken expandedTerm)
{
ExpandTermToken existingTerm;
if (combinedTerms.TryGetValue(expandedTerm.PathToNavProp, out existingTerm))
if (combinedTerms.TryGetValue(expandedTerm.PathToNavigationProp, out existingTerm))
{
combinedTerms[expandedTerm.PathToNavProp] = CombineTerms(expandedTerm, existingTerm);
combinedTerms[expandedTerm.PathToNavigationProp] = CombineTerms(expandedTerm, existingTerm);
}
else
{
combinedTerms.Add(expandedTerm.PathToNavProp, expandedTerm);
combinedTerms.Add(expandedTerm.PathToNavigationProp, expandedTerm);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private SelectItem GenerateExpandItem(ExpandTermToken tokenIn)
{
ExceptionUtils.CheckArgumentNotNull(tokenIn, "tokenIn");

PathSegmentToken currentToken = tokenIn.PathToNavProp;
PathSegmentToken currentToken = tokenIn.PathToNavigationProp;

IEdmStructuredType currentLevelEntityType = this.EdmType;
List<ODataPathSegment> pathSoFar = new List<ODataPathSegment>();
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OData.Core/UriParser/NamedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.OData.UriParser
/// <summary>
/// Class representing a single named value (name and value pair).
/// </summary>
internal sealed class NamedValue
public sealed class NamedValue
{
/// <summary>
/// The name of the value. Or null if the name was not used for this value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private ExpandToken ParseCommaSeperatedExpandList(Func<IEnumerable<ExpandTermTok
List<string> explicitedTokens = new List<string>();
foreach (var tmpTokens in termTokens)
{
var pathToNav = tmpTokens.PathToNavProp;
var pathToNav = tmpTokens.PathToNavigationProp;
if (pathToNav.Identifier != UriQueryConstants.RefSegment)
{
explicitedTokens.Add(pathToNav.Identifier);
Expand All @@ -284,7 +284,7 @@ private ExpandToken ParseCommaSeperatedExpandList(Func<IEnumerable<ExpandTermTok
// Add navigation path if it is not in list yet
foreach (var tmpTokens in starTermTokens)
{
var pathToNav = tmpTokens.PathToNavProp;
var pathToNav = tmpTokens.PathToNavigationProp;
if (pathToNav.Identifier != UriQueryConstants.RefSegment && !explicitedTokens.Contains(pathToNav.Identifier))
{
termTokens.Add(tmpTokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.OData.UriParser
/// <summary>
/// Parser which consumes the query expression ($filter, $orderby) and produces the lexical object model.
/// </summary>
internal sealed class UriQueryExpressionParser
public sealed class UriQueryExpressionParser
{
/// <summary>
/// The maximum number of recursion nesting allowed.
Expand Down Expand Up @@ -55,6 +55,15 @@ internal sealed class UriQueryExpressionParser
/// </summary>
private bool enableCaseInsensitiveBuiltinIdentifier = false;

/// <summary>
/// Creates a UriQueryExpressionParser.
/// </summary>
/// <param name="maxDepth">The maximum depth of each part of the query - a recursion limit.</param>
public UriQueryExpressionParser(int maxDepth)
: this(maxDepth, false)
{
}

/// <summary>
/// Constructor.
/// </summary>
Expand Down Expand Up @@ -95,6 +104,16 @@ internal ExpressionLexer Lexer
get { return this.lexer; }
}

/// <summary>
/// Parses the $filter expression.
/// </summary>
/// <param name="filter">The $filter expression string to parse.</param>
/// <returns>The lexical token representing the filter.</returns>
public QueryToken ParseFilter(string filter)
{
return this.ParseExpressionText(filter);
}

/// <summary>
/// Parses a literal.
/// </summary>
Expand Down Expand Up @@ -186,16 +205,6 @@ internal static string GetEdmConstantNames(IEdmTypeReference edmTypeReference)
}
}

/// <summary>
/// Parses the $filter expression.
/// </summary>
/// <param name="filter">The $filter expression string to parse.</param>
/// <returns>The lexical token representing the filter.</returns>
internal QueryToken ParseFilter(string filter)
{
return this.ParseExpressionText(filter);
}

internal IEnumerable<QueryToken> ParseApply(string apply)
{
Debug.Assert(apply != null, "apply != null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.OData.UriParser
/// <summary>
/// Lexical token representing the All Query
/// </summary>
internal sealed class AllToken : LambdaToken
public sealed class AllToken : LambdaToken
{
/// <summary>
/// Create a AllToken given the expression, parameter, and parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.OData.UriParser
/// <summary>
/// Lexical token representing the Any Query
/// </summary>
internal sealed class AnyToken : LambdaToken
public sealed class AnyToken : LambdaToken
{
/// <summary>
/// Create a AnyToken given the expression, parameter, and parent
Expand Down
Loading

0 comments on commit 0b54111

Please sign in to comment.