@@ -16,12 +16,12 @@ namespace Foundation.Crawler.Extensions.New
16
16
{
17
17
public static class DtoExtensions
18
18
{
19
- public static IEnumerable < IPropertySymbol > DtoForeignProperties (
19
+ public static IEnumerable < PropertyDeclarationSyntax > DtoForeignProperties (
20
20
this RecordDeclarationSyntax dto ,
21
- SemanticModel semanticModel
21
+ IEnumerable < RecordDeclarationSyntax > baseDtos
22
22
)
23
23
{
24
- return dto . GetAllPropertiesWithBaseClass ( semanticModel , true )
24
+ return dto . DtoProperties ( baseDtos )
25
25
. Where ( x => x . HasAttribute ( AttributeNames . ForeignKey ) ) ;
26
26
}
27
27
@@ -74,6 +74,46 @@ this IncrementalGeneratorInitializationContext context
74
74
return combinedDtosProvider ! ;
75
75
}
76
76
77
+ public static IncrementalValueProvider < ImmutableArray < RecordDeclarationSyntax > > BaseDtos (
78
+ this IncrementalGeneratorInitializationContext context
79
+ )
80
+ {
81
+ var dtos = context
82
+ . SyntaxProvider . ForAttributeWithMetadataName (
83
+ typeof ( DtoBaseAttribute ) . FullName ,
84
+ static ( n , _ ) => n is RecordDeclarationSyntax ,
85
+ static ( context , cancellationToken ) =>
86
+ {
87
+ RecordDeclarationSyntax classDeclarationSyntax = ( RecordDeclarationSyntax )
88
+ context . TargetNode ;
89
+
90
+ return classDeclarationSyntax . HasAttribute < DtoBaseAttribute > ( )
91
+ ? classDeclarationSyntax
92
+ : null ;
93
+ }
94
+ )
95
+ . Where ( static typeDeclaration => typeDeclaration is not null )
96
+ . Collect ( ) ;
97
+
98
+ return dtos ;
99
+ }
100
+
101
+ public static IEnumerable < PropertyDeclarationSyntax > DtoProperties (
102
+ this RecordDeclarationSyntax dto ,
103
+ IEnumerable < RecordDeclarationSyntax > baseDtos
104
+ )
105
+ {
106
+ // Annahme: dto ist eine RecordDeclarationSyntax oder ClassDeclarationSyntax
107
+ var allProperties = new List < PropertyDeclarationSyntax > ( ) ;
108
+
109
+ // Füge die Properties der aktuellen Klasse (dto) hinzu
110
+ allProperties . AddRange ( dto . GetProperties ( ) ) ;
111
+
112
+ // Extrahiere rekursiv die Properties der Basisklassen
113
+ ExtractBaseProperties ( dto . BaseList , baseDtos , allProperties ) ;
114
+ return allProperties ;
115
+ }
116
+
77
117
private static string GetNamespace ( SyntaxNode node )
78
118
{
79
119
var nameSpace = string . Empty ;
@@ -128,5 +168,36 @@ public static string GetEntityName(this RecordDeclarationSyntax dto, bool plural
128
168
}
129
169
return name ;
130
170
}
171
+
172
+ static void ExtractBaseProperties (
173
+ BaseListSyntax baseList ,
174
+ IEnumerable < RecordDeclarationSyntax > baseDtos ,
175
+ List < PropertyDeclarationSyntax > properties
176
+ )
177
+ {
178
+ if ( baseList == null )
179
+ return ; // Keine Basisklasse vorhanden
180
+
181
+ foreach ( var baseTypeSyntax in baseList . Types )
182
+ {
183
+ // Hole den Namen der Basisklasse
184
+ var baseTypeName = baseTypeSyntax . Type . GetName ( ) ;
185
+
186
+ if ( ! string . IsNullOrEmpty ( baseTypeName ) )
187
+ {
188
+ // Suche nach der Basisklasse in den bekannten baseDtos
189
+ var baseDto = baseDtos . FirstOrDefault ( x => x . Identifier . Text == baseTypeName ) ;
190
+
191
+ if ( baseDto != null )
192
+ {
193
+ // Füge die Properties der Basisklasse hinzu
194
+ properties . AddRange ( baseDto . GetProperties ( ) ) ;
195
+
196
+ // Rekursiv weitermachen, falls diese Basisklasse ebenfalls eine Basisklasse hat
197
+ ExtractBaseProperties ( baseDto . BaseList , baseDtos , properties ) ;
198
+ }
199
+ }
200
+ }
201
+ }
131
202
}
132
203
}
0 commit comments