public static DataType ConvertToTaupoPrimitiveDataType(IEdmPrimitiveTypeReference edmPrimitiveTypeReference)
        {
            PrimitiveDataType    result        = null;
            EdmPrimitiveTypeKind primitiveKind = edmPrimitiveTypeReference.PrimitiveKind();

            if (!facetlessDataTypeLookup.TryGetValue(primitiveKind, out result))
            {
                switch (primitiveKind)
                {
                case EdmPrimitiveTypeKind.Binary:
                    var edmBinary = edmPrimitiveTypeReference.AsBinary();
                    result = EdmDataTypes.Binary(edmBinary.MaxLength);
                    break;

                case EdmPrimitiveTypeKind.DateTimeOffset:
                    var edmDateTimeOffset = edmPrimitiveTypeReference.AsTemporal();
                    result = EdmDataTypes.DateTimeOffset(edmDateTimeOffset.Precision);
                    break;

                case EdmPrimitiveTypeKind.Decimal:
                    var edmDecimal = edmPrimitiveTypeReference.AsDecimal();
                    result = EdmDataTypes.Decimal(edmDecimal.Precision, edmDecimal.Scale);
                    break;

                case EdmPrimitiveTypeKind.String:
                    var edmString = edmPrimitiveTypeReference.AsString();
                    var maxLength = edmString.MaxLength;
                    if (edmString.IsUnbounded == true)
                    {
                        maxLength = MaxLengthMaxTaupoDefaultValue;
                    }

                    result = EdmDataTypes.String(maxLength, edmString.IsUnicode);
                    break;

                case EdmPrimitiveTypeKind.Duration:
                    var edmTime = edmPrimitiveTypeReference.AsTemporal();
                    result = EdmDataTypes.Time(edmTime.Precision);
                    break;

                default:
                    throw new TaupoInvalidOperationException("unexpected Edm Primitive Type Kind: " + primitiveKind);
                }
            }

            return(result.Nullable(edmPrimitiveTypeReference.IsNullable));
        }
