private void PopulatePropertiesFromPaths(ComplexInstance instance, IEnumerable <MemberProperty> properties, string propertyPath, IEnumerable <NamedValue> namedValues)
        {
            ExceptionUtilities.CheckArgumentNotNull(instance, "instance");
            ExceptionUtilities.CheckArgumentNotNull(properties, "properties");
            ExceptionUtilities.CheckArgumentNotNull(namedValues, "namedValues");

            foreach (MemberProperty property in properties)
            {
                string childPropertyPath = property.Name;
                if (propertyPath != null)
                {
                    childPropertyPath = propertyPath + "." + property.Name;
                }

                CollectionDataType collectionDataType = property.PropertyType as CollectionDataType;
                PrimitiveDataType  primitiveDataType  = property.PropertyType as PrimitiveDataType;
                if (primitiveDataType != null)
                {
                    NamedValue memberPropertyNamedValue = namedValues.SingleOrDefault(nv => nv.Name == childPropertyPath);
                    if (memberPropertyNamedValue != null)
                    {
                        instance.Add(this.PrimitiveProperty(property, memberPropertyNamedValue.Value));
                    }
                }
                else if (collectionDataType != null)
                {
                    IEnumerable <NamedValue> bagNamedValues = namedValues.Where(nv => nv.Name.StartsWith(childPropertyPath + ".", StringComparison.Ordinal)).ToList();
                    if (bagNamedValues.Count() > 0)
                    {
                        this.PopulateMultiValuePropertyFromPaths(instance, property, collectionDataType.ElementDataType, childPropertyPath, bagNamedValues);
                    }
                    else
                    {
                        this.PopulateCollectionPropertyWithNullOrEmpty(instance, property, childPropertyPath, namedValues);
                    }
                }
                else
                {
                    var complexDataType = property.PropertyType as ComplexDataType;
                    ExceptionUtilities.CheckObjectNotNull(complexDataType, "Property '{0}' was not primitive, a collection, or complex", property.Name);

                    IEnumerable <NamedValue> complexInstanceNamedValues = namedValues.Where(nv => nv.Name.StartsWith(childPropertyPath + ".", StringComparison.Ordinal)).ToList();
                    if (complexInstanceNamedValues.Count() > 0)
                    {
                        PropertyInstance memberPropertyInstance = this.ComplexProperty(property, childPropertyPath, complexInstanceNamedValues);
                        instance.Add(memberPropertyInstance);
                    }
                    else
                    {
                        // Check for null case
                        IEnumerable <NamedValue> exactMatches = namedValues.Where(nv => nv.Name == childPropertyPath).ToList();
                        ExceptionUtilities.Assert(exactMatches.Count() < 2, "Should only find at most one property path {0} when looking for null value", childPropertyPath);
                        if (exactMatches.Count() == 1)
                        {
                            instance.Add(new ComplexProperty(property.Name, new ComplexInstance(complexDataType.Definition.FullName, true)));
                        }
                    }
                }
            }
        }
Exemple #2
0
        IEdmTypeReference IDataTypeVisitor <IEdmTypeReference> .Visit(CollectionDataType dataType)
        {
            var elementTypeReference = this.ConvertToEdmTypeReference(dataType.ElementDataType);

            return(elementTypeReference.ToCollectionTypeReference()
                   .Nullable(dataType.IsNullable));
        }
