Skip to content

Commit

Permalink
Merge pull request #10 from altasoft/bugfix/EqualsAndCompareTo
Browse files Browse the repository at this point in the history
Refactor Equals and CompareTo method body generation
  • Loading branch information
temonk authored May 7, 2024
2 parents 9158c97 + 4f863f0 commit 58006f0
Show file tree
Hide file tree
Showing 27 changed files with 357 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Product>Domain Primitives</Product>
<Company>ALTA Software llc.</Company>
<Copyright>Copyright © 2024 ALTA Software llc.</Copyright>
<Version>3.0.0</Version>
<Version>3.0.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/AltaSoft.DomainPrimitives.Generator/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@ private static void Process(GeneratorData data, string ctorCode, DomainPrimitive
.NewLine();
}

MethodGeneratorHelper.GenerateEquatableOperators(data.ClassName, data.FieldName, data.TypeSymbol.IsValueType, builder);
MethodGeneratorHelper.GenerateEquatableOperators(data.ClassName, data.TypeSymbol.IsValueType, builder);
builder.NewLine();

MethodGeneratorHelper.GenerateComparableCode(data.ClassName, data.FieldName, data.TypeSymbol.IsValueType, builder);
MethodGeneratorHelper.GenerateComparableCode(data.ClassName, data.TypeSymbol.IsValueType, builder);
builder.NewLine();

