Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelisk committed Oct 24, 2024
1 parent 1551b3a commit 01ebe74
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Codelisk.GeneratorAttributes.WebAttributes.Dto
{
[AttributeUsage(AttributeTargets.Interface, AllowMultiple = true, Inherited = true)]
public class DtoBaseInterfaceAttribute : Attribute { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,99 @@ this IncrementalGeneratorInitializationContext context
return dtos;
}

public static IncrementalValueProvider<
ImmutableArray<InterfaceDeclarationSyntax>
> BaseDtoInterfaces(this IncrementalGeneratorInitializationContext context)
{
var dtos = context
.SyntaxProvider.ForAttributeWithMetadataName(
typeof(DtoBaseInterfaceAttribute).FullName,
static (n, _) => n is InterfaceDeclarationSyntax,
static (context, cancellationToken) =>
{
InterfaceDeclarationSyntax interfaceDeclarationSyntax =
(InterfaceDeclarationSyntax)context.TargetNode;

return interfaceDeclarationSyntax.HasAttribute<DtoBaseInterfaceAttribute>()
? interfaceDeclarationSyntax
: null;
}
)
.Where(static typeDeclaration => typeDeclaration is not null)
.Collect();

return dtos;
}

public static IEnumerable<BaseTypeSyntax> DtoInterfaces(
this RecordDeclarationSyntax dto,
ImmutableArray<RecordDeclarationSyntax> baseDtos,
ImmutableArray<InterfaceDeclarationSyntax> baseInterfaces
)
{
var allInterfaces = new HashSet<BaseTypeSyntax>();
FindInterfacesRecursively(dto, baseDtos, baseInterfaces, allInterfaces);
return allInterfaces;
}

private static void FindInterfacesRecursively(
RecordDeclarationSyntax dto,
ImmutableArray<RecordDeclarationSyntax> baseDtos,
ImmutableArray<InterfaceDeclarationSyntax> baseInterfaces,
HashSet<BaseTypeSyntax> allInterfaces
)
{
// Überprüfe, ob das aktuelle DTO Interfaces hat
if (dto.BaseList != null)
{
foreach (var baseType in dto.BaseList.Types)
{
// Überprüfe auch, ob das BaseType selbst ein Interface ist
if (baseInterfaces.Any(i => i.Identifier.Text == baseType.Type.ToString()))
{
allInterfaces.Add(baseType);
}

var typeName = baseType.Type.GetName();
// Finde die Basis-DTO
var baseDto = baseDtos.FirstOrDefault(b =>
b.Identifier.Text == baseType.Type.GetName()
);

// Wenn die Basis-DTO gefunden wurde, die Interfaces hinzufügen
if (baseDto != null)
{
// Füge alle Interfaces der Basisklasse hinzu
AddInterfaces(baseDto.BaseList, baseInterfaces, allInterfaces);

// Rekursiv die Interfaces von den Basisklassen der Basisklasse hinzufügen
FindInterfacesRecursively(baseDto, baseDtos, baseInterfaces, allInterfaces);
}
}
}
}

private static void AddInterfaces(
BaseListSyntax baseList,
ImmutableArray<InterfaceDeclarationSyntax> baseInterfaces,
HashSet<BaseTypeSyntax> allInterfaces
)
{
int count = 0;
foreach (var baseType in baseList.Types)
{
if (count > 0)
{
allInterfaces.Add(baseType);
}
if (baseInterfaces.Any(i => i.Identifier.Text == baseType.Type.GetName()))
{
allInterfaces.Add(baseType);
}
count++;
}
}

public static IEnumerable<PropertyDeclarationSyntax> DtoProperties(
this RecordDeclarationSyntax dto,
IEnumerable<RecordDeclarationSyntax> baseDtos
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Generators.Base.Extensions.New
{
public static class BaseTypeSyntaxExtensions
{
public static string GetFullTypeName(this BaseTypeSyntax baseType)
{
var result = baseType.GetNamespace() + "." + baseType.Type.ToString();
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
using Generators.Base.Extensions.Common;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Generators.Base.Extensions.New
{
public static class InterfaceDeclarationSyntaxExtensions
{
public static bool HasAttribute<TAttribute>(
this InterfaceDeclarationSyntax RecordDeclarationSyntax
)
{
// Check if the type declaration has the BaseContext attribute
foreach (var attributeList in RecordDeclarationSyntax.AttributeLists)
{
foreach (var attribute in attributeList.Attributes)
{
var sdf = attribute.Name.ToFullString();
if (attribute.Name.ToString() == typeof(TAttribute).RealAttributeName())
{
return true;
}
}
}
return false;
}
}
}

0 comments on commit 01ebe74

Please sign in to comment.