Skip to content

Commit 2108a61

Browse files
authored
Merge pull request #345 from betwixt-labs/cs-union-interface
generator(cs): make union members extend a partial interface
2 parents 1a78cd6 + ccb3e2f commit 2108a61

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION="3.0.13"
1+
VERSION="3.0.14"
22
MAJOR=3
33
MINOR=0
4-
PATCH=13
4+
PATCH=14

Core/Generators/CSharp/CSharpGenerator.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text;
45
using System.Text.Encodings.Web;
@@ -121,7 +122,25 @@ public override ValueTask<string> Compile(BebopSchema schema, GeneratorConfig co
121122
_ => string.Empty
122123
};
123124
builder.AppendLine(recordAttribute);
124-
builder.AppendLine($"public partial class {definition.ClassName()} : {BebopRecord}, {IDecodable}<{definition.ClassName()}>, global::System.IEquatable<{definition.ClassName()}> {{");
125+
126+
var implementedInterfaces = new List<string>
127+
{
128+
BebopRecord,
129+
$"{IDecodable}<{definition.ClassName()}>",
130+
$"global::System.IEquatable<{definition.ClassName()}>"
131+
};
132+
133+
// Check if the definition's parent is a UnionDefinition
134+
if (definition.Parent is UnionDefinition ud)
135+
{
136+
// Add the union interface to the list of implemented interfaces
137+
implementedInterfaces.Add($"I{ud.ClassName()}Member");
138+
}
139+
140+
// Join the interfaces with commas
141+
var interfaceList = string.Join(", ", implementedInterfaces);
142+
143+
builder.AppendLine($"public partial class {definition.ClassName()} : {interfaceList} {{");
125144
builder.Indent(indentStep);
126145

127146
if (fd is MessageDefinition)
@@ -469,17 +488,24 @@ private string CompileDecodeUnion(UnionDefinition definition)
469488
/// </summary>
470489
private void CompileUnionFamily(IndentedStringBuilder builder, UnionDefinition ud)
471490
{
491+
492+
472493
var recordAttribute = "[global::Bebop.Attributes.BebopRecord(global::Bebop.Runtime.BebopKind.Union)]";
473494
var genericPositionalArguments = string.Join(", ", ud.Branches.Select(b => $"T{b.GenericIndex()}")).Trim();
474495
var genericTypeArguments = string.Join(", ", ud.Branches.Select(b => $"{PrefixNamespace(b.Definition.ClassName())}")).Trim();
475496
var genericConstraints = string.Join(' ', ud.Branches.Select(b => $"where T{b.GenericIndex()}: {PrefixNamespace(b.Definition.ClassName())}")).Trim();
476497

477498
var structName = $"{ud.ClassName()}Union";
499+
var interfaceName = $"I{ud.ClassName()}Member";
500+
501+
478502

479503
var nullCheck = LanguageVersion == CSharpNine ? "is null" : "== null";
480504
var notNullCheck = LanguageVersion == CSharpNine ? "is not null" : "!= null";
481505
var isCheck = LanguageVersion == CSharpNine ? "is" : "==";
482506

507+
508+
483509
void CompileValueProperty()
484510
{
485511
builder.AppendLine($"public {BebopRecord} Value => _discriminator switch {{").Indent(4);
@@ -573,6 +599,18 @@ void CompileEquals(bool isClass)
573599
CompileHashCode();
574600
builder.AppendLine("#endregion");
575601
}
602+
603+
void CompileUnionInterface()
604+
{
605+
builder.AppendLine("/// <summary>");
606+
builder.AppendLine($"/// Interface for members of the {ud.ClassName()} union");
607+
builder.AppendLine("/// </summary>");
608+
builder.AppendLine(GeneratedAttribute);
609+
builder.AppendLine($"public partial interface {interfaceName} {{").Indent(indentStep);
610+
611+
builder.Dedent(indentStep).AppendLine("}").AppendLine();
612+
}
613+
576614
// Compiles a read-only struct which holds our union.
577615
void CompileUnionStruct()
578616
{
@@ -726,6 +764,7 @@ void CompileUnionConcreteClass()
726764
CompileUnionBaseClass();
727765
CompileUnionConcreteClass();
728766
CompileUnionStruct();
767+
CompileUnionInterface();
729768
}
730769

731770
#endregion

Laboratory/C#/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# C# Bebop Laboratory
22

33
To run the C# tests, from PowerShell:
4-
5-
dotnet run --project ..\..\Compiler\ --cs ".\GeneratedTestCode\Output.g.cs" --namespace Bebop.Codegen --files (gci ..\Schemas\Valid\*.bop)
4+
dotnet run --project ../../Compiler/ -i ../Schemas/Valid/*.bop build -g "cs:./GeneratedTestCode/Output.g.cs,namespace=Bebop.Codegen"
65
dotnet test -nowarn:CS0618

0 commit comments

Comments
 (0)