Exemple #1
0
        /// <summary>
        /// Validates that a given primitive value is of the expected (primitive) type.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="valuePrimitiveTypeReference">The primitive type reference for the value - some callers have this already, so we save the lookup here.</param>
        /// <param name="expectedTypeReference">The expected type for the value.</param>
        /// <remarks>
        /// Some callers have the primitive type reference already resolved (from the value type)
        /// so this method is an optimized version to not lookup the primitive type reference again.
        /// </remarks>
        internal static void ValidateIsExpectedPrimitiveType(object value, IEdmPrimitiveTypeReference valuePrimitiveTypeReference, IEdmTypeReference expectedTypeReference)
        {
            Debug.Assert(value != null, "value != null");
            Debug.Assert(expectedTypeReference != null, "expectedTypeReference != null");

            if (valuePrimitiveTypeReference == null)
            {
                throw new ODataException(Strings.ValidationUtils_UnsupportedPrimitiveType(value.GetType().FullName));
            }

            Debug.Assert(valuePrimitiveTypeReference.IsEquivalentTo(EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType())), "The value and valuePrimitiveTypeReference don't match.");
            if (!expectedTypeReference.IsODataPrimitiveTypeKind() && !expectedTypeReference.IsODataTypeDefinitionTypeKind())
            {
                // non-primitive type found for primitive value.
                throw new ODataException(Strings.ValidationUtils_NonPrimitiveTypeForPrimitiveValue(expectedTypeReference.FullName()));
            }

            ValidateMetadataPrimitiveType(expectedTypeReference, valuePrimitiveTypeReference);
        }
        private static bool IsEquivalentTo(this IEdmPrimitiveTypeReference thisType, IEdmPrimitiveTypeReference otherType)
        {
            EdmPrimitiveTypeKind edmPrimitiveTypeKind = thisType.PrimitiveKind();

            if (edmPrimitiveTypeKind == otherType.PrimitiveKind())
            {
                EdmPrimitiveTypeKind edmPrimitiveTypeKind1 = edmPrimitiveTypeKind;
                switch (edmPrimitiveTypeKind1)
                {
                case EdmPrimitiveTypeKind.Binary:
                {
                    return(thisType.IsEquivalentTo((IEdmBinaryTypeReference)otherType));
                }

                case EdmPrimitiveTypeKind.Boolean:
                case EdmPrimitiveTypeKind.Byte:
                {
                    if (edmPrimitiveTypeKind.IsSpatial())
                    {
                        break;
                    }
                    if (thisType.IsNullable != otherType.IsNullable)
                    {
                        return(false);
                    }
                    else
                    {
                        return(thisType.Definition.IsEquivalentTo(otherType.Definition));
                    }
                }

                case EdmPrimitiveTypeKind.DateTime:
                case EdmPrimitiveTypeKind.DateTimeOffset:
                {
                    return(thisType.IsEquivalentTo((IEdmTemporalTypeReference)otherType));
                }

                case EdmPrimitiveTypeKind.Decimal:
                {
                    return(thisType.IsEquivalentTo((IEdmDecimalTypeReference)otherType));
                }

                default:
                {
                    if (edmPrimitiveTypeKind1 == EdmPrimitiveTypeKind.String)
                    {
                        return(thisType.IsEquivalentTo((IEdmStringTypeReference)otherType));
                    }
                    else if (edmPrimitiveTypeKind1 == EdmPrimitiveTypeKind.Stream)
                    {
                        if (edmPrimitiveTypeKind.IsSpatial())
                        {
                            break;
                        }
                        if (thisType.IsNullable != otherType.IsNullable)
                        {
                            return(false);
                        }
                        else
                        {
                            return(thisType.Definition.IsEquivalentTo(otherType.Definition));
                        }
                    }
                    else if (edmPrimitiveTypeKind1 == EdmPrimitiveTypeKind.Time)
                    {
                        return(thisType.IsEquivalentTo((IEdmTemporalTypeReference)otherType));
                    }
                    if (edmPrimitiveTypeKind.IsSpatial())
                    {
                        break;
                    }
                    if (thisType.IsNullable != otherType.IsNullable)
                    {
                        return(false);
                    }
                    else
                    {
                        return(thisType.Definition.IsEquivalentTo(otherType.Definition));
                    }
                }
                }
                return(thisType.IsEquivalentTo((IEdmSpatialTypeReference)otherType));
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Validates that a given primitive value is of the expected (primitive) type.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="valuePrimitiveTypeReference">The primitive type reference for the value - some callers have this already, so we save the lookup here.</param>
        /// <param name="expectedTypeReference">The expected type for the value.</param>
        /// <param name="bypassValidation">Bypass the validation if it is true.</param>
        /// <remarks>
        /// Some callers have the primitive type reference already resolved (from the value type)
        /// so this method is an optimized version to not lookup the primitive type reference again.
        /// </remarks>
        internal static void ValidateIsExpectedPrimitiveType(object value, IEdmPrimitiveTypeReference valuePrimitiveTypeReference, IEdmTypeReference expectedTypeReference, bool bypassValidation = false)
        {
            Debug.Assert(value != null, "value != null");
            Debug.Assert(expectedTypeReference != null, "expectedTypeReference != null");

            if (bypassValidation)
            {
                return;
            }

            if (valuePrimitiveTypeReference == null)
            {
                throw new ODataException(Strings.ValidationUtils_UnsupportedPrimitiveType(value.GetType().FullName));
            }

            Debug.Assert(valuePrimitiveTypeReference.IsEquivalentTo(EdmLibraryExtensions.GetPrimitiveTypeReference(value.GetType())), "The value and valuePrimitiveTypeReference don't match.");
            if (!expectedTypeReference.IsODataPrimitiveTypeKind() && !expectedTypeReference.IsODataTypeDefinitionTypeKind())
            {
                // non-primitive type found for primitive value.
                throw new ODataException(Strings.ValidationUtils_NonPrimitiveTypeForPrimitiveValue(expectedTypeReference.ODataFullName()));
            }

            ValidateMetadataPrimitiveType(expectedTypeReference, valuePrimitiveTypeReference);
        }