Skip to content

Commit 4da9663

Browse files
committed
Smart enums implement comparison operators as well.
1 parent 73ea3dc commit 4da9663

File tree

7 files changed

+442
-1
lines changed

7 files changed

+442
-1
lines changed

samples/Thinktecture.Runtime.Extensions.Samples/SmartEnums/SmartEnumDemos.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ private static void DemoForNonValidatableEnum(ILogger logger)
7474
var comparison = ProductGroup.Apple.CompareTo(ProductGroup.Orange); // -1
7575
logger.Information("Comparison: {Comparison}", comparison);
7676

77+
var isBigger = ProductGroup.Apple > ProductGroup.Orange;
78+
logger.Information("{Apple} > {Orange} = {IsBigger}", ProductGroup.Apple, ProductGroup.Orange, isBigger);
79+
7780
logger.Information("==== Demo for abstract static members ====");
7881

7982
PrintAllItems<ProductType, string>(logger);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public sealed class DefaultMemberState : IMemberState, IEquatable<DefaultMemberS
2323
public bool IsFormattable { get; }
2424
public bool IsComparable { get; }
2525
public bool IsParsable { get; }
26+
public bool HasComparisonOperators { get; }
2627

2728
public DefaultMemberState(string name, ITypeSymbol type, string argumentName, bool isStatic)
2829
{
@@ -51,6 +52,10 @@ public DefaultMemberState(string name, ITypeSymbol type, string argumentName, bo
5152
{
5253
IsParsable = true;
5354
}
55+
else if (@interface.IsIComparisonOperators(_type))
56+
{
57+
HasComparisonOperators = true;
58+
}
5459
}
5560
}
5661

@@ -79,7 +84,8 @@ public bool Equals(DefaultMemberState? other)
7984
&& IsReferenceType == other.IsReferenceType
8085
&& IsFormattable == other.IsFormattable
8186
&& IsComparable == other.IsComparable
82-
&& IsParsable == other.IsParsable;
87+
&& IsParsable == other.IsParsable
88+
&& HasComparisonOperators == other.HasComparisonOperators;
8389
}
8490

8591
public override int GetHashCode()
@@ -95,6 +101,7 @@ public override int GetHashCode()
95101
hashCode = (hashCode * 397) ^ IsFormattable.GetHashCode();
96102
hashCode = (hashCode * 397) ^ IsComparable.GetHashCode();
97103
hashCode = (hashCode * 397) ^ IsParsable.GetHashCode();
104+
hashCode = (hashCode * 397) ^ HasComparisonOperators.GetHashCode();
98105

99106
return hashCode;
100107
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public sealed class EnumSettings : IEquatable<EnumSettings>
77
public string? KeyPropertyName { get; }
88
public bool SkipIComparable { get; }
99
public bool SkipIParsable { get; }
10+
public bool SkipIComparisonOperators { get; }
1011
public bool SkipIFormattable { get; }
1112
public bool SkipToString { get; }
1213

@@ -15,6 +16,7 @@ public EnumSettings(AttributeData? attribute)
1516
KeyPropertyName = attribute?.FindKeyPropertyName().TrimAndNullify();
1617
SkipIComparable = attribute?.FindSkipIComparable() ?? false;
1718
SkipIParsable = attribute?.FindSkipIParsable() ?? false;
19+
SkipIComparisonOperators = attribute?.FindSkipIComparisonOperators() ?? false;
1820
SkipIFormattable = attribute?.FindSkipIFormattable() ?? false;
1921
SkipToString = attribute?.FindSkipToString() ?? false;
2022
}
@@ -34,6 +36,7 @@ public bool Equals(EnumSettings? other)
3436
return KeyPropertyName == other.KeyPropertyName
3537
&& SkipIComparable == other.SkipIComparable
3638
&& SkipIParsable == other.SkipIParsable
39+
&& SkipIComparisonOperators == other.SkipIComparisonOperators
3740
&& SkipIFormattable == other.SkipIFormattable
3841
&& SkipToString == other.SkipToString;
3942
}
@@ -45,6 +48,7 @@ public override int GetHashCode()
4548
var hashCode = KeyPropertyName?.GetHashCode() ?? 0;
4649
hashCode = (hashCode * 397) ^ SkipIComparable.GetHashCode();
4750
hashCode = (hashCode * 397) ^ SkipIParsable.GetHashCode();
51+
hashCode = (hashCode * 397) ^ SkipIComparisonOperators.GetHashCode();
4852
hashCode = (hashCode * 397) ^ SkipIFormattable.GetHashCode();
4953
hashCode = (hashCode * 397) ^ SkipToString.GetHashCode();
5054

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSourceGeneratorState.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public ImmutableArray<IInterfaceCodeGenerator> GetInterfaceCodeGenerators()
156156
if (!Settings.SkipIParsable && (KeyProperty.IsString() || KeyProperty.IsParsable))
157157
generators = generators.Add(IsValidatable ? ParsableCodeGenerator.InstanceForValidatableEnum : ParsableCodeGenerator.Instance);
158158

159+
if (!Settings.SkipIComparisonOperators && KeyProperty.HasComparisonOperators)
160+
generators = generators.Add(ComparisonOperatorsCodeGenerator.Default);
161+
159162
return generators;
160163
}
161164
}

src/Thinktecture.Runtime.Extensions/EnumGenerationAttribute.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Numerics;
2+
13
namespace Thinktecture;
24

35
/// <summary>
@@ -33,6 +35,14 @@ public string KeyPropertyName
3335
/// </summary>
3436
public bool SkipIParsable { get; set; }
3537

38+
/// <summary>
39+
/// Indication whether the generator should skip the implementation of <see cref="IComparisonOperators{TSelf,TOther,TResult}"/> or not.
40+
///
41+
/// This setting has no effect if:
42+
/// - the key is not an <see cref="IComparisonOperators{TSelf,TOther,TResult}"/> itself.
43+
/// </summary>
44+
public bool SkipIComparisonOperators { get; set; }
45+
3646
/// <summary>
3747
/// Indication whether the generator should skip the implementation of <see cref="IFormattable"/> or not.
3848
///

0 commit comments

Comments
 (0)