Ejemplo n.º 1
0
        private static IApiCollectionType CreateApiCollectionType(ApiMutableCollectionType apiMutableCollectionType, ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableCollectionType != null);
            Contract.Requires(apiSchemaProxy != null);

            var apiMutableSchema     = apiMutableCollectionType.ApiMutableSchema;
            var apiItemTypeKind      = apiMutableCollectionType.ApiItemTypeKind;
            var apiItemTypeModifiers = apiMutableCollectionType.ApiItemTypeModifiers;
            var clrItemType          = apiMutableCollectionType.ClrItemType;

            switch (apiItemTypeKind)
            {
            case ApiTypeKind.Scalar:
            {
                apiMutableSchema.AddClrImplicitScalarType(clrItemType);
                break;
            }

            case ApiTypeKind.Enumeration:
            {
                apiMutableSchema.AddClrImplicitEnumerationType(clrItemType);
                break;
            }
            }

            var apiItemTypeResolver = new ApiSchemaProxyTypeResolver(apiSchemaProxy, apiItemTypeKind, clrItemType);
            var apiCollectionType   = ApiTypeFactory.CreateApiCollectionType(apiItemTypeResolver, apiItemTypeModifiers);

            return(apiCollectionType);
        }
Ejemplo n.º 2
0
        private static IApiProperty CreateApiProperty(ApiMutableObjectType apiMutableObjectType,
                                                      ApiMutableProperty apiMutableProperty,
                                                      ApiSchemaProxy apiSchemaProxy)
        {
            Contract.Requires(apiMutableObjectType != null);
            Contract.Requires(apiMutableProperty != null);
            Contract.Requires(apiSchemaProxy != null);

            var apiMutableSchema = apiMutableProperty.ApiMutableSchema;
            var apiName          = apiMutableProperty.ApiName;
            var apiDescription   = apiMutableProperty.ApiDescription;
            var apiTypeKind      = apiMutableProperty.ApiTypeKind;
            var apiTypeModifiers = apiMutableProperty.ApiTypeModifiers;
            var clrName          = apiMutableProperty.ClrName;
            var clrType          = apiMutableProperty.ClrType;

            switch (apiTypeKind)
            {
            case ApiTypeKind.Enumeration:
            {
                apiMutableSchema.AddClrImplicitEnumerationType(clrType);
                break;
            }

            case ApiTypeKind.Collection:
            {
                // Create the known concrete API collection type.
                var apiCollectionTypeConfiguration = apiMutableProperty.ApiCollectionTypeConfiguration;
                var apiCollectionType = apiCollectionTypeConfiguration.Create(apiMutableSchema, apiSchemaProxy);
                return(ApiTypeFactory.CreateApiProperty(apiName, apiDescription, apiCollectionType, apiTypeModifiers, clrName));
            }

            case ApiTypeKind.Scalar:
            {
                apiMutableSchema.AddClrImplicitScalarType(clrType);
                break;
            }
            }

            // API identity properties will always be required.
            var clrIdentityPropertyName = apiMutableObjectType?.ClrIdentityProperty?.ClrPropertyName;

            if (clrName == clrIdentityPropertyName)
            {
                apiTypeModifiers |= ApiTypeModifiers.Required;
            }

            var apiTypeResolver = new ApiSchemaProxyTypeResolver(apiSchemaProxy, apiTypeKind, clrType);

            return(ApiTypeFactory.CreateApiProperty(apiName, apiDescription, apiTypeResolver, apiTypeModifiers, clrName));
        }
        // ReSharper disable once ReturnTypeCanBeEnumerable.Local
        private static IReadOnlyCollection <IApiRelationship> CreateApiRelationships(ApiMutableObjectType apiMutableObjectType,
                                                                                     ApiSchemaProxy apiSchemaProxy,
                                                                                     IEnumerable <IApiProperty> apiProperties,
                                                                                     IApiIdentity apiIdentity)
        {
            Contract.Requires(apiMutableObjectType != null);
            Contract.Requires(apiSchemaProxy != null);
            Contract.Requires(apiProperties != null);

            // If this API object type has no identity, it may not contain any relationships.
            if (apiIdentity == null)
            {
                return(null);
            }

            var apiMutableSchema = apiMutableObjectType.ApiMutableSchema;
            var clrRelationshipPropertyCollection = apiMutableObjectType.ClrRelationshipPropertyCollection;
            var apiRelationships = clrRelationshipPropertyCollection
                                   .Where(x =>
            {
                // Can not create an API relationship if the related API object type does not have identity (must be an API resource type).
                var clrPropertyType     = x.ClrPropertyType;
                var apiPropertyTypeKind = clrPropertyType.GetApiTypeKind(out var clrPropertyItemType);
                switch (apiPropertyTypeKind)
                {
                case ApiTypeKind.Object:
                    {
                        var isApiResourceType = apiMutableSchema.ClrResourceTypes.Contains(clrPropertyType);
                        if (!isApiResourceType)
                        {
                            return(false);
                        }

                        break;
                    }

                case ApiTypeKind.Collection:
                    {
                        var apiItemTypeKind = clrPropertyItemType.GetApiTypeKind();
                        switch (apiItemTypeKind)
                        {
                        case ApiTypeKind.Collection:
                            {
                                // Unable to handle collections within collections.
                                var message = $"Unable to create API relationship for an API property [{nameof(x.ClrPropertyName)}={x.ClrPropertyName}] that contains collections within collections";
                                throw new ApiSchemaException(message);
                            }
                        }

                        var isApiResourceType = apiMutableSchema.ClrResourceTypes.Contains(clrPropertyItemType);
                        if (!isApiResourceType)
                        {
                            return(false);
                        }

                        break;
                    }

                default:
                    {
                        return(false);
                    }
                }

                return(true);
            })
                                   .Select(x =>
            {
                var clrPropertyName     = x.ClrPropertyName;
                var clrPropertyType     = x.ClrPropertyType;
                var apiProperty         = apiProperties.Single(y => y.ClrName == clrPropertyName);
                var apiPropertyTypeKind = clrPropertyType.GetApiTypeKind(out var clrPropertyItemType);

                ApiRelationshipCardinality apiCardinality;
                Type clrType;
                switch (apiPropertyTypeKind)
                {
                case ApiTypeKind.Object:
                    {
                        apiCardinality = ApiRelationshipCardinality.ToOne;
                        clrType        = clrPropertyType;
                        break;
                    }

                case ApiTypeKind.Collection:
                    {
                        apiCardinality = ApiRelationshipCardinality.ToMany;
                        clrType        = clrPropertyItemType;
                        break;
                    }

                default:
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                }

                var apiRelatedTypeResolver = new ApiSchemaProxyTypeResolver(apiSchemaProxy, ApiTypeKind.Object, clrType);
                var apiRelationship        = ApiTypeFactory.CreateApiRelationship(apiProperty, apiCardinality, apiRelatedTypeResolver);

                return(apiRelationship);
            })
                                   .ToList();

            foreach (var apiRelationship in apiRelationships)
            {
                ApiFrameworkLog.Debug($"Created {apiRelationship}".Indent(IndentConstants.ApiMutableObjectTypeRelationship));
            }

            return(apiRelationships);
        }