Beispiel #1
0
 public void ShouldErrorWhenIncorrectCharacterAtStart()
 {
     ExpressionLexer lexer = new ExpressionLexer("#$*@#", false, false);
     ExpressionToken resultToken;
     Exception error = null;
     bool result = lexer.TryPeekNextToken(out resultToken, out error);
     result.Should().BeFalse();
     error.Should().NotBeNull();
     error.Message.Should().Be(ODataErrorStrings.ExpressionLexer_InvalidCharacter("#", "0", "#$*@#"));
 }
Beispiel #2
0
        public void ShouldErrorWhenIncorrectCharacterAtStart()
        {
            ExpressionLexer lexer = new ExpressionLexer("#$*@#", false, false);
            ExpressionToken resultToken;
            Exception       error  = null;
            bool            result = lexer.TryPeekNextToken(out resultToken, out error);

            Assert.False(result);
            Assert.NotNull(error);
            Assert.Equal(ODataErrorStrings.ExpressionLexer_InvalidCharacter("#", "0", "#$*@#"), error.Message);
        }
Beispiel #3
0
 public void ShouldOutputTokenAndTrueWhenNoError()
 {
     ExpressionLexer lexer = new ExpressionLexer("null", false, false);
     ExpressionToken resultToken;
     Exception error = null;
     bool result = lexer.TryPeekNextToken(out resultToken, out error);
     lexer.CurrentToken.Should().NotBe(resultToken);
     result.Should().BeTrue();
     resultToken.Kind.Should().Be(ExpressionTokenKind.NullLiteral);
     error.Should().BeNull();
 }
Beispiel #4
0
        public void ShouldOutputTokenAndTrueWhenNoError()
        {
            ExpressionLexer lexer = new ExpressionLexer("null", false, false);
            ExpressionToken resultToken;
            Exception       error  = null;
            bool            result = lexer.TryPeekNextToken(out resultToken, out error);

            Assert.NotEqual(resultToken, lexer.CurrentToken);
            Assert.True(result);
            Assert.Equal(ExpressionTokenKind.NullLiteral, resultToken.Kind);
            Assert.Null(error);
        }
Beispiel #5
0
        /// <summary>
        /// Converts the given <paramref name="value"/> to a corresponding CLR type. Expects the
        /// <paramref name="value"/> to have already been properly unescaped from an actual Uri.
        /// </summary>
        /// <param name="value">Value from a Uri to be converted.</param>
        /// <param name="version">Version to be compliant with.</param>
        /// <param name="model">Optional model to perform verification against.</param>
        /// <param name="typeReference">Optional IEdmTypeReference to perform verification against.
        ///  Callers must provide a <paramref name="model"/> containing this type if it is specified.</param>
        /// <returns>A CLR object that the <paramref name="value"/> represents or an EnumNode.</returns>
        public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
        {
            ExceptionUtils.CheckArgumentNotNull(value, "value");
            if (typeReference != null && model == null)
            {
                throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeRefWithoutModel);
            }

            if (model == null)
            {
                model = Microsoft.OData.Edm.EdmCoreModel.Instance;
            }

            // Let ExpressionLexer try to get a primitive
            ExpressionLexer lexer = new ExpressionLexer(value, false /*moveToFirstToken*/, false /*useSemicolonDelimeter*/);
            Exception       error;
            ExpressionToken token;

            lexer.TryPeekNextToken(out token, out error);

            if (token.Kind == ExpressionTokenKind.BracedExpression && typeReference != null && typeReference.IsStructured())
            {
                return(ODataUriConversionUtils.ConvertFromResourceValue(value, model, typeReference));
            }

            if (token.Kind == ExpressionTokenKind.BracketedExpression)
            {
                return(ODataUriConversionUtils.ConvertFromCollectionValue(value, model, typeReference));
            }

            QueryNode enumConstNode;

            if ((token.Kind == ExpressionTokenKind.Identifier) && // then try parsing the entire text as enum value
                EnumBinder.TryBindIdentifier(lexer.ExpressionText, null, model, out enumConstNode))
            {
                return(((ConstantNode)enumConstNode).Value);
            }

            object result = lexer.ReadLiteralToken();

            // If we have a typeReference then perform verification and convert if necessary
            if (typeReference != null)
            {
                result = ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(result, value, model, typeReference);
            }

            return(result);
        }
        /// <summary>
        /// Converts the given <paramref name="value"/> to a corresponding CLR type. Expects the 
        /// <paramref name="value"/> to have already been properly unescaped from an actual Uri.
        /// </summary>
        /// <param name="value">Value from a Uri to be converted.</param>
        /// <param name="version">Version to be compliant with.</param>
        /// <param name="model">Optional model to perform verification against.</param>
        /// <param name="typeReference">Optional IEdmTypeReference to perform verification against. 
        ///  Callers must provide a <paramref name="model"/> containing this type if it is specified.</param>
        /// <returns>A CLR object that the <paramref name="value"/> represents or an EnumNode.</returns>
        public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
        {
            ExceptionUtils.CheckArgumentNotNull(value, "value");
            if (typeReference != null && model == null)
            {
                throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeRefWithoutModel);
            }

            if (model == null)
            {
                model = Microsoft.OData.Edm.Library.EdmCoreModel.Instance;
            }

            // Let ExpressionLexer try to get a primitive
            ExpressionLexer lexer = new ExpressionLexer(value, false /*moveToFirstToken*/, false /*useSemicolonDelimeter*/);
            Exception error;
            ExpressionToken token;

            lexer.TryPeekNextToken(out token, out error);

            if (token.Kind == ExpressionTokenKind.BracketedExpression)
            {
                return ODataUriConversionUtils.ConvertFromComplexOrCollectionValue(value, model, typeReference);
            }

            QueryNode enumConstNode;
            if ((token.Kind == ExpressionTokenKind.Identifier)  // then try parsing the entire text as enum value
                && EnumBinder.TryBindIdentifier(lexer.ExpressionText, null, model, out enumConstNode))
            {
                return ((ConstantNode)enumConstNode).Value;
            }

            object result = lexer.ReadLiteralToken();

            // If we have a typeReference then perform verification and convert if necessary
            if (typeReference != null)
            {
                result = ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(result, model, typeReference);
            }

            return result;
        }