Skip to content

Commit 5242f13

Browse files
authored
Add support for NewId package (#52)
* Add support for NewId package * Fix an issue in the componentmodel converter * Quick bugfixes * Add integration tests and adjust backing type name * Add snapshot tests
1 parent b0eb121 commit 5242f13

File tree

273 files changed

+24181
-0
lines changed

Some content is hidden

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

273 files changed

+24181
-0
lines changed

src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public enum StronglyTypedIdBackingType
1717
String = 3,
1818
Long = 4,
1919
NullableString = 5,
20+
MassTransitNewId = 6,
2021
}
2122
}

src/StronglyTypedIds/EmbeddedSources.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ internal static class EmbeddedSources
7676
true
7777
);
7878

79+
internal static readonly ResourceCollection NewIdResources = new(
80+
AutoGeneratedHeader,
81+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_Base.cs"),
82+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_NewtonsoftJsonConverter.cs"),
83+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_SystemTextJsonConverter.cs"),
84+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_TypeConverter.cs"),
85+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"),
86+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
87+
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
88+
false
89+
);
90+
7991
internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]";
8092
internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]";
8193
internal const string SystemTextJsonAttributeSource = " [System.Text.Json.Serialization.JsonConverter(typeof(TESTIDSystemTextJsonConverter))]";

src/StronglyTypedIds/SourceGenerationHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static string CreateId(
3131
StronglyTypedIdBackingType.Long => EmbeddedSources.LongResources,
3232
StronglyTypedIdBackingType.String => EmbeddedSources.StringResources,
3333
StronglyTypedIdBackingType.NullableString => EmbeddedSources.NullableStringResources,
34+
StronglyTypedIdBackingType.MassTransitNewId => EmbeddedSources.NewIdResources,
3435
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
3536
};
3637

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
 readonly partial struct TESTID : INTERFACES
2+
{
3+
public MassTransit.NewId Value { get; }
4+
5+
public TESTID(MassTransit.NewId value)
6+
{
7+
Value = value;
8+
}
9+
10+
public static TESTID New() => new TESTID(MassTransit.NewId.Next());
11+
public static readonly TESTID Empty = new TESTID(MassTransit.NewId.Empty);
12+
13+
public bool Equals(TESTID other) => this.Value.Equals(other.Value);
14+
public override bool Equals(object obj)
15+
{
16+
if (ReferenceEquals(null, obj)) return false;
17+
return obj is TESTID other && Equals(other);
18+
}
19+
20+
public override int GetHashCode() => Value.GetHashCode();
21+
22+
public override string ToString() => Value.ToString();
23+
public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
24+
public static bool operator !=(TESTID a, TESTID b) => !(a == b);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+

2+
public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler<TESTID>
3+
{
4+
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
5+
{
6+
parameter.Value = value.Value.ToGuid();
7+
}
8+
9+
public override TESTID Parse(object value)
10+
{
11+
return value switch
12+
{
13+
System.Guid guidValue => new TESTID(MassTransit.NewId.FromGuid(guidValue)),
14+
string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new TESTID(MassTransit.NewId.FromGuid(result)),
15+
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
16+
};
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+

2+
public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, System.Guid>
3+
{
4+
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
5+
: base(
6+
id => id.Value.ToGuid(),
7+
value => new TESTID(MassTransit.NewId.FromGuid(value)),
8+
mappingHints
9+
) { }
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
 public int CompareTo(TESTID other) => Value.CompareTo(other.Value);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+

2+
class TESTIDNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
3+
{
4+
public override bool CanConvert(System.Type objectType)
5+
{
6+
return objectType == typeof(TESTID);
7+
}
8+
9+
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
10+
{
11+
var id = (TESTID)value;
12+
serializer.Serialize(writer, id.Value.ToGuid());
13+
}
14+
15+
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
16+
{
17+
var guid = serializer.Deserialize<System.Guid?>(reader);
18+
return guid.HasValue ? new TESTID(MassTransit.NewId.FromGuid(guid.Value)) : null;
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+

2+
class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<TESTID>
3+
{
4+
public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
5+
{
6+
return new TESTID(MassTransit.NewId.FromGuid(reader.GetGuid()));
7+
}
8+
9+
public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options)
10+
{
11+
writer.WriteStringValue(value.Value.ToGuid());
12+
}
13+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+

2+
class TESTIDTypeConverter : System.ComponentModel.TypeConverter
3+
{
4+
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
5+
{
6+
return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
7+
sourceType == typeof(string) || base.CanConvertFrom
8+
(context, sourceType);
9+
}
10+
11+
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
12+
{
13+
return value switch
14+
{
15+
MassTransit.NewId newIdValue => new TESTID(newIdValue),
16+
System.Guid guidValue => new TESTID(MassTransit.NewId.FromGuid(guidValue)),
17+
string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new TESTID(MassTransit.NewId.FromGuid(result)),
18+
_ => base.ConvertFrom(context, culture, value),
19+
};
20+
}
21+
22+
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
23+
{
24+
return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
25+
}
26+
27+
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
28+
{
29+
if (value is TESTID idValue)
30+
{
31+
if (destinationType == typeof(MassTransit.NewId))
32+
{
33+
return idValue.Value;
34+
}
35+
36+
if (destinationType == typeof(System.Guid))
37+
{
38+
return idValue.Value.ToGuid();
39+
}
40+
41+
if (destinationType == typeof(string))
42+
{
43+
return idValue.Value.ToGuid().ToString();
44+
}
45+
}
46+
47+
return base.ConvertTo(context, culture, value, destinationType);
48+
}
49+
}

0 commit comments

Comments
 (0)