Exemple #1
0
            /// <summary>
            /// Visits a collection item.
            /// </summary>
            /// <param name="collectionValue">The collection to visit.</param>
            protected override ODataPayloadElement VisitCollectionValue(ODataCollectionValue collectionValue)
            {
                if (collectionValue == null)
                {
                    return(new PrimitiveMultiValue(null, true));
                }

                // Try to parse the type name and if the item type name is a primitive EDM type, then this is a primitive collection.
                string typeName = collectionValue.TypeName;

                List <object> items = new List <object>();

                foreach (object item in collectionValue.Items)
                {
                    items.Add((PrimitiveValue)this.Visit(item));
                }

                PrimitiveMultiValue primitiveCollection = new PrimitiveMultiValue(typeName, false);

                foreach (object item in items)
                {
                    primitiveCollection.Add((PrimitiveValue)item);
                }

                this.ConvertSerializationTypeNameAnnotation(collectionValue, primitiveCollection);

                return(primitiveCollection);
            }
        private void PopulatePrimitiveBagPropertyFromPaths(ComplexInstance instance, MemberProperty memberProperty, string propertyPath, IEnumerable <NamedValue> namedValues, PrimitiveDataType primitiveElementDataType)
        {
            int  i         = 0;
            bool completed = false;

            var primitiveCollection = new PrimitiveMultiValue(primitiveElementDataType.BuildMultiValueTypeName(), false);

            while (!completed)
            {
                IEnumerable <NamedValue> primitiveItemNamedValues = namedValues.Where(pp => pp.Name == propertyPath + "." + i).ToList();
                if (primitiveItemNamedValues.Count() == 0)
                {
                    completed = true;
                }
                else
                {
                    ExceptionUtilities.Assert(primitiveItemNamedValues.Count() < 2, "Should not get more than one value for a primitive Bag item for path '{0}'", propertyPath + "." + i);
                    var value = primitiveItemNamedValues.Single();

                    // Do something with the value
                    primitiveCollection.Add(PrimitiveValue(primitiveElementDataType, value.Value));
                }

                i++;
            }

            if (i > 1)
            {
                instance.Add(new PrimitiveMultiValueProperty(memberProperty.Name, primitiveCollection));
            }
        }
Exemple #3
0
            /// <summary>
            /// Visits a collection item.
            /// </summary>
            /// <param name="collectionValue">The collection to visit.</param>
            protected override ODataPayloadElement VisitCollectionValue(ODataCollectionValue collectionValue)
            {
                if (collectionValue == null)
                {
                    return(new PrimitiveMultiValue(null, true));
                }

                bool?isPrimitiveCollection = null;

                // Try to parse the type name and if the item type name is a primitive EDM type, then this is a primitive collection.
                string typeName = collectionValue.TypeName;

                if (typeName != null)
                {
                    string itemTypeName = EntityModelUtils.GetCollectionItemTypeName(typeName);
                    isPrimitiveCollection = itemTypeName != null && EntityModelUtils.GetPrimitiveEdmType(itemTypeName) != null;
                }

                List <object> items = new List <object>();

                foreach (object item in collectionValue.Items)
                {
                    if (!isPrimitiveCollection.HasValue)
                    {
                        ODataComplexValue complexItemValue = item as ODataComplexValue;

                        // If the first item is a complex value, then the collection is of complex kind.
                        // Note that if the first item is null, we assume primitive collection, since we can't really decide (and it's an invalid thing anyway)
                        isPrimitiveCollection = complexItemValue == null;
                    }

                    if (isPrimitiveCollection.Value)
                    {
                        items.Add((PrimitiveValue)this.Visit(item));
                    }
                    else
                    {
                        ODataComplexValue complexItemValue = item as ODataComplexValue;
                        ExceptionUtilities.Assert(
                            item == null || complexItemValue != null,
                            "The collection was determined to be of complex values but one of its items is not an ODataComplexValue.");
                        items.Add((ComplexInstance)this.Visit(complexItemValue));
                    }
                }

                if (!isPrimitiveCollection.HasValue)
                {
                    // If we could not tell until now (possible only if there was no type name and no items)
                    // assume primitive collection.
                    isPrimitiveCollection = true;
                }

                if (isPrimitiveCollection == true)
                {
                    PrimitiveMultiValue primitiveCollection = new PrimitiveMultiValue(typeName, false);
                    foreach (object item in items)
                    {
                        primitiveCollection.Add((PrimitiveValue)item);
                    }

                    this.ConvertSerializationTypeNameAnnotation(collectionValue, primitiveCollection);

                    return(primitiveCollection);
                }
                else
                {
                    ComplexMultiValue complexCollection = new ComplexMultiValue(typeName, false);
                    foreach (object item in items)
                    {
                        complexCollection.Add((ComplexInstance)item);
                    }

                    this.ConvertSerializationTypeNameAnnotation(collectionValue, complexCollection);

                    return(complexCollection);
                }
            }
        private void PopulatePrimitiveBagPropertyFromPaths(ComplexInstance instance, MemberProperty memberProperty, string propertyPath, IEnumerable<NamedValue> namedValues, PrimitiveDataType primitiveElementDataType)
        {
            int i = 0;
            bool completed = false;

            var primitiveCollection = new PrimitiveMultiValue(primitiveElementDataType.BuildMultiValueTypeName(), false);
            while (!completed)
            {
                IEnumerable<NamedValue> primitiveItemNamedValues = namedValues.Where(pp => pp.Name == propertyPath + "." + i).ToList();
                if (primitiveItemNamedValues.Count() == 0)
                {
                    completed = true;
                }
                else
                {
                    ExceptionUtilities.Assert(primitiveItemNamedValues.Count() < 2, "Should not get more than one value for a primitive Bag item for path '{0}'", propertyPath + "." + i);
                    var value = primitiveItemNamedValues.Single();

                    // Do something with the value
                    primitiveCollection.Add(PrimitiveValue(primitiveElementDataType, value.Value));
                }

                i++;
            }

            if (i > 1)
            {
                instance.Add(new PrimitiveMultiValueProperty(memberProperty.Name, primitiveCollection));
            }
        }