Skip to content

Commit 3082e96

Browse files
authored
String Parsable (#18)
1 parent 74d9576 commit 3082e96

11 files changed

+112
-48
lines changed

src/StronglyTypedIds/Templates/String/String_Convertible.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public float ToSingle(System.IFormatProvider? provider)
6464

6565
public string ToString(System.IFormatProvider? provider)
6666
{
67-
throw new System.NotImplementedException();
67+
return Value.ToString(provider);
6868
}
6969

7070
public object ToType(System.Type conversionType, System.IFormatProvider? provider)

src/StronglyTypedIds/Templates/String/String_NewtonsoftJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type o
1616
{
1717
if (objectType == typeof(TESTID?))
1818
{
19-
var value = serializer.Deserialize<string?>(reader);
19+
var value = serializer.Deserialize<string>(reader);
2020

2121
return value is null ? null : new TESTID(value);
2222
}
2323

2424
return new TESTID(serializer.Deserialize<string>(reader));
2525
}
26-
}
26+
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11

2-
public static TESTID Parse(string s, IFormatProvider? provider)
2+
#nullable enable
3+
public static TESTID Parse(string s, System.IFormatProvider? provider)
34
{
4-
throw new NotImplementedException();
5+
return new TESTID(s.ToString(provider));
56
}
67

7-
public static bool TryParse(string? s, IFormatProvider? provider, out TESTID result)
8+
public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
89
{
9-
result = new TESTID(s);
10-
return true;
10+
var ok = s != null;
11+
if (ok)
12+
{
13+
result = new TESTID(s!.ToString(provider));
14+
}
15+
else
16+
{
17+
result = new TESTID("");
18+
}
19+
return ok;
1120
}
21+
#nullable disable

test/IntegrationLibraries.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
<ItemGroup>
2323
<PackageReference Include="NewId" Version="4.0.0-develop.44" />
24-
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
24+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
2525
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
26-
<PackageReference Include="Dapper" Version="2.0.90" />
26+
<PackageReference Include="Dapper" Version="2.0.143" />
2727
<PackageReference Include="xunit" Version="2.4.2" />
2828
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
2929
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -34,4 +34,4 @@
3434
<PrivateAssets>all</PrivateAssets>
3535
</PackageReference>
3636
</ItemGroup>
37-
</Project>
37+
</Project>

test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
<ItemGroup>
2525
<PackageReference Include="NHibernate" Version="5.4.1" />
26-
<PackageReference Include="Dapper" Version="2.0.123" />
2726
</ItemGroup>
2827

2928
<ItemGroup>

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Core3_1.verified.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,26 @@
7373
#pragma warning restore CA1036
7474

7575

76-
public static MyTestId Parse(string s, IFormatProvider? provider)
76+
#nullable enable
77+
public static MyTestId Parse(string s, System.IFormatProvider? provider)
7778
{
78-
throw new NotImplementedException();
79+
return new MyTestId(s.ToString(provider));
7980
}
8081

81-
public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
82+
public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
8283
{
83-
result = new MyTestId(s);
84-
return true;
84+
var ok = s != null;
85+
if (ok)
86+
{
87+
result = new MyTestId(s!.ToString(provider));
88+
}
89+
else
90+
{
91+
result = new MyTestId("");
92+
}
93+
return ok;
8594
}
95+
#nullable disable
8696

8797

8898
//ICONVERTIBLE
@@ -150,7 +160,7 @@
150160

151161
public string ToString(System.IFormatProvider? provider)
152162
{
153-
throw new System.NotImplementedException();
163+
return Value.ToString(provider);
154164
}
155165

156166
public object ToType(System.Type conversionType, System.IFormatProvider? provider)
@@ -284,7 +294,7 @@
284294
{
285295
if (objectType == typeof(MyTestId?))
286296
{
287-
var value = serializer.Deserialize<string?>(reader);
297+
var value = serializer.Deserialize<string>(reader);
288298

289299
return value is null ? null : new MyTestId(value);
290300
}
@@ -293,6 +303,7 @@
293303
}
294304
}
295305

306+
296307
class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<MyTestId>
297308
{
298309
public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet6_0.verified.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,26 @@
7373
#pragma warning restore CA1036
7474

7575

76-
public static MyTestId Parse(string s, IFormatProvider? provider)
76+
#nullable enable
77+
public static MyTestId Parse(string s, System.IFormatProvider? provider)
7778
{
78-
throw new NotImplementedException();
79+
return new MyTestId(s.ToString(provider));
7980
}
8081

81-
public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
82+
public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
8283
{
83-
result = new MyTestId(s);
84-
return true;
84+
var ok = s != null;
85+
if (ok)
86+
{
87+
result = new MyTestId(s!.ToString(provider));
88+
}
89+
else
90+
{
91+
result = new MyTestId("");
92+
}
93+
return ok;
8594
}
95+
#nullable disable
8696

8797

8898
//ICONVERTIBLE
@@ -150,7 +160,7 @@
150160

151161
public string ToString(System.IFormatProvider? provider)
152162
{
153-
throw new System.NotImplementedException();
163+
return Value.ToString(provider);
154164
}
155165

156166
public object ToType(System.Type conversionType, System.IFormatProvider? provider)
@@ -284,7 +294,7 @@
284294
{
285295
if (objectType == typeof(MyTestId?))
286296
{
287-
var value = serializer.Deserialize<string?>(reader);
297+
var value = serializer.Deserialize<string>(reader);
288298

289299
return value is null ? null : new MyTestId(value);
290300
}
@@ -293,6 +303,7 @@
293303
}
294304
}
295305

