Skip to content

Commit

Permalink
Add OverrideBehaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Jan 6, 2024
1 parent cabecd5 commit 7821784
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/Immediate.Handlers/Generators/ImmediateHandlersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private sealed record Handler

public required EquatableReadOnlyList<Parameter> Parameters { get; init; }

public EquatableReadOnlyList<Behavior>? OverrideBehaviors { get; init; }
public EquatableReadOnlyList<Behavior?>? OverrideBehaviors { get; init; }
public RenderMode? OverrideRenderMode { get; init; }

}
Expand Down Expand Up @@ -127,11 +127,25 @@ CancellationToken cancellationToken
var compilation = semanticModel.Compilation;
cancellationToken.ThrowIfCancellationRequested();

var attr = context.Attributes[0];
if (attr.ConstructorArguments.Length != 1)
return ParseBehaviors(context.Attributes[0], compilation, cancellationToken);
}

private static EquatableReadOnlyList<Behavior?> ParseBehaviors(
AttributeData attribute,
Compilation compilation,
CancellationToken cancellationToken
)
{
cancellationToken.ThrowIfCancellationRequested();
var behaviorType = typeof(Behavior<,>);
var behaviorTypeSymbol = compilation.GetTypeByMetadataName(behaviorType.FullName);
if (behaviorTypeSymbol is null)
return [];

if (attribute.ConstructorArguments.Length != 1)
return [];

var ca = attr.ConstructorArguments[0];
var ca = attribute.ConstructorArguments[0];
var arrayTypeSymbol = compilation.CreateArrayTypeSymbol(
compilation.GetTypeByMetadataName("System.Type")!, 1)!;
if (!SymbolEqualityComparer.Default.Equals(
Expand All @@ -142,12 +156,6 @@ CancellationToken cancellationToken
return [];
}

cancellationToken.ThrowIfCancellationRequested();
var behaviorType = typeof(Behavior<,>);
var behaviorTypeSymbol = compilation.GetTypeByMetadataName(behaviorType.FullName);
if (behaviorTypeSymbol is null)
return [];

cancellationToken.ThrowIfCancellationRequested();
return ca.Values
.Select(v =>
Expand Down Expand Up @@ -287,6 +295,9 @@ CancellationToken cancellationToken
cancellationToken.ThrowIfCancellationRequested();
var renderMode = GetOverrideRenderMode(symbol);

cancellationToken.ThrowIfCancellationRequested();
var behaviors = GetOverrideBehaviors(symbol, context.SemanticModel.Compilation, cancellationToken);

cancellationToken.ThrowIfCancellationRequested();
return new()
{
Expand All @@ -300,6 +311,7 @@ CancellationToken cancellationToken
Parameters = parameters,

OverrideRenderMode = renderMode,
OverrideBehaviors = behaviors,
};
}

Expand All @@ -309,6 +321,12 @@ CancellationToken cancellationToken
? ParseRenderMode(rma)
: null;

private static EquatableReadOnlyList<Behavior?>? GetOverrideBehaviors(INamedTypeSymbol symbol, Compilation compilation, CancellationToken cancellationToken) =>
symbol.GetAttribute("Immediate.Handlers.Shared.BehaviorsAttribute")
is { } ba
? ParseBehaviors(ba, compilation, cancellationToken)
: null;

private static GenericType BuildGenericType(INamedTypeSymbol type)
{
var name = type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
Expand Down

0 comments on commit 7821784

Please sign in to comment.