if (data.GenerateImplicitOperators)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,9 @@ internal static void GenerateAdditionCode(string className, string fieldName, So
/// Generates code for implementing the IComparable interface for the specified type.
/// </summary>
/// <param name="className">The name of the class.</param>
/// <param name="fieldName">The name of the field to compare.</param>
/// <param name="isValueType">A flag indicating if the type is a value type.</param>
/// <param name="builder">The source code builder.</param>
internal static void GenerateComparableCode(string className, string fieldName, bool isValueType, SourceCodeBuilder builder)
internal static void GenerateComparableCode(string className, bool isValueType, SourceCodeBuilder builder)
{
builder.AppendInheritDoc()
.AppendLine("public int CompareTo(object? obj)")
Expand All @@ -457,7 +456,16 @@ internal static void GenerateComparableCode(string className, string fieldName,
.CloseBracket();

var nullable = isValueType ? "" : "?";
builder.NewLine().AppendInheritDoc().AppendLine($"public int CompareTo({className}{nullable} other) => {fieldName}.CompareTo(other{nullable}.{fieldName});");

builder.NewLine().AppendInheritDoc()
.AppendLine($"public int CompareTo({className}{nullable} other)")
.OpenBracket()
.Append("if (").AppendIf(!isValueType, "other is null || ").AppendLine("!other._isInitialized)")
.AppendIndentation().AppendLine("return 1;")
.AppendLine("if (!_isInitialized)")
.AppendIndentation().AppendLine("return -1;")
.AppendLine("return _value.CompareTo(other._value);")
.CloseBracket();
}

/// <summary>
Expand Down Expand Up @@ -680,10 +688,9 @@ public static void GenerateParsable(GeneratorData data, SourceCodeBuilder builde
/// Generates equality and inequality operators for the specified type.
/// </summary>
/// <param name="className">The name of the class.</param>
/// <param name="fieldName">The name of the field to compare.</param>
/// <param name="isValueType">A flag indicating if the type is a value type.</param>
/// <param name="builder">The source code builder.</param>
public static void GenerateEquatableOperators(string className, string fieldName, bool isValueType, SourceCodeBuilder builder)
public static void GenerateEquatableOperators(string className, bool isValueType, SourceCodeBuilder builder)
{
builder.AppendInheritDoc()
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
Expand All @@ -693,7 +700,12 @@ public static void GenerateEquatableOperators(string className, string fieldName

builder.AppendInheritDoc()
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
.AppendLine($"public bool Equals({className}{nullable} other) => {fieldName} == other{nullable}.{fieldName};");
.AppendLine($"public bool Equals({className}{nullable} other)")
.OpenBracket()
.Append("if (").AppendIf(!isValueType, "other is null || ").AppendLine("!_isInitialized || !other._isInitialized)")
.AppendIndentation().AppendLine("return false;")
.AppendLine("return _value.Equals(other._value);")
.CloseBracket();

builder.AppendInheritDoc()
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public BoolValue()
public override bool Equals(object? obj) => obj is BoolValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(BoolValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(BoolValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(BoolValue left, BoolValue right) => left.Equals(right);
Expand All @@ -84,7 +89,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(BoolValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(BoolValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "bool"/> to <see cref = "BoolValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ public ByteValue()
public override bool Equals(object? obj) => obj is ByteValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(ByteValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(ByteValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(ByteValue left, ByteValue right) => left.Equals(right);
Expand All @@ -88,7 +93,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(ByteValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(ByteValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "byte"/> to <see cref = "ByteValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ public CharValue()
public override bool Equals(object? obj) => obj is CharValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(CharValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(CharValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(CharValue left, CharValue right) => left.Equals(right);
Expand All @@ -88,7 +93,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(CharValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(CharValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "char"/> to <see cref = "CharValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public DateOnlyValue()
public override bool Equals(object? obj) => obj is DateOnlyValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(DateOnlyValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(DateOnlyValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(DateOnlyValue left, DateOnlyValue right) => left.Equals(right);
Expand All @@ -89,7 +94,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(DateOnlyValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(DateOnlyValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "DateOnly"/> to <see cref = "DateOnlyValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public DateTimeOffsetValue()
public override bool Equals(object? obj) => obj is DateTimeOffsetValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(DateTimeOffsetValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(DateTimeOffsetValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(DateTimeOffsetValue left, DateTimeOffsetValue right) => left.Equals(right);
Expand All @@ -89,7 +94,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(DateTimeOffsetValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(DateTimeOffsetValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "DateTimeOffset"/> to <see cref = "DateTimeOffsetValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public DateTimeValue()
public override bool Equals(object? obj) => obj is DateTimeValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(DateTimeValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(DateTimeValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(DateTimeValue left, DateTimeValue right) => left.Equals(right);
Expand All @@ -89,7 +94,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(DateTimeValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(DateTimeValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "DateTime"/> to <see cref = "DateTimeValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public DecimalValue()
public override bool Equals(object? obj) => obj is DecimalValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(DecimalValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(DecimalValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(DecimalValue left, DecimalValue right) => left.Equals(right);
Expand All @@ -93,7 +98,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(DecimalValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(DecimalValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "decimal"/> to <see cref = "DecimalValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public DoubleValue()
public override bool Equals(object? obj) => obj is DoubleValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(DoubleValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(DoubleValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(DoubleValue left, DoubleValue right) => left.Equals(right);
Expand All @@ -93,7 +98,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(DoubleValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(DoubleValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "double"/> to <see cref = "DoubleValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public FloatValue()
public override bool Equals(object? obj) => obj is FloatValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(FloatValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(FloatValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(FloatValue left, FloatValue right) => left.Equals(right);
Expand All @@ -93,7 +98,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(FloatValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(FloatValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "float"/> to <see cref = "FloatValue"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public GuidValue()
public override bool Equals(object? obj) => obj is GuidValue other && Equals(other);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(GuidValue other) => _valueOrThrow == other._valueOrThrow;
public bool Equals(GuidValue other)
{
if (!_isInitialized || !other._isInitialized)
return false;
return _value.Equals(other._value);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(GuidValue left, GuidValue right) => left.Equals(right);
Expand All @@ -87,7 +92,14 @@ public int CompareTo(object? obj)
}

/// <inheritdoc/>
public int CompareTo(GuidValue other) => _valueOrThrow.CompareTo(other._valueOrThrow);
public int CompareTo(GuidValue other)
{
if (!other._isInitialized)
return 1;
if (!_isInitialized)
return -1;
return _value.CompareTo(other._value);
}

/// <summary>
/// Implicit conversion from <see cref = "Guid"/> to <see cref = "GuidValue"/>
Expand Down
Loading

0 comments on commit 58006f0

Please sign in to comment.