private static IApiIdentity CreateApiIdentity(ApiMutableObjectType apiMutableObjectType, IEnumerable <IApiProperty> apiProperties) { Contract.Requires(apiMutableObjectType != null); Contract.Requires(apiProperties != null); var clrIdentityProperty = apiMutableObjectType.ClrIdentityProperty; if (clrIdentityProperty == null) { return(null); } var clrIdentityPropertyName = clrIdentityProperty.ClrPropertyName; if (String.IsNullOrWhiteSpace(clrIdentityPropertyName)) { return(null); } var apiIdentityProperty = (ApiProperty)apiProperties.SingleOrDefault(x => x.ClrName == clrIdentityPropertyName); if (apiIdentityProperty == null) { var clrTypeName = apiMutableObjectType.ClrObjectType.Name; var message = $"Unable to create API identity for an API object type [clrType={clrTypeName}] because the configured API identity property [clrName={clrIdentityPropertyName}] does not exist."; throw new ApiSchemaException(message); } var apiIdentity = ApiTypeFactory.CreateApiIdentity(apiIdentityProperty); ApiFrameworkLog.Debug($"Created {apiIdentity}".Indent(IndentConstants.ApiMutableObjectTypeIdentity)); return(apiIdentity); }
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); }
private static IApiScalarType CreateApiScalarType(ApiMutableScalarType apiMutableScalarType) { Contract.Requires(apiMutableScalarType != null); var apiName = apiMutableScalarType.ApiName; var apiDescription = apiMutableScalarType.ApiDescription; var clrScalarType = apiMutableScalarType.ClrScalarType; var apiScalarType = ApiTypeFactory.CreateApiScalarType(apiName, apiDescription, clrScalarType); return(apiScalarType); }
private static IApiEnumerationType CreateApiEnumerationType(ApiMutableEnumerationType apiMutableEnumerationType) { Contract.Requires(apiMutableEnumerationType != null); var apiName = apiMutableEnumerationType.ApiName; var apiDescription = apiMutableEnumerationType.ApiDescription; var apiEnumerationValues = CreateApiEnumValues(apiMutableEnumerationType); var clrEnumerationType = apiMutableEnumerationType.ClrEnumerationType; var apiEnumerationType = ApiTypeFactory.CreateApiEnumerationType(apiName, apiDescription, apiEnumerationValues, clrEnumerationType); return(apiEnumerationType); }
private static IApiEnumerationValue CreateApiEnumerationValue(ApiMutableEnumerationValue apiMutableEnumerationValue) { Contract.Requires(apiMutableEnumerationValue != null); var apiName = apiMutableEnumerationValue.ApiName; var apiDescription = apiMutableEnumerationValue.ApiDescription; var clrName = apiMutableEnumerationValue.ClrName; var clrOrdinal = apiMutableEnumerationValue.ClrOrdinal; var apiEnumerationValue = ApiTypeFactory.CreateApiEnumerationValue(apiName, apiDescription, clrName, clrOrdinal); return(apiEnumerationValue); }
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)); }
private static IApiObjectType CreateApiObjectType(ApiMutableObjectType apiMutableObjectType, ApiSchemaProxy apiSchemaProxy) { Contract.Requires(apiMutableObjectType != null); Contract.Requires(apiSchemaProxy != null); var apiName = apiMutableObjectType.ApiName; var apiDescription = apiMutableObjectType.ApiDescription; var apiProperties = CreateApiProperties(apiMutableObjectType, apiSchemaProxy); var apiIdentity = CreateApiIdentity(apiMutableObjectType, apiProperties); var apiRelationships = CreateApiRelationships(apiMutableObjectType, apiSchemaProxy, apiProperties, apiIdentity); var clrObjectType = apiMutableObjectType.ClrObjectType; var apiObjectType = ApiTypeFactory.CreateApiObjectType(apiName, apiDescription, apiProperties, apiIdentity, apiRelationships, clrObjectType); return(apiObjectType); }
private static IApiSchema CreateApiSchema(ApiMutableSchema apiMutableSchema, ApiPrecedenceStack apiPrecedenceStack) { Contract.Requires(apiMutableSchema != null); Contract.Requires(apiPrecedenceStack != null); var apiSchemaProxy = new ApiSchemaProxy(); // Create API object types before other API types so implicit primitive property types (enums, scalars, etc.) are added to the mutable schema object. var apiObjectTypes = CreateApiObjectTypes(apiMutableSchema, apiSchemaProxy); var apiEnumerationTypes = CreateApiEnumerationTypes(apiMutableSchema, apiPrecedenceStack, apiSchemaProxy); var apiScalarTypes = CreateApiScalarTypes(apiMutableSchema, apiPrecedenceStack, apiSchemaProxy); var apiSchemaName = apiMutableSchema.ApiName; var apiSchema = ApiTypeFactory.CreateApiSchema(apiSchemaName, apiEnumerationTypes, apiObjectTypes, apiScalarTypes); apiSchemaProxy.Initialize(apiSchema); return(apiSchema); }
// 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); }