Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to .NET 7 and C# 11 #465

Merged
merged 23 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ jobs:
uses: actions/setup-dotnet@v3.0.3
with:
dotnet-version: |
3.1.x
5.0.x
6.0.x
global-json-file: "./global.json"
- name: "Dotnet Tool Restore"
run: dotnet tool restore
Expand Down Expand Up @@ -74,6 +73,7 @@ jobs:
path: "./Artefacts"
- name: "Publish Test Summary"
uses: test-summary/action@v2
if: always()
with:
paths: "./Artefacts/*/*.xml"

Expand Down
3 changes: 2 additions & 1 deletion Benchmarks/Schema.NET.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ namespace Schema.NET.Benchmarks;

public class Program
{
private static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
private static void Main(string[] args) =>
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup Label="Build">
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net5.0;net472</TargetFrameworks>
<TargetFrameworks>net7.0;net6.0;net472</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions Benchmarks/Schema.NET.Benchmarks/SchemaBenchmarkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Schema.NET.Benchmarks;
[HtmlExporter]
[CsvMeasurementsExporter]
[RPlotExporter]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net472)]
public abstract class SchemaBenchmarkBase
Expand Down
4 changes: 3 additions & 1 deletion Source/Common/DateTimeToIso8601DateValuesJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Schema.NET;
/// <seealso cref="ValuesJsonConverter" />
public class DateTimeToIso8601DateValuesJsonConverter : ValuesJsonConverter
{
private const string DateFormat = "yyyy-MM-dd";

/// <summary>
/// Writes the object retrieved from <see cref="IValues" /> when one is found.
/// </summary>
Expand All @@ -38,7 +40,7 @@ public override void WriteObject(Utf8JsonWriter writer, object? value, JsonSeria

if (value is DateTime dateTimeType)
{
writer.WriteStringValue(dateTimeType.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
writer.WriteStringValue(dateTimeType.ToString(DateFormat, CultureInfo.InvariantCulture));
}
else
{
Expand Down
16 changes: 9 additions & 7 deletions Source/Common/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ public static bool TryParse(
string? value,
out object? result)
{
#if NETSTANDARD2_0 || NET472 || NET461
#if NETSTANDARD2_0 || NET472 || NET462
try
{
result = Enum.Parse(enumType, value);
return true;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch
#pragma warning restore CA1031 // Do not catch general exception types
{
result = null;
return false;
Expand Down Expand Up @@ -63,15 +61,19 @@ public static bool TryParseEnumFromSchemaUri(
string? enumString;
if (value is not null && value.StartsWith(Constants.HttpSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
{
#pragma warning disable IDE0057 // Use range operator. Need to multi-target.
#if NETCOREAPP3_0_OR_GREATER
enumString = value[(Constants.HttpSchemaOrgUrl.Length + 1)..];
#else
enumString = value.Substring(Constants.HttpSchemaOrgUrl.Length + 1);
#pragma warning restore IDE0057 // Use range operator. Need to multi-target.
#endif
}
else if (value is not null && value.StartsWith(Constants.HttpsSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
{
#pragma warning disable IDE0057 // Use range operator. Need to multi-target.
#if NETCOREAPP3_0_OR_GREATER
enumString = value[(Constants.HttpsSchemaOrgUrl.Length + 1)..];
#else
enumString = value.Substring(Constants.HttpsSchemaOrgUrl.Length + 1);
#pragma warning restore IDE0057 // Use range operator. Need to multi-target.
#endif
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions Source/Common/FastActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ internal static class FastActivator
}

private static Func<T1, object> CreateConstructorDelegate<T1>(ConstructorInfo constructor) => Expression.Lambda<Func<T1, object>>(
Expression.Convert(
Expression.New(constructor, ConstructorParameter<T1>.SingleParameter),
typeof(object)),
ConstructorParameter<T1>.SingleParameter).Compile();
Expression.Convert(
Expression.New(constructor, ConstructorParameter<T1>.SingleParameter),
typeof(object)),
ConstructorParameter<T1>.SingleParameter).Compile();

private static ConstructorInfo? GetConstructorInfo(Type objectType, Type parameter1)
{
Expand Down
6 changes: 1 addition & 5 deletions Source/Common/HashCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public struct HashCode : IEquatable<HashCode>
/// <returns>
/// The result of the conversion.
/// </returns>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator int(HashCode hashCode) => hashCode.value;
#pragma warning restore CA2225 // Operator overloads have named alternates

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

private static int CombineHashCodes(int h1, int h2)
{
Expand Down
4 changes: 0 additions & 4 deletions Source/Common/JsonLdContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public class JsonLdContext : IEquatable<JsonLdContext>
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The result of the conversion.</returns>
#pragma warning disable CA1062 // Validate arguments of public methods.
public static implicit operator string?(JsonLdContext context) => context.Name;
#pragma warning restore CA1062 // Validate arguments of public methods

/// <summary>
/// Implements the operator ==.
Expand All @@ -40,9 +38,7 @@ public class JsonLdContext : IEquatable<JsonLdContext>
/// <returns>
/// The result of the operator.
/// </returns>
#pragma warning disable CA1062 // Validate arguments of public methods
public static bool operator ==(JsonLdContext left, JsonLdContext right) => left.Equals(right);
#pragma warning restore CA1062 // Validate arguments of public methods

/// <summary>
/// Implements the operator !=.
Expand Down
24 changes: 2 additions & 22 deletions Source/Common/OneOrMany{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace Schema.NET;
/// </summary>
/// <typeparam name="T">The type of the values.</typeparam>
/// <seealso cref="ICollection{T}" />
#pragma warning disable CA1710 // Identifiers should have correct suffix.
public readonly struct OneOrMany<T>
#pragma warning restore CA1710 // Identifiers should have correct suffix.
: IReadOnlyCollection<T>, IEnumerable<T>, IValues, IEquatable<OneOrMany<T>>
{
private readonly T[]? collection;
Expand All @@ -21,9 +23,7 @@ public readonly struct OneOrMany<T>
/// <param name="item">The single item value.</param>
public OneOrMany(T item)
{
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
if (item is null || (item is string itemAsString && string.IsNullOrWhiteSpace(itemAsString)))
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
{
this.collection = null;
this.HasOne = false;
Expand Down Expand Up @@ -51,9 +51,7 @@ public OneOrMany(ReadOnlySpan<T> span)
for (var i = 0; i < span.Length; i++)
{
var item = span[i];
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
if (!string.IsNullOrWhiteSpace(item as string))
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
{
items[index] = item;
index++;
Expand All @@ -65,9 +63,7 @@ public OneOrMany(ReadOnlySpan<T> span)
for (var i = 0; i < span.Length; i++)
{
var item = span[i];
#pragma warning disable CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
if (item is not null)
#pragma warning restore CA1508 // TODO: Remove this suppression in .NET 6 where the warning is fixed.
{
items[index] = item;
index++;
Expand Down Expand Up @@ -145,29 +141,21 @@ public OneOrMany(IEnumerable<object> collection)
/// </summary>
/// <param name="item">The single item value.</param>
/// <returns>The result of the conversion.</returns>
#pragma warning disable CA2225 // Operator overloads have named alternates
public static implicit operator OneOrMany<T>(T item) => new(item);
#pragma warning restore CA2225 // Operator overloads have named alternates

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

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

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

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

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

/// <summary>
/// Implements the operator ==.
Expand Down
30 changes: 18 additions & 12 deletions Source/Common/PropertyValueSpecification.Partial.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Schema.NET;

using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -9,6 +8,13 @@ namespace Schema.NET;
/// </summary>
public partial class PropertyValueSpecification
{
private const string MaxLengthPropertyName = "maxlength=";
private const string MinLengthPropertyName = "minlength=";
private const string NamePropertyName = "name=";
private const string PatternPropertyName = "pattern=";
private const string RequiredPropertyName = "required";
private const char SpaceDelimeter = ' ';

/// <summary>
/// Returns a <see cref="string" /> that represents the short hand representation of this instance.
/// See https://schema.org/docs/actions.html#part-3.
Expand All @@ -22,45 +28,45 @@ public override string ToString()

if (this.ValueMaxLength.First() is double maxLength)
{
stringBuilder.Append("maxlength=");
stringBuilder.Append(MaxLengthPropertyName);
stringBuilder.Append(maxLength);
}

if (this.ValueMinLength.First() is double minLength)
{
AppendSpace(stringBuilder);
stringBuilder.Append("minlength=");
AppendSpaceDelimeter(stringBuilder);
stringBuilder.Append(MinLengthPropertyName);
stringBuilder.Append(minLength);
}

if (this.ValueName.First() is string name)
{
AppendSpace(stringBuilder);
stringBuilder.Append("name=");
AppendSpaceDelimeter(stringBuilder);
stringBuilder.Append(NamePropertyName);
stringBuilder.Append(name);
}

if (this.ValuePattern.First() is string pattern)
{
AppendSpace(stringBuilder);
stringBuilder.Append("pattern=");
AppendSpaceDelimeter(stringBuilder);
stringBuilder.Append(PatternPropertyName);
stringBuilder.Append(pattern);
}

if (this.ValueRequired.First() is true)
{
AppendSpace(stringBuilder);
stringBuilder.Append("required");
AppendSpaceDelimeter(stringBuilder);
stringBuilder.Append(RequiredPropertyName);
}

return stringBuilder.ToString();
}

private static void AppendSpace(StringBuilder stringBuilder)
private static void AppendSpaceDelimeter(StringBuilder stringBuilder)
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append(' ');
stringBuilder.Append(SpaceDelimeter);
}
}
}
16 changes: 15 additions & 1 deletion Source/Common/SchemaEnumJsonConverter{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Schema.NET;
/// <summary>
/// Converts a Schema enumeration to and from JSON.
/// </summary>
/// <typeparam name="T">The enum type to convert</typeparam>
/// <typeparam name="T">The enumeration type to convert.</typeparam>
public class SchemaEnumJsonConverter<T> : JsonConverter<T>
where T : struct, Enum
{
Expand Down Expand Up @@ -41,10 +41,14 @@ public SchemaEnumJsonConverter()
/// <returns>The enumeration value.</returns>
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(typeToConvert);
#else
if (typeToConvert is null)
{
throw new ArgumentNullException(nameof(typeToConvert));
}
#endif

var valueString = reader.GetString();
if (EnumHelper.TryParseEnumFromSchemaUri(typeToConvert, valueString, out var result))
Expand All @@ -63,11 +67,21 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
/// <param name="options">The JSON serializer options.</param>
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
ArgumentNullException.ThrowIfNull(options);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}

if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
#endif

writer.WriteStringValue(this.valueNameMap[value]);
}
}
Loading