private static void AppendBinaryFacets(this StringBuilder sb, IEdmBinaryTypeReference type)
        {
            string str;
            bool?  isFixedLength = type.IsFixedLength;

            sb.AppendKeyValue("FixedLength", isFixedLength.ToString());
            if (!type.IsUnbounded)
            {
                int?maxLength = type.MaxLength;
                if (!maxLength.HasValue)
                {
                    return;
                }
            }
            StringBuilder stringBuilder = sb;
            string        str1          = "MaxLength";

            if (type.IsUnbounded)
            {
                str = "Max";
            }
            else
            {
                int?nullable = type.MaxLength;
                str = nullable.ToString();
            }
            stringBuilder.AppendKeyValue(str1, str);
        }
예제 #2
0
 private static bool TryAssertBinaryConstantAsType(IEdmBinaryConstantExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
 {
     if (type.IsBinary())
     {
         IEdmBinaryTypeReference edmBinaryTypeReference = type.AsBinary();
         int?maxLength = edmBinaryTypeReference.MaxLength;
         if (maxLength.HasValue)
         {
             int?nullable = edmBinaryTypeReference.MaxLength;
             if ((int)expression.Value.Length > nullable.Value)
             {
                 EdmError[] edmError   = new EdmError[1];
                 int?       maxLength1 = edmBinaryTypeReference.MaxLength;
                 edmError[0]      = new EdmError(expression.Location(), EdmErrorCode.BinaryConstantLengthOutOfRange, Strings.EdmModel_Validator_Semantic_BinaryConstantLengthOutOfRange((int)expression.Value.Length, maxLength1.Value));
                 discoveredErrors = edmError;
                 return(false);
             }
         }
         discoveredErrors = Enumerable.Empty <EdmError>();
         return(true);
     }
     else
     {
         EdmError[] edmErrorArray = new EdmError[1];
         edmErrorArray[0] = new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType);
         discoveredErrors = edmErrorArray;
         return(false);
     }
 }
예제 #3
0
 private static bool IsEquivalentTo(this IEdmBinaryTypeReference thisType, IEdmBinaryTypeReference otherType)
 {
     return(thisType != null && otherType != null &&
            thisType.IsNullable == otherType.IsNullable &&
            thisType.IsUnbounded == otherType.IsUnbounded &&
            thisType.MaxLength == otherType.MaxLength);
 }
예제 #4
0
        public void ConstructableTypeReferenceToStringTest()
        {
            IEdmEntityType  astonishing  = new EdmEntityType("AwesomeNamespace", "AstonishingEntity", null, false, false);
            IEdmComplexType breathTaking = new EdmComplexType("AwesomeNamespace", "BreathtakingComplex", null, false);

            IEdmEntityTypeReference          entity      = new EdmEntityTypeReference(astonishing, true);
            IEdmComplexTypeReference         complex     = new EdmComplexTypeReference(breathTaking, true);
            IEdmPrimitiveTypeReference       primitive   = EdmCoreModel.Instance.GetInt32(true);
            IEdmStringTypeReference          stringType  = EdmCoreModel.Instance.GetString(false, 128, false, true);
            IEdmBinaryTypeReference          binary      = EdmCoreModel.Instance.GetBinary(true, null, true);
            IEdmTemporalTypeReference        temporal    = EdmCoreModel.Instance.GetTemporal(EdmPrimitiveTypeKind.DateTimeOffset, 1, true);
            IEdmDecimalTypeReference         decimalType = EdmCoreModel.Instance.GetDecimal(3, 2, true);
            IEdmSpatialTypeReference         spatial     = EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.Geography, 1, true);
            IEdmEntityReferenceTypeReference entityRef   = new EdmEntityReferenceTypeReference(new EdmEntityReferenceType(astonishing), true);
            IEdmCollectionTypeReference      collection  = EdmCoreModel.GetCollection(EdmCoreModel.Instance.GetInt32(true));
            IEdmTypeReference type = new EdmEntityTypeReference(astonishing, true);

            Assert.AreEqual("[AwesomeNamespace.AstonishingEntity Nullable=True]", entity.ToString(), "To string correct");
            Assert.AreEqual("[AwesomeNamespace.BreathtakingComplex Nullable=True]", complex.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Int32 Nullable=True]", primitive.ToString(), "To string correct");
            Assert.AreEqual("[Edm.String Nullable=True MaxLength=128 Unicode=False]", stringType.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Binary Nullable=True MaxLength=max]", binary.ToString(), "To string correct");
            Assert.AreEqual("[Edm.DateTimeOffset Nullable=True Precision=1]", temporal.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Decimal Nullable=True Precision=3 Scale=2]", decimalType.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Geography Nullable=True SRID=1]", spatial.ToString(), "To string correct");
            Assert.AreEqual("[Collection([Edm.Int32 Nullable=True]) Nullable=True]", collection.ToString(), "To string correct");
            Assert.AreEqual("[EntityReference(AwesomeNamespace.AstonishingEntity) Nullable=True]", entityRef.ToString(), "To string correct");
            Assert.AreEqual("[AwesomeNamespace.AstonishingEntity Nullable=True]", type.ToString(), "To string correct");
        }
