Skip to content

Commit

Permalink
StronglyTypedId.Tennisi
Browse files Browse the repository at this point in the history
  • Loading branch information
kkgonsovsky committed Apr 3, 2023
1 parent c7eeb88 commit b04b45f
Show file tree
Hide file tree
Showing 472 changed files with 10,262 additions and 376 deletions.
16 changes: 14 additions & 2 deletions src/StronglyTypedIds.Attributes/StronglyTypedIdImplementations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@ public enum StronglyTypedIdImplementations

// ReSharper disable once InconsistentNaming
/// <summary>
/// Implement the <see cref="IParsable{T}"/> interface
/// Implement the <see cref="IParsable"/> interface
/// </summary>
IParsable = 8
IParsable = 8,

// ReSharper disable once InconsistentNaming
/// <summary>
/// Implement the <see cref="IConvertible"/> interface
/// </summary>
IConvertible = 16,

// ReSharper disable once InconsistentNaming
/// <summary>
/// Implement the <see cref="IStronglyTypedId"/> interface
/// </summary>
IStronglyTypedId = 32
}
}
19 changes: 19 additions & 0 deletions src/StronglyTypedIds/EmbeddedSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Parsable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_Convertible.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_StronglyTypedId.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_SwaggerSchemaFilter.cs"),
false
);
Expand All @@ -40,6 +42,8 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Parsable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Convertible.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_StronglyTypedId.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_SwaggerSchemaFilter.cs"),
false
);
Expand All @@ -54,6 +58,8 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Parsable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_Convertible.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_StronglyTypedId.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_SwaggerSchemaFilter.cs"),
false
);
Expand All @@ -68,6 +74,8 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Parsable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_Convertible.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_StronglyTypedId.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_SwaggerSchemaFilter.cs"),
false
);
Expand All @@ -82,6 +90,8 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Parsable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_Convertible.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_StronglyTypedId.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_SwaggerSchemaFilter.cs"),
true
);
Expand All @@ -96,6 +106,8 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Parsable.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Convertible.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_StronglyTypedId.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SwaggerSchemaFilter.cs"),
false
);
Expand Down Expand Up @@ -132,6 +144,9 @@ public readonly struct ResourceCollection
public string DapperTypeHandler { get; }
public string Comparable { get; }
public string Parsable { get; }
public string Convertible { get; }

public string StronglyTypedId { get; }

