Skip to content

Commit 571ae2a

Browse files
author
machibuse
committed
- Add support for wrapping all non-nullable strings with #nullable enable by a global setting
- Add support for wrapping all nullable strings to string? by a global setting
1 parent 42d2d6e commit 571ae2a

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

Source/Porticle.Grpc.TypeMapper/ClassVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Porticle.Grpc.TypeMapper;
77

8-
public class ClassVisitor(TaskLoggingHelper log) : CSharpSyntaxRewriter
8+
public class ClassVisitor(TaskLoggingHelper log, bool wrapAllNonNullableStrings, bool wrapAllNullableStringValues) : CSharpSyntaxRewriter
99
{
1010
public override SyntaxNode? VisitClassDeclaration(ClassDeclarationSyntax node)
1111
{
@@ -19,7 +19,7 @@ public class ClassVisitor(TaskLoggingHelper log) : CSharpSyntaxRewriter
1919
var trivia = node.GetLeadingTrivia().Add(SyntaxFactory.Comment("/// <remark>" + marker + "</remark>")).Add(SyntaxFactory.CarriageReturnLineFeed);
2020
node = node.WithLeadingTrivia(trivia);
2121

22-
var propertyVisitor = new PropertyVisitor(log);
22+
var propertyVisitor = new PropertyVisitor(log, wrapAllNonNullableStrings, wrapAllNullableStringValues);
2323
node = (ClassDeclarationSyntax)propertyVisitor.Visit(node);
2424

2525
if (propertyVisitor.NeedGuidConverter) node = node.AddMembers(ClassFromSource(ListWrappers.RepeatedFieldGuidWrapper));

Source/Porticle.Grpc.TypeMapper/PropertyVisitor.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace 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();

Source/Porticle.Grpc.TypeMapper/ProtoPostProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override bool Execute()
4848
var root = tree.GetRoot();
4949
File.WriteAllText(filePath + "_", root.ToFullString());
5050

51-
var classVisitor = new ClassVisitor(Log);
51+
var classVisitor = new ClassVisitor(Log, WrapAllNonNullableStrings, WrapAllNullableStringValues);
5252
root = classVisitor.Visit(root);
5353

5454
File.WriteAllText(filePath, root.ToFullString());

0 commit comments

Comments
 (0)