예제 #5
0
 private static void AppendBinaryFacets(this StringBuilder sb, IEdmBinaryTypeReference type)
 {
     if (type.IsUnbounded || type.MaxLength != null)
     {
         sb.AppendKeyValue(EdmConstants.FacetName_MaxLength, (type.IsUnbounded) ? EdmConstants.Value_Max : type.MaxLength.ToString());
     }
 }
예제 #6
0
        private static bool IsEquivalentTo(this IEdmBinaryTypeReference thisType, IEdmBinaryTypeReference otherType)
        {
            bool hasValue;

            if (thisType.IsNullable == otherType.IsNullable)
            {
                bool?isFixedLength = thisType.IsFixedLength;
                bool?nullable      = otherType.IsFixedLength;
                if (isFixedLength.GetValueOrDefault() != nullable.GetValueOrDefault())
                {
                    hasValue = false;
                }
                else
                {
                    hasValue = isFixedLength.HasValue == nullable.HasValue;
                }
                if (hasValue && thisType.IsUnbounded == otherType.IsUnbounded)
                {
                    int?maxLength  = thisType.MaxLength;
                    int?maxLength1 = otherType.MaxLength;
                    if (maxLength.GetValueOrDefault() != maxLength1.GetValueOrDefault())
                    {
                        return(false);
                    }
                    else
                    {
                        return(maxLength.HasValue == maxLength1.HasValue);
                    }
                }
            }
            return(false);
        }
		private static void AppendBinaryFacets(this StringBuilder sb, IEdmBinaryTypeReference type)
		{
			string str;
			bool? isFixedLength = type.IsFixedLength;
			sb.AppendKeyValue("FixedLength", isFixedLength.ToString());
			if (!type.IsUnbounded)
			{
				int? maxLength = type.MaxLength;
				if (!maxLength.HasValue)
				{
					return;
				}
			}
			StringBuilder stringBuilder = sb;
			string str1 = "MaxLength";
			if (type.IsUnbounded)
			{
				str = "Max";
			}
			else
			{
				int? nullable = type.MaxLength;
				str = nullable.ToString();
			}
			stringBuilder.AppendKeyValue(str1, str);
		}
예제 #8
0
        /// <summary>
        /// Convert a primitive value which didn't match any of the known values of the TypeCode enumeration.
        /// </summary>
        /// <param name="primitiveValue">The value to convert.</param>
        /// <param name="type">The expected primitive type or null.</param>
        /// <returns>The converted value.</returns>
        private static IEdmDelayedValue ConvertPrimitiveValueWithoutTypeCode(object primitiveValue, IEdmPrimitiveTypeReference type)
        {
            byte[] bytes = primitiveValue as byte[];
            if (bytes != null)
            {
                IEdmBinaryTypeReference binaryType = (IEdmBinaryTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Binary);
                return(new EdmBinaryConstant(binaryType, bytes));
            }

            if (primitiveValue is Date)
            {
                IEdmPrimitiveTypeReference dateType = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Date);
                return(new EdmDateConstant(dateType, (Date)primitiveValue));
            }

            if (primitiveValue is DateTimeOffset)
            {
                IEdmTemporalTypeReference dateTimeOffsetType = (IEdmTemporalTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.DateTimeOffset);
                return(new EdmDateTimeOffsetConstant(dateTimeOffsetType, (DateTimeOffset)primitiveValue));
            }

            if (primitiveValue is Guid)
            {
                type = EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Guid);
                return(new EdmGuidConstant(type, (Guid)primitiveValue));
            }

            if (primitiveValue is TimeOfDay)
            {
                IEdmTemporalTypeReference timeOfDayType = (IEdmTemporalTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.TimeOfDay);
                return(new EdmTimeOfDayConstant(timeOfDayType, (TimeOfDay)primitiveValue));
            }

            if (primitiveValue is TimeSpan)
            {
                IEdmTemporalTypeReference timeType = (IEdmTemporalTypeReference)EnsurePrimitiveType(type, EdmPrimitiveTypeKind.Duration);
                return(new EdmDurationConstant(timeType, (TimeSpan)primitiveValue));
            }

            if (primitiveValue is ISpatial)
            {
                // TODO: [JsonLight] Add support for spatial values in ODataEdmStructuredValue
                throw new NotImplementedException();
            }

#if ASTORIA_CLIENT
            IEdmDelayedValue convertPrimitiveValueWithoutTypeCode;
            if (TryConvertClientSpecificPrimitiveValue(primitiveValue, type, out convertPrimitiveValueWithoutTypeCode))
            {
                return(convertPrimitiveValueWithoutTypeCode);
            }
#endif

            throw new ODataException(ErrorStrings.EdmValueUtils_UnsupportedPrimitiveType(primitiveValue.GetType().FullName));
        }
 internal void WriteBinaryTypeAttributes(IEdmBinaryTypeReference reference)
 {
     if (reference.IsUnbounded)
     {
         this.WriteRequiredAttribute(CsdlConstants.Attribute_MaxLength, CsdlConstants.Value_Max, EdmValueWriter.StringAsXml);
     }
     else
     {
         this.WriteOptionalAttribute(CsdlConstants.Attribute_MaxLength, reference.MaxLength, EdmValueWriter.IntAsXml);
     }
 }
