Skip to content

Commit

Permalink
Fix for New() and Empty-field on record struct (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Kalle Skattegård <[email protected]>
  • Loading branch information
skattegard and SkattegardConsid authored Nov 27, 2023
1 parent 8bc42e7 commit 28e8d55
Show file tree
Hide file tree
Showing 37 changed files with 189 additions and 19 deletions.
10 changes: 9 additions & 1 deletion src/Strongly/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,16 @@ static string CreateStrongValue(
var ctorIndex =
baseDef.IndexOf(EmbeddedSources.CtorKey, StringComparison.InvariantCulture);

var emptyMethodIndex =
baseDef.IndexOf("public static readonly TYPENAME Empty =", StringComparison.InvariantCulture);

var emptyMethod = emptyMethodIndex == -1
? ""
: baseDef.Substring(emptyMethodIndex) + Environment.NewLine;

baseDef = baseDef
.Substring(0, ctorIndex + EmbeddedSources.CtorKey.Length)
.Substring(0, ctorIndex + EmbeddedSources.CtorValueKey.Length)
.Insert(ctorIndex, emptyMethod)
.Replace("readonly partial struct", "readonly partial record struct")
+ Environment.NewLine;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Strongly/Templates/BigInteger/BigInteger_Base.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
 public static readonly TYPENAME Empty = new TYPENAME(System.Numerics.BigInteger.Zero);
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public static readonly TYPENAME Empty = new TYPENAME(System.Numerics.BigInteger.Zero);
4 changes: 2 additions & 2 deletions src/Strongly/Templates/Decimal/Decimal_Base.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
 public static readonly TYPENAME Empty = new TYPENAME(decimal.Zero);
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);
public static readonly TYPENAME Empty = new TYPENAME(decimal.Zero);
5 changes: 3 additions & 2 deletions src/Strongly/Templates/Double/Double_Base.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
 public static readonly TYPENAME Empty = new TYPENAME(0.0);
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);
public static readonly TYPENAME Empty = new TYPENAME(0.0);

5 changes: 3 additions & 2 deletions src/Strongly/Templates/Float/Float_Base.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
 public static readonly TYPENAME Empty = new TYPENAME(0f);
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);
public static readonly TYPENAME Empty = new TYPENAME(0f);

11 changes: 5 additions & 6 deletions src/Strongly/Templates/Guid/Guid_Base.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

[NEW_METHOD]

public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public static readonly TYPENAME Empty = new TYPENAME(System.Guid.Empty);


[NEW_METHOD]

1 change: 1 addition & 0 deletions src/Strongly/Templates/Long/Long_Base.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public static readonly TYPENAME Empty = new TYPENAME(0);
7 changes: 4 additions & 3 deletions src/Strongly/Templates/NewId/NewId_Base.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);


public static readonly TYPENAME Empty = new TYPENAME(MassTransit.NewId.Empty);

public static TYPENAME New() => new TYPENAME(MassTransit.NewId.Next());
public static readonly TYPENAME Empty = new TYPENAME(MassTransit.NewId.Empty);


6 changes: 6 additions & 0 deletions test/Strongly.Tests/BigIntegerIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class BigIntegerIdTests
const string BigNString = "307056936428751749926718693741896073217";
static readonly BigInteger BigN = BigInteger.Parse(BigNString);

