Skip to content

Commit 4f4d08d

Browse files
Copilotdex3r
andauthored
Switch generators to use data abstraction layer (#87)
* Initial plan * Add data abstraction layer: DataMethodBody, DataMethodBodyBuilders, DataGeneratorsFactory - Create DataMethodBody hierarchy (DataSimpleReturnBody, DataSwitchBody) - Create DataMethodBodyBuilders for generating C# source from data model - Create DataGeneratorsFactory for building data from various sources - Refactor fluent and simple patterns in pipeline to use data layer - Add 22 unit tests for data building layer and code generation Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> Agent-Logs-Url: https://github.com/dex3r/EasySourceGenerators/sessions/e9007179-53d4-426e-8329-db95a8b21854 * Comment out explicit [SwitchCase]/[SwitchDefault] attribute-based generation Commented out the attribute-based SwitchCase/SwitchDefault pattern in the pipeline and all related tests. This pattern will be replaced with a data-driven approach in a future PR. The data abstraction layer (DataMethodBody, DataMethodBodyBuilders, DataGeneratorsFactory) is ready to be extended for the new pattern. Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> Agent-Logs-Url: https://github.com/dex3r/EasySourceGenerators/sessions/e9007179-53d4-426e-8329-db95a8b21854 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
1 parent b3ae50d commit 4f4d08d

File tree

13 files changed

+1216
-464
lines changed

13 files changed

+1216
-464
lines changed

EasySourceGenerators.Examples/PiExample.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33

44
namespace EasySourceGenerators.Examples;
55

6-
public static partial class PiExample
7-
{
8-
public static partial int GetPiDecimal(int decimalNumber);
9-
10-
[GeneratesMethod(nameof(GetPiDecimal))]
11-
[SwitchCase(arg1: 0)]
12-
[SwitchCase(arg1: 1)]
13-
[SwitchCase(arg1: 2)]
14-
static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
15-
SlowMath.CalculatePiDecimal(decimalNumber);
16-
17-
[GeneratesMethod(nameof(GetPiDecimal))]
18-
[SwitchDefault]
19-
static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber);
20-
}
6+
// NOTE: [SwitchCase] attribute-based generation is commented out pending replacement
7+
// with a data-driven approach. See PiExampleFluent.cs for the fluent API equivalent.
8+
// public static partial class PiExample
9+
// {
10+
// public static partial int GetPiDecimal(int decimalNumber);
11+
//
12+
// [GeneratesMethod(nameof(GetPiDecimal))]
13+
// [SwitchCase(arg1: 0)]
14+
// [SwitchCase(arg1: 1)]
15+
// [SwitchCase(arg1: 2)]
16+
// static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
17+
// SlowMath.CalculatePiDecimal(decimalNumber);
18+
//
19+
// [GeneratesMethod(nameof(GetPiDecimal))]
20+
// [SwitchDefault]
21+
// static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber);
22+
// }
2123