예제 #10
0
 internal void WriteBinaryTypeAttributes(IEdmBinaryTypeReference reference)
 {
     if (!reference.IsUnbounded)
     {
         this.WriteOptionalAttribute <int?>("MaxLength", reference.MaxLength, new Func <int?, string>(EdmValueWriter.IntAsXml));
     }
     else
     {
         this.WriteRequiredAttribute <string>("MaxLength", "Max", new Func <string, string>(EdmValueWriter.StringAsXml));
     }
     this.WriteOptionalAttribute <bool?>("FixedLength", reference.IsFixedLength, new Func <bool?, string>(EdmValueWriter.BooleanAsXml));
 }
        /// <summary>
        /// Create a <see cref="OpenApiSchema"/> for a <see cref="IEdmPrimitiveTypeReference"/>.
        /// </summary>
        /// <param name="context">The OData context.</param>
        /// <param name="primitiveType">The Edm primitive reference.</param>
        /// <returns>The created <see cref="OpenApiSchema"/>.</returns>
        public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiveTypeReference primitiveType)
        {
            Utils.CheckArgumentNull(context, nameof(context));
            Utils.CheckArgumentNull(primitiveType, nameof(primitiveType));

            OpenApiSchema schema = context.CreateSchema(primitiveType.PrimitiveDefinition());

            if (schema != null)
            {
                switch (primitiveType.PrimitiveKind())
                {
                case EdmPrimitiveTypeKind.Binary:     // binary
                    IEdmBinaryTypeReference binaryTypeReference = (IEdmBinaryTypeReference)primitiveType;
                    schema.MaxLength = binaryTypeReference.MaxLength;
                    break;

                case EdmPrimitiveTypeKind.Decimal:     // decimal
                    IEdmDecimalTypeReference decimalTypeReference = (IEdmDecimalTypeReference)primitiveType;
                    if (decimalTypeReference.Precision != null)
                    {
                        if (decimalTypeReference.Scale != null)
                        {
                            // The precision is represented with the maximum and minimum keywords and a value of ±(10^ (precision - scale) - 10^ scale).
                            double tmp = Math.Pow(10, decimalTypeReference.Precision.Value - decimalTypeReference.Scale.Value)
                                         - Math.Pow(10, -decimalTypeReference.Scale.Value);
                            schema.Minimum = (decimal?)(tmp * -1.0);
                            schema.Maximum = (decimal?)(tmp);
                        }
                        else
                        {
                            // If the scale facet has a numeric value, and ±(10^precision - 1) if the scale is variable
                            double tmp = Math.Pow(10, decimalTypeReference.Precision.Value) - 1;
                            schema.Minimum = (decimal?)(tmp * -1.0);
                            schema.Maximum = (decimal?)(tmp);
                        }
                    }

                    // The scale of properties of type Edm.Decimal are represented with the OpenAPI Specification keyword multipleOf and a value of 10 ^ -scale
                    schema.MultipleOf = decimalTypeReference.Scale == null ? null : (decimal?)(Math.Pow(10, decimalTypeReference.Scale.Value * -1));
                    break;

                case EdmPrimitiveTypeKind.String:     // string
                    IEdmStringTypeReference stringTypeReference = (IEdmStringTypeReference)primitiveType;
                    schema.MaxLength = stringTypeReference.MaxLength;
                    break;
                }

                // Nullable properties are marked with the keyword nullable and a value of true.
                schema.Nullable = primitiveType.IsNullable ? true : false;
            }

            return(schema);
        }
예제 #12
0
        private static bool TryAssertBinaryConstantAsType(IEdmBinaryConstantExpression expression, IEdmTypeReference type, out IEnumerable <EdmError> discoveredErrors)
        {
            if (!type.IsBinary())
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.ExpressionPrimitiveKindNotValidForAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionPrimitiveKindNotValidForAssertedType) };
                return(false);
            }

            IEdmBinaryTypeReference binaryType = type.AsBinary();

            if (binaryType.MaxLength.HasValue && expression.Value.Length > binaryType.MaxLength.Value)
            {
                discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.BinaryConstantLengthOutOfRange, Edm.Strings.EdmModel_Validator_Semantic_BinaryConstantLengthOutOfRange(expression.Value.Length, binaryType.MaxLength.Value)) };
                return(false);
            }

            discoveredErrors = Enumerable.Empty <EdmError>();
            return(true);
        }
예제 #13
0
        public static IEdmBinaryTypeReference AsBinary(this IEdmTypeReference type)
        {
            EdmUtil.CheckArgumentNull <IEdmTypeReference>(type, "type");
            IEdmBinaryTypeReference edmBinaryTypeReference = type as IEdmBinaryTypeReference;

            if (edmBinaryTypeReference == null)
            {
                string          str       = type.FullName();
                List <EdmError> edmErrors = new List <EdmError>(type.Errors());
                if (edmErrors.Count == 0)
                {
                    edmErrors.AddRange(EdmTypeSemantics.ConversionError(type.Location(), str, "Binary"));
                }
                return(new BadBinaryTypeReference(str, type.IsNullable, edmErrors));
            }
            else
            {
                return(edmBinaryTypeReference);
            }
        }
