Skip to content

Commit

Permalink
Add support for multiple interfaces and more samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
domn1995 committed Jun 13, 2022
1 parent 83bddb7 commit 5dc5b79
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 42 deletions.
8 changes: 8 additions & 0 deletions samples/Dunet.Cli/IChoice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Dunet.Choices;

[Union]
internal interface IChoice
{
void Yes();
void No();
}
41 changes: 29 additions & 12 deletions samples/Dunet.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
// See https://aka.ms/new-console-template for more information

using Dunet.Choices;
using Dunet.Shapes;

var rectangle = new Rectangle(10, 10);
var triangle = new Triangle(10, 10);
var circle = new Circle(10);

var rectangleArea = Calculator.GetArea(rectangle);
var triangleArea = Calculator.GetArea(triangle);
var circleArea = Calculator.GetArea(circle);
var getArea = (IShape shape) =>
shape switch
{
Rectangle rect => rect.Length * rect.Width,
Circle circle => 2.0 * Math.PI * circle.Radius,
Triangle triangle => 1.0 / 2.0 * triangle.Base * triangle.Height,
_ => 0d,
};

var rectangleArea = getArea(rectangle);
var triangleArea = getArea(triangle);
var circleArea = getArea(circle);

Console.WriteLine($"Rectangle area: {rectangleArea}");
Console.WriteLine($"Triangle area: {triangleArea}");
Console.WriteLine($"Circle area: {circleArea}");

class Calculator
var choice = GetChoice();

if (choice is Yes)
{
Console.WriteLine("YES!!!");
}

if (choice is No)
{
public static double GetArea(IShape shape) =>
shape switch
{
Rectangle rect => rect.Length * rect.Width,
Circle circle => 2.0 * Math.PI * circle.Radius,
Triangle triangle => 1.0 / 2.0 * triangle.Base * triangle.Height,
_ => 0d,
};
Console.WriteLine("NO!!!");
}

static IChoice GetChoice() =>
Console.ReadLine() switch
{
"yes" => new Yes(),
_ => new No(),
};
51 changes: 27 additions & 24 deletions src/Dunet/DiscriminatedUnionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ private static List<RecordToGenerate> GetRecordsToGenerate(
CancellationToken cancellationToken
)
{
var interfaceMethods = new List<Method>();
var recordsToGenerate = new List<RecordToGenerate>();
foreach (var iface in interfaces)
{
var methodDeclarations = iface.SyntaxTree
.GetRoot()
var interfaceMethods = new List<Method>();
var methodDeclarations = iface
.DescendantNodes()
.Where(node => node.IsKind(SyntaxKind.MethodDeclaration))
.OfType<MethodDeclarationSyntax>()
Expand All @@ -133,29 +133,32 @@ CancellationToken cancellationToken
var method = new Method(methodReturnType, methodName, parameters);
interfaceMethods.Add(method);
}
}

var interfaceDeclaration = interfaces.First();
var semanticModel = compilation.GetSemanticModel(interfaceDeclaration.SyntaxTree);
var interfaceSymbol = semanticModel.GetDeclaredSymbol(interfaceDeclaration);
var @namespace = interfaceSymbol?.ContainingNamespace.ToString() ?? "";

var recordsToGenerate = new List<RecordToGenerate>();
var semanticModel = compilation.GetSemanticModel(iface.SyntaxTree);
var interfaceSymbol = semanticModel.GetDeclaredSymbol(iface);
if (interfaceSymbol is null)
{
continue;
}
var @namespace = interfaceSymbol.ContainingNamespace.ToString() ?? "";

foreach (var interfaceMethod in interfaceMethods)
{
var name = interfaceMethod.Name;
var recordProperties = interfaceMethod.Parameters
.Select(param => new Parameter(Type: param.Type, Name: param.Name.ToPropertyCase()))
.ToList();
var recordToGenerate = new RecordToGenerate(
Namespace: @namespace,
Name: name,
Interface: "IShape",
Properties: new(recordProperties),
Methods: interfaceMethods
);
recordsToGenerate.Add(recordToGenerate);
foreach (var interfaceMethod in interfaceMethods)
{
var name = interfaceMethod.Name;
var recordProperties = interfaceMethod.Parameters
.Select(
param => new Parameter(Type: param.Type, Name: param.Name.ToPropertyCase())
)
.ToList();
var recordToGenerate = new RecordToGenerate(
Namespace: @namespace,
Name: name,
Interface: interfaceSymbol.Name,
Properties: new(recordProperties),
Methods: interfaceMethods
);
recordsToGenerate.Add(recordToGenerate);
}
}

return recordsToGenerate;
Expand Down
9 changes: 5 additions & 4 deletions src/Dunet/Dunet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/domn1995/dunet</RepositoryUrl>
<PackageTags>source; generator; discriminated; union; functional; tagged;</PackageTags>
<AssemblyVersion>0.1.0</AssemblyVersion>
<FileVersion>0.1.0</FileVersion>
<AssemblyVersion>0.1.1</AssemblyVersion>
<FileVersion>0.1.1</FileVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>0.1.0 - Initial release.</PackageReleaseNotes>
<PackageReleaseNotes>0.1.1 - Support any number of interfaces.
0.1.0 - Initial release.</PackageReleaseNotes>
<RepositoryType>git</RepositoryType>
<PackageIcon>favicon.png</PackageIcon>
<SignAssembly>False</SignAssembly>
<Version>0.1.0</Version>
<Version>0.1.1</Version>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Dunet/UnionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public record {recordToGenerate.Name}("
var parameterType = interfaceMethod.Parameters[j].Type;
var parameterName = interfaceMethod.Parameters[j].Name;
builder.Append(
$"{parameterType} {parameterName}{(j != parametersCount - 1 ? ", " : ")")}"
$"{parameterType} {parameterName}{(j != parametersCount - 1 ? ", " : "")}"
);
}

builder.AppendLine(" => throw new System.InvalidOperationException();");
builder.AppendLine(") => throw new System.InvalidOperationException();");
}

builder.Append(
Expand Down
14 changes: 14 additions & 0 deletions test/Dunet.Test/DiscriminatedUnionGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public interface IShape
IShape Circle(double radius);
IShape Rectangle(double length, double width);
IShape Triangle(double @base, double height);
}
[Union]
public interface IChoice
{
void Yes();
void No();
}";
// Act.
var (generation, compilation) = RunGenerator(source);
Expand All @@ -47,6 +54,13 @@ public interface IShape
IShape Circle(double radius);
IShape Rectangle(double length, double width);
IShape Triangle(double @base, double height);
}}
[Union]
public interface IChoice
{{
void Yes();
void No();
}}";

// Act.
Expand Down

0 comments on commit 5dc5b79

Please sign in to comment.