Exemple #3
0
        /// <summary>
        /// Determines whether an entity set is expected to be returned from a function call
        /// </summary>
        /// <param name="function">the function being called</param>
        /// <param name="previousEntitySet">the binding entity set</param>
        /// <param name="returningEntitySet">the expected entity set, if appropriate</param>
        /// <returns>whether or not an entity set is expected</returns>
        public static bool TryGetExpectedActionEntitySet(this Function function, EntitySet previousEntitySet, out EntitySet returningEntitySet)
        {
            ExceptionUtilities.CheckArgumentNotNull(function, "function");

            ServiceOperationAnnotation serviceOperationAnnotation = function.Annotations.OfType <ServiceOperationAnnotation>().Single();
            EntityDataType             entityDataType             = function.ReturnType as EntityDataType;
            CollectionDataType         collectionDataType         = function.ReturnType as CollectionDataType;

            if (collectionDataType != null)
            {
                entityDataType = collectionDataType.ElementDataType as EntityDataType;
            }

            if (entityDataType != null)
            {
                if (serviceOperationAnnotation.EntitySetPath == null)
                {
                    returningEntitySet = function.Model.GetDefaultEntityContainer().EntitySets.Single(es => es.EntityType == entityDataType.Definition);
                }
                else
                {
                    string             navigationPropertyName = serviceOperationAnnotation.EntitySetPath.Substring(serviceOperationAnnotation.EntitySetPath.LastIndexOf('/') + 1);
                    NavigationProperty navigationProperty     = previousEntitySet.EntityType.AllNavigationProperties.Single(np => np.Name == navigationPropertyName);
                    var            associationSets            = function.Model.GetDefaultEntityContainer().AssociationSets.Where(a => a.AssociationType == navigationProperty.Association);
                    AssociationSet associationSet             = associationSets.Single(a => a.Ends.Any(e => e.EntitySet == previousEntitySet));
                    returningEntitySet = associationSet.Ends.Single(es => es.EntitySet != previousEntitySet).EntitySet;
                }

                return(true);
            }

            returningEntitySet = null;
            return(false);
        }
Exemple #4
0
        private IDataGenerator GetPropertyDataGenerator(MemberProperty property, bool isUnique)
        {
            if (property.PropertyType == null)
            {
                throw new TaupoInvalidOperationException(
                          string.Format(CultureInfo.InvariantCulture, "Cannot create data generator for the property '{0}' because it's type is null.", property.Name));
            }

            // First check if custom data generator has been specified for this property and use it.
            var customDataGen = property.Annotations.OfType <DataGeneratorAnnotation>().SingleOrDefault();

            if (customDataGen != null)
            {
                return(customDataGen.DataGenerator);
            }

            // If there is no custom data generator create data generator based on data generation hints.
            List <DataGenerationHint> dataGenHints = property.Annotations.OfType <DataGenerationHintsAnnotation>().SelectMany(a => a.Hints).ToList();

            CollectionDataType collectionDataType = property.PropertyType as CollectionDataType;

            if (collectionDataType != null)
            {
                return(this.ResolveCollectionDataGenerator(collectionDataType, dataGenHints));
            }
            else
            {
                return(this.ResolveNonCollectionDataGenerator(property.PropertyType, property.IsPrimaryKey || isUnique, dataGenHints));
            }
        }
Exemple #5
0
        private IDataGenerator ResolveCollectionDataGenerator(CollectionDataType collectionType, IList <DataGenerationHint> dataGenHints)
        {
            IDataGenerator elementDataGenerator = this.ResolveNonCollectionDataGenerator(collectionType.ElementDataType, false, dataGenHints);
            int            minCount             = dataGenHints.Max <CollectionMinCountHint, int>(0);
            int            maxCount             = dataGenHints.Min <CollectionMaxCountHint, int>(Math.Max(minCount, 10));

            return(new CollectionStructuralDataGenerator(elementDataGenerator, this.Random, minCount, maxCount));
        }
