Skip to content

Commit 24c2b26

Browse files
authored
Merge pull request #6 from tennisi/parsable
IParsable feature for Implementations
2 parents e462cab + 681af0f commit 24c2b26

File tree

411 files changed

+41247
-37
lines changed

Some content is hidden

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

411 files changed

+41247
-37
lines changed

StronglyTypedId.sln

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2020
CHANGELOG.md = CHANGELOG.md
2121
build.sh = build.sh
2222
NuGet.integration-tests.config = NuGet.integration-tests.config
23+
global.json = global.json
2324
EndProjectSection
2425
EndProject
2526
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}"
@@ -105,14 +106,12 @@ Global
105106
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Release|x86.ActiveCfg = Release|Any CPU
106107
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Release|x86.Build.0 = Release|Any CPU
107108
{A7355210-7DDC-4968-84B7-79002113EA6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
108-
{A7355210-7DDC-4968-84B7-79002113EA6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
109109
{A7355210-7DDC-4968-84B7-79002113EA6E}.Debug|x64.ActiveCfg = Debug|Any CPU
110110
{A7355210-7DDC-4968-84B7-79002113EA6E}.Debug|x86.ActiveCfg = Debug|Any CPU
111111
{A7355210-7DDC-4968-84B7-79002113EA6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
112112
{A7355210-7DDC-4968-84B7-79002113EA6E}.Release|x64.ActiveCfg = Release|Any CPU
113113
{A7355210-7DDC-4968-84B7-79002113EA6E}.Release|x86.ActiveCfg = Release|Any CPU
114114
{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115-
{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}.Debug|Any CPU.Build.0 = Debug|Any CPU
116115
{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}.Debug|x64.ActiveCfg = Debug|Any CPU
117116
{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}.Debug|x86.ActiveCfg = Debug|Any CPU
118117
{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}.Release|Any CPU.ActiveCfg = Release|Any CPU

src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace StronglyTypedIds
88
[Flags]
99
public enum StronglyTypedIdImplementations
1010
{
11-
// Used with HasFlag, so needs to be 1, 2, 4 etc
11+
// Used with HasFlag, so needs to be 1, 2, 4, 8 etc
1212

1313
/// <summary>
1414
/// Don't implement any additional members for the strongly typed ID
@@ -33,5 +33,11 @@ public enum StronglyTypedIdImplementations
3333
/// Implement the <see cref="IComparable{T}"/> interface
3434
/// </summary>
3535
IComparable = 4,
36+
37+
// ReSharper disable once InconsistentNaming
38+
/// <summary>
39+
/// Implement the <see cref="IParsable{T}"/> interface
40+
/// </summary>
41+
IParsable = 8
3642
}
37-
}
43+
}

src/StronglyTypedIds.Attributes/StronglyTypedIds.Attributes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Nullable>enable</Nullable>
65
<RootNamespace>StronglyTypedIds</RootNamespace>
6+
<Nullable>enable</Nullable>
77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

src/StronglyTypedIds/Constants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System;
2+
13
namespace StronglyTypedIds
24
{
35
internal static class Constants
46
{
57
public const string Usage = nameof(Usage);
68
}
7-
}
9+
}

src/StronglyTypedIds/EmbeddedSources.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal static class EmbeddedSources
2525
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_EfCoreValueConverter.cs"),
2626
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"),
2727
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"),
28+
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Parsable.cs"),
2829
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"),
2930
false
3031
);
@@ -38,6 +39,7 @@ internal static class EmbeddedSources
3839
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_EfCoreValueConverter.cs"),
3940
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"),
4041
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"),
42+
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Parsable.cs"),
4143
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"),
4244
false
4345
);
@@ -51,6 +53,7 @@ internal static class EmbeddedSources
5153
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_EfCoreValueConverter.cs"),
5254
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"),
5355
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"),
56+
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Parsable.cs"),
5457
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"),
5558
false
5659
);
@@ -64,6 +67,7 @@ internal static class EmbeddedSources
6467
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_EfCoreValueConverter.cs"),
6568
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"),
6669
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"),
70+
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Parsable.cs"),
6771
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"),
6872
false
6973
);
@@ -77,6 +81,7 @@ internal static class EmbeddedSources
7781
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_EfCoreValueConverter.cs"),
7882
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"),
7983
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"),
84+
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Parsable.cs"),
8085
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"),
8186
true
8287
);
@@ -90,6 +95,7 @@ internal static class EmbeddedSources
9095
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"),
9196
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
9297
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
98+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Parsable.cs"),
9399
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.cs"),
94100
false
95101
);
@@ -125,6 +131,7 @@ public readonly struct ResourceCollection
125131
public string EfCoreValueConverter { get; }
126132
public string DapperTypeHandler { get; }
127133
public string Comparable { get; }
134+
public string Parsable { get; }
128135

