Skip to content

Commit

Permalink
add parse and tryparse
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Oct 21, 2022
1 parent 6d36325 commit 2d3efa1
Show file tree
Hide file tree
Showing 1,568 changed files with 19,140 additions and 699 deletions.
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Guid/Guid_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ public override bool Equals(object obj)
public override string ToString() => Value.ToString();
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

public static TESTID Parse(string value) => new TESTID(System.Guid.Parse(value));
public static bool TryParse(string value, out TESTID result)
{
if (System.Guid.TryParse(value, out System.Guid parseResult))
{
result = new TESTID(parseResult);
return true;
}
result = default;
return false;
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Int/Int_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ public override bool Equals(object obj)
public override string ToString() => Value.ToString();
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

public static TESTID Parse(string value) => new TESTID(int.Parse(value));
public static bool TryParse(string value, out TESTID result)
{
if (int.TryParse(value, out int parseResult))
{
result = new TESTID(parseResult);
return true;
}
result = default;
return false;
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Long/Long_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ public override bool Equals(object obj)
public override string ToString() => Value.ToString();
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

public static TESTID Parse(string value) => new TESTID(long.Parse(value));
public static bool TryParse(string value, out TESTID result)
{
if (long.TryParse(value, out long parseResult))
{
result = new TESTID(parseResult);
return true;
}
result = default;
return false;
}
15 changes: 15 additions & 0 deletions src/StronglyTypedIds/Templates/NewId/NewId_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ public override bool Equals(object obj)
public override string ToString() => Value.ToString();
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

public static TESTID Parse(string value) => new TESTID(new MassTransit.NewId(in value));
public static bool TryParse(string value, out TESTID result)
{
try
{
result = new TESTID(new MassTransit.NewId(in value));
return true;
}
catch
{
result = default;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ public override bool Equals(object? obj)
public override string? ToString() => Value;
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

public static TESTID Parse(string? value) => new TESTID(value?.Trim());
public static bool TryParse(string? value, out TESTID result)
{
result = new TESTID(value?.Trim());
return true;
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/String/String_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ public override bool Equals(object obj)
public override string ToString() => Value;
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

public static TESTID Parse(string value) => new TESTID(value.Trim());
public static bool TryParse(string value, out TESTID result)
{
if (string.IsNullOrWhiteSpace(value))
{
result = default;
return false;
}
result = new TESTID(value.Trim());
return true;
}
36 changes: 36 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,42 @@ public void CantCreateEmptyGeneratedId1()
Assert.NotEqual((object)bar, (object)foo);
}

[Fact]
public void CanParseString()
{
var value = Guid.NewGuid();
var foo = GuidId1.Parse(value.ToString());
var bar = new GuidId1(value);

Assert.Equal(bar, foo);
}

[Fact]
public void ThrowWhenInvalidParseString()
{
Assert.Throws<FormatException>(() => GuidId1.Parse(""));
}

[Fact]
public void CanFailTryParse()
{
var result = GuidId1.TryParse("", out _);
Assert.False(result);
}


[Fact]
public void CanTryParseSuccessfully()
{
var value = Guid.NewGuid();
var result = GuidId1.TryParse(value.ToString(), out GuidId1 foo);
var bar = new GuidId1(value);

Assert.True(result);
Assert.Equal(bar, foo);
}


[Fact]
public void CanSerializeToGuid_WithTypeConverter()
{
Expand Down
37 changes: 37 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ public void DifferentTypesAreUnequal()
Assert.NotEqual((object)bar, (object)foo);
}

[Fact]
public void CanParseString()
{
var value = 1;
var foo = IntId.Parse(value.ToString());
var bar = new IntId(value);

Assert.Equal(bar, foo);
}

[Fact]
public void ThrowWhenInvalidParseString()
{
Assert.Throws<FormatException>(() => IntId.Parse(""));
}

[Fact]
public void CanFailTryParse()
{
var result = IntId.TryParse("", out _);
Assert.False(result);
}


[Fact]
public void CanTryParseSuccessfully()
{
var value = 2;
var result = IntId.TryParse(value.ToString(), out IntId foo);
var bar = new IntId(value);

Assert.True(result);
Assert.Equal(bar, foo);
}



[Fact]
public void CanSerializeToInt_WithNewtonsoftJsonProvider()
{
Expand Down
36 changes: 36 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ public void DifferentTypesAreUnequal()
Assert.NotEqual((object)bar, (object)foo);
}

[Fact]
public void CanParseString()
{
var value = 1L;
var foo = LongId.Parse(value.ToString());
var bar = new LongId(value);

Assert.Equal(bar, foo);
}

[Fact]
public void ThrowWhenInvalidParseString()
{
Assert.Throws<FormatException>(() => LongId.Parse(""));
}

[Fact]
public void CanFailTryParse()
{
var result = LongId.TryParse("", out _);
Assert.False(result);
}


[Fact]
public void CanTryParseSuccessfully()
{
var value = 2L;
var result = LongId.TryParse(value.ToString(), out LongId foo);
var bar = new LongId(value);

Assert.True(result);
Assert.Equal(bar, foo);
}


[Fact]
public void CanSerializeToLong_WithNewtonsoftJsonProvider()
{
Expand Down
35 changes: 35 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ public void CantCreateEmptyGeneratedId1()
Assert.NotEqual((object)bar, (object)foo);
}

[Fact]
public void CanParseString()
{
var value = NewId.Next();
var foo = NewIdId1.Parse(value.ToString());
var bar = new NewIdId1(value);

Assert.Equal(bar, foo);
}

[Fact]
public void ThrowWhenInvalidParseString()
{
Assert.Throws<FormatException>(() => NewIdId1.Parse("invalid"));
}

[Fact]
public void CanFailTryParse()
{
var result = NewIdId1.TryParse("invalid", out _);
Assert.False(result);
}


[Fact]
public void CanTryParseSuccessfully()
{
var value = NewId.Next();
var result = NewIdId1.TryParse(value.ToString(), out NewIdId1 foo);
var bar = new NewIdId1(value);

Assert.True(result);
Assert.Equal(bar, foo);
}

[Fact]
public void CanSerializeToNewId_WithTypeConverter()
{
Expand Down
21 changes: 21 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ public void DifferentTypesAreUnequal()
Assert.NotEqual((object)bar, (object)foo);
}

[Fact]
public void CanParseSuccessfully()
{
var value = "123ABC";
var foo = NullableStringId.Parse($" {value} ");
var bar = new NullableStringId(value);

Assert.Equal(bar, foo);
}

[Fact]
public void CanTryParseSuccessfully()
{
var value = "123ABC";
var result = NullableStringId.TryParse($" {value} ", out NullableStringId foo);
var bar = new NullableStringId(value);

Assert.True(result);
Assert.Equal(bar, foo);
}

[Fact]
public void CanSerializeToString_WithNewtonsoftJsonProvider()
{
Expand Down
34 changes: 34 additions & 0 deletions test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,40 @@ public void DifferentTypesAreUnequal()
Assert.NotEqual((object)bar, (object)foo);
}


[Fact]
public void CanParseSuccessfully()
{
var value = "123ABC";
var foo = StringId.Parse($" {value} ");
var bar = new StringId(value);

Assert.Equal(bar, foo);
}


[Fact]
public void CanTryParseSuccessfully()
{
var value = "123ABC";
var result = StringId.TryParse($" {value} ", out StringId foo);
var bar = new StringId(value);

Assert.True(result);
Assert.Equal(bar, foo);
}


[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null!)]
public void CaTryParseFailOnInvalidStrings(string value)
{
var result = StringId.TryParse(value, out _);
Assert.False(result);
}

[Fact]
public void CanSerializeToString_WithNewtonsoftJsonProvider()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
Expand Down Expand Up @@ -35,5 +35,17 @@ namespace Some.Namespace
public override string ToString() => Value.ToString();
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);

public static MyTestId Parse(string value) => new MyTestId(System.Guid.Parse(value));
public static bool TryParse(string value, out MyTestId result)
{
if (System.Guid.TryParse(value, out System.Guid parseResult))
{
result = new MyTestId(parseResult);
return true;
}
result = default;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
Expand Down Expand Up @@ -35,5 +35,17 @@ namespace Some.Namespace
public override string ToString() => Value.ToString();
public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);

public static MyTestId Parse(string value) => new MyTestId(System.Guid.Parse(value));
public static bool TryParse(string value, out MyTestId result)
{
if (System.Guid.TryParse(value, out System.Guid parseResult))
{
result = new MyTestId(parseResult);
return true;
}
result = default;
return false;
}
}
}
Loading

0 comments on commit 2d3efa1

Please sign in to comment.