Skip to content

Commit f2e4f12

Browse files
committed
Added support for class generated identifiers.
1 parent 7e3ca87 commit f2e4f12

18 files changed

+228
-130
lines changed

Diff for: ComparisonBenchmarks/ComparisonBenchmarks.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ItemGroup>
1414
<PackageReference Include="BenchmarkDotNet" Version="0.14.0"/>
1515
<PackageReference Include="FluentResults" Version="3.16.0"/>
16-
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
16+
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
1717
<PackageReference Include="Rascal" Version="1.1.0"/>
1818
</ItemGroup>
1919

Diff for: src/LightResults.Extensions.GeneratedIdentifier/GeneratedIdentifierSourceGenerator.cs

+146-80
Large diffs are not rendered by default.

Diff for: src/LightResults.Extensions.GeneratedIdentifier/LightResults.Extensions.GeneratedIdentifier.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<!-- Output -->
3737
<PropertyGroup>
3838
<AssemblyName>LightResults.Extensions.GeneratedIdentifier</AssemblyName>
39-
<Version>9.0.0-preview.4</Version>
39+
<Version>9.0.0-preview.5</Version>
4040
<AssemblyVersion>9.0.0.0</AssemblyVersion>
4141
<FileVersion>9.0.0.0</FileVersion>
4242
<NeutralLanguage>en-US</NeutralLanguage>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace LightResults.Extensions.GeneratedIdentifier.Fixtures.Identifiers;
22

33
[GeneratedIdentifier<string>]
4-
public partial struct TestStringId;
4+
public partial class TestStringId;

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Fixtures/LightResults.Extensions.GeneratedIdentifier.Fixtures.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<ItemGroup>
2525
<PackageReference Include="LightResults" Version="9.0.0-preview.15"/>
26-
<PackageReference Include="LightResults.Extensions.ValueObjects" Version="9.0.0-preview.1"/>
26+
<PackageReference Include="LightResults.Extensions.ValueObjects" Version="9.0.0-preview.3"/>
2727
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.10"/>
2828
</ItemGroup>
2929

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ readonly partial struct TestGuidId :
139139
/// <inheritdoc />
140140
public int CompareTo(object? obj)
141141
{
142-
if (ReferenceEquals(null, obj)) return 1;
143-
return obj is TestGuidId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestGuidId)}");
142+
if (ReferenceEquals(null, obj))
143+
return 1;
144+
return obj is TestGuidId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestGuidId)}.");
144145
}
145146

146147
/// <summary>Determines whether the first instance of <see cref="TestGuidId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateGuidIdentifier_WithoutNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ readonly partial struct TestGuidId :
137137
/// <inheritdoc />
138138
public int CompareTo(object? obj)
139139
{
140-
if (ReferenceEquals(null, obj)) return 1;
141-
return obj is TestGuidId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestGuidId)}");
140+
if (ReferenceEquals(null, obj))
141+
return 1;
142+
return obj is TestGuidId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestGuidId)}.");
142143
}
143144

144145
/// <summary>Determines whether the first instance of <see cref="TestGuidId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ readonly partial struct TestIntId :
139139
/// <inheritdoc />
140140
public int CompareTo(object? obj)
141141
{
142-
if (ReferenceEquals(null, obj)) return 1;
143-
return obj is TestIntId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestIntId)}");
142+
if (ReferenceEquals(null, obj))
143+
return 1;
144+
return obj is TestIntId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestIntId)}.");
144145
}
145146

146147
/// <summary>Determines whether the first instance of <see cref="TestIntId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateIntIdentifier_WithoutNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ readonly partial struct TestIntId :
137137
/// <inheritdoc />
138138
public int CompareTo(object? obj)
139139
{
140-
if (ReferenceEquals(null, obj)) return 1;
141-
return obj is TestIntId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestIntId)}");
140+
if (ReferenceEquals(null, obj))
141+
return 1;
142+
return obj is TestIntId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestIntId)}.");
142143
}
143144

144145
/// <summary>Determines whether the first instance of <see cref="TestIntId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ readonly partial struct TestLongId :
139139
/// <inheritdoc />
140140
public int CompareTo(object? obj)
141141
{
142-
if (ReferenceEquals(null, obj)) return 1;
143-
return obj is TestLongId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestLongId)}");
142+
if (ReferenceEquals(null, obj))
143+
return 1;
144+
return obj is TestLongId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestLongId)}.");
144145
}
145146

