Skip to content

Commit 2d3efa1

Browse files
committed
add parse and tryparse
1 parent 6d36325 commit 2d3efa1

File tree

1,568 files changed

+19140
-699
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,568 files changed

+19140
-699
lines changed

src/StronglyTypedIds/Templates/Guid/Guid_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,15 @@ public override bool Equals(object obj)
2222
public override string ToString() => Value.ToString();
2323
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2424
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
25+
26+
public static TESTID Parse(string value) => new TESTID(System.Guid.Parse(value));
27+
public static bool TryParse(string value, out TESTID result)
28+
{
29+
if (System.Guid.TryParse(value, out System.Guid parseResult))
30+
{
31+
result = new TESTID(parseResult);
32+
return true;
33+
}
34+
result = default;
35+
return false;
36+
}

src/StronglyTypedIds/Templates/Int/Int_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ public override bool Equals(object obj)
2121
public override string ToString() => Value.ToString();
2222
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2323
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
24+
25+
public static TESTID Parse(string value) => new TESTID(int.Parse(value));
26+
public static bool TryParse(string value, out TESTID result)
27+
{
28+
if (int.TryParse(value, out int parseResult))
29+
{
30+
result = new TESTID(parseResult);
31+
return true;
32+
}
33+
result = default;
34+
return false;
35+
}

src/StronglyTypedIds/Templates/Long/Long_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ public override bool Equals(object obj)
2121
public override string ToString() => Value.ToString();
2222
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2323
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
24+
25+
public static TESTID Parse(string value) => new TESTID(long.Parse(value));
26+
public static bool TryParse(string value, out TESTID result)
27+
{
28+
if (long.TryParse(value, out long parseResult))
29+
{
30+
result = new TESTID(parseResult);
31+
return true;
32+
}
33+
result = default;
34+
return false;
35+
}

src/StronglyTypedIds/Templates/NewId/NewId_Base.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ public override bool Equals(object obj)
2222
public override string ToString() => Value.ToString();
2323
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2424
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
25+
26+
public static TESTID Parse(string value) => new TESTID(new MassTransit.NewId(in value));
27+
public static bool TryParse(string value, out TESTID result)
28+
{
29+
try
30+
{
31+
result = new TESTID(new MassTransit.NewId(in value));
32+
return true;
33+
}
34+
catch
35+
{
36+
result = default;
37+
return false;
38+
}
39+
}

src/StronglyTypedIds/Templates/NullableString/NullableString_Base.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ public override bool Equals(object? obj)
2929
public override string? ToString() => Value;
3030
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
3131
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
32+
33+
public static TESTID Parse(string? value) => new TESTID(value?.Trim());
34+
public static bool TryParse(string? value, out TESTID result)
35+
{
36+
result = new TESTID(value?.Trim());
37+
return true;
38+
}

src/StronglyTypedIds/Templates/String/String_Base.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ public override bool Equals(object obj)
3030
public override string ToString() => Value;
3131
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
3232
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
33+
34+
public static TESTID Parse(string value) => new TESTID(value.Trim());
35+
public static bool TryParse(string value, out TESTID result)
36+
{
37+
if (string.IsNullOrWhiteSpace(value))
38+
{
39+
result = default;
40+
return false;
41+
}
42+
result = new TESTID(value.Trim());
43+
return true;
44+
}

test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ public void CantCreateEmptyGeneratedId1()
7474
Assert.NotEqual((object)bar, (object)foo);
7575
}
7676

77+
[Fact]
78+
public void CanParseString()
79+
{
80+
var value = Guid.NewGuid();
81+
var foo = GuidId1.Parse(value.ToString());
82+
var bar = new GuidId1(value);
83+
84+
Assert.Equal(bar, foo);
85+
}
86+
87+
[Fact]
88+
public void ThrowWhenInvalidParseString()
89+
{
90+
Assert.Throws<FormatException>(() => GuidId1.Parse(""));
91+
}
92+
93+
[Fact]
94+
public void CanFailTryParse()
95+
{
96+
var result = GuidId1.TryParse("", out _);
97+
Assert.False(result);
98+
}
99+
100+
101+
[Fact]
102+
public void CanTryParseSuccessfully()
103+
{
104+
var value = Guid.NewGuid();
105+
var result = GuidId1.TryParse(value.ToString(), out GuidId1 foo);
106+
var bar = new GuidId1(value);
107+
108+
Assert.True(result);
109+
Assert.Equal(bar, foo);
110+
}
111+
112+
77113
[Fact]
78114
public void CanSerializeToGuid_WithTypeConverter()
79115
{

test/StronglyTypedIds.IntegrationTests/IntIdTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,43 @@ public void DifferentTypesAreUnequal()
6464
Assert.NotEqual((object)bar, (object)foo);
6565
}
6666

