|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq;
|
3 | 4 | using System.Text;
|
4 | 5 | using System.Text.Encodings.Web;
|
@@ -121,7 +122,25 @@ public override ValueTask<string> Compile(BebopSchema schema, GeneratorConfig co
|
121 | 122 | _ => string.Empty
|
122 | 123 | };
|
123 | 124 | 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} {{"); |
125 | 144 | builder.Indent(indentStep);
|
126 | 145 |
|
127 | 146 | if (fd is MessageDefinition)
|
@@ -469,17 +488,24 @@ private string CompileDecodeUnion(UnionDefinition definition)
|
469 | 488 | /// </summary>
|
470 | 489 | private void CompileUnionFamily(IndentedStringBuilder builder, UnionDefinition ud)
|
471 | 490 | {
|
| 491 | + |
| 492 | + |
472 | 493 | var recordAttribute = "[global::Bebop.Attributes.BebopRecord(global::Bebop.Runtime.BebopKind.Union)]";
|
473 | 494 | var genericPositionalArguments = string.Join(", ", ud.Branches.Select(b => $"T{b.GenericIndex()}")).Trim();
|
474 | 495 | var genericTypeArguments = string.Join(", ", ud.Branches.Select(b => $"{PrefixNamespace(b.Definition.ClassName())}")).Trim();
|
475 | 496 | var genericConstraints = string.Join(' ', ud.Branches.Select(b => $"where T{b.GenericIndex()}: {PrefixNamespace(b.Definition.ClassName())}")).Trim();
|
476 | 497 |
|
477 | 498 | var structName = $"{ud.ClassName()}Union";
|
| 499 | + var interfaceName = $"I{ud.ClassName()}Member"; |
| 500 | + |
| 501 | + |
478 | 502 |
|
479 | 503 | var nullCheck = LanguageVersion == CSharpNine ? "is null" : "== null";
|
480 | 504 | var notNullCheck = LanguageVersion == CSharpNine ? "is not null" : "!= null";
|
481 | 505 | var isCheck = LanguageVersion == CSharpNine ? "is" : "==";
|
482 | 506 |
|
| 507 | + |
| 508 | + |
483 | 509 | void CompileValueProperty()
|
484 | 510 | {
|
485 | 511 | builder.AppendLine($"public {BebopRecord} Value => _discriminator switch {{").Indent(4);
|
@@ -573,6 +599,18 @@ void CompileEquals(bool isClass)
|
573 | 599 | CompileHashCode();
|
574 | 600 | builder.AppendLine("#endregion");
|
575 | 601 | }
|
| 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 | + |
576 | 614 | // Compiles a read-only struct which holds our union.
|
577 | 615 | void CompileUnionStruct()
|
578 | 616 | {
|
@@ -726,6 +764,7 @@ void CompileUnionConcreteClass()
|
726 | 764 | CompileUnionBaseClass();
|
727 | 765 | CompileUnionConcreteClass();
|
728 | 766 | CompileUnionStruct();
|
| 767 | + CompileUnionInterface(); |
729 | 768 | }
|
730 | 769 |
|
731 | 770 | #endregion
|
|
0 commit comments