[Fact]
public void RecordHaveEmpty()
{
_ = RecordBigIntegerId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/ByteIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class ByteIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = ByteId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/DecimalIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class DecimalIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordDecimalId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
7 changes: 7 additions & 0 deletions test/Strongly.Tests/DefaultIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ namespace Strongly.IntegrationTests;

public class DefaultIdTests
{
[Fact]
public void RecordHaveEmptyAndNew()
{
_ = RecordDefaultId1.New();
_ = RecordDefaultId1.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/DoubleIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class DoubleIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordDoubleId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/FloatIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class FloatIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordFloatId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
7 changes: 7 additions & 0 deletions test/Strongly.Tests/GuidCombIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Strongly.IntegrationTests;

public class GuidCombIdTests
{
[Fact]
public void RecordHaveEmptyAndNew()
{
_ = RecordGuidCombId1.New();
_ = RecordGuidCombId1.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
7 changes: 7 additions & 0 deletions test/Strongly.Tests/GuidIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ public void RecordSameValuesAreEqual()
Assert.Equal(foo1, foo2);
}

[Fact]
public void RecordHaveNewAndEmpty()
{
_ = RecordGuidId1.New();
_ = RecordGuidId1.Empty;
}

[Fact]
public void ImplicitCasts()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/IntIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class IntIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordIntId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/LongIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class LongIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordLongId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
7 changes: 7 additions & 0 deletions test/Strongly.Tests/MassTransitNewIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Strongly.IntegrationTests;

public class MassTransitNewIdTests
{
[Fact]
public void RecordHaveEmptyAndNew()
{
_ = RecordNewIdId1.Empty;
_ = RecordNewIdId1.New();
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/NativeIntIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class NativeIntIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordNativeIntId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
7 changes: 7 additions & 0 deletions test/Strongly.Tests/SequentialGuidIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Strongly.IntegrationTests;

public class SequentialGuidIdTests
{
[Fact]
public void RecordHaveEmptyAndNew()
{
_ = RecordSequentialGuidId1.New();
_ = RecordSequentialGuidId1.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/ShortIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class ShortIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordShortId.Empty;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down
6 changes: 6 additions & 0 deletions test/Strongly.Tests/StringIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Strongly.IntegrationTests;

public class StringIdTests
{
[Fact]
public void RecordHaveEmpty()
{
_ = RecordStringId.Empty;
}

[Fact]
public async Task ThrowsIfTryToCreateWithNull()
{
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/BigIntegerId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ partial struct BigIntegerId
{
}

[Strongly(backingType: StronglyType.BigInteger)]
partial record struct RecordBigIntegerId
{
}

[Strongly(converters: StronglyConverter.None,
backingType: StronglyType.BigInteger)]
public partial struct NoConverterBigIntegerId
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/ByteId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public partial struct ByteId
{
}

[Strongly(backingType: StronglyType.Byte)]
public partial record struct RecordByteId
{
}

[Strongly(converters: StronglyConverter.None, backingType: StronglyType.Byte)]
public partial struct NoConverterByteId
{
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/DecimalId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public partial struct DecimalId
{
}

[Strongly(backingType: StronglyType.Decimal)]
public partial record struct RecordDecimalId
{
}

[Strongly(converters: StronglyConverter.None,
backingType: StronglyType.Decimal)]
public partial struct NoConverterDecimalId
Expand Down
3 changes: 3 additions & 0 deletions test/Strongly.Tests/Types/DefaultId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Strongly.IntegrationTests.Types;
[Strongly]
partial struct DefaultId1 { }

[Strongly]
partial record struct RecordDefaultId1 { }

[Strongly]
public partial struct DefaultId2 { }

Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/DoubleId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public partial struct DoubleId
{
}

[Strongly(backingType: StronglyType.Double)]
public partial record struct RecordDoubleId
{
}

[Strongly(converters: StronglyConverter.None,
backingType: StronglyType.Double)]
public partial struct NoConverterDoubleId
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/FloatId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public partial struct FloatId
{
}

[Strongly(backingType: StronglyType.Float)]
public partial record struct RecordFloatId
{
}

[Strongly(converters: StronglyConverter.None,
backingType: StronglyType.Float)]
public partial struct NoConverterFloatId
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/GuidCombId.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
namespace Strongly.IntegrationTests.Types;

[Strongly(backingType: StronglyType.GuidComb)]
public partial record struct RecordGuidCombId1
{
}

[Strongly(backingType: StronglyType.GuidComb)]
public partial struct GuidCombId1
{
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/IntId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public partial struct IntId
{
}

[Strongly(backingType: StronglyType.Int)]
public partial record struct RecordIntId
{
}

[Strongly(converters: StronglyConverter.None, backingType: StronglyType.Int)]
public partial struct NoConverterIntId
{
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/LongId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ partial struct LongId
{
}

[Strongly(backingType: StronglyType.Long)]
partial record struct RecordLongId
{
}

[Strongly(converters: StronglyConverter.None, backingType: StronglyType.Long)]
public partial struct NoConverterLongId
{
Expand Down
5 changes: 5 additions & 0 deletions test/Strongly.Tests/Types/NIntId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ public partial struct NativeIntId
{
}

[Strongly(backingType: StronglyType.NativeInt)]
public partial record struct RecordNativeIntId
{
}

[Strongly(converters: StronglyConverter.None, backingType: StronglyType.NativeInt)]
public partial struct NoConverterNativeIntId
{
Expand Down
Loading

0 comments on commit 28e8d55

Please sign in to comment.