Skip to content

Commit b04b45f

Browse files
committed
StronglyTypedId.Tennisi
1 parent c7eeb88 commit b04b45f

File tree

472 files changed

+10262
-376
lines changed

Some content is hidden

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

472 files changed

+10262
-376
lines changed

src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,20 @@ public enum StronglyTypedIdImplementations
3636

3737
// ReSharper disable once InconsistentNaming
3838
/// <summary>
39-
/// Implement the <see cref="IParsable{T}"/> interface
39+
/// Implement the <see cref="IParsable"/> interface
4040
/// </summary>
41-
IParsable = 8
41+
IParsable = 8,
42+
43+
// ReSharper disable once InconsistentNaming
44+
/// <summary>
45+
/// Implement the <see cref="IConvertible"/> interface
46+
/// </summary>
47+
IConvertible = 16,
48+
49+
// ReSharper disable once InconsistentNaming
50+
/// <summary>
51+
/// Implement the <see cref="IStronglyTypedId"/> interface
52+
/// </summary>
53+
IStronglyTypedId = 32
4254
}
4355
}

src/StronglyTypedIds/EmbeddedSources.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal static class EmbeddedSources
2626
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"),
2727
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"),
2828
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Parsable.cs"),
29+
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Convertible.cs"),
30+
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_StronglyTypedId.cs"),
2931
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"),
3032
false
3133
);
@@ -40,6 +42,8 @@ internal static class EmbeddedSources
4042
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"),
4143
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"),
4244
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Parsable.cs"),
45+
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Convertible.cs"),
46+
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_StronglyTypedId.cs"),
4347
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"),
4448
false
4549
);
@@ -54,6 +58,8 @@ internal static class EmbeddedSources
5458
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"),
5559
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"),
5660
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Parsable.cs"),
61+
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Convertible.cs"),
62+
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_StronglyTypedId.cs"),
5763
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"),
5864
false
5965
);
@@ -68,6 +74,8 @@ internal static class EmbeddedSources
6874
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"),
6975
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"),
7076
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Parsable.cs"),
77+
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Convertible.cs"),
78+
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_StronglyTypedId.cs"),
7179
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"),
7280
false
7381
);
@@ -82,6 +90,8 @@ internal static class EmbeddedSources
8290
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"),
8391
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"),
8492
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Parsable.cs"),
93+
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Convertible.cs"),
94+
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_StronglyTypedId.cs"),
8595
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"),
8696
true
8797
);
@@ -96,6 +106,8 @@ internal static class EmbeddedSources
96106
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
97107
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
98108
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Parsable.cs"),
109+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Convertible.cs"),
110+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_StronglyTypedId.cs"),
99111
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.cs"),
100112
false
101113
);
@@ -132,6 +144,9 @@ public readonly struct ResourceCollection
132144
public string DapperTypeHandler { get; }
133145
public string Comparable { get; }
134146
public string Parsable { get; }
147+
public string Convertible { get; }
148+
149+
public string StronglyTypedId { get; }
135150

136151
public ResourceCollection(
137152
string header,
@@ -143,6 +158,8 @@ public ResourceCollection(
143158
string dapperTypeHandler,
144159
string comparable,
145160
string parsable,
161+
string convertible,
162+
string stronglyTypedId,
146163
string swaggerSchemaFilter,
147164
bool nullableEnable)
148165
{
@@ -155,7 +172,9 @@ public ResourceCollection(
155172
DapperTypeHandler = dapperTypeHandler;
156173
Comparable = comparable;
157174
Parsable = parsable;
175+
Convertible = convertible;
158176
NullableEnable = nullableEnable;
177+
StronglyTypedId = stronglyTypedId;
159178
Header = header;
160179
}
161180
}

src/StronglyTypedIds/ParentClass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace StronglyTypedIds;
24

35
internal class ParentClass
@@ -14,4 +16,4 @@ public ParentClass(string keyword, string name, string constraints, ParentClass?
1416
public string Keyword { get; }
1517
public string Name { get; }
1618
public string Constraints { get; }
17-
}
19+
}

src/StronglyTypedIds/SourceGenerationHelper.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static string CreateId(
3535
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
3636
};
3737

38-
return CreateId(idNamespace, idName, parentClass, converters, implementations, resources, sb);
38+
return CreateId(idNamespace, idName, parentClass, converters, implementations, resources,backingType, sb);
3939
}
4040

4141
static string CreateId(
@@ -45,6 +45,7 @@ static string CreateId(
4545
StronglyTypedIdConverter converters,
4646
StronglyTypedIdImplementations implementations,
4747
EmbeddedSources.ResourceCollection resources,
48+
StronglyTypedIdBackingType backingType,
4849
StringBuilder? sb)
4950
{
5051
if (string.IsNullOrEmpty(idName))
@@ -74,6 +75,8 @@ static string CreateId(
7475
var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable);
7576
var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable);
7677
var useIParsable = implementations.IsSet(StronglyTypedIdImplementations.IParsable);
78+
var useIConvertible = implementations.IsSet(StronglyTypedIdImplementations.IConvertible);
79+
var useIStronglyTypedId = implementations.IsSet(StronglyTypedIdImplementations.IStronglyTypedId);
7780

7881
var parentsCount = 0;
7982

@@ -131,7 +134,7 @@ static string CreateId(
131134

132135

133136
sb.Append(resources.BaseId);
134-
ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable);
137+
ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable, useIConvertible, useIStronglyTypedId, backingType);
135138