public ResourceCollection(
string header,
Expand All @@ -143,6 +158,8 @@ public ResourceCollection(
string dapperTypeHandler,
string comparable,
string parsable,
string convertible,
string stronglyTypedId,
string swaggerSchemaFilter,
bool nullableEnable)
{
Expand All @@ -155,7 +172,9 @@ public ResourceCollection(
DapperTypeHandler = dapperTypeHandler;
Comparable = comparable;
Parsable = parsable;
Convertible = convertible;
NullableEnable = nullableEnable;
StronglyTypedId = stronglyTypedId;
Header = header;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/StronglyTypedIds/ParentClass.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace StronglyTypedIds;

internal class ParentClass
Expand All @@ -14,4 +16,4 @@ public ParentClass(string keyword, string name, string constraints, ParentClass?
public string Keyword { get; }
public string Name { get; }
public string Constraints { get; }
}
}
45 changes: 42 additions & 3 deletions src/StronglyTypedIds/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static string CreateId(
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
};

return CreateId(idNamespace, idName, parentClass, converters, implementations, resources, sb);
return CreateId(idNamespace, idName, parentClass, converters, implementations, resources,backingType, sb);
}

static string CreateId(
Expand All @@ -45,6 +45,7 @@ static string CreateId(
StronglyTypedIdConverter converters,
StronglyTypedIdImplementations implementations,
EmbeddedSources.ResourceCollection resources,
StronglyTypedIdBackingType backingType,
StringBuilder? sb)
{
if (string.IsNullOrEmpty(idName))
Expand Down Expand Up @@ -74,6 +75,8 @@ static string CreateId(
var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable);
var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable);
var useIParsable = implementations.IsSet(StronglyTypedIdImplementations.IParsable);
var useIConvertible = implementations.IsSet(StronglyTypedIdImplementations.IConvertible);
var useIStronglyTypedId = implementations.IsSet(StronglyTypedIdImplementations.IStronglyTypedId);

var parentsCount = 0;

Expand Down Expand Up @@ -131,7 +134,7 @@ static string CreateId(


sb.Append(resources.BaseId);
ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable);
ReplaceInterfaces(sb, useIEquatable, useIComparable, useIParsable, useIConvertible, useIStronglyTypedId, backingType);

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

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

if (useIConvertible)
{
sb.AppendLine(resources.Convertible);
}

if (useIStronglyTypedId)
{
sb.AppendLine(resources.StronglyTypedId);
}

if (useEfCoreValueConverter)
{
sb.AppendLine(resources.EfCoreValueConverter);
Expand Down Expand Up @@ -191,7 +204,23 @@ static string CreateId(
return sb.ToString();
}

private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable)

private static string BackingType(StronglyTypedIdBackingType backingType)
{
var resources = backingType switch
{
StronglyTypedIdBackingType.Guid => "System.Guid",
StronglyTypedIdBackingType.Int => "int",
StronglyTypedIdBackingType.Long => "long",
StronglyTypedIdBackingType.String => "string",
StronglyTypedIdBackingType.NullableString => "string?",
StronglyTypedIdBackingType.MassTransitNewId => "MassTransit.NewId",
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
};
return resources;
}

private static void ReplaceInterfaces(StringBuilder sb, bool useIEquatable, bool useIComparable, bool useIParsable, bool useIConvertible, bool useIStronglyTypedId, StronglyTypedIdBackingType backingType)
{
var interfaces = new List<string>();

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

if (useIConvertible)
{
interfaces.Add("System.IConvertible");
}

if (useIStronglyTypedId)
{
interfaces.Add($"IStronglyTypedId<{BackingType(backingType)}>");
}

if (interfaces.Count > 0)
{
sb.Replace("INTERFACES", string.Join(", ", interfaces));
Expand Down
1 change: 0 additions & 1 deletion src/StronglyTypedIds/Templates/Guid/Guid_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public override bool Equals(object obj)
if (ReferenceEquals(null, obj)) return false;
return obj is TESTID other && Equals(other);
}

public override int GetHashCode() => Value.GetHashCode();

public override string ToString() => Value.ToString();
Expand Down
89 changes: 89 additions & 0 deletions src/StronglyTypedIds/Templates/Guid/Guid_Convertible.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

//ICONVERTIBLE
#nullable enable

public System.TypeCode GetTypeCode()
{
throw new System.NotImplementedException();
}

public bool ToBoolean(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public byte ToByte(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public char ToChar(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public System.DateTime ToDateTime(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public decimal ToDecimal(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public double ToDouble(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public short ToInt16(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public int ToInt32(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public long ToInt64(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public sbyte ToSByte(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public float ToSingle(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public string ToString(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public object ToType(System.Type conversionType, System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public ushort ToUInt16(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public uint ToUInt32(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}

public ulong ToUInt64(System.IFormatProvider? provider)
{
throw new System.NotImplementedException();
}
#nullable disable
5 changes: 5 additions & 0 deletions src/StronglyTypedIds/Templates/Guid/Guid_StronglyTypedId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

public static IStronglyTypedId<System.Guid> Create(System.Guid value)
{
return new TESTID(value);
}
1 change: 1 addition & 0 deletions src/StronglyTypedIds/Templates/Int/Int_Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public override bool Equals(object obj)
public override int GetHashCode() => Value.GetHashCode();

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);
Loading

0 comments on commit b04b45f

Please sign in to comment.