129136
public ResourceCollection(
130137
string header,
@@ -135,6 +142,7 @@ public ResourceCollection(
135142
string efCoreValueConverter,
136143
string dapperTypeHandler,
137144
string comparable,
145+
string parsable,
138146
string swaggerSchemaFilter,
139147
bool nullableEnable)
140148
{
@@ -146,6 +154,7 @@ public ResourceCollection(
146154
EfCoreValueConverter = efCoreValueConverter;
147155
DapperTypeHandler = dapperTypeHandler;
148156
Comparable = comparable;
157+
Parsable = parsable;
149158
NullableEnable = nullableEnable;
150159
Header = header;
151160
}
@@ -163,4 +172,4 @@ internal static string LoadTemplateForEmitting(string resourceName)
163172
#endif";
164173
}
165174
}
166-
}
175+
}

src/StronglyTypedIds/SourceGenerationHelper.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static string CreateId(
7373

7474
var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable);
7575
var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable);
76+
var useIParsable = implementations.IsSet(StronglyTypedIdImplementations.IParsable);
7677

7778
var parentsCount = 0;
7879

@@ -130,7 +131,7 @@ static string CreateId(
130131

131132

132133
sb.Append(resources.BaseId);
133-
ReplaceInterfaces(sb, useIEquatable, useIComparable);
134+
ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable);
134135

135136
// IEquatable is already implemented whether or not the interface is implemented
136137

@@ -139,6 +140,11 @@ static string CreateId(
139140
sb.AppendLine(resources.Comparable);
140141
}
141142

143+
if (useIParsable)
144+
{
145+
sb.AppendLine(resources.Parsable);
146+
}
147+
142148
if (useEfCoreValueConverter)
143149
{
144150
sb.AppendLine(resources.EfCoreValueConverter);
@@ -185,7 +191,7 @@ static string CreateId(
185191
return sb.ToString();
186192
}
187193

188-
private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable)
194+
private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable)
189195
{
190196
var interfaces = new List<string>();
191197

@@ -199,6 +205,11 @@ private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool
199205
interfaces.Add("System.IEquatable<TESTID>");
200206
}
201207

208+
if (useIParsable)
209+
{
210+
interfaces.Add("System.IParsable<TESTID>");
211+
}
212+
202213
if (interfaces.Count > 0)
203214
{
204215
sb.Replace("INTERFACES", string.Join(", ", interfaces));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable enable
2+
public static TESTID Parse(string s, System.IFormatProvider? provider)
3+
{
4+
return new TESTID(Guid.Parse(s));
5+
}
6+
7+
public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
8+
{
9+
long res = 0;
10+
var ok = Guid.TryParse(s, out res);
11+
result = new TESTID(res);
12+
return ok;
13+
}
14+
#nullable disable
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable enable
2+
public static TESTID Parse(string s, System.IFormatProvider? provider)
3+
{
4+
return new TESTID(int.Parse(s));
5+
}
6+
7+
public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
8+
{
9+
int res = 0;
10+
var ok = int.TryParse(s, out res);
11+
result = new TESTID(res);
12+
return ok;
13+
}
14+
#nullable disable

src/StronglyTypedIds/Templates/Long/Long_DapperTypeHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ public override TESTID Parse(object value)
1212
{
1313
long longValue => new TESTID(longValue),
1414
int intValue => new TESTID(intValue),
15+
decimal decimalValue => new TESTID((long)decimalValue),
1516
short shortValue => new TESTID(shortValue),
1617
string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new TESTID(result),
1718
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
1819
};
1920
}
20-
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#nullable enable
2+
public static TESTID Parse(string s, System.IFormatProvider? provider)
3+
{
4+
return new TESTID(long.Parse(s));
5+
}
6+
7+
public static bool TryParse(string? s, System.IFormatProvider? provider, out TESTID result)
8+
{
9+
long res = 0;
10+
var ok = long.TryParse(s, out res);
11+
result = new TESTID(res);
12+
return ok;
13+
}
14+
#nullable disable

0 commit comments

Comments
 (0)