Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Pure.DI/InterfaceGeneration/GeneratedInterfaceDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Pure.DI;

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

internal sealed class GeneratedInterfaceDetails(
AttributeData? generationAttribute,
ITypeSymbol typeSymbol,
ClassDeclarationSyntax classSyntax)
{
public string NamespaceName { get; } = PrepareNamespaceValue(generationAttribute, typeSymbol.ContainingNamespace.ToDisplayString());

public string InterfaceName { get; } = PrepareValue(
generationAttribute,
InterfaceGenerator.InterfaceParameterName,
$"I{classSyntax.Identifier.Text}");

public string AccessLevel { get; } = PrepareValue(
generationAttribute,
InterfaceGenerator.AsInternalParameterName,
false)
? "internal"
: "public";

private static string PrepareNamespaceValue(AttributeData? generationAttribute, string defaultValue)
{
var value = PrepareValue(generationAttribute, InterfaceGenerator.NamespaceParameterName, defaultValue);
return value ?? defaultValue;
}

private static T PrepareValue<T>(AttributeData? generationAttribute, string key, T defaultValue)
{
var parameterSymbol = generationAttribute?.AttributeConstructor?.Parameters.SingleOrDefault(x => x.Name == key);
if (parameterSymbol != null)
{
var index = generationAttribute!.AttributeConstructor!.Parameters.IndexOf(parameterSymbol);
var result = generationAttribute.ConstructorArguments[index].Value;
if (result != null)
{
return (T)result;
}
}

return defaultValue;
}
}
Loading