2224
/*
2325
This will generate the following method:
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using EasySourceGenerators.Generators;
2+
3+
namespace EasySourceGenerators.GeneratorTests;
4+
5+
[TestFixture]
6+
public class DataGeneratorsFactoryTests
7+
{
8+
[Test]
9+
public void CreateSimpleReturnBody_WithValue_CreatesCorrectData()
10+
{
11+
DataSimpleReturnBody result = DataGeneratorsFactory.CreateSimpleReturnBody("hello");
12+
13+
Assert.That(result.ReturnValue, Is.EqualTo("hello"));
14+
}
15+
16+
[Test]
17+
public void CreateSimpleReturnBody_WithNull_CreatesDataWithNullValue()
18+
{
19+
DataSimpleReturnBody result = DataGeneratorsFactory.CreateSimpleReturnBody(null);
20+
21+
Assert.That(result.ReturnValue, Is.Null);
22+
}
23+
24+
[Test]
25+
public void CreateSwitchBodyFromFluentData_WithCasesAndDefault_CreatesCorrectData()
26+
{
27+
SwitchBodyData fluentData = new SwitchBodyData(
28+
CasePairs: new List<(object key, string value)> { (1, "2"), (2, "4"), (3, "6") },
29+
HasDefaultCase: true);
30+
31+
DataSwitchBody result = DataGeneratorsFactory.CreateSwitchBodyFromFluentData(fluentData, "0");
32+
33+
Assert.That(result.Cases, Has.Count.EqualTo(3));
34+
Assert.That(result.Cases[0].Key, Is.EqualTo(1));
35+
Assert.That(result.Cases[0].FormattedValue, Is.EqualTo("2"));
36+
Assert.That(result.Cases[1].Key, Is.EqualTo(2));
37+
Assert.That(result.Cases[1].FormattedValue, Is.EqualTo("4"));
38+
Assert.That(result.Cases[2].Key, Is.EqualTo(3));
39+
Assert.That(result.Cases[2].FormattedValue, Is.EqualTo("6"));
40+
Assert.That(result.DefaultCase, Is.Not.Null);
41+
Assert.That(result.DefaultCase!.Expression, Is.EqualTo("0"));
42+
}
43+
44+
[Test]
45+
public void CreateSwitchBodyFromFluentData_WithoutDefault_CreatesDataWithNullDefaultCase()
46+
{
47+
SwitchBodyData fluentData = new SwitchBodyData(
48+
CasePairs: new List<(object key, string value)> { (1, "one") },
49+
HasDefaultCase: false);
50+
51+
DataSwitchBody result = DataGeneratorsFactory.CreateSwitchBodyFromFluentData(fluentData, null);
52+
53+
Assert.That(result.Cases, Has.Count.EqualTo(1));
54+
Assert.That(result.DefaultCase, Is.Null);
55+
}
56+
57+
[Test]
58+
public void CreateSwitchBodyFromFluentData_WithEmptyCases_CreatesEmptyData()
59+
{
60+
SwitchBodyData fluentData = new SwitchBodyData(
61+
CasePairs: new List<(object key, string value)>(),
62+
HasDefaultCase: false);
63+
64+
DataSwitchBody result = DataGeneratorsFactory.CreateSwitchBodyFromFluentData(fluentData, null);
65+
66+
Assert.That(result.Cases, Is.Empty);
67+
Assert.That(result.DefaultCase, Is.Null);
68+
}
69+
70+
[Test]
71+
public void CreateSwitchBodyFromFluentData_WithDefaultOnly_CreatesDataWithDefaultAndNoCases()
72+
{
73+
SwitchBodyData fluentData = new SwitchBodyData(
74+
CasePairs: new List<(object key, string value)>(),
75+
HasDefaultCase: true);
76+
77+
DataSwitchBody result = DataGeneratorsFactory.CreateSwitchBodyFromFluentData(fluentData, "888");
78+
79+
Assert.That(result.Cases, Is.Empty);
80+
Assert.That(result.DefaultCase, Is.Not.Null);
81+
Assert.That(result.DefaultCase!.Expression, Is.EqualTo("888"));
82+
}
83+
84+
[Test]
85+
public void CreateSwitchBodyFromFluentData_WithThrowExpression_PreservesExpression()
86+
{
87+
SwitchBodyData fluentData = new SwitchBodyData(
88+
CasePairs: new List<(object key, string value)> { (1, "\"Dog\"") },
89+
HasDefaultCase: true);
90+
91+
DataSwitchBody result = DataGeneratorsFactory.CreateSwitchBodyFromFluentData(
92+
fluentData,
93+
"throw new ArgumentException(\"Unknown\")");
94+
95+
Assert.That(result.DefaultCase, Is.Not.Null);
96+
Assert.That(result.DefaultCase!.Expression, Is.EqualTo("throw new ArgumentException(\"Unknown\")"));
97+
}
98+
99+
[Test]
100+
public void CreateSwitchBodyFromFluentData_PreservesKeyTypes()
101+
{
102+
SwitchBodyData fluentData = new SwitchBodyData(
103+
CasePairs: new List<(object key, string value)>
104+
{
105+
(true, "\"Yes\""),
106+
("hello", "\"World\""),
107+
(42, "\"Answer\"")
108+
},
109+
HasDefaultCase: false);
110+
111+
DataSwitchBody result = DataGeneratorsFactory.CreateSwitchBodyFromFluentData(fluentData, null);
112+
113+
Assert.That(result.Cases[0].Key, Is.TypeOf<bool>());
114+
Assert.That(result.Cases[1].Key, Is.TypeOf<string>());
115+
Assert.That(result.Cases[2].Key, Is.TypeOf<int>());
116+
}
117+
}

0 commit comments

Comments
 (0)