146147
/// <summary>Determines whether the first instance of <see cref="TestLongId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateLongIdentifier_WithoutNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ readonly partial struct TestLongId :
137137
/// <inheritdoc />
138138
public int CompareTo(object? obj)
139139
{
140-
if (ReferenceEquals(null, obj)) return 1;
141-
return obj is TestLongId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestLongId)}");
140+
if (ReferenceEquals(null, obj))
141+
return 1;
142+
return obj is TestLongId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestLongId)}.");
142143
}
143144

144145
/// <summary>Determines whether the first instance of <see cref="TestLongId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ readonly partial struct TestShortId :
139139
/// <inheritdoc />
140140
public int CompareTo(object? obj)
141141
{
142-
if (ReferenceEquals(null, obj)) return 1;
143-
return obj is TestShortId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestShortId)}");
142+
if (ReferenceEquals(null, obj))
143+
return 1;
144+
return obj is TestShortId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestShortId)}.");
144145
}
145146

146147
/// <summary>Determines whether the first instance of <see cref="TestShortId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateShortIdentifier_WithoutNamespace.verified.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ readonly partial struct TestShortId :
137137
/// <inheritdoc />
138138
public int CompareTo(object? obj)
139139
{
140-
if (ReferenceEquals(null, obj)) return 1;
141-
return obj is TestShortId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestShortId)}");
140+
if (ReferenceEquals(null, obj))
141+
return 1;
142+
return obj is TestShortId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestShortId)}.");
142143
}
143144

144145
/// <summary>Determines whether the first instance of <see cref="TestShortId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithNamespace.verified.txt

+25-13
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ namespace MyProject.Identifiers;
2121

2222
[TypeConverter(typeof(TestStringIdTypeConverter))]
2323
[JsonConverter(typeof(TestStringIdJsonConverter))]
24-
readonly partial struct TestStringId :
24+
partial class TestStringId :
2525
ICreatableValueObject<string, TestStringId>,
2626
IValueObject<string, TestStringId>,
2727
IComparable<TestStringId>,
2828
IComparable
2929
{
30-
/// <summary>Gets whether this identifier is the default value.</summary>
31-
public bool IsDefault => _value == default;
32-
3330
string IValueObject<string, TestStringId>.Value => _value;
3431

3532
private readonly string _value;
@@ -62,15 +59,23 @@ readonly partial struct TestStringId :
6259
return Result.Ok<TestStringId>(new TestStringId(value, true));
6360
}
6461

65-
/// <inheritdoc />
66-
public bool Equals(TestStringId other)
67-
{
68-
return _value == other._value;
69-
}
62+
/// <inheritdoc />
63+
public bool Equals(TestStringId? other)
64+
{
65+
if (other is null)
66+
return false;
67+
if (ReferenceEquals(this, other))
68+
return true;
69+
return _value == other._value;
70+
}
7071

7172
/// <inheritdoc />
7273
public override bool Equals(object? obj)
7374
{
75+
if (obj is null)
76+
return false;
77+
if (ReferenceEquals(this, obj))
78+
return true;
7479
return obj is TestStringId other && Equals(other);
7580
}
7681

@@ -99,16 +104,23 @@ readonly partial struct TestStringId :
99104
}
100105

101106
/// <inheritdoc />
102-
public int CompareTo(TestStringId other)
107+
public int CompareTo(TestStringId? other)
103108
{
104-
return _value.CompareTo(other._value);
109+
if (other is null)
110+
return 1;
111+
if (ReferenceEquals(this, other))
112+
return 0;
113+
return string.Compare(_value, other._value, StringComparison.Ordinal);
105114
}
106115

107116
/// <inheritdoc />
108117
public int CompareTo(object? obj)
109118
{
110-
if (ReferenceEquals(null, obj)) return 1;
111-
return obj is TestStringId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestStringId)}");
119+
if (obj is null)
120+
return 1;
121+
if (ReferenceEquals(this, obj))
122+
return 0;
123+
return obj is TestStringId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestStringId)}.");
112124
}
113125

