public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model) { if (value == null) { value = new ODataUriNullValue(); } if (model == null) { model = EdmCoreModel.Instance; } ODataUriNullValue nullValue = value as ODataUriNullValue; if (nullValue != null) { return(ODataUriConversionUtils.ConvertToUriNullValue(nullValue, model)); } ODataCollectionValue collectionValue = value as ODataCollectionValue; if (collectionValue != null) { return(ODataUriConversionUtils.ConvertToUriCollectionLiteral(collectionValue, model, version)); } ODataComplexValue complexValue = value as ODataComplexValue; if (complexValue != null) { return(ODataUriConversionUtils.ConvertToUriComplexLiteral(complexValue, model, version)); } return(ODataUriConversionUtils.ConvertToUriPrimitiveLiteral(value, version)); }
/// <summary> /// Verifies that the given <paramref name="primitiveValue"/> is or can be coerced to <paramref name="expectedTypeReference"/>, and coerces it if necessary. /// </summary> /// <param name="primitiveValue">An EDM primitive value to verify.</param> /// <param name="model">Model to verify against.</param> /// <param name="expectedTypeReference">Expected type reference.</param> /// <param name="version">The version to use for reading.</param> /// <returns>Coerced version of the <paramref name="primitiveValue"/>.</returns> internal static object VerifyAndCoerceUriPrimitiveLiteral(object primitiveValue, IEdmModel model, IEdmTypeReference expectedTypeReference, ODataVersion version) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(primitiveValue, "primitiveValue"); ExceptionUtils.CheckArgumentNotNull(model, "model"); ExceptionUtils.CheckArgumentNotNull(expectedTypeReference, "expectedTypeReference"); // First deal with null literal ODataUriNullValue nullValue = primitiveValue as ODataUriNullValue; if (nullValue != null) { if (!expectedTypeReference.IsNullable) { throw new ODataException(o.Strings.ODataUriUtils_ConvertFromUriLiteralNullOnNonNullableType(expectedTypeReference.ODataFullName())); } IEdmType actualResolvedType = ValidationUtils.ValidateValueTypeName(model, nullValue.TypeName, expectedTypeReference.Definition.TypeKind); Debug.Assert(actualResolvedType != null, "This is a primitive-only codepath so actualResolvedType != null."); if (actualResolvedType.IsSpatial()) { ODataVersionChecker.CheckSpatialValue(version); } if (TypePromotionUtils.CanConvertTo(actualResolvedType.ToTypeReference(), expectedTypeReference)) { nullValue.TypeName = expectedTypeReference.ODataFullName(); return(nullValue); } throw new ODataException(o.Strings.ODataUriUtils_ConvertFromUriLiteralNullTypeVerificationFailure(expectedTypeReference.ODataFullName(), nullValue.TypeName)); } // Only other positive case is a numeric primitive that needs to be coerced IEdmPrimitiveTypeReference expectedPrimitiveTypeReference = expectedTypeReference.AsPrimitiveOrNull(); if (expectedPrimitiveTypeReference == null) { throw new ODataException(o.Strings.ODataUriUtils_ConvertFromUriLiteralTypeVerificationFailure(expectedTypeReference.ODataFullName(), primitiveValue)); } object coercedResult = CoerceNumericType(primitiveValue, expectedPrimitiveTypeReference.PrimitiveDefinition()); if (coercedResult != null) { return(coercedResult); } Type actualType = primitiveValue.GetType(); Type targetType = TypeUtils.GetNonNullableType(EdmLibraryExtensions.GetPrimitiveClrType(expectedPrimitiveTypeReference)); // If target type is assignable from actual type, we're OK if (targetType.IsAssignableFrom(actualType)) { return(primitiveValue); } throw new ODataException(o.Strings.ODataUriUtils_ConvertFromUriLiteralTypeVerificationFailure(expectedPrimitiveTypeReference.ODataFullName(), primitiveValue)); }
public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model) { if (value == null) { value = new ODataUriNullValue(); } if (model == null) { model = EdmCoreModel.Instance; } ODataUriNullValue nullValue = value as ODataUriNullValue; if (nullValue != null) { return ODataUriConversionUtils.ConvertToUriNullValue(nullValue, model); } ODataCollectionValue collectionValue = value as ODataCollectionValue; if (collectionValue != null) { return ODataUriConversionUtils.ConvertToUriCollectionLiteral(collectionValue, model, version); } ODataComplexValue complexValue = value as ODataComplexValue; if (complexValue != null) { return ODataUriConversionUtils.ConvertToUriComplexLiteral(complexValue, model, version); } return ODataUriConversionUtils.ConvertToUriPrimitiveLiteral(value, version); }
private object ParseNullLiteral() { this.NextToken(); ODataUriNullValue value2 = new ODataUriNullValue(); if (this.ExpressionText != "null") { int num = ("'".Length * 2) + "null".Length; int startIndex = "'".Length + "null".Length; value2.TypeName = this.ExpressionText.Substring(startIndex, this.ExpressionText.Length - num); } return(value2); }
internal static string ConvertToUriNullValue(ODataUriNullValue nullValue, IEdmModel model) { ExceptionUtils.CheckArgumentNotNull<ODataUriNullValue>(nullValue, "nullValue"); if (nullValue.TypeName != null) { ValidationUtils.ValidateTypeName(model, nullValue.TypeName); StringBuilder builder = new StringBuilder(); builder.Append("null"); builder.Append("'"); builder.Append(nullValue.TypeName); builder.Append("'"); return builder.ToString(); } return "null"; }
/// <summary> /// Converts an <see cref="ODataUriNullValue"/> to a string for use in a Url. /// </summary> /// <param name="nullValue">Instance to convert.</param> /// <returns>A string representation of <paramref name="nullValue"/> to be added to a Url.</returns> internal static string ConvertToUriNullValue(ODataUriNullValue nullValue) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(nullValue, "nullValue"); if (nullValue.TypeName != null) { StringBuilder builder = new StringBuilder(); builder.Append(ExpressionConstants.KeywordNull); builder.Append(ExpressionConstants.LiteralSingleQuote); builder.Append(nullValue.TypeName); builder.Append(ExpressionConstants.LiteralSingleQuote); return(builder.ToString()); } return(ExpressionConstants.KeywordNull); }
/// <summary> /// Parses null literals. /// </summary> /// <returns>The literal token produced by building the given literal.</returns> private object ParseNullLiteral() { Debug.Assert(this.CurrentToken.Kind == ExpressionTokenKind.NullLiteral, "this.lexer.CurrentToken.Kind == ExpressionTokenKind.NullLiteral"); this.NextToken(); ODataUriNullValue nullValue = new ODataUriNullValue(); if (this.ExpressionText == ExpressionConstants.KeywordNull) { return(nullValue); } int nullLiteralLength = ExpressionConstants.LiteralSingleQuote.Length * 2 + ExpressionConstants.KeywordNull.Length; int startOfTypeIndex = ExpressionConstants.LiteralSingleQuote.Length + ExpressionConstants.KeywordNull.Length; nullValue.TypeName = this.ExpressionText.Substring(startOfTypeIndex, this.ExpressionText.Length - nullLiteralLength); return(nullValue); }
internal static object VerifyAndCoerceUriPrimitiveLiteral(object primitiveValue, IEdmModel model, IEdmTypeReference expectedTypeReference, ODataVersion version) { ExceptionUtils.CheckArgumentNotNull<object>(primitiveValue, "primitiveValue"); ExceptionUtils.CheckArgumentNotNull<IEdmModel>(model, "model"); ExceptionUtils.CheckArgumentNotNull<IEdmTypeReference>(expectedTypeReference, "expectedTypeReference"); ODataUriNullValue value2 = primitiveValue as ODataUriNullValue; if (value2 != null) { if (!expectedTypeReference.IsNullable) { throw new ODataException(Microsoft.Data.OData.Strings.ODataUriUtils_ConvertFromUriLiteralNullOnNonNullableType(expectedTypeReference.ODataFullName())); } IEdmType type = ValidationUtils.ValidateValueTypeName(model, value2.TypeName, expectedTypeReference.Definition.TypeKind); if (type.IsSpatial()) { ODataVersionChecker.CheckSpatialValue(version); } if (!TypePromotionUtils.CanConvertTo(type.ToTypeReference(), expectedTypeReference)) { throw new ODataException(Microsoft.Data.OData.Strings.ODataUriUtils_ConvertFromUriLiteralNullTypeVerificationFailure(expectedTypeReference.ODataFullName(), value2.TypeName)); } value2.TypeName = expectedTypeReference.ODataFullName(); return value2; } IEdmPrimitiveTypeReference reference = expectedTypeReference.AsPrimitiveOrNull(); if (reference == null) { throw new ODataException(Microsoft.Data.OData.Strings.ODataUriUtils_ConvertFromUriLiteralTypeVerificationFailure(expectedTypeReference.ODataFullName(), primitiveValue)); } object obj2 = CoerceNumericType(primitiveValue, reference.PrimitiveDefinition()); if (obj2 != null) { return obj2; } Type c = primitiveValue.GetType(); if (!TypeUtils.GetNonNullableType(EdmLibraryExtensions.GetPrimitiveClrType(reference)).IsAssignableFrom(c)) { throw new ODataException(Microsoft.Data.OData.Strings.ODataUriUtils_ConvertFromUriLiteralTypeVerificationFailure(reference.ODataFullName(), primitiveValue)); } return primitiveValue; }
private object ParseNullLiteral() { this.NextToken(); ODataUriNullValue value2 = new ODataUriNullValue(); if (this.ExpressionText != "null") { int num = ("'".Length * 2) + "null".Length; int startIndex = "'".Length + "null".Length; value2.TypeName = this.ExpressionText.Substring(startIndex, this.ExpressionText.Length - num); } return value2; }
/// <summary> /// Parses null literals. /// </summary> /// <returns>The literal token produced by building the given literal.</returns> private object ParseNullLiteral() { Debug.Assert(this.CurrentToken.Kind == ExpressionTokenKind.NullLiteral, "this.lexer.CurrentToken.Kind == ExpressionTokenKind.NullLiteral"); this.NextToken(); ODataUriNullValue nullValue = new ODataUriNullValue(); if (this.ExpressionText == ExpressionConstants.KeywordNull) { return nullValue; } int nullLiteralLength = ExpressionConstants.LiteralSingleQuote.Length * 2 + ExpressionConstants.KeywordNull.Length; int startOfTypeIndex = ExpressionConstants.LiteralSingleQuote.Length + ExpressionConstants.KeywordNull.Length; nullValue.TypeName = this.ExpressionText.Substring(startOfTypeIndex, this.ExpressionText.Length - nullLiteralLength); return nullValue; }