Skip to content

Commit

Permalink
Add test for nested flattened union type for partition generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mafii committed Sep 25, 2024
1 parent ae82c6c commit 1334c9d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: DiscriminatedUnionAttribute.g.cs
// <auto-generated/>
#nullable enable

namespace Funcky
{
[global::System.Diagnostics.Conditional("Funcky_DiscriminatedUnion")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class DiscriminatedUnionAttribute : global::System.Attribute
{
/// <summary>Allow only consumers in the same assembly to use the exhaustive <c>Match</c> and <c>Switch</c> methods.</summary>
public bool NonExhaustive { get; set; }

/// <summary>Generates exhaustive <c>Match</c> and <c>Switch</c> methods for the entire type hierarchy.</summary>
public bool Flatten { get; set; }

/// <summary>If a specialized partition extension method for <c>IEnumerable<YourType></c> should be generated. Defaults to <see langword="false"/>.</summary>
public bool GeneratePartitionExtension { get; set; }

/// <summary>Customized the generic type name used for the result in the generated <c>Match</c> methods. Defaults to <c>TResult</c>.</summary>
public string? MatchResultTypeName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//HintName: DiscriminatedUnionGenerator.g.cs
// <auto-generated/>
#nullable enable

namespace Funcky.DiscriminatedUnion.Test
{
partial record FlattenedNestedUnionWithPartition
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public abstract TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public abstract void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer);

partial record Keyword
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer) => keyword(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer) => keyword(this);
}

partial record Literal
{
partial record Number
{
partial record Integer
{
[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public override TResult Match<TResult>(global::System.Func<Keyword, TResult> keyword, global::System.Func<Literal.Number.Integer, TResult> integer) => integer(this);

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public override void Switch(global::System.Action<Keyword> keyword, global::System.Action<Literal.Number.Integer> integer) => integer(this);
}
}
}
}

[global::System.CodeDom.Compiler.GeneratedCode("Funcky.DiscriminatedUnion.SourceGeneration", "1.2.0.0")]
public static partial class FlattenedNestedUnionWithPartitionEnumerableExtensions
{
public static (global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Keyword> keyword, global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Literal.Number.Integer> integer) Partition(this global::System.Collections.Generic.IEnumerable<FlattenedNestedUnionWithPartition> source)
{
var keywordItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Keyword>();
var integerItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Literal.Number.Integer>();
foreach (var item in source)
{
item.Switch(keyword: keywordItems.Add, integer: integerItems.Add);
}
return (keywordItems.AsReadOnly(), integerItems.AsReadOnly());
}

public static TResult Partition<TResult>(this global::System.Collections.Generic.IEnumerable<FlattenedNestedUnionWithPartition> source, global::System.Func<global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Keyword>, global::System.Collections.Generic.IReadOnlyList<FlattenedNestedUnionWithPartition.Literal.Number.Integer>, TResult> resultSelector)
{
var keywordItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Keyword>();
var integerItems = new global::System.Collections.Generic.List<FlattenedNestedUnionWithPartition.Literal.Number.Integer>();
foreach (var item in source)
{
item.Switch(keyword: keywordItems.Add, integer: integerItems.Add);
}
return resultSelector(keywordItems.AsReadOnly(), integerItems.AsReadOnly());
}
}
}
1 change: 1 addition & 0 deletions Funcky.DiscriminatedUnion.Test/SourceGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public sealed class SourceGeneratorTest
[InlineData("JsonPolymorphic")]
[InlineData("UnionWithPartitionUsage")]
[InlineData("FlattenedUnionWithPartition")]
[InlineData("FlattenedNestedUnionWithPartition")]
public async Task GeneratesExpectedSourceCode(string sourceFileName) => await Verify(sourceFileName);

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Funcky.DiscriminatedUnion.Test;

[DiscriminatedUnion(Flatten = true, GeneratePartitionExtension = true)]
public abstract partial record FlattenedNestedUnionWithPartition
{
public sealed partial record Keyword(string Value) : FlattenedNestedUnionWithPartition;

public abstract partial record Literal : FlattenedNestedUnionWithPartition
{
public abstract partial record Number : Literal
{
public sealed partial record Integer(int Value) : Number;
}
}
}

public static class FlattenedNestedUnionWithPartitionTest
{
public static void Test(FlattenedNestedUnionWithPartition[] items)
{
var (keywords, integers) = items.Partition();
}
}

0 comments on commit 1334c9d

Please sign in to comment.