Skip to content

Commit

Permalink
Fix class name clash when a dbus property is named properties
Browse files Browse the repository at this point in the history
  • Loading branch information
affederaffe committed Sep 29, 2024
1 parent 21ea3bf commit f4980bb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Tmds.DBus.SourceGenerator/DBusSourceGenerator.Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ private static string SanitizeIdentifier(string identifier)
return isAnyKeyword ? $"@{identifier}" : identifier;
}

private static string GetPropertiesClassIdentifier(DBusInterface dBusInterface) => $"{Pascalize(dBusInterface.Name!)}Properties";

internal static (DBusValue DBusValue, DBusValue[] InnerDBusTypes, DBusType DBusType) ParseDBusValue(string signature) =>
SignatureReader.Transform<(DBusValue, DBusValue[], DBusType)>(Encoding.ASCII.GetBytes(signature), MapDBusToDotNet);

Expand Down
95 changes: 60 additions & 35 deletions Tmds.DBus.SourceGenerator/DBusSourceGenerator.Proxy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Linq;

using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -185,7 +184,7 @@ private static ObjectCreationExpressionSyntax MakeMatchRule(DBusSignal dBusSigna
MakeAssignmentExpression(IdentifierName("Member"), MakeLiteralExpression(dBusSignal.Name!)),
MakeAssignmentExpression(IdentifierName("Interface"), IdentifierName("Interface"))));

private void AddWatchPropertiesChanged(ref ClassDeclarationSyntax cl) =>
private void AddWatchPropertiesChanged(ref ClassDeclarationSyntax cl, DBusInterface dBusInterface) =>
cl = cl.AddMembers(
MethodDeclaration(
GenericName("ValueTask")
Expand All @@ -201,7 +200,8 @@ private void AddWatchPropertiesChanged(ref ClassDeclarationSyntax cl) =>
IdentifierName("Exception")),
GenericName("PropertyChanges")
.AddTypeArgumentListArguments(
IdentifierName("Properties")))),
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface))))),
Parameter(Identifier("emitOnCapturedContext"))
.WithType(PredefinedType(Token(SyntaxKind.BoolKeyword)))
.WithDefault(
Expand Down Expand Up @@ -229,7 +229,8 @@ private void AddWatchPropertiesChanged(ref ClassDeclarationSyntax cl) =>
LocalFunctionStatement(
GenericName("PropertyChanges")
.AddTypeArgumentListArguments(
IdentifierName("Properties")),
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface))),
"ReadMessage")
.AddModifiers(Token(SyntaxKind.StaticKeyword))
.AddParameterListParameters(
Expand Down Expand Up @@ -266,7 +267,8 @@ private void AddWatchPropertiesChanged(ref ClassDeclarationSyntax cl) =>
InvocationExpression(
ObjectCreationExpression(GenericName("PropertyChanges")
.AddTypeArgumentListArguments(
IdentifierName("Properties"))))
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface)))))
.AddArgumentListArguments(
Argument(InvocationExpression(
IdentifierName("ReadProperties"))
Expand Down Expand Up @@ -295,10 +297,10 @@ private void AddProperties(ref ClassDeclarationSyntax cl, DBusInterface dBusInte
_ => current
});

AddGetAllMethod(ref cl);
AddReadProperties(ref cl, dBusInterface.Properties);
AddGetAllMethod(ref cl, dBusInterface);
AddReadProperties(ref cl, dBusInterface);
AddPropertiesClass(ref cl, dBusInterface);
AddWatchPropertiesChanged(ref cl);
AddWatchPropertiesChanged(ref cl, dBusInterface);
}

private MethodDeclarationSyntax MakeGetMethod(DBusProperty dBusProperty)
Expand Down Expand Up @@ -360,7 +362,7 @@ private MethodDeclarationSyntax MakeSetMethod(DBusProperty dBusProperty)
MakeCallMethodReturnBody(args, createMessageBody));
}

private static void AddGetAllMethod(ref ClassDeclarationSyntax cl)
private static void AddGetAllMethod(ref ClassDeclarationSyntax cl, DBusInterface dBusInterface)
{
BlockSyntax createGetAllMessageBody = MakeCreateMessageBody(MakeLiteralExpression("org.freedesktop.DBus.Properties"), "GetAll", "s",
ExpressionStatement(
Expand Down Expand Up @@ -396,7 +398,8 @@ private static void AddGetAllMethod(ref ClassDeclarationSyntax cl)
MethodDeclaration(
GenericName("Task")
.AddTypeArgumentListArguments(
IdentifierName("Properties")),
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface))),
"GetAllPropertiesAsync")
.AddModifiers(Token(SyntaxKind.PublicKeyword))
.WithBody(
Expand All @@ -414,63 +417,84 @@ private static void AddGetAllMethod(ref ClassDeclarationSyntax cl)

private static void AddPropertiesClass(ref ClassDeclarationSyntax cl, DBusInterface dBusInterface)
{
ClassDeclarationSyntax propertiesClass = ClassDeclaration("Properties")
.AddModifiers(Token(SyntaxKind.PublicKeyword));
ClassDeclarationSyntax propertiesClass = ClassDeclaration(
GetPropertiesClassIdentifier(dBusInterface))
.AddModifiers(
Token(SyntaxKind.PublicKeyword));

propertiesClass = dBusInterface.Properties!.Aggregate(propertiesClass, static (current, property) =>
current.AddMembers(
MakeGetSetProperty(GetDotnetType(property, AccessMode.Read), Pascalize(property.Name!), Token(SyntaxKind.PublicKeyword))));
MakeGetSetProperty(
GetDotnetType(property, AccessMode.Read), Pascalize(property.Name!), Token(SyntaxKind.PublicKeyword))));

