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
44 changes: 26 additions & 18 deletions src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
"""


#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
/// <summary>
/// Returns a boolean telling whether an enum with the given name exists in the enumeration
/// </summary>
Expand Down Expand Up @@ -508,10 +507,10 @@ public static bool IsDefined(in global::System.ReadOnlySpan<char> name, bool all
sb.Append(
"""

global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current,
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(
""")
.Append(SymbolDisplay.FormatLiteral(dn, quote: true))
.Append(", global::System.StringComparison.Ordinal) => true,");
.Append("), global::System.StringComparison.Ordinal) => true,");
}
}

Expand Down Expand Up @@ -541,10 +540,10 @@ public static bool IsDefined(in global::System.ReadOnlySpan<char> name, bool all
sb.Append(
"""

global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(
""").Append(fullyQualifiedName).Append('.')
.Append(member.Key)
.Append("), global::System.StringComparison.Ordinal) => true,");
.Append(")), global::System.StringComparison.Ordinal) => true,");
}

sb.Append(
Expand All @@ -553,7 +552,6 @@ public static bool IsDefined(in global::System.ReadOnlySpan<char> name, bool all
_ => false,
};
}
#endif
""");

sb.Append(
Expand Down Expand Up @@ -949,7 +947,6 @@ private static bool TryParseWithCase(
}
}

#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
/// <summary>
/// Converts the string representation of the name or numeric value of
/// an <see cref="
Expand Down Expand Up @@ -1170,11 +1167,11 @@ private static bool TryParseIgnoreCase(
sb.Append(
"""

case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current,
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(
""")
.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(
"""
, global::System.StringComparison.OrdinalIgnoreCase):
), global::System.StringComparison.OrdinalIgnoreCase):
result =
""").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(
"""
Expand Down Expand Up @@ -1206,11 +1203,11 @@ private static bool TryParseIgnoreCase(
sb.Append(
"""

case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(
""").Append(fullyQualifiedName).Append('.')
.Append(member.Key).Append(
"""
), global::System.StringComparison.OrdinalIgnoreCase):
)), global::System.StringComparison.OrdinalIgnoreCase):
result =
""").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(
"""
Expand All @@ -1225,7 +1222,13 @@ private static bool TryParseIgnoreCase(
case global::System.ReadOnlySpan<char> current when
""").Append(enumToGenerate.UnderlyingType).Append(
"""
.TryParse(name, out var numericResult):
.TryParse(
name
#if !NETCOREAPP || NETCOREAPP2_0 || NETCOREAPP1_1 || NETCOREAPP1_0

.ToString()
#endif
, out var numericResult):
result = (
""").Append(fullyQualifiedName).Append(
"""
Expand Down Expand Up @@ -1267,11 +1270,11 @@ private static bool TryParseWithCase(
sb.Append(
"""

case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current,
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(
""")
.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(
"""
, global::System.StringComparison.Ordinal):
), global::System.StringComparison.Ordinal):
result =
""").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(
"""
Expand Down Expand Up @@ -1303,11 +1306,11 @@ private static bool TryParseWithCase(
sb.Append(
"""

case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(
""").Append(fullyQualifiedName).Append('.')
.Append(member.Key).Append(
"""
), global::System.StringComparison.Ordinal):
)), global::System.StringComparison.Ordinal):
result =
""").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(
"""
Expand All @@ -1322,7 +1325,13 @@ private static bool TryParseWithCase(
case global::System.ReadOnlySpan<char> current when
""").Append(enumToGenerate.UnderlyingType).Append(
"""
.TryParse(name, out var numericResult):
.TryParse(
name
#if !NETCOREAPP || NETCOREAPP2_0 || NETCOREAPP1_1 || NETCOREAPP1_0

.ToString()
#endif
, out var numericResult):
result = (
""").Append(fullyQualifiedName).Append(
"""
Expand All @@ -1333,7 +1342,6 @@ private static bool TryParseWithCase(
return false;
}
}
#endif
""");

var orderedNames = GetNamesOrderedByValue(enumToGenerate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Memory" Version="4.6.3" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr; this is the breaking change that I don't want to introduce 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it's only for the targets that benefit from that? Chances are users are already using that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just personally don't really want the complexity🤷‍♂️ You don't get ReadOnlySpan<T> support in the built-in Enum.ToString() etc for NetStandard, and it breaks for people who are currently using the library in .NET Standard and aren't using NetStandard. Also means you can no longer use it in SourceGenerators for example (because adding those packages is a world of pain)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get in all the good places.

Right now, I'm copying the generated code when I need .NET Standard 2.0 support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep it as is for now and later introduce static extension methods to support implicit conversions to and from string and ReadOnlySpan<char> when targeting the .NET 10 SDK. That said, we should bear in mind that .NET 8 will remain supported for nearly 18 more months.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep it as is for now and later introduce static extension methods to support implicit conversions to and from string and ReadOnlySpan<char> when targeting the .NET 10 SDK. That said, we should bear in mind that .NET 8 will remain supported for nearly 18 more months.

I really don't follow? 😅 Definitely don't want implicit conversions from enums to string, that's not what you mean, right? And yeah, if you don't have the .NET 10 SDK then you just don't get the extension member versions. Not sure what that has to do with this PR though? 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not from and to an enum to string. From and to string and ReadOnlySpan<char> for .NET Standard 2.0. And that would be a file scoped extension.

</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ namespace MyTestNameSpace
};
}

#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
/// <summary>
/// Returns a boolean telling whether an enum with the given name exists in the enumeration
/// </summary>
Expand All @@ -142,13 +141,12 @@ namespace MyTestNameSpace
{
return name switch
{
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.First)), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.Second)), global::System.StringComparison.Ordinal) => true,
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.Third)), global::System.StringComparison.Ordinal) => true,
_ => false,
};
}
#endif

/// <summary>
/// Converts the string representation of the name or numeric value of
Expand Down Expand Up @@ -332,7 +330,6 @@ namespace MyTestNameSpace
}
}

#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
/// <summary>
/// Converts the string representation of the name or numeric value of
/// an <see cref="global::MyTestNameSpace.MyEnum" /> to the equivalent instance.
Expand Down Expand Up @@ -461,16 +458,22 @@ namespace MyTestNameSpace
{
switch (name)
{
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.OrdinalIgnoreCase):
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.First)), global::System.StringComparison.OrdinalIgnoreCase):
result = global::MyTestNameSpace.MyEnum.First;
return true;
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.OrdinalIgnoreCase):
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.Second)), global::System.StringComparison.OrdinalIgnoreCase):
result = global::MyTestNameSpace.MyEnum.Second;
return true;
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.OrdinalIgnoreCase):
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.Third)), global::System.StringComparison.OrdinalIgnoreCase):
result = global::MyTestNameSpace.MyEnum.Third;
return true;
case global::System.ReadOnlySpan<char> current when int.TryParse(name, out var numericResult):
case global::System.ReadOnlySpan<char> current when int.TryParse(
name
#if !NETCOREAPP || NETCOREAPP2_0 || NETCOREAPP1_1 || NETCOREAPP1_0

.ToString()
#endif
, out var numericResult):
result = (global::MyTestNameSpace.MyEnum)numericResult;
return true;
default:
Expand All @@ -489,24 +492,29 @@ namespace MyTestNameSpace
{
switch (name)
{
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.First), global::System.StringComparison.Ordinal):
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.First)), global::System.StringComparison.Ordinal):
result = global::MyTestNameSpace.MyEnum.First;
return true;
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Second), global::System.StringComparison.Ordinal):
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.Second)), global::System.StringComparison.Ordinal):
result = global::MyTestNameSpace.MyEnum.Second;
return true;
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(global::MyTestNameSpace.MyEnum.Third), global::System.StringComparison.Ordinal):
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, global::System.MemoryExtensions.AsSpan(nameof(global::MyTestNameSpace.MyEnum.Third)), global::System.StringComparison.Ordinal):
result = global::MyTestNameSpace.MyEnum.Third;
return true;
case global::System.ReadOnlySpan<char> current when int.TryParse(name, out var numericResult):
case global::System.ReadOnlySpan<char> current when int.TryParse(
name
#if !NETCOREAPP || NETCOREAPP2_0 || NETCOREAPP1_1 || NETCOREAPP1_0

.ToString()
#endif
, out var numericResult):
result = (global::MyTestNameSpace.MyEnum)numericResult;
return true;
default:
result = default;
return false;
}
}
#endif

/// <summary>
/// Retrieves an array of the values of the members defined in
Expand Down
Loading
Loading