Exemple #2
0
        /// <summary>
        /// Given the full name of an EDM primitive type, returns that type.
        /// </summary>
        /// <param name="edmTypeName">The full name of the EDM primitive type to get.</param>
        /// <returns>The EDM primitive type of the specified name; or null if no EDM primitive type of the specified name was found.</returns>
        public static PrimitiveDataType GetPrimitiveEdmType(string edmTypeName)
        {
            ExceptionUtilities.CheckArgumentNotNull(edmTypeName, "edmTypeName");

            PrimitiveDataType primitiveDataType = EdmDataTypes.GetAllPrimitiveTypes(EdmVersion.Latest).SingleOrDefault(t => t.FullEdmName() == edmTypeName);

            // OData only supports nullable versions of Edm.String and Edm.Binary
            if (string.CompareOrdinal(EdmConstants.EdmStringTypeName, edmTypeName) == 0 ||
                string.CompareOrdinal(EdmConstants.EdmBinaryTypeName, edmTypeName) == 0)
            {
                return(primitiveDataType.Nullable());
            }
            else
            {
                return(primitiveDataType);
            }
        }
        private PrimitiveDataType ResolvePrimitiveType(PrimitiveDataType dataTypeSpecification)
        {
            if (dataTypeSpecification.IsNullable)
            {
                return dataTypeSpecification;
            }

            var facet = dataTypeSpecification.GetFacet<PrimitiveClrTypeFacet>();
            if (facet != null)
            {
                var type = facet.Value;
                if (type.IsClass() || (type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {
                    return dataTypeSpecification.Nullable();
                }
            }

            return dataTypeSpecification;
        }
        private PrimitiveDataType ResolvePrimitiveType(PrimitiveDataType dataTypeSpecification)
        {
            if (dataTypeSpecification.IsNullable)
            {
                return(dataTypeSpecification);
            }

            var facet = dataTypeSpecification.GetFacet <PrimitiveClrTypeFacet>();

            if (facet != null)
            {
                var type = facet.Value;
                if (type.IsClass() || (type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable <>)))
                {
                    return(dataTypeSpecification.Nullable());
                }
            }

            return(dataTypeSpecification);
        }
Exemple #5
0
        /// <summary>
        /// Given a CLR type this methods returns the EDM primitive type for it.
        /// </summary>
        /// <param name="clrType">The CLR type to get the EDM type for.</param>
        /// <returns>EDM primitive type representing the same type as the specified <paramref name="clrType"/>.</returns>
        public static PrimitiveDataType GetPrimitiveEdmType(Type clrType)
        {
            ExceptionUtilities.CheckArgumentNotNull(clrType, "clrType");

            PrimitiveDataType primitiveDataType = null;

            if (!primitiveClrToEdmTypeMap.TryGetValue(clrType, out primitiveDataType) && typeof(ISpatial).IsAssignableFrom(clrType))
            {
                Type bestMatch = typeof(object);
                foreach (var p in primitiveClrToEdmTypeMap.Where(v => typeof(ISpatial).IsAssignableFrom(v.Key)))
                {
                    if (p.Key.IsAssignableFrom(clrType) && bestMatch.IsAssignableFrom(p.Key))
                    {
                        primitiveDataType = p.Value;
                        bestMatch         = p.Key;
                    }
                }

                if (primitiveDataType != null)
                {
                    primitiveClrToEdmTypeMap.Add(clrType, primitiveDataType);
                }
            }

            if (primitiveDataType == null)
            {
                ExceptionUtilities.Assert(false, "Can't find primitive EDM type for CLR type {0}.", clrType.ToString());
            }

            if (clrType == typeof(string) || clrType == typeof(byte[]))
            {
                return(primitiveDataType.Nullable());
            }

            return(primitiveDataType);
        }
Exemple #6
0
        public void PrimitiveTopLevelValueWithDisabledTypeConversionTest()
        {
            IEdmModel testModel = TestModels.BuildTestModel();

            IEnumerable <ReaderContentTypeTestDescriptor> testDescriptors = primitiveValueConversionTestCases
                                                                            .SelectMany(testCase => TestValues.PrimitiveTypes
                                                                                        .SelectMany(nonNullableTargetType => new bool[] { true, false }
                                                                                                    .SelectMany(includeNullableType => new bool[] { true, false }
                                                                                                                .Select(useExpectedType =>
            {
                PrimitiveDataType targetType = EntityModelUtils.GetPrimitiveEdmType(nonNullableTargetType);
                if (includeNullableType)
                {
                    targetType = targetType.Nullable();
                }

                ODataPayloadElement resultValue;
                if (nonNullableTargetType == typeof(byte[]))
                {
                    resultValue = testCase.ConversionValues.Where(cv => cv.ClrValue.GetType() == typeof(byte[])).Single().DeepCopy();
                }
                else
                {
                    resultValue = testCase.ConversionValues.Where(cv => cv.ClrValue.GetType() == typeof(string)).Single().DeepCopy();
                }

                ODataPayloadElement payloadElement;
                if (useExpectedType)
                {
                    payloadElement = PayloadBuilder.PrimitiveValue(testCase.SourceString).ExpectedPrimitiveValueType(targetType);
                }
                else
                {
                    payloadElement = PayloadBuilder.PrimitiveValue(testCase.SourceString);
                }

                return(new ReaderContentTypeTestDescriptor(this.Settings)
                {
                    PayloadElement = payloadElement,
                    ExpectedResultPayloadElement = (testConfig) => resultValue,
                    ContentType = ComputeContentType(nonNullableTargetType),
                    ExpectedFormat = ODataFormat.RawValue,
                });
            }))));

            // add variants that use a metadata provider
            testDescriptors = testDescriptors.Concat(testDescriptors.Select(td => new ReaderContentTypeTestDescriptor(td)
            {
                PayloadEdmModel = testModel
            }));

            this.CombinatorialEngineProvider.RunCombinations(
                testDescriptors,
                // restricting the set of default format configurations to limiti runtime of the tests
                this.ReaderTestConfigurationProvider.DefaultFormatConfigurations.Where(tc => tc.MessageReaderSettings.EnableMessageStreamDisposal && !tc.IsRequest),
                (testDescriptor, testConfig) =>
            {
                testConfig = new ReaderTestConfiguration(testConfig);
                testConfig.MessageReaderSettings.EnablePrimitiveTypeConversion = false;

                testDescriptor.RunTest(testConfig);
            });
        }