Skip to content

Commit 5b95f47

Browse files
authored
Merge pull request RehanSaeed#465 from RehanSaeed/net-7
Upgrade to .NET 7 and C# 11
2 parents c937af7 + 95fbe75 commit 5b95f47

File tree

72 files changed

+1800
-1334
lines changed

Some content is hidden

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

72 files changed

+1800
-1334
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ jobs:
3737
uses: actions/[email protected]
3838
with:
3939
dotnet-version: |
40-
3.1.x
41-
5.0.x
40+
6.0.x
4241
global-json-file: "./global.json"
4342
- name: "Dotnet Tool Restore"
4443
run: dotnet tool restore
@@ -74,6 +73,7 @@ jobs:
7473
path: "./Artefacts"
7574
- name: "Publish Test Summary"
7675
uses: test-summary/action@v2
76+
if: always()
7777
with:
7878
paths: "./Artefacts/*/*.xml"
7979

Benchmarks/Schema.NET.Benchmarks/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace Schema.NET.Benchmarks;
44

55
public class Program
66
{
7-
private static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
7+
private static void Main(string[] args) =>
8+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
89
}

Benchmarks/Schema.NET.Benchmarks/Schema.NET.Benchmarks.csproj

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

33
<PropertyGroup Label="Build">
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net6.0;net5.0;net472</TargetFrameworks>
5+
<TargetFrameworks>net7.0;net6.0;net472</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

Benchmarks/Schema.NET.Benchmarks/SchemaBenchmarkBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Schema.NET.Benchmarks;
1212
[HtmlExporter]
1313
[CsvMeasurementsExporter]
1414
[RPlotExporter]
15+
[SimpleJob(RuntimeMoniker.Net70)]
1516
[SimpleJob(RuntimeMoniker.Net60)]
1617
[SimpleJob(RuntimeMoniker.Net472)]
1718
public abstract class SchemaBenchmarkBase

