private static ICollection <FunctionParameterToken> HandleComplexOrCollectionParameterValueIfExists(IEdmModel model, IEdmOperation operation, ICollection <FunctionParameterToken> parameterTokens, bool enableCaseInsensitive, bool enableUriTemplateParsing = false) { ICollection <FunctionParameterToken> partiallyParsedParametersWithComplexOrCollection = new Collection <FunctionParameterToken>(); foreach (FunctionParameterToken paraToken in parameterTokens) { FunctionParameterToken funcParaToken; IEdmOperationParameter functionParameter = operation.FindParameter(paraToken.ParameterName); if (enableCaseInsensitive && functionParameter == null) { functionParameter = ODataUriResolver.ResolveOpearationParameterNameCaseInsensitive(operation, paraToken.ParameterName); // The functionParameter can not be null here, else this method won't be called. funcParaToken = new FunctionParameterToken(functionParameter.Name, paraToken.ValueToken); } else { funcParaToken = paraToken; } FunctionParameterAliasToken aliasToken = funcParaToken.ValueToken as FunctionParameterAliasToken; if (aliasToken != null) { aliasToken.ExpectedParameterType = functionParameter.Type; } LiteralToken valueToken = funcParaToken.ValueToken as LiteralToken; string valueStr = null; if (valueToken != null && (valueStr = valueToken.Value as string) != null && !string.IsNullOrEmpty(valueToken.OriginalText)) { var lexer = new ExpressionLexer(valueToken.OriginalText, true /*moveToFirstToken*/, false /*useSemicolonDelimiter*/, true /*parsingFunctionParameters*/); if (lexer.CurrentToken.Kind == ExpressionTokenKind.BracketedExpression) { object result; UriTemplateExpression expression; if (enableUriTemplateParsing && UriTemplateParser.TryParseLiteral(lexer.CurrentToken.Text, functionParameter.Type, out expression)) { result = expression; } else { // ExpressionTokenKind.BracketedExpression means text like [{\"Street\":\"NE 24th St.\",\"City\":\"Redmond\"},{\"Street\":\"Pine St.\",\"City\":\"Seattle\"}] // so now try convert it into complex or collection type value: result = ODataUriUtils.ConvertFromUriLiteral(valueStr, ODataVersion.V4, model, functionParameter.Type); } LiteralToken newValueToken = new LiteralToken(result, valueToken.OriginalText); FunctionParameterToken newFuncParaToken = new FunctionParameterToken(funcParaToken.ParameterName, newValueToken); partiallyParsedParametersWithComplexOrCollection.Add(newFuncParaToken); continue; } } partiallyParsedParametersWithComplexOrCollection.Add(funcParaToken); } return(partiallyParsedParametersWithComplexOrCollection); }
public static FunctionParameterToken ShouldHaveParameter(this FunctionCallToken token, string name) { Assert.NotNull(token); FunctionParameterToken argument = token.Arguments.SingleOrDefault(arg => arg.ParameterName == name); Assert.NotNull(argument); return(argument); }
/// <summary> /// Bind a function parameter token /// </summary> /// <param name="token">The token to bind.</param> /// <returns>A semantically bound FunctionCallNode</returns> protected virtual QueryNode BindFunctionParameter(FunctionParameterToken token) { // TODO: extract this into its own binder class. if (token.ParameterName != null) { return(new NamedFunctionParameterNode(token.ParameterName, this.Bind(token.ValueToken))); } return(this.Bind(token.ValueToken)); }
private static ICollection <FunctionParameterToken> HandleComplexOrCollectionParameterValueIfExists(IEdmModel model, IEdmOperation functionOrOpertion, ICollection <FunctionParameterToken> parameterTokens, bool enableUriTemplateParsing = false) { ICollection <FunctionParameterToken> partiallyParsedParametersWithComplexOrCollection = new Collection <FunctionParameterToken>(); foreach (FunctionParameterToken funcParaToken in parameterTokens) { LiteralToken valueToken = funcParaToken.ValueToken as LiteralToken; string valueStr = null; if (valueToken != null && (valueStr = valueToken.Value as string) != null) { var lexer = new ExpressionLexer(valueStr, true /*moveToFirstToken*/, false /*useSemicolonDelimiter*/, true /*parsingFunctionParameters*/); if (lexer.CurrentToken.Kind == ExpressionTokenKind.BracketedExpression) { var functionParameter = functionOrOpertion.FindParameter(funcParaToken.ParameterName); if (functionParameter == null) { throw new ODataException(Strings.ODataParameterWriterCore_ParameterNameNotFoundInOperation(funcParaToken.ParameterName, functionOrOpertion.Name)); } object result; UriTemplateExpression expression; if (enableUriTemplateParsing && UriTemplateParser.TryParseLiteral(lexer.CurrentToken.Text, functionParameter.Type, out expression)) { result = expression; } else { // ExpressionTokenKind.BracketedExpression means text like [{\"Street\":\"NE 24th St.\",\"City\":\"Redmond\"},{\"Street\":\"Pine St.\",\"City\":\"Seattle\"}] // so now try convert it into complex or collection type value: result = ODataUriUtils.ConvertFromUriLiteral(valueStr, ODataVersion.V4, model, functionParameter.Type); } LiteralToken newValueToken = new LiteralToken(result, valueToken.OriginalText); FunctionParameterToken newFuncParaToken = new FunctionParameterToken(funcParaToken.ParameterName, newValueToken); partiallyParsedParametersWithComplexOrCollection.Add(newFuncParaToken); continue; } } partiallyParsedParametersWithComplexOrCollection.Add(funcParaToken); } return(partiallyParsedParametersWithComplexOrCollection); }
public void ParseFilterByThisInSelectWorks() { // Arrange & Act SelectToken selectToken = ParseSelectClause("RelatedSSNs($filter=endswith($this,'xyz'))"); // Assert Assert.NotNull(selectToken); SelectTermToken selectTermToken = Assert.Single(selectToken.SelectTerms); selectTermToken.PathToProperty.ShouldBeNonSystemToken("RelatedSSNs"); Assert.NotNull(selectTermToken.FilterOption); FunctionCallToken functionCallToken = (FunctionCallToken)selectTermToken.FilterOption; functionCallToken.ShouldBeFunctionCallToken("endswith"); Assert.Equal(2, functionCallToken.Arguments.Count()); FunctionParameterToken parameterToken = functionCallToken.Arguments.First(); parameterToken.ValueToken.ShouldBeRangeVariableToken(ExpressionConstants.This); }
/// <summary> /// Tries to create a parameter using any representation based on the provided delegate for creating it from a converted value. /// </summary> /// <typeparam name="TParam">The type used to represent a parameter.</typeparam> /// <param name="parameterToken">The token from the syntactic parsing step.</param> /// <param name="configuration">The configuration for the URI Parser.</param> /// <param name="expectedType">The type that the parameter is expected to resolve to.</param> /// <param name="createParameter">Callback to create the final parameter from the parsed value.</param> /// <param name="parameter">The parameter if one was successfully created.</param> /// <returns>Whether the parameter could be created from the parameterToken.</returns> private static bool TryCreateParameter <TParam>(FunctionParameterToken parameterToken, ODataUriParserConfiguration configuration, IEdmTypeReference expectedType, Func <object, TParam> createParameter, out TParam parameter) { Debug.Assert(parameterToken != null, "parameterToken != null"); QueryToken valueToken = parameterToken.ValueToken; object convertedValue; if (valueToken.Kind == QueryTokenKind.FunctionParameterAlias && configuration.FunctionParameterAliasCallback == null) { convertedValue = new ODataUnresolvedFunctionParameterAlias(((FunctionParameterAliasToken)valueToken).Alias, expectedType); } else { string textToParse; if (valueToken.Kind == QueryTokenKind.FunctionParameterAlias) { textToParse = configuration.FunctionParameterAliasCallback(((FunctionParameterAliasToken)valueToken).Alias); } else if (valueToken.Kind == QueryTokenKind.RawFunctionParameterValue) { textToParse = ((RawFunctionParameterValueToken)valueToken).RawText; } else { parameter = default(TParam); return(false); } if (textToParse == null) { convertedValue = null; } else { convertedValue = ODataUriUtils.ConvertFromUriLiteral(textToParse, ODataVersion.V3, configuration.Model, expectedType); } } parameter = createParameter(convertedValue); return(true); }
private static void VerifyFunctionParameterTokensAreEqual(FunctionParameterToken expected, FunctionParameterToken actual, AssertionHandler assert) { assert.AreEqual(expected.ParameterName, actual.ParameterName, "The Name of the function call token doesn't match the expected one."); VerifyQueryTokensAreEqual(expected.ValueToken, actual.ValueToken, assert); }
/// <summary> /// Visits a FuntionParameterToken /// </summary> /// <param name="tokenIn">The FunctionParameterToken to bind</param> /// <returns>A user defined value</returns> public virtual T Visit(FunctionParameterToken tokenIn) { throw new NotImplementedException(); }
public Expression Visit(FunctionParameterToken tokenIn) { throw new NotImplementedException(); }
private static ICollection<FunctionParameterToken> HandleComplexOrCollectionParameterValueIfExists(IEdmModel model, IEdmOperation operation, ICollection<FunctionParameterToken> parameterTokens, bool enableCaseInsensitive, bool enableUriTemplateParsing = false) { ICollection<FunctionParameterToken> partiallyParsedParametersWithComplexOrCollection = new Collection<FunctionParameterToken>(); foreach (FunctionParameterToken paraToken in parameterTokens) { FunctionParameterToken funcParaToken; IEdmOperationParameter functionParameter = operation.FindParameter(paraToken.ParameterName); if (enableCaseInsensitive && functionParameter == null) { functionParameter = ODataUriResolver.ResolveOpearationParameterNameCaseInsensitive(operation, paraToken.ParameterName); // The functionParameter can not be null here, else this method won't be called. funcParaToken = new FunctionParameterToken(functionParameter.Name, paraToken.ValueToken); } else { funcParaToken = paraToken; } FunctionParameterAliasToken aliasToken = funcParaToken.ValueToken as FunctionParameterAliasToken; if (aliasToken != null) { aliasToken.ExpectedParameterType = functionParameter.Type; } LiteralToken valueToken = funcParaToken.ValueToken as LiteralToken; string valueStr = null; if (valueToken != null && (valueStr = valueToken.Value as string) != null && !string.IsNullOrEmpty(valueToken.OriginalText)) { var lexer = new ExpressionLexer(valueToken.OriginalText, true /*moveToFirstToken*/, false /*useSemicolonDelimiter*/, true /*parsingFunctionParameters*/); if (lexer.CurrentToken.Kind == ExpressionTokenKind.BracketedExpression) { object result; UriTemplateExpression expression; if (enableUriTemplateParsing && UriTemplateParser.TryParseLiteral(lexer.CurrentToken.Text, functionParameter.Type, out expression)) { result = expression; } else { // ExpressionTokenKind.BracketedExpression means text like [{\"Street\":\"NE 24th St.\",\"City\":\"Redmond\"},{\"Street\":\"Pine St.\",\"City\":\"Seattle\"}] // so now try convert it into complex or collection type value: result = ODataUriUtils.ConvertFromUriLiteral(valueStr, ODataVersion.V4, model, functionParameter.Type); } LiteralToken newValueToken = new LiteralToken(result, valueToken.OriginalText); FunctionParameterToken newFuncParaToken = new FunctionParameterToken(funcParaToken.ParameterName, newValueToken); partiallyParsedParametersWithComplexOrCollection.Add(newFuncParaToken); continue; } } partiallyParsedParametersWithComplexOrCollection.Add(funcParaToken); } return partiallyParsedParametersWithComplexOrCollection; }
/// <summary> /// Bind a function parameter token /// </summary> /// <param name="token">The token to bind.</param> /// <returns>A semantically bound FunctionCallNode</returns> protected virtual QueryNode BindFunctionParameter(FunctionParameterToken token) { // TODO: extract this into its own binder class. if (token.ParameterName != null) { return new NamedFunctionParameterNode(token.ParameterName, this.Bind(token.ValueToken)); } return this.Bind(token.ValueToken); }
public bool Visit(FunctionParameterToken tokenIn) { throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "QueryToken of type '{0}' is not supported.", tokenIn.Kind)); }