55
66namespace Porticle . Grpc . TypeMapper ;
77
8- public class PropertyVisitor ( TaskLoggingHelper log ) : CSharpSyntaxRewriter
8+ public class PropertyVisitor ( TaskLoggingHelper log , bool wrapAllNonNullableStrings , bool wrapAllNullableStringValues ) : CSharpSyntaxRewriter
99{
1010 public HashSet < PropertyToField > ReplaceProps = new ( ) ;
1111
@@ -25,7 +25,10 @@ public class PropertyVisitor(TaskLoggingHelper log) : CSharpSyntaxRewriter
2525 {
2626 if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[GrpcGuid]" ) ) return ConvertToGuidProperty ( property ) ;
2727
28- if ( property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[NullableString]" ) ) return ConvertToNullableStringProperty ( property ) ;
28+ if ( wrapAllNullableStringValues || property . GetLeadingTrivia ( ) . ToFullString ( ) . Contains ( "[NullableString]" ) )
29+ return ConvertToNullableStringProperty ( property , wrapAllNullableStringValues ) ;
30+
31+ if ( wrapAllNonNullableStrings ) return ConvertToNonNullableStringProperty ( property ) ;
2932
3033 return null ;
3134 }
@@ -180,21 +183,23 @@ public class PropertyVisitor(TaskLoggingHelper log) : CSharpSyntaxRewriter
180183 . WithType ( SyntaxFactory . NullableType ( property . Type . WithoutTrivia ( ) ) . WithTriviaFrom ( property . Type ) ) ;
181184 }
182185
183- private PropertyDeclarationSyntax ? ConvertToNullableStringProperty ( PropertyDeclarationSyntax property )
186+ private PropertyDeclarationSyntax ? ConvertToNullableStringProperty ( PropertyDeclarationSyntax property , bool wrapAllNullableStringValues )
184187 {
185188 var setter = property . AccessorList ? . Accessors . FirstOrDefault ( a => a . IsKind ( SyntaxKind . SetAccessorDeclaration ) ) ;
186189
187190 if ( setter ? . Body == null )
188191 {
189- log . LogError ( $ "No setter found in property { property . Identifier } ") ;
192+ if ( ! wrapAllNullableStringValues ) log . LogError ( $ "No setter found in property { property . Identifier } ") ;
193+
190194 return null ;
191195 }
192196
193197 var isNullable = ! setter . Body . ToFullString ( ) . Contains ( "ProtoPreconditions.CheckNotNull" ) ;
194198
195199 if ( ! isNullable )
196200 {
197- log . LogError ( $ "String property { property . Identifier } ist not nullable") ;
201+ if ( ! wrapAllNullableStringValues ) log . LogError ( $ "String property { property . Identifier } ist not nullable") ;
202+
198203 return null ;
199204 }
200205
@@ -212,6 +217,29 @@ public class PropertyVisitor(TaskLoggingHelper log) : CSharpSyntaxRewriter
212217 . WithTrailingTrivia ( property . GetTrailingTrivia ( ) . AddRange ( trailingTrivia ) ) ;
213218 }
214219
220+ private PropertyDeclarationSyntax ? ConvertToNonNullableStringProperty ( PropertyDeclarationSyntax property )
221+ {
222+ var setter = property . AccessorList ? . Accessors . FirstOrDefault ( a => a . IsKind ( SyntaxKind . SetAccessorDeclaration ) ) ;
223+
224+ if ( setter ? . Body == null ) return null ;
225+
226+ var isNullable = ! setter . Body . ToFullString ( ) . Contains ( "ProtoPreconditions.CheckNotNull" ) ;
227+
228+ if ( isNullable ) return null ;
229+
230+ var enableDirective =
231+ SyntaxFactory . Trivia ( SyntaxFactory . NullableDirectiveTrivia ( SyntaxFactory . Token ( SyntaxKind . EnableKeyword ) . WithLeadingTrivia ( SyntaxFactory . Space ) , true ) ) ;
232+ var disableDirective =
233+ SyntaxFactory . Trivia ( SyntaxFactory . NullableDirectiveTrivia ( SyntaxFactory . Token ( SyntaxKind . DisableKeyword ) . WithLeadingTrivia ( SyntaxFactory . Space ) , true ) ) ;
234+
235+ var leadingTrivia = SyntaxFactory . TriviaList ( enableDirective , SyntaxFactory . ElasticCarriageReturnLineFeed ) ;
236+ var trailingTrivia = SyntaxFactory . TriviaList ( SyntaxFactory . ElasticCarriageReturnLineFeed , disableDirective ) ;
237+
238+ return property
239+ . WithLeadingTrivia ( leadingTrivia . AddRange ( property . GetLeadingTrivia ( ) ) )
240+ . WithTrailingTrivia ( property . GetTrailingTrivia ( ) . AddRange ( trailingTrivia ) ) ;
241+ }
242+
215243 private PropertyDeclarationSyntax ? ConvertToGuidProperty ( PropertyDeclarationSyntax property )
216244 {
217245 var setter = property . GetSetter ( ) ;
0 commit comments