Skip to content

Commit

Permalink
fix: Limit the number of types to process (#816)
Browse files Browse the repository at this point in the history
- Limit the amount of types to process, ignore interface, abstract and
generic classes
- This change enables to write a base webhook for multiple types instead
of duplicating the webhook logic
  • Loading branch information
dsempi authored Oct 22, 2024
1 parent 44d26f2 commit 092ae45
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/KubeOps.Cli/Transpilation/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,56 +147,42 @@ public static Task<MetadataLoadContext> ForSolution(
return mlc;
});

public static IEnumerable<Type> GetEntities(this MetadataLoadContext context) => context.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
public static IEnumerable<Type> GetEntities(this MetadataLoadContext context) => context
.GetTypesToInspect()
.Select(t => (t, attrs: CustomAttributeData.GetCustomAttributes(t)))
.Where(e => e.attrs.Any(a => a.AttributeType.Name == nameof(KubernetesEntityAttribute)) &&
e.attrs.All(a => a.AttributeType.Name != nameof(IgnoreAttribute)))
.Select(e => e.t);

public static IEnumerable<CustomAttributeData> GetRbacAttributes(this MetadataLoadContext context)
{
foreach (var type in context.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.SelectMany(t =>
t.GetCustomAttributesData<GenericRbacAttribute>()))
{
yield return type;
}

foreach (var type in context.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.SelectMany(t =>
t.GetCustomAttributesData<EntityRbacAttribute>()))
{
yield return type;
}
}
public static IEnumerable<CustomAttributeData> GetRbacAttributes(this MetadataLoadContext context) => context
.GetTypesToInspect().SelectMany(t => t.GetCustomAttributesData<GenericRbacAttribute>().Concat(t.GetCustomAttributesData<EntityRbacAttribute>()));

public static IEnumerable<ValidationWebhook> GetValidatedEntities(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.GetTypesToInspect()
.Where(t => t.BaseType?.Name == typeof(ValidationWebhook<>).Name &&
t.BaseType?.Namespace == typeof(ValidationWebhook<>).Namespace)
.Distinct()
.Select(t => new ValidationWebhook(t, context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata));

public static IEnumerable<MutationWebhook> GetMutatedEntities(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.GetTypesToInspect()
.Where(t => t.BaseType?.Name == typeof(MutationWebhook<>).Name &&
t.BaseType?.Namespace == typeof(MutationWebhook<>).Namespace)
.Distinct()
.Select(t => new MutationWebhook(t, context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata));

public static IEnumerable<EntityMetadata> GetConvertedEntities(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.GetTypesToInspect()
.Where(t => t.BaseType?.Name == typeof(ConversionWebhook<>).Name &&
t.BaseType?.Namespace == typeof(ConversionWebhook<>).Namespace)
.Distinct()
.Select(t => context.ToEntityMetadata(t.BaseType!.GenericTypeArguments[0]).Metadata);

private static IEnumerable<TypeInfo> GetTypesToInspect(this MetadataLoadContext context) => context
.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.Where(t => !t.IsInterface && !t.IsAbstract && !t.IsGenericType);

#if NET7_0_OR_GREATER
[GeneratedRegex(".*")]
private static partial Regex DefaultRegex();
Expand Down

0 comments on commit 092ae45

Please sign in to comment.