예제 #14
0
        /// <summary>
        /// If this reference is of a binary type, this will return a valid binary type reference to the type definition. Otherwise, it will return a bad binary type reference.
        /// </summary>
        /// <param name="type">Reference to the calling object.</param>
        /// <returns>A valid binary type reference if the definition of the reference is of a binary type. Otherwise a bad binary type reference.</returns>
        public static IEdmBinaryTypeReference AsBinary(this IEdmTypeReference type)
        {
            EdmUtil.CheckArgumentNull(type, "type");
            IEdmBinaryTypeReference binaryType = type as IEdmBinaryTypeReference;

            if (binaryType != null)
            {
                return(binaryType);
            }

            string          typeFullName = type.FullName();
            List <EdmError> errors       = new List <EdmError>(type.Errors());

            if (errors.Count == 0)
            {
                errors.AddRange(ConversionError(type.Location(), typeFullName, EdmConstants.Type_Binary));
            }

            return(new BadBinaryTypeReference(typeFullName, type.IsNullable, errors));
        }
예제 #15
0
        public void CanConfig_MaxLengthOfStringAndBinaryType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            var entity = builder.EntityType <PrecisionEnitity>().HasKey(p => p.Id);

            entity.Property(p => p.StringProperty).MaxLength = 5;
            entity.Property(p => p.BinaryProperty).MaxLength = 3;

            // Act
            IEdmModel               model         = builder.GetEdmModel();
            IEdmEntityType          edmEntityType = model.SchemaElements.OfType <IEdmEntityType>().First(p => p.Name == "PrecisionEnitity");
            IEdmStringTypeReference stringType    =
                (IEdmStringTypeReference)edmEntityType.DeclaredProperties.First(p => p.Name.Equals("StringProperty")).Type;
            IEdmBinaryTypeReference binaryType =
                (IEdmBinaryTypeReference)edmEntityType.DeclaredProperties.First(p => p.Name.Equals("BinaryProperty")).Type;

            // Assert
            Assert.Equal(stringType.MaxLength.Value, 5);
            Assert.Equal(binaryType.MaxLength.Value, 3);
        }
예제 #16
0
		private static bool IsEquivalentTo(this IEdmBinaryTypeReference thisType, IEdmBinaryTypeReference otherType)
		{
			bool hasValue;
			if (thisType.IsNullable == otherType.IsNullable)
			{
				bool? isFixedLength = thisType.IsFixedLength;
				bool? nullable = otherType.IsFixedLength;
				if (isFixedLength.GetValueOrDefault() != nullable.GetValueOrDefault())
				{
					hasValue = false;
				}
				else
				{
					hasValue = isFixedLength.HasValue == nullable.HasValue;
				}
				if (hasValue && thisType.IsUnbounded == otherType.IsUnbounded)
				{
					int? maxLength = thisType.MaxLength;
					int? maxLength1 = otherType.MaxLength;
					if (maxLength.GetValueOrDefault() != maxLength1.GetValueOrDefault())
					{
						return false;
					}
					else
					{
						return maxLength.HasValue == maxLength1.HasValue;
					}
				}
			}
			return false;
		}