136139
// IEquatable is already implemented whether or not the interface is implemented
137140

@@ -145,6 +148,16 @@ static string CreateId(
145148
sb.AppendLine(resources.Parsable);
146149
}
147150

151+
if (useIConvertible)
152+
{
153+
sb.AppendLine(resources.Convertible);
154+
}
155+
156+
if (useIStronglyTypedId)
157+
{
158+
sb.AppendLine(resources.StronglyTypedId);
159+
}
160+
148161
if (useEfCoreValueConverter)
149162
{
150163
sb.AppendLine(resources.EfCoreValueConverter);
@@ -191,7 +204,23 @@ static string CreateId(
191204
return sb.ToString();
192205
}
193206

194-
private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable)
207+
208+
private static string BackingType(StronglyTypedIdBackingType backingType)
209+
{
210+
var resources = backingType switch
211+
{
212+
StronglyTypedIdBackingType.Guid => "System.Guid",
213+
StronglyTypedIdBackingType.Int => "int",
214+
StronglyTypedIdBackingType.Long => "long",
215+
StronglyTypedIdBackingType.String => "string",
216+
StronglyTypedIdBackingType.NullableString => "string?",
217+
StronglyTypedIdBackingType.MassTransitNewId => "MassTransit.NewId",
218+
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
219+
};
220+
return resources;
221+
}
222+
223+
private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable, bool useIConvertible, bool useIStronglyTypedId, StronglyTypedIdBackingType backingType)
195224
{
196225
var interfaces = new List<string>();
197226

@@ -210,6 +239,16 @@ private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool
210239
interfaces.Add("System.IParsable<TESTID>");
211240
}
212241

242+
if (useIConvertible)
243+
{
244+
interfaces.Add("System.IConvertible");
245+
}
246+
247+
if (useIStronglyTypedId)
248+
{
249+
interfaces.Add($"IStronglyTypedId<{BackingType(backingType)}>");
250+
}
251+
213252
if (interfaces.Count > 0)
214253
{
215254
sb.Replace("INTERFACES", string.Join(", ", interfaces));

src/StronglyTypedIds/Templates/Guid/Guid_Base.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public override bool Equals(object obj)
1616
if (ReferenceEquals(null, obj)) return false;
1717
return obj is TESTID other && Equals(other);
1818
}
19-
2019
public override int GetHashCode() => Value.GetHashCode();
2120

2221
public override string ToString() => Value.ToString();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+

2+
//ICONVERTIBLE
3+
#nullable enable
4+
5+
public System.TypeCode GetTypeCode()
6+
{
7+
throw new System.NotImplementedException();
8+
}
9+
10+
public bool ToBoolean(System.IFormatProvider? provider)
11+
{
12+
throw new System.NotImplementedException();
13+
}
14+
15+
public byte ToByte(System.IFormatProvider? provider)
16+
{
17+
throw new System.NotImplementedException();
18+
}
19+
20+
public char ToChar(System.IFormatProvider? provider)
21+
{
22+
throw new System.NotImplementedException();
23+
}
24+
25+
public System.DateTime ToDateTime(System.IFormatProvider? provider)
26+
{
27+
throw new System.NotImplementedException();
28+
}
29+
30+
public decimal ToDecimal(System.IFormatProvider? provider)
31+
{
32+
throw new System.NotImplementedException();
33+
}
34+
35+
public double ToDouble(System.IFormatProvider? provider)
36+
{
37+
throw new System.NotImplementedException();
38+
}
39+
40+
public short ToInt16(System.IFormatProvider? provider)
41+
{
42+
throw new System.NotImplementedException();
43+
}
44+
45+
public int ToInt32(System.IFormatProvider? provider)
46+
{
47+
throw new System.NotImplementedException();
48+
}
49+
50+
public long ToInt64(System.IFormatProvider? provider)
51+
{
52+
throw new System.NotImplementedException();
53+
}
54+
55+
public sbyte ToSByte(System.IFormatProvider? provider)
56+
{
57+
throw new System.NotImplementedException();
58+
}
59+
60+
public float ToSingle(System.IFormatProvider? provider)
61+
{
62+
throw new System.NotImplementedException();
63+
}
64+
65+
public string ToString(System.IFormatProvider? provider)
66+
{
67+
throw new System.NotImplementedException();
68+
}
69+
70+
public object ToType(System.Type conversionType, System.IFormatProvider? provider)
71+
{
72+
throw new System.NotImplementedException();
73+
}
74+
75+
public ushort ToUInt16(System.IFormatProvider? provider)
76+
{
77+
throw new System.NotImplementedException();
78+
}
79+
80+
public uint ToUInt32(System.IFormatProvider? provider)
81+
{
82+
throw new System.NotImplementedException();
83+
}
84+
85+
public ulong ToUInt64(System.IFormatProvider? provider)
86+
{
87+
throw new System.NotImplementedException();
88+
}
89+
#nullable disable
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+

2+
public static IStronglyTypedId<System.Guid> Create(System.Guid value)
3+
{
4+
return new TESTID(value);
5+
}

src/StronglyTypedIds/Templates/Int/Int_Base.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public override bool Equals(object obj)
1919
public override int GetHashCode() => Value.GetHashCode();
2020

2121
public override string ToString() => Value.ToString();
22+
2223
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
2324
public static bool operator !=(TESTID a, TESTID b) => !(a == b);

0 commit comments

Comments
 (0)