Source/Common/DateTimeToIso8601DateValuesJsonConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Schema.NET;
1313
/// <seealso cref="ValuesJsonConverter" />
1414
public class DateTimeToIso8601DateValuesJsonConverter : ValuesJsonConverter
1515
{
16+
private const string DateFormat = "yyyy-MM-dd";
17+
1618
/// <summary>
1719
/// Writes the object retrieved from <see cref="IValues" /> when one is found.
1820
/// </summary>
@@ -38,7 +40,7 @@ public override void WriteObject(Utf8JsonWriter writer, object? value, JsonSeria
3840

3941
if (value is DateTime dateTimeType)
4042
{
41-
writer.WriteStringValue(dateTimeType.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
43+
writer.WriteStringValue(dateTimeType.ToString(DateFormat, CultureInfo.InvariantCulture));
4244
}
4345
else
4446
{

Source/Common/EnumHelper.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ public static bool TryParse(
2525
string? value,
2626
out object? result)
2727
{
28-
#if NETSTANDARD2_0 || NET472 || NET461
28+
#if NETSTANDARD2_0 || NET472 || NET462
2929
try
3030
{
3131
result = Enum.Parse(enumType, value);
3232
return true;
3333
}
34-
#pragma warning disable CA1031 // Do not catch general exception types
3534
catch
36-
#pragma warning restore CA1031 // Do not catch general exception types
3735
{
3836
result = null;
3937
return false;
@@ -63,15 +61,19 @@ public static bool TryParseEnumFromSchemaUri(
6361
string? enumString;
6462
if (value is not null && value.StartsWith(Constants.HttpSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
6563
{
66-
#pragma warning disable IDE0057 // Use range operator. Need to multi-target.
64+
#if NETCOREAPP3_0_OR_GREATER
65+
enumString = value[(Constants.HttpSchemaOrgUrl.Length + 1)..];
66+
#else
6767
enumString = value.Substring(Constants.HttpSchemaOrgUrl.Length + 1);
68-
#pragma warning restore IDE0057 // Use range operator. Need to multi-target.
68+
#endif
6969
}
7070
else if (value is not null && value.StartsWith(Constants.HttpsSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
7171
{
72-
#pragma warning disable IDE0057 // Use range operator. Need to multi-target.
72+
#if NETCOREAPP3_0_OR_GREATER
73+
enumString = value[(Constants.HttpsSchemaOrgUrl.Length + 1)..];
74+
#else
7375
enumString = value.Substring(Constants.HttpsSchemaOrgUrl.Length + 1);
74-
#pragma warning restore IDE0057 // Use range operator. Need to multi-target.
76+
#endif
7577
}
7678
else
7779
{

Source/Common/FastActivator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ internal static class FastActivator
3535
}
3636

3737
private static Func<T1, object> CreateConstructorDelegate<T1>(ConstructorInfo constructor) => Expression.Lambda<Func<T1, object>>(
38-
Expression.Convert(
39-
Expression.New(constructor, ConstructorParameter<T1>.SingleParameter),
40-
typeof(object)),
41-
ConstructorParameter<T1>.SingleParameter).Compile();
38+
Expression.Convert(
39+
Expression.New(constructor, ConstructorParameter<T1>.SingleParameter),
40+
typeof(object)),
41+
ConstructorParameter<T1>.SingleParameter).Compile();
4242

4343
private static ConstructorInfo? GetConstructorInfo(Type objectType, Type parameter1)
4444
{

Source/Common/HashCode.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public struct HashCode : IEquatable<HashCode>
2525
/// <returns>
2626
/// The result of the conversion.
2727
/// </returns>
28-
#pragma warning disable CA2225 // Operator overloads have named alternates
2928
public static implicit operator int(HashCode hashCode) => hashCode.value;
30-
#pragma warning restore CA2225 // Operator overloads have named alternates
3129

3230
/// <summary>
3331
/// Implements the operator ==.
@@ -113,9 +111,7 @@ public override bool Equals(object? obj)
113111
/// <exception cref="NotSupportedException">Implicitly convert this struct to an <see cref="int" /> to get the hash code.</exception>
114112
[EditorBrowsable(EditorBrowsableState.Never)]
115113
public override int GetHashCode() =>
116-
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
117-
throw new NotSupportedException("Implicitly convert this struct to an int to get the hash code.");
118-
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
114+
throw new NotSupportedException("Implicitly convert this struct to an int to get the hash code.");
119115

120116
private static int CombineHashCodes(int h1, int h2)
121117
{

Source/Common/JsonLdContext.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public class JsonLdContext : IEquatable<JsonLdContext>
2828
/// </summary>
2929
/// <param name="context">The context.</param>
3030
/// <returns>The result of the conversion.</returns>
31-
#pragma warning disable CA1062 // Validate arguments of public methods.
3231
public static implicit operator string?(JsonLdContext context) => context.Name;
33-
#pragma warning restore CA1062 // Validate arguments of public methods
3432

3533
/// <summary>
3634
/// Implements the operator ==.
@@ -40,9 +38,7 @@ public class JsonLdContext : IEquatable<JsonLdContext>
4038
/// <returns>
4139
/// The result of the operator.
4240
/// </returns>
43-
#pragma warning disable CA1062 // Validate arguments of public methods
4441
public static bool operator ==(JsonLdContext left, JsonLdContext right) => left.Equals(right);
45-
#pragma warning restore CA1062 // Validate arguments of public methods
4642

4743
/// <summary>
4844
/// Implements the operator !=.

Source/Common/OneOrMany{T}.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ namespace Schema.NET;
1010
/// </summary>
1111
/// <typeparam name="T">The type of the values.</typeparam>
1212
/// <seealso cref="ICollection{T}" />
13+
#pragma warning disable CA1710 // Identifiers should have correct suffix.
1314
public readonly struct OneOrMany<T>
15+
#pragma warning restore CA1710 // Identifiers should have correct suffix.
1416
: IReadOnlyCollection<T>, IEnumerable<T>, IValues, IEquatable<OneOrMany<T>>
1517
{
1618
private readonly T[]? collection;
@@ -21,9 +23,7 @@ public readonly struct OneOrMany<T>
2123
/// <param name="item">The single item value.</param>
2224
public OneOrMany(T item)
2325
{
24-
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
2526
if (item is null || (item is string itemAsString && string.IsNullOrWhiteSpace(itemAsString)))
26-
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
2727
{
2828
this.collection = null;
2929
this.HasOne = false;
@@ -51,9 +51,7 @@ public OneOrMany(ReadOnlySpan<T> span)
5151
for (var i = 0; i < span.Length; i++)
5252
{
5353
var item = span[i];
54-
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
5554
if (!string.IsNullOrWhiteSpace(item as string))
56-
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
5755
{
5856
items[index] = item;
5957
index++;
@@ -65,9 +63,7 @@ public OneOrMany(ReadOnlySpan<T> span)
6563
for (var i = 0; i < span.Length; i++)
6664
{
6765
var item = span[i];
68-
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
6966
if (item is not null)
70-
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
7167
{
7268
items[index] = item;
7369
index++;
@@ -145,29 +141,21 @@ public OneOrMany(IEnumerable<object> collection)
145141
/// </summary>
146142
/// <param name="item">The single item value.</param>
147143
/// <returns>The result of the conversion.</returns>
148-
#pragma warning disable CA2225 // Operator overloads have named alternates
149144
public static implicit operator OneOrMany<T>(T item) => new(item);
150-
#pragma warning restore CA2225 // Operator overloads have named alternates
151145

152146
/// <summary>
153147
/// Performs an implicit conversion from <typeparamref name="T[]"/> to <see cref="OneOrMany{T}"/>.
154148
/// </summary>
155149
/// <param name="array">The array of values.</param>
156150
/// <returns>The result of the conversion.</returns>
157-
#pragma warning disable CA2225 // Operator overloads have named alternates
158151
public static implicit operator OneOrMany<T>(T[] array) => new(array);
159-
#pragma warning restore CA2225 // Operator overloads have named alternates
160152

161153
/// <summary>
162154
/// Performs an implicit conversion from <see cref="List{T}"/> to <see cref="OneOrMany{T}"/>.
163155
/// </summary>
164156
/// <param name="list">The list of values.</param>
165157
/// <returns>The result of the conversion.</returns>
166-
#pragma warning disable CA2225 // Operator overloads have named alternates
167-
#pragma warning disable CA1002 // Do not expose generic lists
168158
public static implicit operator OneOrMany<T>(List<T> list) => new(list);
169-
#pragma warning restore CA1002 // Do not expose generic lists
170-
#pragma warning restore CA2225 // Operator overloads have named alternates
171159

172160
/// <summary>
173161
/// Performs an implicit conversion from <see cref="OneOrMany{T}"/> to <typeparamref name="T"/>.
@@ -176,9 +164,7 @@ public OneOrMany(IEnumerable<object> collection)
176164
/// <returns>
177165
/// The result of the conversion.
178166
/// </returns>
179-
#pragma warning disable CA2225 // Operator overloads have named alternates
180167
public static implicit operator T?(OneOrMany<T> oneOrMany) => oneOrMany.FirstOrDefault();
181-
#pragma warning restore CA2225 // Operator overloads have named alternates
182168

183169
/// <summary>
184170
/// Performs an implicit conversion from <see cref="OneOrMany{T}"/> to <typeparamref name="T[]"/>.
@@ -187,9 +173,7 @@ public OneOrMany(IEnumerable<object> collection)
187173
/// <returns>
188174
/// The result of the conversion.
189175
/// </returns>
190-
#pragma warning disable CA2225 // Operator overloads have named alternates
191176
public static implicit operator T[](OneOrMany<T> oneOrMany) => oneOrMany.ToArray();
192-
#pragma warning restore CA2225 // Operator overloads have named alternates
193177

194178
/// <summary>
195179
/// Performs an implicit conversion from <see cref="OneOrMany{T}"/> to <see cref="List{T}"/>.
@@ -198,11 +182,7 @@ public OneOrMany(IEnumerable<object> collection)
198182
/// <returns>
199183
/// The result of the conversion.
200184
/// </returns>
201-
#pragma warning disable CA2225 // Operator overloads have named alternates
202-
#pragma warning disable CA1002 // Do not expose generic lists
203185
public static implicit operator List<T>(OneOrMany<T> oneOrMany) => oneOrMany.ToList();
204-
#pragma warning restore CA1002 // Do not expose generic lists
205-
#pragma warning restore CA2225 // Operator overloads have named alternates
206186

207187
/// <summary>
208188
/// Implements the operator ==.
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace Schema.NET;
22

3-
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text;
65

@@ -9,6 +8,13 @@ namespace Schema.NET;
98
/// </summary>
109
public partial class PropertyValueSpecification
1110
{
11+
private const string MaxLengthPropertyName = "maxlength=";
12+
private const string MinLengthPropertyName = "minlength=";
13+
private const string NamePropertyName = "name=";
14+
private const string PatternPropertyName = "pattern=";
15+
private const string RequiredPropertyName = "required";
16+
private const char SpaceDelimeter = ' ';
17+
1218
/// <summary>
1319
/// Returns a <see cref="string" /> that represents the short hand representation of this instance.
1420
/// See https://schema.org/docs/actions.html#part-3.
@@ -22,45 +28,45 @@ public override string ToString()
2228

2329
if (this.ValueMaxLength.First() is double maxLength)
2430
{
25-
stringBuilder.Append("maxlength=");
31+
stringBuilder.Append(MaxLengthPropertyName);
2632
stringBuilder.Append(maxLength);
2733
}
2834

2935
if (this.ValueMinLength.First() is double minLength)
3036
{
31-
AppendSpace(stringBuilder);
32-
stringBuilder.Append("minlength=");
37+
AppendSpaceDelimeter(stringBuilder);
38+
stringBuilder.Append(MinLengthPropertyName);
3339
stringBuilder.Append(minLength);
3440
}
3541

3642
if (this.ValueName.First() is string name)
3743
{
38-
AppendSpace(stringBuilder);
39-
stringBuilder.Append("name=");
44+
AppendSpaceDelimeter(stringBuilder);
45+
stringBuilder.Append(NamePropertyName);
4046
stringBuilder.Append(name);
4147
}
4248

4349
if (this.ValuePattern.First() is string pattern)
4450
{
45-
AppendSpace(stringBuilder);
46-
stringBuilder.Append("pattern=");
51+
AppendSpaceDelimeter(stringBuilder);
52+
stringBuilder.Append(PatternPropertyName);
4753
stringBuilder.Append(pattern);
4854
}
4955

5056
if (this.ValueRequired.First() is true)
5157
{
52-
AppendSpace(stringBuilder);
53-
stringBuilder.Append("required");
58+
AppendSpaceDelimeter(stringBuilder);
59+
stringBuilder.Append(RequiredPropertyName);
5460
}
5561

5662
return stringBuilder.ToString();
5763
}
5864

59-
private static void AppendSpace(StringBuilder stringBuilder)
65+
private static void AppendSpaceDelimeter(StringBuilder stringBuilder)
6066
{
6167
if (stringBuilder.Length > 0)
6268
{
63-
stringBuilder.Append(' ');
69+
stringBuilder.Append(SpaceDelimeter);
6470
}
6571
}
6672
}

Source/Common/SchemaEnumJsonConverter{T}.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Schema.NET;
1010
/// <summary>
1111
/// Converts a Schema enumeration to and from JSON.
1212
/// </summary>
13-
/// <typeparam name="T">The enum type to convert</typeparam>
13+
/// <typeparam name="T">The enumeration type to convert.</typeparam>
1414
public class SchemaEnumJsonConverter<T> : JsonConverter<T>
1515
where T : struct, Enum
1616
{
@@ -41,10 +41,14 @@ public SchemaEnumJsonConverter()
4141
/// <returns>The enumeration value.</returns>
4242
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
4343
{
44+
#if NET6_0_OR_GREATER
45+
ArgumentNullException.ThrowIfNull(typeToConvert);
46+
#else
4447
if (typeToConvert is null)
4548
{
4649
throw new ArgumentNullException(nameof(typeToConvert));
4750
}
51+
#endif
4852

4953
var valueString = reader.GetString();
5054
if (EnumHelper.TryParseEnumFromSchemaUri(typeToConvert, valueString, out var result))
@@ -63,11 +67,21 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
6367
/// <param name="options">The JSON serializer options.</param>
6468
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
6569
{
70+
#if NET6_0_OR_GREATER
71+
ArgumentNullException.ThrowIfNull(writer);
72+
ArgumentNullException.ThrowIfNull(options);
73+
#else
6674
if (writer is null)
6775
{
6876
throw new ArgumentNullException(nameof(writer));
6977
}
7078

79+
if (options is null)
80+
{
81+
throw new ArgumentNullException(nameof(options));
82+
}
83+
#endif
84+
7185
writer.WriteStringValue(this.valueNameMap[value]);
7286
}
7387
}

0 commit comments

Comments
 (0)