44
55namespace Porticle . Grpc . GuidMapper ;
66
7-
8- public class ClassVisitor : CSharpSyntaxRewriter
9- {
10- public override SyntaxNode ? VisitClassDeclaration ( ClassDeclarationSyntax node )
11- {
12- Console . WriteLine ( "Visit Class " + node . Identifier . ValueText ) ;
13- var propertyVisitor = new PropertyVisitor ( ) ;
14- node = ( ClassDeclarationSyntax ) propertyVisitor . Visit ( node ) ;
15-
16-
17- Console . WriteLine ( "Visit Methods for " + propertyVisitor . ReplaceProps . Count + " props" ) ;
18- var methodVisitor = new MethodVisitor ( propertyVisitor . ReplaceProps ) ;
19- node = ( ClassDeclarationSyntax ) methodVisitor . Visit ( node ) ;
20-
21- return node ;
22- }
23- }
24-
257public class PropertyVisitor : CSharpSyntaxRewriter
268{
279 public HashSet < PropertyToField > ReplaceProps = new ( ) ;
2810
11+ public bool NeedNullableStringConverter { get ; set ; }
12+ public bool NeedNullableGuidConverter { get ; set ; }
13+ public bool NeedGuidConverter { get ; set ; }
14+
15+
2916 public override SyntaxNode ? VisitPropertyDeclaration ( PropertyDeclarationSyntax node )
3017 {
3118 var newProperty = CheckProperty ( node ) ;
@@ -35,20 +22,120 @@ public class PropertyVisitor : CSharpSyntaxRewriter
3522
3623 private PropertyDeclarationSyntax ? CheckProperty ( PropertyDeclarationSyntax property )
3724 {
38- if ( property . Type . ToString ( ) != "string" )
39- // dont change anything
40- return null ;
25+ if ( property . Type . ToString ( ) == "string" )
26+ {
27+ if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[GrpcGuid]" ) )
28+ {
29+ return ConvertToGuidProperty ( property ) ;
30+ }
31+
32+ if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[NullableString]" ) )
33+ {
34+ return ConvertToNullableStringProperty ( property ) ;
35+ }
4136
42- if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[GrpcGuid]" ) )
37+ return null ;
38+ }
39+
40+ if ( property . Type . ToString ( ) == "pbc::RepeatedField<string>" )
4341 {
44- return ConvertToGuidProperty ( property ) ;
42+
43+ // Manipulate getter
44+ var getter = property . AccessorList ? . Accessors . FirstOrDefault ( a => a . IsKind ( SyntaxKind . GetAccessorDeclaration ) ) ;
45+
46+ if ( getter ? . Body == null )
47+ {
48+ Console . WriteLine ( $ "[Error] No getter found in property { property . Identifier } ") ;
49+ return null ;
50+ }
51+
52+ var returnStatement = getter . Body ? . Statements . OfType < ReturnStatementSyntax > ( ) . FirstOrDefault ( ) ;
53+
54+ if ( returnStatement ? . Expression == null )
55+ {
56+ Console . WriteLine ( $ "[Error] Getter has no valid return statement in property { property . Identifier } ") ;
57+ return null ;
58+ }
59+
60+ var originalReturnExpression = returnStatement . Expression ;
61+
62+ var containingClass = property . Ancestors ( )
63+ . OfType < ClassDeclarationSyntax > ( )
64+ . FirstOrDefault ( ) ;
65+
66+ var matchingField = containingClass . Members
67+ . OfType < FieldDeclarationSyntax > ( )
68+ . FirstOrDefault ( field =>
69+ field . Declaration . Variables . Any ( v => v . Identifier . Text == "_repeated_" + originalReturnExpression . ToFullString ( ) + "codec" )
70+ ) ;
71+
72+
73+ bool isNullable = matchingField . ToFullString ( ) . Contains ( "ForClassWrapper<string>" ) ;
74+
75+ Console . WriteLine ( "isNullable " + isNullable + " " + property . Identifier . ToFullString ( ) ) ;
76+
77+ if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[GrpcGuid]" ) )
78+ {
79+ if ( isNullable )
80+ {
81+ var newReturnExpression = SyntaxFactory . InvocationExpression (
82+ SyntaxFactory . ParseExpression ( "new RepeatedFieldNullableGuidWrapper" ) , // Die Methode
83+ SyntaxFactory . ArgumentList ( SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . Argument ( originalReturnExpression ) ) ) ) ;
84+ var newReturnStatement = returnStatement . WithExpression ( newReturnExpression ) . WithTrailingTrivia ( SyntaxFactory . Space ) ;
85+ var newGetterBody = SyntaxFactory . Block ( newReturnStatement ) ;
86+ var newGetter = getter . WithBody ( newGetterBody . WithTrailingTrivia ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
87+ property = property . ReplaceNode ( getter , newGetter ) ;
88+ return property . WithType ( SyntaxFactory . ParseTypeName ( "System.Collections.Generic.IList<Guid?>" ) . WithTrailingTrivia ( SyntaxFactory . ElasticSpace ) ) ;
89+ }
90+ else
91+ {
92+ var newReturnExpression = SyntaxFactory . InvocationExpression (
93+ SyntaxFactory . ParseExpression ( "new RepeatedFieldGuidWrapper" ) , // Die Methode
94+ SyntaxFactory . ArgumentList ( SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . Argument ( originalReturnExpression ) ) ) ) ;
95+ var newReturnStatement = returnStatement . WithExpression ( newReturnExpression ) . WithTrailingTrivia ( SyntaxFactory . Space ) ;
96+ var newGetterBody = SyntaxFactory . Block ( newReturnStatement ) ;
97+ var newGetter = getter . WithBody ( newGetterBody . WithTrailingTrivia ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
98+ property = property . ReplaceNode ( getter , newGetter ) ;
99+ return property . WithType ( SyntaxFactory . ParseTypeName ( "System.Collections.Generic.IList<Guid>" ) . WithTrailingTrivia ( SyntaxFactory . ElasticSpace ) ) ;
100+ }
101+ }
102+
103+ if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[NullableString]" ) )
104+ {
105+ if ( isNullable )
106+ {
107+ var newReturnExpression = SyntaxFactory . InvocationExpression (
108+ SyntaxFactory . ParseExpression ( "new RepeatedFieldNullableStringWrapper" ) , // Die Methode
109+ SyntaxFactory . ArgumentList ( SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . Argument ( originalReturnExpression ) ) ) ) ;
110+ var newReturnStatement = returnStatement . WithExpression ( newReturnExpression ) . WithTrailingTrivia ( SyntaxFactory . Space ) ;
111+ var newGetterBody = SyntaxFactory . Block ( newReturnStatement ) ;
112+ var newGetter = getter . WithBody ( newGetterBody . WithTrailingTrivia ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
113+ property = property . ReplaceNode ( getter , newGetter ) ;
114+ return property . WithType ( SyntaxFactory . ParseTypeName ( "System.Collections.Generic.IList<string?>" ) . WithTrailingTrivia ( SyntaxFactory . ElasticSpace ) ) ;
115+ }
116+ }
117+
118+
119+ return null ;
45120 }
121+
46122
47- if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[NullableString]" ) )
123+ if ( property . Type . ToString ( ) == "string" )
48124 {
49- return ConvertToNullableStringProperty ( property ) ;
50- }
125+ if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[GrpcGuid]" ) )
126+ {
127+ return ConvertToGuidProperty ( property ) ;
128+ }
51129
130+ if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[NullableString]" ) )
131+ {
132+ return ConvertToNullableStringProperty ( property ) ;
133+ }
134+
135+ return null ;
136+ }
137+
138+ // dont change anything
52139 return null ;
53140 }
54141
0 commit comments