67+
[Fact]
68+
public void CanParseString()
69+
{
70+
var value = 1;
71+
var foo = IntId.Parse(value.ToString());
72+
var bar = new IntId(value);
73+
74+
Assert.Equal(bar, foo);
75+
}
76+
77+
[Fact]
78+
public void ThrowWhenInvalidParseString()
79+
{
80+
Assert.Throws<FormatException>(() => IntId.Parse(""));
81+
}
82+
83+
[Fact]
84+
public void CanFailTryParse()
85+
{
86+
var result = IntId.TryParse("", out _);
87+
Assert.False(result);
88+
}
89+
90+
91+
[Fact]
92+
public void CanTryParseSuccessfully()
93+
{
94+
var value = 2;
95+
var result = IntId.TryParse(value.ToString(), out IntId foo);
96+
var bar = new IntId(value);
97+
98+
Assert.True(result);
99+
Assert.Equal(bar, foo);
100+
}
101+
102+
103+
67104
[Fact]
68105
public void CanSerializeToInt_WithNewtonsoftJsonProvider()
69106
{

test/StronglyTypedIds.IntegrationTests/LongIdTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,42 @@ public void DifferentTypesAreUnequal()
6464
Assert.NotEqual((object)bar, (object)foo);
6565
}
6666

67+
[Fact]
68+
public void CanParseString()
69+
{
70+
var value = 1L;
71+
var foo = LongId.Parse(value.ToString());
72+
var bar = new LongId(value);
73+
74+
Assert.Equal(bar, foo);
75+
}
76+
77+
[Fact]
78+
public void ThrowWhenInvalidParseString()
79+
{
80+
Assert.Throws<FormatException>(() => LongId.Parse(""));
81+
}
82+
83+
[Fact]
84+
public void CanFailTryParse()
85+
{
86+
var result = LongId.TryParse("", out _);
87+
Assert.False(result);
88+
}
89+
90+
91+
[Fact]
92+
public void CanTryParseSuccessfully()
93+
{
94+
var value = 2L;
95+
var result = LongId.TryParse(value.ToString(), out LongId foo);
96+
var bar = new LongId(value);
97+
98+
Assert.True(result);
99+
Assert.Equal(bar, foo);
100+
}
101+
102+
67103
[Fact]
68104
public void CanSerializeToLong_WithNewtonsoftJsonProvider()
69105
{

test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,41 @@ public void CantCreateEmptyGeneratedId1()
7575
Assert.NotEqual((object)bar, (object)foo);
7676
}
7777

78+
[Fact]
79+
public void CanParseString()
80+
{
81+
var value = NewId.Next();
82+
var foo = NewIdId1.Parse(value.ToString());
83+
var bar = new NewIdId1(value);
84+
85+
Assert.Equal(bar, foo);
86+
}
87+
88+
[Fact]
89+
public void ThrowWhenInvalidParseString()
90+
{
91+
Assert.Throws<FormatException>(() => NewIdId1.Parse("invalid"));
92+
}
93+
94+
[Fact]
95+
public void CanFailTryParse()
96+
{
97+
var result = NewIdId1.TryParse("invalid", out _);
98+
Assert.False(result);
99+
}
100+
101+
102+
[Fact]
103+
public void CanTryParseSuccessfully()
104+
{
105+
var value = NewId.Next();
106+
var result = NewIdId1.TryParse(value.ToString(), out NewIdId1 foo);
107+
var bar = new NewIdId1(value);
108+
109+
Assert.True(result);
110+
Assert.Equal(bar, foo);
111+
}
112+
78113
[Fact]
79114
public void CanSerializeToNewId_WithTypeConverter()
80115
{

0 commit comments

Comments
 (0)