예제 #17
0
        public void CsdlTypeReferenceToStringTest()
        {
            const string csdl =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<Schema Namespace=""AwesomeNamespace"" Alias=""Alias"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
  <EntityType Name=""AstonishingEntity"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Type=""Int32"" Nullable=""false"" />
  </EntityType>
  <EntityType Name=""AweInspiringEntity"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Type=""Int32"" Nullable=""false"" />
    <Property Name=""AstonishingID"" Type=""Int32"" />
  </EntityType>
  <ComplexType Name=""BreathtakingComplex"">
    <Property Name=""Par1"" Type=""Int32"" Nullable=""false"" />
    <Property Name=""Par2"" Type=""Int32"" Nullable=""false"" />
  </ComplexType>
  <Function Name=""Function1""><ReturnType Type=""Edm.Int32""/>
    <Parameter Name=""P1"" Type=""AwesomeNamespace.AstonishingEntity"" />
    <Parameter Name=""P2"" Type=""AwesomeNamespace.BreathtakingComplex"" />
    <Parameter Name=""P3"" Type=""AwesomeNamespace.ExaltedAssociation"" />
    <Parameter Name=""P4"" Type=""Edm.Int32"" />
    <Parameter Name=""P5"" Type=""Edm.String"" MaxLength=""128"" Unicode=""false"" />
    <Parameter Name=""P6"" Type=""Edm.Stream"" />
    <Parameter Name=""P7"" Type=""Edm.Binary"" MaxLength=""max""/>
    <Parameter Name=""P8"" Type=""Edm.DateTimeOffset"" Precision=""1"" />
    <Parameter Name=""P9"" Type=""Edm.Decimal"" Precision=""3"" Scale=""2""/>
    <Parameter Name=""P10"" Type=""Edm.Geography"" SRID=""1""  />
    <Parameter Name=""P11"" Type=""Ref(AwesomeNamespace.AstonishingEntity)"" />
    <Parameter Name=""P12"" Type=""Collection(Edm.Int32)"" />
    <Parameter Name=""P14"" Type=""AwesomeNamespace.FabulousEnum"" />
  </Function>
  <EnumType Name=""FabulousEnum"">
    <Member Name=""m1"" />
    <Member Name=""m2"" />
  </EnumType>
</Schema>";
            IEdmModel model;
            IEnumerable <EdmError> errors;
            bool parsed = CsdlReader.TryParse(new XmlReader[] { XmlReader.Create(new StringReader(csdl)) }, out model, out errors);

            Assert.IsTrue(parsed, "parsed");
            Assert.IsTrue(errors.Count() == 0, "No errors");

            IEdmOperation operation = (IEdmOperation)(model.FindOperations("AwesomeNamespace.Function1")).First();

            IEdmEntityTypeReference          entity      = operation.FindParameter("P1").Type.AsEntity();
            IEdmComplexTypeReference         complex     = operation.FindParameter("P2").Type.AsComplex();
            IEdmTypeReference                association = operation.FindParameter("P3").Type;
            IEdmPrimitiveTypeReference       primitive   = operation.FindParameter("P4").Type.AsPrimitive();
            IEdmStringTypeReference          stringType  = operation.FindParameter("P5").Type.AsString();
            IEdmPrimitiveTypeReference       stream      = operation.FindParameter("P6").Type.AsPrimitive();
            IEdmBinaryTypeReference          binary      = operation.FindParameter("P7").Type.AsBinary();
            IEdmTemporalTypeReference        temporal    = operation.FindParameter("P8").Type.AsTemporal();
            IEdmDecimalTypeReference         decimalType = operation.FindParameter("P9").Type.AsDecimal();
            IEdmSpatialTypeReference         spatial     = operation.FindParameter("P10").Type.AsSpatial();
            IEdmEntityReferenceTypeReference entityRef   = operation.FindParameter("P11").Type.AsEntityReference();
            IEdmCollectionTypeReference      collection  = operation.FindParameter("P12").Type.AsCollection();
            IEdmEnumTypeReference            enumTypeRef = operation.FindParameter("P14").Type.AsEnum();
            IEdmTypeReference                type        = operation.FindParameter("P1").Type;

            Assert.IsFalse(association.IsBad(), "Associations cannot be types");
            Assert.IsTrue(association.Definition.IsBad(), "Associations cannot be types");
            Assert.AreEqual("[AwesomeNamespace.AstonishingEntity Nullable=True]", entity.ToString(), "To string correct");
            Assert.AreEqual("[AwesomeNamespace.BreathtakingComplex Nullable=True]", complex.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Int32 Nullable=True]", primitive.ToString(), "To string correct");
            Assert.AreEqual("[Edm.String Nullable=True MaxLength=128 Unicode=False]", stringType.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Stream Nullable=True]", stream.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Binary Nullable=True MaxLength=max]", binary.ToString(), "To string correct");
            Assert.AreEqual("[Edm.DateTimeOffset Nullable=True Precision=1]", temporal.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Decimal Nullable=True Precision=3 Scale=2]", decimalType.ToString(), "To string correct");
            Assert.AreEqual("[Edm.Geography Nullable=True SRID=1]", spatial.ToString(), "To string correct");
            Assert.AreEqual("[Collection([Edm.Int32 Nullable=True]) Nullable=True]", collection.ToString(), "To string correct");
            Assert.AreEqual("[EntityReference(AwesomeNamespace.AstonishingEntity) Nullable=True]", entityRef.ToString(), "To string correct");
            Assert.AreEqual("[AwesomeNamespace.FabulousEnum Nullable=True]", enumTypeRef.ToString(), "To string correct");
            Assert.AreEqual("[AwesomeNamespace.AstonishingEntity Nullable=True]", type.ToString(), "To string correct");
        }
예제 #18
0
        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);
        }
