/// <summary> /// Maps an IEdmDelayedVAlue to an instance of an object. /// In the case that the delayedValue object represents a navigation path, /// performs additional work to map the navigation path to a delayedValue /// </summary> /// <param name="delayedValue">DelayedValue that is an element of the list</param> /// <returns>The object representation of the delayedValue</returns> private static object MapListElements(IEdmDelayedValue delayedValue) { IEdmValue edmValue; try { edmValue = delayedValue.Value; return(MapToClr(edmValue)); } catch (ArgumentNullException) { // This is a painful hack to deal with the fact that parsing the navigation // property returned a delayedValue that evaluates a NullException when you call .Value // Attempt to obtain the inner expression because we don't have access to the type of the Delayed Collection var innerExpression = delayedValue.GetPropertyByName <IEdmExpression>("Expression"); switch (innerExpression.ExpressionKind) { case EdmExpressionKind.NavigationPropertyPath: { IEdmPathExpression pe = innerExpression as IEdmPathExpression; if (pe == null) { throw new ArgumentNullException("delayedValue", "A IEdmExpression of delayedValue type NavigationPropertyPath was unable to be cast correctly to a IEdmNavigationPropertyPath"); } // Map navigation properties to strings for convenience return(string.Join("/", pe.Path)); } default: throw new NotImplementedException( string.Format("A custom mapping for ExpressionKind {0} has not yet been implemented.", innerExpression.ExpressionKind)); } } }
public DelayedCast(IEdmTypeReference targetType, IEdmDelayedValue value) { this.delayedValue = value; this.targetType = targetType; }
private static bool MatchesType(IEdmTypeReference targetType, IEdmValue operand, bool testPropertyTypes) { bool flag; IEdmTypeReference type = operand.Type; EdmValueKind valueKind = operand.ValueKind; if (type == null || valueKind == EdmValueKind.Null || !type.Definition.IsOrInheritsFrom(targetType.Definition)) { EdmValueKind edmValueKind = valueKind; switch (edmValueKind) { case EdmValueKind.Binary: { if (!targetType.IsBinary()) { break; } IEdmBinaryTypeReference edmBinaryTypeReference = targetType.AsBinary(); if (!edmBinaryTypeReference.IsUnbounded) { int?maxLength = edmBinaryTypeReference.MaxLength; if (maxLength.HasValue) { int?nullable = edmBinaryTypeReference.MaxLength; return(nullable.Value >= (int)((IEdmBinaryValue)operand).Value.Length); } } return(true); } case EdmValueKind.Boolean: { return(targetType.IsBoolean()); } case EdmValueKind.Collection: { if (!targetType.IsCollection()) { break; } IEdmTypeReference edmTypeReference = targetType.AsCollection().ElementType(); IEnumerator <IEdmDelayedValue> enumerator = ((IEdmCollectionValue)operand).Elements.GetEnumerator(); using (enumerator) { while (enumerator.MoveNext()) { IEdmDelayedValue current = enumerator.Current; if (EdmExpressionEvaluator.MatchesType(edmTypeReference, current.Value)) { continue; } flag = false; return(flag); } return(true); } return(flag); } case EdmValueKind.DateTimeOffset: { return(targetType.IsDateTimeOffset()); } case EdmValueKind.DateTime: { return(targetType.IsDateTime()); } case EdmValueKind.Decimal: { return(targetType.IsDecimal()); } case EdmValueKind.Enum: { return(((IEdmEnumValue)operand).Type.Definition.IsEquivalentTo(targetType.Definition)); } case EdmValueKind.Floating: { if (targetType.IsDouble()) { return(true); } else { if (!targetType.IsSingle()) { return(false); } else { return(EdmExpressionEvaluator.FitsInSingle(((IEdmFloatingValue)operand).Value)); } } } case EdmValueKind.Guid: { return(targetType.IsGuid()); } case EdmValueKind.Integer: { if (targetType.TypeKind() != EdmTypeKind.Primitive) { break; } EdmPrimitiveTypeKind edmPrimitiveTypeKind = targetType.AsPrimitive().PrimitiveKind(); if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Byte) { return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)0, (long)0xff)); } else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.DateTime || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.DateTimeOffset || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Decimal || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Guid) { break; } else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Double || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Int64 || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Single) { return(true); } else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Int16) { return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)-32768, (long)0x7fff)); } else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Int32) { return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)-2147483648, (long)0x7fffffff)); } else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.SByte) { return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)-128, (long)127)); } break; } case EdmValueKind.Null: { return(targetType.IsNullable); } case EdmValueKind.String: { if (!targetType.IsString()) { break; } IEdmStringTypeReference edmStringTypeReference = targetType.AsString(); if (!edmStringTypeReference.IsUnbounded) { int?maxLength1 = edmStringTypeReference.MaxLength; if (maxLength1.HasValue) { int?nullable1 = edmStringTypeReference.MaxLength; return(nullable1.Value >= ((IEdmStringValue)operand).Value.Length); } } return(true); } case EdmValueKind.Structured: { if (!targetType.IsStructured()) { break; } return(EdmExpressionEvaluator.AssertOrMatchStructuredType(targetType.AsStructured(), (IEdmStructuredValue)operand, testPropertyTypes, null)); } case EdmValueKind.Time: { return(targetType.IsTime()); } } return(false); } else { return(true); } return(true); }
/// <summary> /// Tries to convert the given value if it is of a type specific to the client library but still able to be mapped to EDM. /// </summary> /// <param name="primitiveValue">The value to convert.</param> /// <param name="type">The expected type of the value or null.</param> /// <param name="convertedValue">The converted value, if conversion was possible.</param> /// <returns>Whether or not conversion was possible.</returns> private static bool TryConvertClientSpecificPrimitiveValue(object primitiveValue, IEdmPrimitiveTypeReference type, out IEdmDelayedValue convertedValue) { byte[] byteArray; if (ClientConvert.TryConvertBinaryToByteArray(primitiveValue, out byteArray)) { type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Binary); convertedValue = new EdmBinaryConstant((IEdmBinaryTypeReference)type, byteArray); return(true); } PrimitiveType clientPrimitiveType; if (PrimitiveType.TryGetPrimitiveType(primitiveValue.GetType(), out clientPrimitiveType)) { type = EnsurePrimitiveType(type, clientPrimitiveType.PrimitiveKind); if (clientPrimitiveType.PrimitiveKind == EdmPrimitiveTypeKind.String) { { convertedValue = new EdmStringConstant((IEdmStringTypeReference)type, clientPrimitiveType.TypeConverter.ToString(primitiveValue)); return(true); } } } convertedValue = null; return(false); }
/// <summary> /// Tries to convert the given value if it is of a type specific to the client library but still able to be mapped to EDM. /// </summary> /// <param name="primitiveValue">The value to convert.</param> /// <param name="type">The expected type of the value or null.</param> /// <param name="convertedValue">The converted value, if conversion was possible.</param> /// <returns>Whether or not conversion was possible.</returns> private static bool TryConvertClientSpecificPrimitiveValue(object primitiveValue, IEdmPrimitiveTypeReference type, out IEdmDelayedValue convertedValue) { byte[] byteArray; if (ClientConvert.TryConvertBinaryToByteArray(primitiveValue, out byteArray)) { type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Binary); convertedValue = new EdmBinaryConstant((IEdmBinaryTypeReference)type, byteArray); return true; } PrimitiveType clientPrimitiveType; if (PrimitiveType.TryGetPrimitiveType(primitiveValue.GetType(), out clientPrimitiveType)) { type = EnsurePrimitiveType(type, clientPrimitiveType.PrimitiveKind); if (clientPrimitiveType.PrimitiveKind == EdmPrimitiveTypeKind.String) { { convertedValue = new EdmStringConstant((IEdmStringTypeReference)type, clientPrimitiveType.TypeConverter.ToString(primitiveValue)); return true; } } } convertedValue = null; return false; }