cl = cl.AddMembers(propertiesClass);
}

private void AddReadProperties(ref ClassDeclarationSyntax cl, IEnumerable<DBusProperty> dBusProperties) =>
private void AddReadProperties(ref ClassDeclarationSyntax cl, DBusInterface dBusInterface) =>
cl = cl.AddMembers(
MethodDeclaration(
IdentifierName("Properties"), "ReadProperties")
.AddModifiers(Token(SyntaxKind.PrivateKeyword), Token(SyntaxKind.StaticKeyword))
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface)),
"ReadProperties")
.AddModifiers(
Token(SyntaxKind.PrivateKeyword),
Token(SyntaxKind.StaticKeyword))
.AddParameterListParameters(
Parameter(Identifier("reader"))
.WithType(IdentifierName("Reader"))
.AddModifiers(Token(SyntaxKind.RefKeyword)),
Parameter(Identifier("changed"))
Parameter(
Identifier("reader"))
.WithType(
IdentifierName("Reader"))
.AddModifiers(
Token(SyntaxKind.RefKeyword)),
Parameter
(Identifier("changed"))
.WithType(
NullableType(
GenericName("List")
.AddTypeArgumentListArguments(
PredefinedType(Token(SyntaxKind.StringKeyword)))))
PredefinedType(
Token(SyntaxKind.StringKeyword)))))
.WithDefault(
EqualsValueClause(
LiteralExpression(SyntaxKind.NullLiteralExpression))))
.WithBody(
Block(
LocalDeclarationStatement(VariableDeclaration(IdentifierName("Properties"))
.AddVariables(
VariableDeclarator("props")
.WithInitializer(
EqualsValueClause(
InvocationExpression(
ObjectCreationExpression(IdentifierName("Properties"))))))),
LocalDeclarationStatement(VariableDeclaration(IdentifierName("ArrayEnd"))
LocalDeclarationStatement(
VariableDeclaration(
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface)))
.AddVariables(
VariableDeclarator("props")
.WithInitializer(
EqualsValueClause(
InvocationExpression(
ObjectCreationExpression(
IdentifierName(
GetPropertiesClassIdentifier(dBusInterface)))))))),
LocalDeclarationStatement(VariableDeclaration(
IdentifierName("ArrayEnd"))
.AddVariables(
VariableDeclarator("headersEnd")
.WithInitializer(
EqualsValueClause(
InvocationExpression(
MakeMemberAccessExpression("reader", "ReadArrayStart"))
.AddArgumentListArguments(
Argument(MakeMemberAccessExpression("DBusType", "Struct"))))))),
Argument(
MakeMemberAccessExpression("DBusType", "Struct"))))))),
WhileStatement(
InvocationExpression(
MakeMemberAccessExpression("reader", "HasNext"))
.AddArgumentListArguments(Argument(IdentifierName("headersEnd"))),
.AddArgumentListArguments(
Argument(
IdentifierName("headersEnd"))),
Block(
SwitchStatement(
InvocationExpression(
MakeMemberAccessExpression("reader", "ReadString")))
.WithSections(
List(
dBusProperties.Select(property => SwitchSection()
dBusInterface.Properties!.Select(property => SwitchSection()
.AddLabels(
CaseSwitchLabel(
MakeLiteralExpression(property.Name!)))
Expand All @@ -489,13 +513,14 @@ private void AddReadProperties(ref ClassDeclarationSyntax cl, IEnumerable<DBusPr
ExpressionStatement(
ConditionalAccessExpression(
IdentifierName("changed"), InvocationExpression(
MemberBindingExpression(
IdentifierName("Add")))
MemberBindingExpression(
IdentifierName("Add")))
.AddArgumentListArguments(
Argument(
MakeLiteralExpression(Pascalize(property.Name!)))))),
BreakStatement())))))),
ReturnStatement(IdentifierName("props")))));
ReturnStatement(
IdentifierName("props")))));

private static BlockSyntax MakeCallMethodReturnBody(ArgumentListSyntax args, BlockSyntax createMessageBody) =>
Block(
Expand Down

0 comments on commit f4980bb

Please sign in to comment.