Skip to content

Commit

Permalink
Adding empty constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Feb 14, 2023
1 parent 01d263a commit 6e57b60
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 22 deletions.
24 changes: 13 additions & 11 deletions src/ClientGenerator/RoslynTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public ClassDeclarationSyntax GenerateClient(TypeName clientName, ServiceSourceC
properties: properties,
methods: methods,
attributes: attributes,
emptyReflectionContructor: true,
emptyConstructorAccess: AccessType.Protected,
subTypes: resolvedOptions
.SelectMany(o => o.Type?.GeneratedSyntax ?? Array.Empty<MemberDeclarationSyntax>())
.ToList()
Expand Down Expand Up @@ -526,7 +526,8 @@ internal ClassDeclarationSyntax GenerateRecord(TypeName recordTypeName, RecordSo
return GenerateClass(
name: recordTypeName,
properties: properties,
subTypes: subItems
subTypes: subItems,
emptyConstructorAccess: AccessType.Public
);

}
Expand Down Expand Up @@ -1193,28 +1194,29 @@ private static ClassDeclarationSyntax GenerateClass(
List<MethodDeclarationSyntax>? methods = null,
List<TypeName>? implementTypes = null,
List<AttributeListSyntax>? attributes = null,
bool emptyReflectionContructor = false,
AccessType? emptyConstructorAccess = null,
List<MemberDeclarationSyntax>? subTypes = null)
{
if (properties.Any(p => p.Type == null))
{
// TODO
throw new NotImplementedException("Null, empty or reserved properties are not yet supported");
}
IEnumerable<PropertyDeclarationSyntax> properySyntxList = properties
List<ConstructorDeclarationSyntax> constructors = new();
IEnumerable<PropertyDeclarationSyntax> properySyntaxList = properties
.Select(GenerateProperty)
.Where(p => p != null)!;

List<ConstructorDeclarationSyntax> constructors = new()
if (properties.Any())
{
GenerateConstructor(name, AccessType.Public, properties)
};
if (emptyReflectionContructor)
// Only create constructor if there are properties
constructors.Add(GenerateConstructor(name, AccessType.Public, properties));
}
if (emptyConstructorAccess != null)
{
// Empty Constrcutor for reflection
constructors.Add(GenerateConstructor(
name: name,
access: AccessType.Protected,
access: emptyConstructorAccess.Value,
properties: new List<ClassProperty>()
));
}
Expand All @@ -1226,7 +1228,7 @@ private static ClassDeclarationSyntax GenerateClass(
))
.WithMembers(SyntaxFactory.List(
// Properties
properySyntxList.Cast<MemberDeclarationSyntax>()
properySyntaxList.Cast<MemberDeclarationSyntax>()
// Constructors
.Concat(constructors.Cast<MemberDeclarationSyntax>())
// Methods
Expand Down
11 changes: 0 additions & 11 deletions src/ClientGenerator/SyntaxRewriters/NamespacePrefixRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ public NamespacePrefixRemover(string modelNamespace)
this.ModelNamespace = modelNamespace;
}

[return: NotNullIfNotNull("node")]
public override SyntaxNode? Visit(SyntaxNode? node)
{
string? a = node?.ToString();
if (a != null && a.StartsWith("EdjCase.ICP.Agent.Agents"))
{
int b = 2;
}
return base.Visit(node);
}

public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
{
return this.VisitInternal(node, base.VisitIdentifierName);
Expand Down
8 changes: 8 additions & 0 deletions test/Common.Tests/Generators/__snapshots__/Dex.snap
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ namespace Test.Models
this.To = to;
this.ToAmount = toAmount;
}

public Order()
{
}
}
}

Expand Down Expand Up @@ -674,5 +678,9 @@ namespace Test.Models
this.Owner = owner;
this.Token = token;
}

public Balance()
{
}
}
}
Loading

0 comments on commit 6e57b60

Please sign in to comment.