Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,7 @@ public void Emit(StringBuilder writer, IGeneratorContext context)
(false, false) => "class",
};

var containingTypeDeclarations = new List<string>();
var containingType = Symbol.ContainingType;
while (containingType is not null)
{
containingTypeDeclarations.Add((containingType.IsRecord, containingType.IsValueType) switch
{
(true, true) => $"partial record struct {containingType.Name}",
(true, false) => $"partial record {containingType.Name}",
(false, true) => $"partial struct {containingType.Name}",
(false, false) => $"partial class {containingType.Name}",
});
containingType = containingType.ContainingType;
}
containingTypeDeclarations.Reverse();
var containingTypeDeclarations = GetContainingTypeDeclarations();

var nullable = IsValueType ? "" : "?";

Expand Down Expand Up @@ -991,8 +978,17 @@ string EmitUnionTemplate(IGeneratorContext context)
? "Serialize(ref MemoryPackWriter"
: "Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter>";

var code = $$"""
var containingTypeDeclarations = GetContainingTypeDeclarations();

var containingTypesOpening = containingTypeDeclarations
.Select(d => $"{d}{Environment.NewLine}{{")
.NewLine();

var containingTypesClosing = Enumerable.Repeat("}", containingTypeDeclarations.Count)
.NewLine();

var code = $$"""
{{containingTypesOpening}}
partial {{classOrInterfaceOrRecord}} {{TypeName}} : IMemoryPackFormatterRegister
{
static partial void StaticConstructor();
Expand Down Expand Up @@ -1038,6 +1034,7 @@ public override void Deserialize(ref MemoryPackReader reader, {{scopedRef}} {{Ty
}
}
}
{{containingTypesClosing}}
""";

return code;
Expand Down Expand Up @@ -1268,6 +1265,34 @@ partial class {{TypeName}} : IMemoryPackFormatterRegister

return code;
}

IReadOnlyList<string> GetContainingTypeDeclarations()
{
var containingTypeDeclarations = new List<string>();
var containingType = Symbol.ContainingType;
while (containingType is not null)
{
if (containingType.TypeKind == TypeKind.Interface)
{
containingTypeDeclarations.Add($"partial interface {containingType.Name}");
}
else
{
containingTypeDeclarations.Add((containingType.IsRecord, containingType.IsValueType) switch
{
(true, true) => $"partial record struct {containingType.Name}",
(true, false) => $"partial record {containingType.Name}",
(false, true) => $"partial struct {containingType.Name}",
(false, false) => $"partial class {containingType.Name}",
});
}

containingType = containingType.ContainingType;
}
containingTypeDeclarations.Reverse();

return containingTypeDeclarations;
}
}

public partial class MethodMeta
Expand Down
39 changes: 39 additions & 0 deletions tests/MemoryPack.Tests/NestedUnionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace MemoryPack.Tests;

[MemoryPackable]
public partial record NestedUnion
{
[MemoryPackable]
[MemoryPackUnion(0, typeof(NestedUnionA))]
[MemoryPackUnion(1, typeof(NestedUnionB))]
public partial interface INestedUnion
{

}

[MemoryPackable]
public partial record NestedUnionA(string Value) : INestedUnion;

[MemoryPackable]
public partial record NestedUnionB(string Value) : INestedUnion;
}

[MemoryPackable]
public partial record NestedUnionContainer
{
public required NestedUnion.INestedUnion NestedUnion { get; init; }
}

public class NestedUnionTest
{
[Fact]
public void CanSerializeNestedUnion()
{
var data = new NestedUnionContainer { NestedUnion = new NestedUnion.NestedUnionB("Foo") };
var bytes = MemoryPackSerializer.Serialize(data);
var result = MemoryPackSerializer.Deserialize<NestedUnionContainer>(bytes);
result.Should().NotBeNull();
result?.NestedUnion.Should().BeOfType<NestedUnion.NestedUnionB>();
(result?.NestedUnion as NestedUnion.NestedUnionB)?.Value.Should().Be("Foo");
}
}
Loading