114126
/// <summary>Determines whether the first instance of <see cref="TestStringId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.GenerateStringIdentifier_WithoutNamespace.verified.txt

+25-13
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ using LightResults.Extensions.ValueObjects;
1919

2020
[TypeConverter(typeof(TestStringIdTypeConverter))]
2121
[JsonConverter(typeof(TestStringIdJsonConverter))]
22-
readonly partial struct TestStringId :
22+
partial class TestStringId :
2323
ICreatableValueObject<string, TestStringId>,
2424
IValueObject<string, TestStringId>,
2525
IComparable<TestStringId>,
2626
IComparable
2727
{
28-
/// <summary>Gets whether this identifier is the default value.</summary>
29-
public bool IsDefault => _value == default;
30-
3128
string IValueObject<string, TestStringId>.Value => _value;
3229

3330
private readonly string _value;
@@ -60,15 +57,23 @@ readonly partial struct TestStringId :
6057
return Result.Ok<TestStringId>(new TestStringId(value, true));
6158
}
6259

63-
/// <inheritdoc />
64-
public bool Equals(TestStringId other)
65-
{
66-
return _value == other._value;
67-
}
60+
/// <inheritdoc />
61+
public bool Equals(TestStringId? other)
62+
{
63+
if (other is null)
64+
return false;
65+
if (ReferenceEquals(this, other))
66+
return true;
67+
return _value == other._value;
68+
}
6869

6970
/// <inheritdoc />
7071
public override bool Equals(object? obj)
7172
{
73+
if (obj is null)
74+
return false;
75+
if (ReferenceEquals(this, obj))
76+
return true;
7277
return obj is TestStringId other && Equals(other);
7378
}
7479

@@ -97,16 +102,23 @@ readonly partial struct TestStringId :
97102
}
98103

99104
/// <inheritdoc />
100-
public int CompareTo(TestStringId other)
105+
public int CompareTo(TestStringId? other)
101106
{
102-
return _value.CompareTo(other._value);
107+
if (other is null)
108+
return 1;
109+
if (ReferenceEquals(this, other))
110+
return 0;
111+
return string.Compare(_value, other._value, StringComparison.Ordinal);
103112
}
104113

105114
/// <inheritdoc />
106115
public int CompareTo(object? obj)
107116
{
108-
if (ReferenceEquals(null, obj)) return 1;
109-
return obj is TestStringId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestStringId)}");
117+
if (obj is null)
118+
return 1;
119+
if (ReferenceEquals(this, obj))
120+
return 0;
121+
return obj is TestStringId other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TestStringId)}.");
110122
}
111123

112124
/// <summary>Determines whether the first instance of <see cref="TestStringId" /> is less than the second instance.</summary>

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/GeneratedIdentifierSourceGeneratorTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async Task GenerateStringIdentifier(bool withNamespace)
9191
var sources = GetSources("""
9292
/// <summary>Represents an identifier.</summary>
9393
[GeneratedIdentifier<string>]
94-
public partial struct TestStringId;
94+
public partial class TestStringId;
9595
""", withNamespace
9696
);
9797

@@ -126,7 +126,7 @@ namespace MyProject.Identifiers;
126126
127127
namespace LightResults.Extensions.GeneratedIdentifier;
128128
129-
[AttributeUsage(AttributeTargets.Struct)]
129+
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
130130
public sealed class GeneratedIdentifierAttribute<TIdentifier> : Attribute;
131131
""";
132132
}

Diff for: tests/LightResults.Extensions.GeneratedIdentifier.Tests/LightResults.Extensions.GeneratedIdentifier.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
2929
<PackageReference Include="SourceGeneratorTestHelpers.XUnit" Version="8.1.0"/>
3030
<PackageReference Include="Verify.DiffPlex" Version="3.1.2"/>
31-
<PackageReference Include="Verify.Xunit" Version="27.0.1"/>
31+
<PackageReference Include="Verify.Xunit" Version="28.1.3" />
3232
<PackageReference Include="xunit" Version="2.9.2"/>
3333
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
3434
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Diff for: tools/Benchmarks/Benchmarks.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
14+
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
1515
<PackageReference Include="BenchmarkDotNet" Version="0.14.0"/>
1616
</ItemGroup>
1717

0 commit comments

Comments
 (0)