Skip to content

Commit 4ec5917

Browse files
author
machibuse
committed
Enhance PropertyVisitor with handling for nullable string properties and improve documentation in PropertyToField. Adjust VCS mappings in IntelliJ configuration.
1 parent b2bb69e commit 4ec5917

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

Source/.idea/.idea.Porticle.Grpc/.idea/vcs.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
namespace Porticle.Grpc.GuidMapper;
22

3+
/// <summary>
4+
/// Represents a mapping between a property name and its corresponding field name.
5+
/// </summary>
36
public record PropertyToField(string PropertyName, string FieldName);

Source/Porticle.Grpc.GuidMapper/PropertyVisitor.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,43 @@ public class PropertyVisitor : CSharpSyntaxRewriter
3939
// dont change anything
4040
return null;
4141

42-
if (!property.GetLeadingTrivia().ToFullString().Contains("[GrpcGuid]"))
43-
// dont change anything
42+
if (property.GetLeadingTrivia().ToFullString().Contains("[GrpcGuid]"))
43+
{
44+
return ConcertToGuidProperty(property);
45+
}
46+
47+
if (property.GetLeadingTrivia().ToFullString().Contains("[NullableString]"))
48+
{
49+
return ConcertToNullableStringProperty(property);
50+
}
51+
52+
return null;
53+
}
54+
55+
private PropertyDeclarationSyntax? ConcertToNullableStringProperty(PropertyDeclarationSyntax property)
56+
{
57+
var setter = property.AccessorList?.Accessors.FirstOrDefault(a => a.IsKind(SyntaxKind.SetAccessorDeclaration));
58+
59+
if (setter?.Body == null)
60+
{
61+
Console.WriteLine($"[Error] No setter found in property {property.Identifier}");
4462
return null;
63+
}
4564

65+
var isNullable = !setter.Body.ToFullString().Contains("ProtoPreconditions.CheckNotNull");
66+
67+
if (!isNullable)
68+
{
69+
Console.WriteLine($"[Error] String property {property.Identifier} ist not nullable");
70+
return null;
71+
}
72+
73+
// Change property type to string?
74+
return property.WithType(SyntaxFactory.ParseTypeName("global::System.String?").WithTrailingTrivia(SyntaxFactory.Space));
75+
}
76+
77+
private PropertyDeclarationSyntax? ConcertToGuidProperty(PropertyDeclarationSyntax property)
78+
{
4679
var setter = property.AccessorList?.Accessors.FirstOrDefault(a => a.IsKind(SyntaxKind.SetAccessorDeclaration));
4780

4881
if (setter?.Body == null)

0 commit comments

Comments
 (0)