예제 #19
0
        /// <summary>
        /// Clones the specified type reference.
        /// </summary>
        /// <param name="typeReference">The type reference to clone.</param>
        /// <param name="nullable">true to make the cloned type reference nullable; false to make it non-nullable.</param>
        /// <returns>The cloned <see cref="IEdmTypeReference"/> instance.</returns>
        public static IEdmTypeReference Clone(this IEdmTypeReference typeReference, bool nullable)
        {
            if (typeReference == null)
            {
                return(null);
            }

            EdmTypeKind typeKind = typeReference.TypeKind();

            switch (typeKind)
            {
            case EdmTypeKind.Primitive:
                EdmPrimitiveTypeKind kind          = typeReference.PrimitiveKind();
                IEdmPrimitiveType    primitiveType = (IEdmPrimitiveType)typeReference.Definition;
                switch (kind)
                {
                case EdmPrimitiveTypeKind.Boolean:
                case EdmPrimitiveTypeKind.Byte:
                case EdmPrimitiveTypeKind.Double:
                case EdmPrimitiveTypeKind.Guid:
                case EdmPrimitiveTypeKind.Int16:
                case EdmPrimitiveTypeKind.Int32:
                case EdmPrimitiveTypeKind.Int64:
                case EdmPrimitiveTypeKind.SByte:
                case EdmPrimitiveTypeKind.Single:
                case EdmPrimitiveTypeKind.Stream:
                    return(new EdmPrimitiveTypeReference(primitiveType, nullable));

                case EdmPrimitiveTypeKind.Binary:
                    IEdmBinaryTypeReference binaryTypeReference = (IEdmBinaryTypeReference)typeReference;
                    return(new EdmBinaryTypeReference(
                               primitiveType,
                               nullable,
                               binaryTypeReference.IsUnbounded,
                               binaryTypeReference.MaxLength));

                case EdmPrimitiveTypeKind.String:
                    IEdmStringTypeReference stringTypeReference = (IEdmStringTypeReference)typeReference;
                    return(new EdmStringTypeReference(
                               primitiveType,
                               nullable,
                               stringTypeReference.IsUnbounded,
                               stringTypeReference.MaxLength,
                               stringTypeReference.IsUnicode));

                case EdmPrimitiveTypeKind.Decimal:
                    IEdmDecimalTypeReference decimalTypeReference = (IEdmDecimalTypeReference)typeReference;
                    return(new EdmDecimalTypeReference(primitiveType, nullable, decimalTypeReference.Precision, decimalTypeReference.Scale));

                case EdmPrimitiveTypeKind.DateTimeOffset:
                case EdmPrimitiveTypeKind.Duration:
                    IEdmTemporalTypeReference temporalTypeReference = (IEdmTemporalTypeReference)typeReference;
                    return(new EdmTemporalTypeReference(primitiveType, nullable, temporalTypeReference.Precision));

                case EdmPrimitiveTypeKind.Geography:
                case EdmPrimitiveTypeKind.GeographyPoint:
                case EdmPrimitiveTypeKind.GeographyLineString:
                case EdmPrimitiveTypeKind.GeographyPolygon:
                case EdmPrimitiveTypeKind.GeographyCollection:
                case EdmPrimitiveTypeKind.GeographyMultiPolygon:
                case EdmPrimitiveTypeKind.GeographyMultiLineString:
                case EdmPrimitiveTypeKind.GeographyMultiPoint:
                case EdmPrimitiveTypeKind.Geometry:
                case EdmPrimitiveTypeKind.GeometryCollection:
                case EdmPrimitiveTypeKind.GeometryPoint:
                case EdmPrimitiveTypeKind.GeometryLineString:
                case EdmPrimitiveTypeKind.GeometryPolygon:
                case EdmPrimitiveTypeKind.GeometryMultiPolygon:
                case EdmPrimitiveTypeKind.GeometryMultiLineString:
                case EdmPrimitiveTypeKind.GeometryMultiPoint:
                    IEdmSpatialTypeReference spatialTypeReference = (IEdmSpatialTypeReference)typeReference;
                    return(new EdmSpatialTypeReference(primitiveType, nullable, spatialTypeReference.SpatialReferenceIdentifier));

                default:
                    throw new TaupoNotSupportedException("Invalid primitive type kind: " + typeKind.ToString());
                }

            case EdmTypeKind.Entity:
                return(new EdmEntityTypeReference((IEdmEntityType)typeReference.Definition, nullable));

            case EdmTypeKind.Complex:
                return(new EdmComplexTypeReference((IEdmComplexType)typeReference.Definition, nullable));

            case EdmTypeKind.Collection:
                return(new EdmCollectionTypeReference((IEdmCollectionType)typeReference.Definition));

            case EdmTypeKind.EntityReference:
                return(new EdmEntityReferenceTypeReference((IEdmEntityReferenceType)typeReference.Definition, nullable));

            case EdmTypeKind.Enum:
                return(new EdmEnumTypeReference((IEdmEnumType)typeReference.Definition, nullable));

            case EdmTypeKind.None:      // fall through
            default:
                throw new TaupoNotSupportedException("Invalid type kind: " + typeKind.ToString());
            }
        }
예제 #20
0
 private static bool IsEquivalentTo(this IEdmBinaryTypeReference thisType, IEdmBinaryTypeReference otherType)
 {
     return thisType.IsNullable == otherType.IsNullable &&
            thisType.IsFixedLength == otherType.IsFixedLength &&
            thisType.IsUnbounded == otherType.IsUnbounded &&
            thisType.MaxLength == otherType.MaxLength;
 }
예제 #21
0
 internal abstract void WriteBinaryTypeAttributes(IEdmBinaryTypeReference reference);