306+
296307
class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<MyTestId>
297308
{
298309
public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.DotNet7_0.verified.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,26 @@
7373
#pragma warning restore CA1036
7474

7575

76-
public static MyTestId Parse(string s, IFormatProvider? provider)
76+
#nullable enable
77+
public static MyTestId Parse(string s, System.IFormatProvider? provider)
7778
{
78-
throw new NotImplementedException();
79+
return new MyTestId(s.ToString(provider));
7980
}
8081

81-
public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
82+
public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
8283
{
83-
result = new MyTestId(s);
84-
return true;
84+
var ok = s != null;
85+
if (ok)
86+
{
87+
result = new MyTestId(s!.ToString(provider));
88+
}
89+
else
90+
{
91+
result = new MyTestId("");
92+
}
93+
return ok;
8594
}
95+
#nullable disable
8696

8797

8898
//ICONVERTIBLE
@@ -150,7 +160,7 @@
150160

151161
public string ToString(System.IFormatProvider? provider)
152162
{
153-
throw new System.NotImplementedException();
163+
return Value.ToString(provider);
154164
}
155165

156166
public object ToType(System.Type conversionType, System.IFormatProvider? provider)
@@ -284,7 +294,7 @@
284294
{
285295
if (objectType == typeof(MyTestId?))
286296
{
287-
var value = serializer.Deserialize<string?>(reader);
297+
var value = serializer.Deserialize<string>(reader);
288298

289299
return value is null ? null : new MyTestId(value);
290300
}
@@ -293,6 +303,7 @@
293303
}
294304
}
295305

306+
296307
class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<MyTestId>
297308
{
298309
public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.Net4_8.verified.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,26 @@
7373
#pragma warning restore CA1036
7474

7575

76-
public static MyTestId Parse(string s, IFormatProvider? provider)
76+
#nullable enable
77+
public static MyTestId Parse(string s, System.IFormatProvider? provider)
7778
{
78-
throw new NotImplementedException();
79+
return new MyTestId(s.ToString(provider));
7980
}
8081

81-
public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
82+
public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
8283
{
83-
result = new MyTestId(s);
84-
return true;
84+
var ok = s != null;
85+
if (ok)
86+
{
87+
result = new MyTestId(s!.ToString(provider));
88+
}
89+
else
90+
{
91+
result = new MyTestId("");
92+
}
93+
return ok;
8594
}
95+
#nullable disable
8696

8797

8898
//ICONVERTIBLE
@@ -150,7 +160,7 @@
150160

151161
public string ToString(System.IFormatProvider? provider)
152162
{
153-
throw new System.NotImplementedException();
163+
return Value.ToString(provider);
154164
}
155165

156166
public object ToType(System.Type conversionType, System.IFormatProvider? provider)
@@ -284,7 +294,7 @@
284294
{
285295
if (objectType == typeof(MyTestId?))
286296
{
287-
var value = serializer.Deserialize<string?>(reader);
297+
var value = serializer.Deserialize<string>(reader);
288298

289299
return value is null ? null : new MyTestId(value);
290300
}
@@ -293,6 +303,7 @@
293303
}
294304
}
295305

306+
296307
class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<MyTestId>
297308
{
298309
public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)

test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesFullIdCorrectly_type=String.verified.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,26 @@
7373
#pragma warning restore CA1036
7474

7575

76-
public static MyTestId Parse(string s, IFormatProvider? provider)
76+
#nullable enable
77+
public static MyTestId Parse(string s, System.IFormatProvider? provider)
7778
{
78-
throw new NotImplementedException();
79+
return new MyTestId(s.ToString(provider));
7980
}
8081

81-
public static bool TryParse(string? s, IFormatProvider? provider, out MyTestId result)
82+
public static bool TryParse(string? s, System.IFormatProvider? provider, out MyTestId result)
8283
{
83-
result = new MyTestId(s);
84-
return true;
84+
var ok = s != null;
85+
if (ok)
86+
{
87+
result = new MyTestId(s!.ToString(provider));
88+
}
89+
else
90+
{
91+
result = new MyTestId("");
92+
}
93+
return ok;
8594
}
95+
#nullable disable
8696

8797

8898
//ICONVERTIBLE
@@ -150,7 +160,7 @@
150160

151161
public string ToString(System.IFormatProvider? provider)
152162
{
153-
throw new System.NotImplementedException();
163+
return Value.ToString(provider);
154164
}
155165

156166
public object ToType(System.Type conversionType, System.IFormatProvider? provider)
@@ -284,7 +294,7 @@
284294
{
285295
if (objectType == typeof(MyTestId?))
286296
{
287-
var value = serializer.Deserialize<string?>(reader);
297+
var value = serializer.Deserialize<string>(reader);
288298

289299
return value is null ? null : new MyTestId(value);
290300
}
@@ -293,6 +303,7 @@
293303
}
294304
}
295305

306+
296307
class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<MyTestId>
297308
{
298309
public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)

0 commit comments

Comments
 (0)