Skip to content

Commit 634413f

Browse files
committed
Helper methods for parsing are not always necessary
1 parent 48774cd commit 634413f

51 files changed

Lines changed: 18 additions & 702 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ParsableCodeGenerator.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ private void GenerateParse(StringBuilder sb, ParsableGeneratorState state)
8282
public static ").AppendTypeFullyQualified(state.Type).Append(@" Parse(string s, global::System.IFormatProvider? provider)
8383
{");
8484

85+
var needParseMethod = false;
86+
8587
if (state.KeyMember?.IsString() == true || state.HasStringBasedValidateMethod)
8688
{
8789
sb.Append(@"
8890
var validationError = Validate<").AppendTypeFullyQualified(state.Type).Append(">(s, provider, out var result);");
8991
}
9092
else if (state.KeyMember is not null)
9193
{
94+
needParseMethod = true;
9295
sb.Append(@"
9396
var key = ParseValue<").AppendTypeFullyQualified(state.KeyMember).Append(@">(s, provider);
9497
var validationError = Validate<").AppendTypeFullyQualified(state.Type).Append(">(key, provider, out var result);");
@@ -100,14 +103,19 @@ private void GenerateParse(StringBuilder sb, ParsableGeneratorState state)
100103
return result!;
101104
102105
throw new global::System.FormatException(validationError.ToString() ?? ""Unable to parse \""").Append(state.Type.Name).Append(@"\""."");
103-
}
106+
}");
107+
108+
if (needParseMethod)
109+
{
110+
sb.Append(@"
104111
105112
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
106113
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
107114
where TValue : global::System.IParsable<TValue>
108115
{
109116
return TValue.Parse(s, provider);
110117
}");
118+
}
111119
}
112120

113121
private static void GenerateParseForReadOnlySpanOfChar(StringBuilder sb, ParsableGeneratorState state)
@@ -146,6 +154,8 @@ public static bool TryParse(
146154
return false;
147155
}");
148156

157+
var needParseMethod = false;
158+
149159
if (state.KeyMember?.IsString() == true || state.HasStringBasedValidateMethod)
150160
{
151161
sb.Append(@"
@@ -154,6 +164,7 @@ public static bool TryParse(
154164
}
155165
else if (state.KeyMember is not null)
156166
{
167+
needParseMethod = true;
157168
sb.Append(@"
158169
159170
if (!TryParseValue<").AppendTypeFullyQualified(state.KeyMember).Append(@">(s, provider, out var key))
@@ -167,14 +178,19 @@ public static bool TryParse(
167178

168179
sb.Append(@"
169180
return validationError is null;
170-
}
181+
}");
182+
183+
if (needParseMethod)
184+
{
185+
sb.Append(@"
171186
172187
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
173188
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
174189
where TValue : global::System.IParsable<TValue>
175190
{
176191
return TValue.TryParse(s, provider, out result);
177192
}");
193+
}
178194
}
179195

180196
private static void GenerateTryParseForReadOnlySpanOfChar(StringBuilder sb, ParsableGeneratorState state)

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_complex_class_with_8_members_and_ObjectFactoryAttribute_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial class TestValueObject :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestValueObject\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial class TestValueObject :
4538
var validationError = Validate<global::Thinktecture.Tests.TestValueObject>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_complex_class_with_8_members_and_ObjectFactoryAttribute_and_UseForSerialization_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial class TestValueObject :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestValueObject\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial class TestValueObject :
4538
var validationError = Validate<global::Thinktecture.Tests.TestValueObject>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_constructor_call_when_HasCorrespondingConstructor_is_true_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial class TestClass :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestClass\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial class TestClass :
4538
var validationError = Validate<global::Thinktecture.Tests.TestClass>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_int_based_class_with_ObjectFactoryAttribute_and_UseForSerialization_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial class TestEnum :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestEnum\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial class TestEnum :
4538
var validationError = Validate<global::Thinktecture.Tests.TestEnum>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_object_factory_for_ad_hoc_union_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial class TestUnion :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestUnion\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial class TestUnion :
4538
var validationError = Validate<global::Thinktecture.Tests.TestUnion>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_object_factory_for_regular_union_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial record Result :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"Result\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial record Result :
4538
var validationError = Validate<global::Thinktecture.Tests.Result<T>>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/ObjectFactorySourceGeneratorTests.Should_generate_object_factory_for_smart_enum_class_based_on_int_1.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ partial class TestEnum :
2323
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestEnum\".");
2424
}
2525

26-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
27-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
28-
where TValue : global::System.IParsable<TValue>
29-
{
30-
return TValue.Parse(s, provider);
31-
}
32-
3326
/// <inheritdoc />
3427
public static bool TryParse(
3528
string? s,
@@ -45,11 +38,4 @@ partial class TestEnum :
4538
var validationError = Validate<global::Thinktecture.Tests.TestEnum>(s, provider, out result!);
4639
return validationError is null;
4740
}
48-
49-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
50-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
51-
where TValue : global::System.IParsable<TValue>
52-
{
53-
return TValue.TryParse(s, provider, out result);
54-
}
5541
}

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_change_conversion_from_key_Explicit2.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ partial class TestEnum :
3434
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestEnum\".");
3535
}
3636

37-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
38-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
39-
where TValue : global::System.IParsable<TValue>
40-
{
41-
return TValue.Parse(s, provider);
42-
}
43-
4437
#if NET9_0_OR_GREATER
4538
/// <inheritdoc />
4639
public static global::Thinktecture.Tests.TestEnum Parse(global::System.ReadOnlySpan<char> s, global::System.IFormatProvider? provider)
@@ -70,13 +63,6 @@ partial class TestEnum :
7063
return validationError is null;
7164
}
7265

73-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
74-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
75-
where TValue : global::System.IParsable<TValue>
76-
{
77-
return TValue.TryParse(s, provider, out result);
78-
}
79-
8066
#if NET9_0_OR_GREATER
8167
/// <inheritdoc />
8268
public static bool TryParse(

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/SourceGeneratorTests/SmartEnumSourceGeneratorTests.Should_change_conversion_from_key_Implicit2.verified.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ partial class TestEnum :
3434
throw new global::System.FormatException(validationError.ToString() ?? "Unable to parse \"TestEnum\".");
3535
}
3636

37-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
38-
private static TValue ParseValue<TValue>(string s, global::System.IFormatProvider? provider)
39-
where TValue : global::System.IParsable<TValue>
40-
{
41-
return TValue.Parse(s, provider);
42-
}
43-
4437
#if NET9_0_OR_GREATER
4538
/// <inheritdoc />
4639
public static global::Thinktecture.Tests.TestEnum Parse(global::System.ReadOnlySpan<char> s, global::System.IFormatProvider? provider)
@@ -70,13 +63,6 @@ partial class TestEnum :
7063
return validationError is null;
7164
}
7265

73-
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
74-
private static bool TryParseValue<TValue>(string? s, global::System.IFormatProvider? provider, [global::System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out TValue result)
75-
where TValue : global::System.IParsable<TValue>
76-
{
77-
return TValue.TryParse(s, provider, out result);
78-
}
79-
8066
#if NET9_0_OR_GREATER
8167
/// <inheritdoc />
8268
public static bool TryParse(

0 commit comments

Comments
 (0)