private void HandleApiObjectTypeAttribute(IApiSchemaBuilder apiSchemaBuilder, ApiConventionSettings apiConventionSettings, ApiPrecedenceStack apiPrecedenceStack, Type clrType) { Contract.Requires(apiSchemaBuilder != null); Contract.Requires(apiConventionSettings != null); Contract.Requires(clrType != null); var apiObjectTypeAttribute = (ApiObjectTypeAttribute)Attribute.GetCustomAttribute(clrType, typeof(ApiObjectTypeAttribute)); if (apiObjectTypeAttribute == null) { return; } if (ClrTypeDiscoveryRules.CanAddApiObjectType(clrType) == false) { return; } var apiDiscoverySettings = apiConventionSettings.ApiDiscoverySettings; var apiAnnotationDiscoveryPredicateResult = apiDiscoverySettings.ApiAnnotationDiscoveryPredicate?.Invoke(clrType) ?? true; if (apiAnnotationDiscoveryPredicateResult == false) { return; } if (apiObjectTypeAttribute.Excluded) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiSchemaBuilder.Exclude(clrType); apiPrecedenceStack.Pop(); return; } IApiObjectTypeBuilder ApiObjectTypeConfiguration(IApiObjectTypeBuilder apiObjectTypeBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); if (String.IsNullOrWhiteSpace(apiObjectTypeAttribute.Name) == false) { apiObjectTypeBuilder.HasName(apiObjectTypeAttribute.Name); } if (String.IsNullOrWhiteSpace(apiObjectTypeAttribute.Description) == false) { apiObjectTypeBuilder.HasDescription(apiObjectTypeAttribute.Description); } apiPrecedenceStack.Pop(); return(apiObjectTypeBuilder); } apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiSchemaBuilder.ApiObjectType(clrType, ApiObjectTypeConfiguration); apiPrecedenceStack.Pop(); }
// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private static void HandleApiEnumerationValueAttribute(IApiEnumerationTypeBuilder apiEnumerationTypeBuilder, ApiPrecedenceStack apiPrecedenceStack, FieldInfo clrFieldInfo) { Contract.Requires(apiEnumerationTypeBuilder != null); Contract.Requires(apiPrecedenceStack != null); Contract.Requires(clrFieldInfo != null); var apiEnumerationValueAttribute = (ApiEnumerationValueAttribute)Attribute.GetCustomAttribute(clrFieldInfo, typeof(ApiEnumerationValueAttribute)); if (apiEnumerationValueAttribute == null) { return; } IApiEnumerationValueBuilder ApiEnumerationValueConfiguration(IApiEnumerationValueBuilder apiEnumerationValueBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); var apiName = apiEnumerationValueAttribute.Name; if (apiName != null) { apiEnumerationValueBuilder.HasName(apiName); } var apiDescription = apiEnumerationValueAttribute.Description; if (apiDescription != null) { apiEnumerationValueBuilder.HasDescription(apiDescription); } apiPrecedenceStack.Pop(); return(apiEnumerationValueBuilder); } var clrEnumerationType = apiEnumerationTypeBuilder.ClrType; var clrName = clrFieldInfo.Name; var clrOrdinal = (int)Enum.Parse(clrEnumerationType, clrName); apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiEnumerationTypeBuilder.ApiEnumerationValue(clrName, clrOrdinal, ApiEnumerationValueConfiguration); apiPrecedenceStack.Pop(); }
private static void HandleApiRelationshipAttribute(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiPrecedenceStack apiPrecedenceStack, PropertyInfo clrPropertyInfo) { Contract.Requires(apiObjectTypeBuilder != null); Contract.Requires(clrPropertyInfo != null); var apiRelationshipAttribute = Attribute.GetCustomAttribute(clrPropertyInfo, typeof(ApiRelationshipAttribute)); if (apiRelationshipAttribute == null) { return; } var clrPropertyName = clrPropertyInfo.Name; var clrPropertyType = clrPropertyInfo.PropertyType; apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiObjectTypeBuilder.ApiRelationship(clrPropertyName, clrPropertyType); apiPrecedenceStack.Pop(); }
// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private static void HandleApiPropertyAttribute(IApiObjectTypeBuilder apiObjectTypeBuilder, ApiPrecedenceStack apiPrecedenceStack, PropertyInfo clrPropertyInfo) { Contract.Requires(apiObjectTypeBuilder != null); Contract.Requires(apiPrecedenceStack != null); Contract.Requires(clrPropertyInfo != null); var apiPropertyAttribute = (ApiPropertyAttribute)Attribute.GetCustomAttribute(clrPropertyInfo, typeof(ApiPropertyAttribute)); if (apiPropertyAttribute == null) { return; } var clrPropertyName = clrPropertyInfo.Name; var clrPropertyType = clrPropertyInfo.PropertyType; if (apiPropertyAttribute.Excluded) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiObjectTypeBuilder.Exclude(clrPropertyName); apiPrecedenceStack.Pop(); return; } IApiPropertyBuilder ApiPropertyConfiguration(IApiPropertyBuilder apiPropertyBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); if (String.IsNullOrWhiteSpace(apiPropertyAttribute.Name) == false) { apiPropertyBuilder.HasName(apiPropertyAttribute.Name); } if (String.IsNullOrWhiteSpace(apiPropertyAttribute.Description) == false) { apiPropertyBuilder.HasDescription(apiPropertyAttribute.Description); } if (apiPropertyAttribute.Required) { apiPropertyBuilder.IsRequired(); } // ReSharper disable once InvertIf if (apiPropertyAttribute.ItemRequired) { IApiCollectionTypeBuilder ApiCollectionTypeConfiguration(IApiCollectionTypeBuilder apiCollectionTypeBuilder) { apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiCollectionTypeBuilder.ItemIsRequired(); apiPrecedenceStack.Pop(); return(apiCollectionTypeBuilder); } apiPropertyBuilder.ApiCollectionType(ApiCollectionTypeConfiguration); } apiPrecedenceStack.Pop(); return(apiPropertyBuilder); } apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation); apiObjectTypeBuilder.ApiProperty(clrPropertyName, clrPropertyType, ApiPropertyConfiguration); apiPrecedenceStack.Pop(); }