Exemple #6
0
        private static void CachePropertiesValues(List <NamedValue> list, string path, string structuralTypeFullName, IEnumerable <MemberProperty> properties, object obj, IEntityModelObjectServices objectServices)
        {
            var adapter = objectServices.GetObjectAdapter(structuralTypeFullName);

            foreach (MemberProperty property in properties.Where(p => !(p.PropertyType is StreamDataType)))
            {
                object value = adapter.GetMemberValue <object>(obj, property.Name);

                ComplexDataType    complexDataType    = property.PropertyType as ComplexDataType;
                CollectionDataType collectionDataType = property.PropertyType as CollectionDataType;

                if (collectionDataType != null && value != null)
                {
                    var complexElementDataType = collectionDataType.ElementDataType as ComplexDataType;

                    IEnumerable enumerable = value as IEnumerable;
                    ExceptionUtilities.CheckObjectNotNull(enumerable, "Property type is a collection but does not implement IEnumerable. Property path: '{0}'.", path + property.Name);

                    int count = 0;
                    foreach (var collectionElement in enumerable)
                    {
                        string currentPath = path + property.Name + "." + count;
                        if (complexElementDataType == null || collectionElement == null)
                        {
                            list.Add(new NamedValue(currentPath, collectionElement));
                        }
                        else
                        {
                            CachePropertiesValues(
                                list,
                                currentPath + ".",
                                complexElementDataType.Definition.FullName,
                                complexElementDataType.Definition.Properties,
                                collectionElement,
                                objectServices);
                        }

                        count++;
                    }

                    if (count == 0)
                    {
                        list.Add(new NamedValue(path + property.Name, EmptyData.Value));
                    }
                }
                else if (value == null || complexDataType == null)
                {
                    list.Add(new NamedValue(path + property.Name, value));
                }
                else
                {
                    CachePropertiesValues(list, path + property.Name + ".", complexDataType.Definition.FullName, complexDataType.Definition.Properties, value, objectServices);
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Resolves enum definitions in enum data types
        /// </summary>
        /// <param name="dataType">the data type</param>
        /// <returns>Resolved data type</returns>
        public DataType Visit(CollectionDataType dataType)
        {
            var resolvedElementDataType = this.ResolveDataType(dataType.ElementDataType);

            if (resolvedElementDataType != dataType.ElementDataType)
            {
                return(dataType.WithElementDataType(resolvedElementDataType));
            }

            return(dataType);
        }
        private IEnumerable <XObject> GenerateCollection(CollectionDataType collectionDataType)
        {
            var elementDataType = collectionDataType.ElementDataType;

            var result = new List <XObject>();

            result.Add(this.GenerateTypeAttribute(collectionDataType));
            result.Add(this.GenerateNullableAttribute(elementDataType));
            result.AddRange(this.GenerateTypeFacets(elementDataType).Cast <XObject>());

            return(result);
        }
Exemple #9
0
        static SpriteCollectionData GetCollectionData(UnityEngine.Object spriteCollectionData)
        {
            var rawSprites            = GetSpriteDefinitionsRaw(spriteCollectionData);
            SpriteCollectionData data = new SpriteCollectionData();

            data.CollectionName    = (string)CollectionDataType.GetField("spriteCollectionName").GetValue(spriteCollectionData);
            data.MainTexture       = (Texture)((Array)CollectionDataType.GetField("textures").GetValue(spriteCollectionData)).GetValue(0);
            data.GUID              = (string)CollectionDataType.GetField("spriteCollectionName").GetValue(spriteCollectionData);
            data.TextureFilterMode = (FilterMode)CollectionDataType.GetField("textureFilterMode").GetValue(spriteCollectionData);
            data.Version           = (int)CollectionDataType.GetField("version").GetValue(spriteCollectionData);

            data.Definitions = new SpriteDefinitionData[rawSprites.GetLength(0)];

            Debug.Log("Collection Name = " + data.CollectionName);

            Debug.Log("Sprite Count = " + rawSprites.GetLength(0));

            for (int i = 0; i < rawSprites.GetLength(0); i++)
            {
                var rawSprite = rawSprites.GetValue(i);

                var definition = new SpriteDefinitionData();
                definition.Name = (string)SpriteDefinitionType.GetField("name").GetValue(rawSprite) ?? ("unknown_" + i.ToString());

                Debug.Log("Sprite = " + definition.Name);
                Debug.Log("Sprite ID = " + i);

                var oldPositions = (Vector3[])SpriteDefinitionType.GetField("positions").GetValue(rawSprite);
                var newPositions = new Vector2[oldPositions.GetLength(0)];
                for (int j = 0; j < oldPositions.GetLength(0); j++)
                {
                    newPositions[j] = oldPositions[j];
                }
                definition.Positions   = newPositions;
                definition.TextureSize = (Vector2)SpriteDefinitionType.GetField("texelSize").GetValue(rawSprite);
                definition.UVs         = (Vector2[])SpriteDefinitionType.GetField("uvs").GetValue(rawSprite);
                var flippedEnum = SpriteDefinitionType.GetField("flipped").GetValue(rawSprite);
                var enumType    = flippedEnum.GetType();
                definition.Flipped  = Enum.GetName(enumType, flippedEnum) == "Tk2d";
                data.Definitions[i] = definition;
            }

            return(data);
        }
        private XElement GenerateCollectionElementInFunction(CollectionDataType collection, XNamespace xmlNamespace)
        {
            var elementDataType = collection.ElementDataType;

            if (this.IsNominalType(elementDataType))
            {
                return(new XElement(
                           xmlNamespace + "CollectionType",
                           new XAttribute("ElementType", this.GetDataTypeName(elementDataType)),
                           this.GenerateNullableAttributeInFunction(elementDataType),
                           this.GenerateTypeFacets(elementDataType)));
            }
            else
            {
                return(new XElement(
                           xmlNamespace + "CollectionType",
                           this.GenerateTypeElementInFunction(elementDataType, xmlNamespace)));
            }
        }
Exemple #11
0
 /// <summary>
 /// Initializes static members of the DataTypes class.
 /// </summary>
 static DataTypes()
 {
     Integer = new IntegerDataType();
     Stream = new StreamDataType();
     String = new StringDataType();
     Boolean = new BooleanDataType();
     FixedPoint = new FixedPointDataType();
     FloatingPoint = new FloatingPointDataType();
     DateTime = new DateTimeDataType();
     Binary = new BinaryDataType();
     Guid = new GuidDataType();
     TimeOfDay = new TimeOfDayDataType();
     ComplexType = new ComplexDataType();
     EntityType = new EntityDataType();
     CollectionType = new CollectionDataType();
     ReferenceType = new ReferenceDataType();
     RowType = new RowDataType();
     EnumType = new EnumDataType();
     Spatial = new SpatialDataType();
 }
        /// <summary>
        /// Generates property data type information
        /// </summary>
        /// <param name="memberProperty">The property.</param>
        /// <param name="xmlNamespace">The xml namespace.</param>
        /// <returns>XElements and XAttributes for the data type.</returns>
        public IEnumerable <XObject> GeneratePropertyType(MemberProperty memberProperty, XNamespace xmlNamespace)
        {
            List <XObject> result = new List <XObject>();

            if (memberProperty.PropertyType != null)
            {
                CollectionDataType collectionDataType = memberProperty.PropertyType as CollectionDataType;
                if (collectionDataType != null)
                {
                    result.AddRange(this.GenerateCollection(collectionDataType));
                }
                else
                {
                    result.Add(this.GenerateTypeAttribute(memberProperty.PropertyType));
                    result.Add(this.GenerateNullableAttribute(memberProperty.PropertyType));
                    result.AddRange(this.GenerateTypeFacets(memberProperty.PropertyType).Cast <XObject>());
                }
            }

            return(result);
        }
        private void CompareMemberPropertyDatatype(string memberName, DataType expectedDataType, DataType actualDataType)
        {
            PrimitiveDataType  expectedPrimitiveDataType  = expectedDataType as PrimitiveDataType;
            ComplexDataType    expectedComplexDataType    = expectedDataType as ComplexDataType;
            CollectionDataType expectedCollectionDataType = expectedDataType as CollectionDataType;
            SpatialDataType    expectedSpatialDataType    = expectedDataType as SpatialDataType;

            if (expectedPrimitiveDataType != null)
            {
                PrimitiveDataType actualPrimitiveDataType = actualDataType as PrimitiveDataType;
                this.WriteErrorIfFalse(actualPrimitiveDataType != null, "Expected member '{0}' with primitiveDataType '{1}' instead of '{2}'", memberName, expectedPrimitiveDataType, actualDataType);

                if (expectedSpatialDataType != null)
                {
                    SpatialDataType actualSpatialDataType = actualDataType as SpatialDataType;
                    this.CompareSpatialDataType(memberName, expectedSpatialDataType, actualSpatialDataType);
                }
            }
            else if (expectedComplexDataType != null)
            {
                ComplexDataType actualComplexDataType = actualDataType as ComplexDataType;
                if (!this.WriteErrorIfFalse(expectedComplexDataType != null, "Expected member '{0}' with complexDataType '{1}' instead of '{2}'", memberName, expectedComplexDataType, actualDataType))
                {
                    this.WriteErrorIfFalse(expectedComplexDataType.Definition.Name == actualComplexDataType.Definition.Name, "Expected member '{0}' with complexType Name '{1}' not '{2}'", memberName, expectedComplexDataType.Definition.Name, actualComplexDataType.Definition.Name);
                }
            }
            else
            {
                CollectionDataType actualCollectionDataType = actualDataType as CollectionDataType;
                if (!this.WriteErrorIfFalse(expectedCollectionDataType != null, "Expected member '{0}' with collectionType '{1}' instead of '{2}'", memberName, expectedCollectionDataType, actualDataType))
                {
                    this.CompareMemberPropertyDatatype(memberName, expectedCollectionDataType.ElementDataType, actualCollectionDataType.ElementDataType);
                }
            }

            // Complex properties need not be handled specially as they can also have IsNullable = true. The scenario for complex property is given below:
            // For reflection and custom: If the DSV <= 2 then it should always contain Nullable=’false’. For DSV >= 3, it should always contain Nullable=’true’
            // For EF: For all DSV values it should always contain Nullable=’false’ since complex types are always non-nullable in EF.
            this.WriteErrorIfFalse(expectedDataType.IsNullable == actualDataType.IsNullable, "Expected member '{0}' to have an IsNullable of '{1}' instead of '{2}'", memberName, expectedDataType.IsNullable, actualDataType.IsNullable);
        }
Exemple #14
0
 /// <summary>
 /// Visits a collection data type
 /// </summary>
 /// <param name="dataType">The data type to visit</param>
 /// <returns>The backing type for the data type</returns>
 public CodeTypeReference Visit(CollectionDataType dataType)
 {
     throw new TaupoNotSupportedException("Collection backing types cannot be determined without a property");
 }
 /// <summary>
 /// Resolves the specified type into its type reference.
 /// </summary>
 /// <param name="dataType">The data type.</param>
 /// <returns>CodeTypeReference that should be used in code to refer to the type.</returns>
 public virtual CodeTypeReference Visit(CollectionDataType dataType)
 {
     return(Code.GenericType("ICollection", this.Resolve(dataType.ElementDataType)));
 }
 /// <summary>
 /// Visits the specified collection data type.
 /// </summary>
 /// <param name="dataType">Collection data type.</param>
 /// <returns>Implementation-specific value.</returns>
 public IList <string> Visit(CollectionDataType dataType)
 {
     return(emptyList);
 }
 /// <summary>
 /// Visits the specified collection type.
 /// </summary>
 /// <param name="dataType">Data type.</param>
 /// <returns>A clone of the specified <see cref="DataType"/>.</returns>
 public DataType Visit(CollectionDataType dataType)
 {
     return(new CollectionDataType()
            .Nullable(dataType.IsNullable)
            .WithElementDataType(dataType.ElementDataType.Accept(this)));
 }
Exemple #18
0
        /// <summary>
        /// Resolves the specified entity model schema type and returns the Edm model type for it.
        /// </summary>
        /// <param name="model">The model to get the type from.</param>
        /// <param name="schemaType">The entity model schema type to resolve.</param>
        /// <returns>The resolved type for the specified <paramref name="schemaType"/>.</returns>
        public static IEdmTypeReference ResolveEntityModelSchemaType(IEdmModel model, DataType schemaType)
        {
            if (schemaType == null)
            {
                return(null);
            }

            PrimitiveDataType primitiveDataType = schemaType as PrimitiveDataType;

            if (primitiveDataType != null)
            {
                return(GetPrimitiveTypeReference(primitiveDataType));
            }

            if (model == null)
            {
                return(null);
            }

            EntityDataType entityDataType = schemaType as EntityDataType;

            if (entityDataType != null)
            {
                IEdmNamedElement edmType = model.FindType(entityDataType.Definition.FullName);
                ExceptionUtilities.Assert(
                    edmType != null,
                    "The expected entity type '{0}' was not found in the entity model for this test.",
                    entityDataType.Definition.FullName);

                IEdmEntityType entityType = edmType as IEdmEntityType;
                ExceptionUtilities.Assert(
                    entityType != null,
                    "The expected entity type '{0}' is not defined as entity type in the test's metadata.",
                    entityDataType.Definition.FullName);
                return(entityType.ToTypeReference());
            }

            ComplexDataType complexDataType = schemaType as ComplexDataType;

            if (complexDataType != null)
            {
                return(GetComplexType(model, complexDataType));
            }

            CollectionDataType collectionDataType = schemaType as CollectionDataType;

            if (collectionDataType != null)
            {
                DataType          collectionElementType = collectionDataType.ElementDataType;
                PrimitiveDataType primitiveElementType  = collectionElementType as PrimitiveDataType;
                if (primitiveElementType != null)
                {
                    IEdmPrimitiveTypeReference primitiveElementTypeReference = GetPrimitiveTypeReference(primitiveElementType);
                    return(primitiveElementTypeReference.ToCollectionTypeReference());
                }

                ComplexDataType complexElementType = collectionElementType as ComplexDataType;
                if (complexElementType != null)
                {
                    IEdmComplexTypeReference complexElementTypeReference = GetComplexType(model, complexElementType);
                    return(complexElementTypeReference.ToCollectionTypeReference());
                }

                EntityDataType entityElementType = collectionElementType as EntityDataType;
                if (entityElementType != null)
                {
                    IEdmEntityTypeReference entityElementTypeReference = GetEntityType(model, entityElementType);
                    return(entityElementTypeReference.ToCollectionTypeReference());
                }

                throw new NotSupportedException("Collection types only support primitive, complex, and entity element types.");
            }

            StreamDataType streamType = schemaType as StreamDataType;

            if (streamType != null)
            {
                Type systemType = streamType.GetFacet <PrimitiveClrTypeFacet>().Value;
                ExceptionUtilities.Assert(systemType == typeof(Stream), "Expected the system type 'System.IO.Stream' for a stream reference property.");
                return(MetadataUtils.GetPrimitiveTypeReference(systemType));
            }

            throw new NotImplementedException("Unrecognized schema type " + schemaType.GetType().Name + ".");
        }
Exemple #19
0
 /// <summary>
 /// Gets the short qualified Edm name
 /// </summary>
 /// <param name="dataType">The data type.</param>
 /// <returns>short qualified Edm name</returns>
 public string Visit(CollectionDataType dataType)
 {
     throw new TaupoInvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} does not have Edm short qualified name.", dataType));
 }
Exemple #20
0
            /// <summary>
            /// Visits the specified collection type.
            /// </summary>
            /// <param name="dataType">Data type.</param>
            /// <returns>the corresponding default query type.</returns>
            public QueryType Visit(CollectionDataType dataType)
            {
                QueryType elementType = this.GetDefaultQueryType(dataType.ElementDataType);

                return(elementType.CreateCollectionType());
            }
 /// <summary>
 /// Visits the specified data type.
 /// </summary>
 /// <param name="dataType">The data type.</param>
 /// <returns>Name of the collection type.</returns>
 string IDataTypeVisitor <string> .Visit(CollectionDataType dataType)
 {
     return("Collection(" + this.GetTypeName(dataType.ElementDataType) + ")");
 }
Exemple #22
0
 /// <summary>
 /// Visits the specified data type.
 /// </summary>
 /// <param name="dataType">The data type to visit.</param>
 /// <returns>The data types</returns>
 public IEnumerable <DataType> Visit(CollectionDataType dataType)
 {
     return(this.Gather(dataType.ElementDataType).Concat(dataType));
 }
 /// <summary>
 /// Visits the specified collection type.
 /// </summary>
 /// <param name="dataType">Data type.</param>
 /// <returns>the data type with all references resolved</returns>
 public DataType Visit(CollectionDataType dataType)
 {
     return(DataTypes.CollectionType
            .Nullable(dataType.IsNullable)
            .WithElementDataType(this.ResolveReferencesIn(dataType.ElementDataType)));
 }