예제 #22
0
        private static bool MatchesType(IEdmTypeReference targetType, IEdmValue operand, bool testPropertyTypes)
        {
            IEdmTypeReference operandType = operand.Type;
            EdmValueKind      operandKind = operand.ValueKind;

            if (operandType != null && operandKind != EdmValueKind.Null && operandType.Definition.IsOrInheritsFrom(targetType.Definition))
            {
                return(true);
            }

            switch (operandKind)
            {
            case EdmValueKind.Binary:
                if (targetType.IsBinary())
                {
                    IEdmBinaryTypeReference targetBinary = targetType.AsBinary();
                    return(targetBinary.IsUnbounded || !targetBinary.MaxLength.HasValue || targetBinary.MaxLength.Value >= ((IEdmBinaryValue)operand).Value.Length);
                }

                break;

            case EdmValueKind.Boolean:
                return(targetType.IsBoolean());

            case EdmValueKind.Date:
                return(targetType.IsDate());

            case EdmValueKind.DateTimeOffset:
                return(targetType.IsDateTimeOffset());

            case EdmValueKind.Decimal:
                return(targetType.IsDecimal());

            case EdmValueKind.Guid:
                return(targetType.IsGuid());

            case EdmValueKind.Null:
                return(targetType.IsNullable);

            case EdmValueKind.Duration:
                return(targetType.IsDuration());

            case EdmValueKind.String:
                if (targetType.IsString())
                {
                    IEdmStringTypeReference targetString = targetType.AsString();
                    return(targetString.IsUnbounded || !targetString.MaxLength.HasValue || targetString.MaxLength.Value >= ((IEdmStringValue)operand).Value.Length);
                }

                break;

            case EdmValueKind.TimeOfDay:
                return(targetType.IsTimeOfDay());

            case EdmValueKind.Floating:
                return(targetType.IsDouble() || (targetType.IsSingle() && FitsInSingle(((IEdmFloatingValue)operand).Value)));

            case EdmValueKind.Integer:
                if (targetType.TypeKind() == EdmTypeKind.Primitive)
                {
                    switch (targetType.AsPrimitive().PrimitiveKind())
                    {
                    case EdmPrimitiveTypeKind.Int16:
                        return(InRange(((IEdmIntegerValue)operand).Value, Int16.MinValue, Int16.MaxValue));

                    case EdmPrimitiveTypeKind.Int32:
                        return(InRange(((IEdmIntegerValue)operand).Value, Int32.MinValue, Int32.MaxValue));

                    case EdmPrimitiveTypeKind.SByte:
                        return(InRange(((IEdmIntegerValue)operand).Value, SByte.MinValue, SByte.MaxValue));

                    case EdmPrimitiveTypeKind.Byte:
                        return(InRange(((IEdmIntegerValue)operand).Value, Byte.MinValue, Byte.MaxValue));

                    case EdmPrimitiveTypeKind.Int64:
                    case EdmPrimitiveTypeKind.Single:
                    case EdmPrimitiveTypeKind.Double:
                        return(true);
                    }
                }

                break;

            case EdmValueKind.Collection:
                if (targetType.IsCollection())
                {
                    IEdmTypeReference targetElementType = targetType.AsCollection().ElementType();

                    // This enumerates the entire collection, which is unfortunate.
                    foreach (IEdmDelayedValue elementValue in ((IEdmCollectionValue)operand).Elements)
                    {
                        if (!MatchesType(targetElementType, elementValue.Value))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }

                break;

            case EdmValueKind.Enum:
                return(((IEdmEnumValue)operand).Type.Definition.IsEquivalentTo(targetType.Definition));

            case EdmValueKind.Structured:
                if (targetType.IsStructured())
                {
                    return(AssertOrMatchStructuredType(targetType.AsStructured(), (IEdmStructuredValue)operand, testPropertyTypes, null));
                }

                break;
            }

            return(false);
        }
예제 #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EdmBinaryConstant"/> class.
 /// </summary>
 /// <param name="type">Type of the integer.</param>
 /// <param name="value">Integer value represented by this value.</param>
 public EdmBinaryConstant(IEdmBinaryTypeReference type, byte[] value)
     : base(type)
 {
     EdmUtil.CheckArgumentNull(value, "value");
     this.value = value;
 }
예제 #24
0
 protected virtual void ProcessBinaryTypeReference(IEdmBinaryTypeReference reference)
 {
     this.ProcessPrimitiveTypeReference(reference);
 }
예제 #25
0
        internal static IEdmTypeReference Clone(this IEdmTypeReference typeReference, bool nullable)
        {
            if (typeReference == null)
            {
                return(null);
            }
            switch (typeReference.TypeKind())
            {
            case EdmTypeKind.Primitive:
            {
                EdmPrimitiveTypeKind kind2      = typeReference.PrimitiveKind();
                IEdmPrimitiveType    definition = (IEdmPrimitiveType)typeReference.Definition;
                switch (kind2)
                {
                case EdmPrimitiveTypeKind.Binary:
                {
                    IEdmBinaryTypeReference reference = (IEdmBinaryTypeReference)typeReference;
                    return(new EdmBinaryTypeReference(definition, nullable, reference.IsUnbounded, reference.MaxLength, reference.IsFixedLength));
                }

                case EdmPrimitiveTypeKind.Boolean:
                case EdmPrimitiveTypeKind.Byte:
                case EdmPrimitiveTypeKind.Double:
                case EdmPrimitiveTypeKind.Guid:
                case EdmPrimitiveTypeKind.Int16:
                case EdmPrimitiveTypeKind.Int32:
                case EdmPrimitiveTypeKind.Int64:
                case EdmPrimitiveTypeKind.SByte:
                case EdmPrimitiveTypeKind.Single:
                case EdmPrimitiveTypeKind.Stream:
                    return(new EdmPrimitiveTypeReference(definition, nullable));

                case EdmPrimitiveTypeKind.DateTime:
                case EdmPrimitiveTypeKind.DateTimeOffset:
                case EdmPrimitiveTypeKind.Time:
                {
                    IEdmTemporalTypeReference reference4 = (IEdmTemporalTypeReference)typeReference;
                    return(new EdmTemporalTypeReference(definition, nullable, reference4.Precision));
                }

                case EdmPrimitiveTypeKind.Decimal:
                {
                    IEdmDecimalTypeReference reference3 = (IEdmDecimalTypeReference)typeReference;
                    return(new EdmDecimalTypeReference(definition, nullable, reference3.Precision, reference3.Scale));
                }

                case EdmPrimitiveTypeKind.String:
                {
                    IEdmStringTypeReference reference2 = (IEdmStringTypeReference)typeReference;
                    return(new EdmStringTypeReference(definition, nullable, reference2.IsUnbounded, reference2.MaxLength, reference2.IsFixedLength, reference2.IsUnicode, reference2.Collation));
                }

                case EdmPrimitiveTypeKind.Geography:
                case EdmPrimitiveTypeKind.GeographyPoint:
                case EdmPrimitiveTypeKind.GeographyLineString:
                case EdmPrimitiveTypeKind.GeographyPolygon:
                case EdmPrimitiveTypeKind.GeographyCollection:
                case EdmPrimitiveTypeKind.GeographyMultiPolygon:
                case EdmPrimitiveTypeKind.GeographyMultiLineString:
                case EdmPrimitiveTypeKind.GeographyMultiPoint:
                case EdmPrimitiveTypeKind.Geometry:
                case EdmPrimitiveTypeKind.GeometryPoint:
                case EdmPrimitiveTypeKind.GeometryLineString:
                case EdmPrimitiveTypeKind.GeometryPolygon:
                case EdmPrimitiveTypeKind.GeometryCollection:
                case EdmPrimitiveTypeKind.GeometryMultiPolygon:
                case EdmPrimitiveTypeKind.GeometryMultiLineString:
                case EdmPrimitiveTypeKind.GeometryMultiPoint:
                {
                    IEdmSpatialTypeReference reference5 = (IEdmSpatialTypeReference)typeReference;
                    return(new EdmSpatialTypeReference(definition, nullable, reference5.SpatialReferenceIdentifier));
                }
                }
                throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodesCommon.EdmLibraryExtensions_Clone_PrimitiveTypeKind));
            }

            case EdmTypeKind.Entity:
                return(new EdmEntityTypeReference((IEdmEntityType)typeReference.Definition, nullable));

            case EdmTypeKind.Complex:
                return(new EdmComplexTypeReference((IEdmComplexType)typeReference.Definition, nullable));

            case EdmTypeKind.Row:
                return(new EdmRowTypeReference((IEdmRowType)typeReference.Definition, nullable));

            case EdmTypeKind.Collection:
                return(new EdmCollectionTypeReference((IEdmCollectionType)typeReference.Definition, nullable));

            case EdmTypeKind.EntityReference:
                return(new EdmEntityReferenceTypeReference((IEdmEntityReferenceType)typeReference.Definition, nullable));

            case EdmTypeKind.Enum:
                return(new EdmEnumTypeReference((IEdmEnumType)typeReference.Definition, nullable));
            }
            throw new ODataException(Microsoft.Data.OData.Strings.General_InternalError(InternalErrorCodesCommon.EdmLibraryExtensions_Clone_TypeKind));
        }
예제 #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EdmBinaryConstant"/> class.
 /// </summary>
 /// <param name="type">Type of the integer.</param>
 /// <param name="value">Integer value represented by this value.</param>
 public EdmBinaryConstant(IEdmBinaryTypeReference type, byte[] value)
     : base(type)
 {
     EdmUtil.CheckArgumentNull(value, "value");
     this.value = value;
 }
예제 #27
0
 protected override void ProcessBinaryTypeReference(IEdmBinaryTypeReference element)
 {
     this.schemaWriter.WriteBinaryTypeAttributes(element);
 }
 private static void AppendBinaryFacets(this StringBuilder sb, IEdmBinaryTypeReference type)
 {
     sb.AppendKeyValue(EdmConstants.FacetName_FixedLength, type.IsFixedLength.ToString());
     if (type.IsUnbounded || type.MaxLength != null)
     {
         sb.AppendKeyValue(EdmConstants.FacetName_MaxLength, (type.IsUnbounded) ? EdmConstants.Value_Max : type.MaxLength.ToString());
     }
 }