public void ShouldMaterializeTimeOfDayCollection() { var primitiveValues = new List <TimeOfDay>(new TimeOfDay[] { TimeOfDay.MinValue, new TimeOfDay(19, 9, 28, 123), TimeOfDay.MaxValue }); var outputCollection = new List <TimeOfDay>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( primitiveValues, "Edm.TimeOfDay", outputCollection, typeof(TimeOfDay), addToDelegate, false); outputCollection.Should().HaveCount(3); outputCollection[0].Should().Be(TimeOfDay.MinValue); outputCollection[1].Should().Be(new TimeOfDay(19, 9, 28, 123)); outputCollection[2].Should().Be(TimeOfDay.MaxValue); }
public void ShouldMaterializeNullableIntCollection() { var primitiveValues = new List <int?>(new int?[] { 1, null, 10 }); var outputCollection = new List <int?>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( primitiveValues, "Edm.Int32", outputCollection, typeof(int?), addToDelegate, true); outputCollection.Should().HaveCount(3); outputCollection[0].Should().Be(1); outputCollection[1].Should().Be(null); outputCollection[2].Should().Be(10); }
public void ShouldMaterializeDateCollection() { var primitiveValues = new List <Date>(new Date[] { Date.MinValue, new Date(2014, 9, 28), Date.MaxValue }); var outputCollection = new List <Date>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( primitiveValues, "Edm.Date", outputCollection, typeof(Date), addToDelegate, false); outputCollection.Should().HaveCount(3); outputCollection[0].Should().Be(Date.MinValue); outputCollection[1].Should().Be(new Date(2014, 9, 28)); outputCollection[2].Should().Be(Date.MaxValue); }
public void AddingPrimitiveValueToComplexCollectionShouldFail() { var primitiveValues = new List <object>(new object[] { 1 }); var outputCollection = new List <Point>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); Action test = () => this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( primitiveValues, "Point", outputCollection, typeof(Point), addToDelegate, false); test.ShouldThrow <InvalidOperationException>(DSClient.Strings.Collection_PrimitiveTypesInCollectionOfComplexTypesNotAllowed); }
public void AddingCollectionToPrimitiveCollectionShouldFail() { var collectionValues = new List <object>(new object[] { new ODataCollectionValue() }); var outputCollection = new List <int>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); Action test = () => this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( collectionValues, "Edm.Int32", outputCollection, typeof(int), addToDelegate, false); test.ShouldThrow <InvalidOperationException>(DSClient.Strings.Collection_CollectionTypesInCollectionOfPrimitiveTypesNotAllowed); }
[TestMethod] public void NullComplexValueShouldFail() { var primitiveValues = new List <ODataComplexValue>(new ODataComplexValue[] { null }); var outputCollection = new List <Point>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); Action test = () => this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( primitiveValues, "Point", outputCollection, typeof(Point), addToDelegate, false); test.ShouldThrow <InvalidOperationException>().WithMessage(DSClient.Strings.Collection_NullCollectionItemsNotSupported); }
/// <summary> /// Reads a value from the message reader. /// </summary> /// <param name="expectedClientType">The expected client type being materialized into.</param> /// <param name="expectedReaderType">The expected type for the underlying reader.</param> protected override void ReadWithExpectedType(IEdmTypeReference expectedClientType, IEdmTypeReference expectedReaderType) { if (!expectedClientType.IsCollection()) { throw new DataServiceClientException(DSClient.Strings.AtomMaterializer_TypeShouldBeCollectionError(expectedClientType.FullName())); } Type underlyingExpectedType = Nullable.GetUnderlyingType(this.ExpectedType) ?? this.ExpectedType; Debug.Assert(WebUtil.IsCLRTypeCollection(underlyingExpectedType, this.MaterializerContext.Model) || (SingleResult.HasValue && !SingleResult.Value), "expected type must be collection or single result must be false"); // We are here for two cases: // (1) Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T> // (2) Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T Type collectionItemType = underlyingExpectedType; Type collectionICollectionType = ClientTypeUtil.GetImplementationType(underlyingExpectedType, typeof(ICollection <>)); if (collectionICollectionType != null) { // Case 1 : Something like Execute<ICollection<T>>, in which case the underlyingExpectedType is ICollection<T> collectionItemType = collectionICollectionType.GetGenericArguments()[0]; } else { // Case 2 : Execute<T> with the bool singleValue = false, in which case underlyingExpectedType is T collectionICollectionType = typeof(ICollection <>).MakeGenericType(new Type[] { collectionItemType }); } Type clrCollectionType = WebUtil.GetBackingTypeForCollectionProperty(collectionICollectionType); object collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionInstance((IEdmCollectionTypeReference)expectedClientType, clrCollectionType); // Enumerator over our collection reader was created, then ApplyDataCollections was refactored to // take an enumerable instead of a ODataCollectionValue. Enumerator is being used as a bridge ODataCollectionReader collectionReader = messageReader.CreateODataCollectionReader(); NonEntityItemsEnumerable collectionEnumerable = new NonEntityItemsEnumerable(collectionReader); bool isElementNullable = expectedClientType.AsCollection().ElementType().IsNullable; this.CollectionValueMaterializationPolicy.ApplyCollectionDataValues( collectionEnumerable, null /*wireTypeName*/, collectionInstance, collectionItemType, ClientTypeUtil.GetAddToCollectionDelegate(collectionICollectionType), isElementNullable); this.currentValue = collectionInstance; }
public void ShouldMaterializeComplexCollection() { var primitiveValues = new List <ODataComplexValue>(new ODataComplexValue[] { new ODataComplexValue() { Properties = new ODataProperty[] { new ODataProperty() { Name = "X", Value = 15 }, new ODataProperty() { Name = "Y", Value = 18 } } }, new ODataComplexValue() { Properties = new ODataProperty[] { new ODataProperty() { Name = "X", Value = 22 }, new ODataProperty() { Name = "Y", Value = 25 } } }, new ODataComplexValue() { Properties = new ODataProperty[] { new ODataProperty() { Name = "X", Value = -100 }, new ODataProperty() { Name = "Y", Value = -201 } } }, }); var outputCollection = new List <Point>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( primitiveValues, "Point", outputCollection, typeof(Point), addToDelegate, false); outputCollection.Should().HaveCount(3); outputCollection[0].X.Should().Be(15); outputCollection[0].Y.Should().Be(18); outputCollection[1].X.Should().Be(22); outputCollection[1].Y.Should().Be(25); outputCollection[2].X.Should().Be(-100); outputCollection[2].Y.Should().Be(-201); }
public void AddingCollectionToComplexCollectionShouldFail() { var collectionValues = new List <object>(new object[] { new ODataCollectionValue() }); var outputCollection = new List <Point>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); Action test = () => this.CreateCollectionValueMaterializationPolicy().ApplyCollectionDataValues( collectionValues, "Point", outputCollection, typeof(Point), addToDelegate, false); // Error message is a little off we could improve this test.ShouldThrow <InvalidOperationException>(DSClient.Strings.Collection_ComplexTypesInCollectionOfPrimitiveTypesNotAllowed); }
public void ShouldMaterializeConcreteComplexCollectionDeclaredAsAbstract() { var complexValues = new List <ODataComplexValue>(new[] { new ODataComplexValue() { Properties = new ODataProperty[] { new ODataProperty() { Name = "Points", Value = 0 }, new ODataProperty() { Name = "Diameter", Value = 15 } } }, new ODataComplexValue() { Properties = new ODataProperty[] { new ODataProperty() { Name = "Points", Value = 0 }, new ODataProperty() { Name = "Diameter", Value = 18 } } }, }); var testContext = new TestMaterializerContext(); testContext.ResolveTypeForMaterializationOverrideFunc = (Type type, string name) => { var edmType = testContext.Model.GetOrCreateEdmType(typeof(Circle)); return(new ClientTypeAnnotation(edmType, typeof(Circle), "Circle", testContext.Model)); }; var outputCollection = new List <Shape>(); var addToDelegate = ClientTypeUtil.GetAddToCollectionDelegate(outputCollection.GetType()); this.CreateCollectionValueMaterializationPolicy(testContext).ApplyCollectionDataValues( complexValues, "Shape", outputCollection, typeof(Shape), addToDelegate, true); outputCollection.Should().HaveCount(2); outputCollection[0].Points.Should().Be(0); ((Circle)outputCollection[0]).Diameter.Should().Be(15); outputCollection[1].Points.Should().Be(0); ((Circle)outputCollection[1]).Diameter.Should().Be(18); }
/// <summary> /// Convert an instance annotation to clr object. /// </summary> /// <param name="instanceAnnotation">Instance annotation to be converted</param> /// <param name="clrInstanceAnnotation">The clr object</param> /// <returns>A dictionary of clr-typed instance annotation</returns> private bool TryConvertToClrInstanceAnnotation(ODataInstanceAnnotation instanceAnnotation, out object clrInstanceAnnotation) { clrInstanceAnnotation = null; var primitiveValue = instanceAnnotation.Value as ODataPrimitiveValue; if (primitiveValue != null) { clrInstanceAnnotation = primitiveValue.Value; return(true); } var enumValue = instanceAnnotation.Value as ODataEnumValue; if (enumValue != null) { var type = this.MaterializerContext.Context.ResolveTypeFromName(enumValue.TypeName); if (type != null) { clrInstanceAnnotation = EnumValueMaterializationPolicy.MaterializeODataEnumValue(type, enumValue); return(true); } return(false); } var collectionValue = instanceAnnotation.Value as ODataCollectionValue; if (collectionValue != null) { var serverSideModel = this.MaterializerContext.Context.Format.LoadServiceModel(); var valueTerm = serverSideModel.FindTerm(instanceAnnotation.Name); if (valueTerm != null && valueTerm.Type != null && valueTerm.Type.Definition != null) { var edmCollectionType = valueTerm.Type.Definition as IEdmCollectionType; if (edmCollectionType != null) { Type collectionItemType = null; var elementType = edmCollectionType.ElementType; PrimitiveType primitiveType; if (PrimitiveType.TryGetPrimitiveType(elementType.FullName(), out primitiveType)) { collectionItemType = primitiveType.ClrType; } else { collectionItemType = this.MaterializerContext.Context.ResolveTypeFromName(elementType.FullName()); } if (collectionItemType != null) { Type collectionICollectionType = typeof(ICollection <>).MakeGenericType(new Type[] { collectionItemType }); ClientTypeAnnotation collectionClientTypeAnnotation = this.MaterializerContext.ResolveTypeForMaterialization( collectionICollectionType, collectionValue.TypeName); bool isElementNullable = edmCollectionType.ElementType.IsNullable; var collectionInstance = this.CollectionValueMaterializationPolicy.CreateCollectionInstance( collectionClientTypeAnnotation.EdmTypeReference as IEdmCollectionTypeReference, collectionClientTypeAnnotation.ElementType); this.CollectionValueMaterializationPolicy.ApplyCollectionDataValues( collectionValue.Items, collectionValue.TypeName, collectionInstance, collectionItemType, ClientTypeUtil.GetAddToCollectionDelegate(collectionICollectionType), isElementNullable); clrInstanceAnnotation = collectionInstance; return(true); } } } return(false); } var nullValue = instanceAnnotation.Value as ODataNullValue; if (nullValue != null) { clrInstanceAnnotation = null; return(true); } return(false); }