Skip to content

Commit 99a61d2

Browse files
committed
Add more tests
1 parent 49b57f5 commit 99a61d2

7 files changed

+1923
-299
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by the StronglyTypedId source generator
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#pragma warning disable 1591 // publicly visible type or member must be documented
11+
12+
#nullable enable
13+
namespace SomeNamespace1
14+
{
15+
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
16+
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
17+
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
18+
partial struct MyId :
19+
#if NET6_0_OR_GREATER
20+
global::System.ISpanFormattable,
21+
#endif
22+
#if NET7_0_OR_GREATER
23+
global::System.IParsable<MyId>, global::System.ISpanParsable<MyId>,
24+
#endif
25+
#if NET8_0_OR_GREATER
26+
global::System.IUtf8SpanFormattable,
27+
#endif
28+
global::System.IComparable<MyId>, global::System.IEquatable<MyId>, global::System.IFormattable
29+
{
30+
public global::System.Guid Value { get; }
31+
32+
public MyId(global::System.Guid value)
33+
{
34+
Value = value;
35+
}
36+
37+
public static MyId New() => new MyId(global::System.Guid.NewGuid());
38+
public static readonly MyId Empty = new MyId(global::System.Guid.Empty);
39+
40+
/// <inheritdoc cref="global::System.IEquatable{T}"/>
41+
public bool Equals(MyId other) => this.Value.Equals(other.Value);
42+
public override bool Equals(object? obj)
43+
{
44+
if (ReferenceEquals(null, obj)) return false;
45+
return obj is MyId other && Equals(other);
46+
}
47+
48+
public override int GetHashCode() => Value.GetHashCode();
49+
50+
public override string ToString() => Value.ToString();
51+
52+
public static bool operator ==(MyId a, MyId b) => a.Equals(b);
53+
public static bool operator !=(MyId a, MyId b) => !(a == b);
54+
public static bool operator > (MyId a, MyId b) => a.CompareTo(b) > 0;
55+
public static bool operator < (MyId a, MyId b) => a.CompareTo(b) < 0;
56+
public static bool operator >= (MyId a, MyId b) => a.CompareTo(b) >= 0;
57+
public static bool operator <= (MyId a, MyId b) => a.CompareTo(b) <= 0;
58+
59+
/// <inheritdoc cref="global::System.IComparable{TSelf}"/>
60+
public int CompareTo(MyId other) => Value.CompareTo(other.Value);
61+
62+
public partial class MyIdTypeConverter : global::System.ComponentModel.TypeConverter
63+
{
64+
public override bool CanConvertFrom(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Type sourceType)
65+
{
66+
return sourceType == typeof(global::System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
67+
}
68+
69+
public override object? ConvertFrom(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Globalization.CultureInfo? culture, object value)
70+
{
71+
return value switch
72+
{
73+
global::System.Guid guidValue => new MyId(guidValue),
74+
string stringValue when !string.IsNullOrEmpty(stringValue) && global::System.Guid.TryParse(stringValue, out var result) => new MyId(result),
75+
_ => base.ConvertFrom(context, culture, value),
76+
};
77+
}
78+
79+
public override bool CanConvertTo(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Type? sourceType)
80+
{
81+
return sourceType == typeof(global::System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
82+
}
83+
84+
public override object? ConvertTo(global::System.ComponentModel.ITypeDescriptorContext? context, global::System.Globalization.CultureInfo? culture, object? value, global::System.Type destinationType)
85+
{
86+
if (value is MyId idValue)
87+
{
88+
if (destinationType == typeof(global::System.Guid))
89+
{
90+
return idValue.Value;
91+
}
92+
93+
if (destinationType == typeof(string))
94+
{
95+
return idValue.Value.ToString();
96+
}
97+
}
98+
99+
return base.ConvertTo(context, culture, value, destinationType);
100+
}
101+
}
102+
103+
public partial class MyIdSystemTextJsonConverter : global::System.Text.Json.Serialization.JsonConverter<MyId>
104+
{
105+
public override bool CanConvert(global::System.Type typeToConvert)
106+
=> typeToConvert == typeof(global::System.Guid) || typeToConvert == typeof(string) || base.CanConvert(typeToConvert);
107+
108+
public override MyId Read(ref global::System.Text.Json.Utf8JsonReader reader, global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options)
109+
=> new (reader.GetGuid());
110+
111+
public override void Write(global::System.Text.Json.Utf8JsonWriter writer, MyId value, global::System.Text.Json.JsonSerializerOptions options)
112+
=> writer.WriteStringValue(value.Value);
113+
114+
#if NET6_0_OR_GREATER
115+
public override MyId ReadAsPropertyName(ref global::System.Text.Json.Utf8JsonReader reader, global::System.Type typeToConvert, global::System.Text.Json.JsonSerializerOptions options)
116+
=> new(global::System.Guid.Parse(reader.GetString() ?? throw new global::System.FormatException("The string for the MyId property was null")));
117+
118+
public override void WriteAsPropertyName(global::System.Text.Json.Utf8JsonWriter writer, MyId value, global::System.Text.Json.JsonSerializerOptions options)
119+
=> writer.WritePropertyName(value.Value.ToString());
120+
#endif
121+
}
122+
123+
public static MyId Parse(string input)
124+
=> new(global::System.Guid.Parse(input));
125+
126+
#if NET7_0_OR_GREATER
127+
/// <inheritdoc cref="global::System.IParsable{TSelf}"/>
128+
public static MyId Parse(string input, global::System.IFormatProvider? provider)
129+
=> new(global::System.Guid.Parse(input, provider));
130+
131+
/// <inheritdoc cref="global::System.IParsable{TSelf}"/>
132+
public static bool TryParse(
133+
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] string? input,
134+
global::System.IFormatProvider? provider,
135+
out MyId result)
136+
{
137+
if (input is null)
138+
{
139+
result = default;
140+
return false;
141+
}
142+
143+
if (global::System.Guid.TryParse(input, provider, out var guid))
144+
{
145+
result = new(guid);
146+
return true;
147+
}
148+
else
149+
{
150+
result = default;
151+
return false;
152+
}
153+
}
154+
#endif
155+
156+
/// <inheritdoc cref="global::System.IFormattable"/>
157+
public string ToString(
158+
#if NET7_0_OR_GREATER
159+
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
160+
#endif
161+
string? format,
162+
global::System.IFormatProvider? formatProvider)
163+
=> Value.ToString(format, formatProvider);
164+
165+
#if NETCOREAPP2_1_OR_GREATER
166+
public static MyId Parse(global::System.ReadOnlySpan<char> input)
167+
=> new(global::System.Guid.Parse(input));
168+
#endif
169+
170+
#if NET6_0_OR_GREATER
171+
#if NET7_0_OR_GREATER
172+
/// <inheritdoc cref="global::System.ISpanParsable{TSelf}"/>
173+
#endif
174+
public static MyId Parse(global::System.ReadOnlySpan<char> input, global::System.IFormatProvider? provider)
175+
#if NET7_0_OR_GREATER
176+
=> new(global::System.Guid.Parse(input, provider));
177+
#else
178+
=> new(global::System.Guid.Parse(input));
179+
#endif
180+
181+
#if NET7_0_OR_GREATER
182+
/// <inheritdoc cref="global::System.ISpanParsable{TSelf}"/>
183+
#endif
184+
public static bool TryParse(global::System.ReadOnlySpan<char> input, global::System.IFormatProvider? provider, out MyId result)
185+
{
186+
#if NET7_0_OR_GREATER
187+
if (global::System.Guid.TryParse(input, provider, out var guid))
188+
#else
189+
if (global::System.Guid.TryParse(input, out var guid))
190+
#endif
191+
{
192+
result = new(guid);
193+
return true;
194+
}
195+
else
196+
{
197+
result = default;
198+
return false;
199+
}
200+
}
201+
202+
/// <inheritdoc cref="global::System.ISpanFormattable"/>
203+
public bool TryFormat(
204+
global::System.Span<char> destination,
205+
out int charsWritten,
206+
#if NET7_0_OR_GREATER
207+
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
208+
#endif
209+
global::System.ReadOnlySpan<char> format,
210+
global::System.IFormatProvider? provider)
211+
=> Value.TryFormat(destination, out charsWritten, format);
212+
213+
/// <inheritdoc cref="global::System.ISpanFormattable"/>
214+
public bool TryFormat(
215+
global::System.Span<char> destination,
216+
out int charsWritten,
217+
#if NET7_0_OR_GREATER
218+
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
219+
#endif
220+
global::System.ReadOnlySpan<char> format = default)
221+
=> Value.TryFormat(destination, out charsWritten, format);
222+
#endif
223+
#if NET8_0_OR_GREATER
224+
/// <inheritdoc cref="global::System.IUtf8SpanFormattable.TryFormat" />
225+
public bool TryFormat(
226+
global::System.Span<byte> utf8Destination,
227+
out int bytesWritten,
228+
[global::System.Diagnostics.CodeAnalysis.StringSyntax(global::System.Diagnostics.CodeAnalysis.StringSyntaxAttribute.GuidFormat)]
229+
global::System.ReadOnlySpan<char> format,
230+
global::System.IFormatProvider? provider)
231+
=> Value.TryFormat(utf8Destination, out bytesWritten, format);
232+
#endif
233+
}
234+
}
235+
236+
//------------------------------------------------------------------------------
237+
// <auto-generated>
238+
// This code was generated by the StronglyTypedId source generator
239+
//
240+
// Changes to this file may cause incorrect behavior and will be lost if
241+
// the code is regenerated.
242+
// </auto-generated>
243+
//------------------------------------------------------------------------------
244+
245+
#pragma warning disable 1591 // publicly visible type or member must be documented
246+
247+
#nullable enable
248+
namespace SomeNamespace2
249+
{
250+
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
251+
partial struct MyIdConverters
252+
{
253+
public partial class DapperTypeHandler : global::Dapper.SqlMapper.TypeHandler<SomeNamespace1.MyId>
254+
{
255+
public override void SetValue(global::System.Data.IDbDataParameter parameter, SomeNamespace1.MyId value)
256+
{
257+
parameter.Value = value.Value;
258+
}
259+
260+
public override SomeNamespace1.MyId Parse(object value)
261+
{
262+
return value switch
263+
{
264+
global::System.Guid guidValue => new SomeNamespace1.MyId(guidValue),
265+
string stringValue when !string.IsNullOrEmpty(stringValue) && global::System.Guid.TryParse(stringValue, out var result) => new SomeNamespace1.MyId(result),
266+
_ => throw new global::System.InvalidCastException($"Unable to cast object of type {value.GetType()} to SomeNamespace1.MyId"),
267+
};
268+
}
269+
}
270+
}
271+
}

0 commit comments

Comments
 (0)