Skip to content

Commit

Permalink
Migrated Roslyn Analyzers and Generators to netstandard20
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Mar 12, 2024
1 parent 84af078 commit e7d0e8b
Show file tree
Hide file tree
Showing 41 changed files with 788 additions and 214 deletions.
4 changes: 3 additions & 1 deletion src/Application.Interfaces/Filtering.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Common.Extensions;

namespace Application.Interfaces;

/// <summary>
Expand All @@ -13,7 +15,7 @@ public Filtering()

public Filtering(string field)
{
ArgumentException.ThrowIfNullOrEmpty(field);
field.ThrowIfNotValuedParameter(nameof(field));
if (!_fields.Contains(field))
{
_fields.Add(field);
Expand Down
4 changes: 3 additions & 1 deletion src/Application.Interfaces/Sorting.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Common.Extensions;

namespace Application.Interfaces;

/// <summary>
Expand All @@ -7,7 +9,7 @@ public class Sorting
{
public Sorting(string by, SortDirection direction = SortDirection.Ascending)
{
ArgumentException.ThrowIfNullOrEmpty(by);
by.ThrowIfNotValuedParameter(nameof(by));
By = by;
Direction = direction;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Common/Error.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if !ANALYZERS_NONPLATFORM
using Common.Extensions;
#endif

namespace Common;

Expand All @@ -25,6 +27,7 @@ internal Error(ErrorCode code, string? message = null)

public ErrorCode Code { get; }

#if !ANALYZERS_NONPLATFORM
/// <summary>
/// Wraps the existing message within the specified message
/// </summary>
Expand All @@ -39,7 +42,8 @@ public Error Wrap(string message)
? $"{message}{Environment.NewLine}\t{Message}"
: message);
}

#endif

/// <summary>
/// Creates a <see cref="ErrorCode.NoError" /> error
/// </summary>
Expand Down
19 changes: 12 additions & 7 deletions src/Common/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#if COMMON_PROJECT
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
using System.Text;
#endif

#if COMMON_PROJECT
using JetBrains.Annotations;
#endif

Expand All @@ -11,7 +14,7 @@ namespace Common.Extensions;

public static class CollectionExtensions
{
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the <see cref="target" /> string exists in the <see cref="collection" />
/// </summary>
Expand All @@ -34,7 +37,7 @@ public static TResult First<TResult>(this IReadOnlyList<TResult> list)
return list[0];
}
#endif
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the collection contains any items
/// </summary>
Expand All @@ -44,7 +47,7 @@ public static bool HasAny<T>(this IEnumerable<T>? collection)
{
return false;
}
#if COMMON_PROJECT
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
return !collection.HasNone();
#elif GENERATORS_WEB_API_PROJECT
return !collection!.HasNone();
Expand All @@ -59,7 +62,7 @@ public static bool HasNone<T>(this IEnumerable<T> collection)
return !collection.Any();
}
#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Joins all values separated by the <see cref="separator" />
/// </summary>
Expand All @@ -78,7 +81,8 @@ public static string Join<T>(this IEnumerable<T> values, string separator)

return stringBuilder.ToString();
}

#endif
#if COMMON_PROJECT
/// <summary>
/// Returns a string value for all the items in the list, separated by the specified <see cref="orKeyword" />
/// </summary>
Expand All @@ -94,7 +98,8 @@ public static TResult Last<TResult>(this IReadOnlyList<TResult> list)
{
return list[^1];
}

#endif
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the <see cref="target" /> string does not exist in the <see cref="collection" />
/// </summary>
Expand Down
44 changes: 21 additions & 23 deletions src/Common/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#if COMMON_PROJECT
using System.Diagnostics.CodeAnalysis;
using AutoMapper;
#endif
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM || ANALYZERS_PLATFORM
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
#endif

Expand All @@ -20,7 +22,7 @@ public static TTarget Convert<TSource, TTarget>(this TSource source)
return mapper.Map<TTarget>(source);
}
#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM || ANALYZERS_PLATFORM
/// <summary>
/// Whether the object does exist
/// </summary>
Expand All @@ -29,17 +31,8 @@ public static bool Exists([NotNullWhen(true)] this object? instance)
{
return instance is not null;
}
#elif GENERATORS_WEB_API_PROJECT
/// <summary>
/// Whether the object does exist
/// </summary>
public static bool Exists(this object? instance)
{
return instance is not null;
}

#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the parameter <see cref="value" /> from being invalid according to the <see cref="validation" />,
/// and if invalid, returns a <see cref="ErrorCode.Validation" /> error
Expand Down Expand Up @@ -93,7 +86,7 @@ public static bool IsNotValuedParameter(this string? value, string parameterName
return IsInvalidParameter(value.HasValue, parameterName, null, out error);
}
#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM || ANALYZERS_PLATFORM
/// <summary>
/// Whether the object does not exist
/// </summary>
Expand All @@ -102,14 +95,6 @@ public static bool NotExists([NotNullWhen(false)] this object? instance)
{
return instance is null;
}
#elif GENERATORS_WEB_API_PROJECT
/// <summary>
/// Whether the object does not exist
/// </summary>
public static bool NotExists(this object? instance)
{
return instance is null;
}
#endif
#if COMMON_PROJECT
/// <summary>
Expand Down Expand Up @@ -145,7 +130,8 @@ public static void ThrowIfInvalidParameter<TValue>(this TValue? value, Predicate
throw new ArgumentOutOfRangeException(parameterName, errorMessage);
}
}

#endif
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException" /> if the specified <see cref="value" /> does not have a value
/// </summary>
Expand All @@ -172,5 +158,17 @@ private static bool IsInvalidParameter(Func<bool> predicate, string parameterNam
error = Error.NoError;
return false;
}

/// <summary>
/// Throws an <see cref="ArgumentNullException" /> if the specified <see cref="value" /> is null
/// </summary>
public static void ThrowIfNullParameter<TValue>(this TValue? value, string parameterName,
string? errorMessage = null)
{
if (value is null)
{
throw new ArgumentNullException(parameterName, errorMessage);
}
}
#endif
}
}
72 changes: 40 additions & 32 deletions src/Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#if COMMON_PROJECT
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
#endif
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || GENERATORS_COMMON_PROJECT || ANALYZERS_NONPLATFORM
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
#endif

#elif GENERATORS_WEB_API_PROJECT
#if GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
#elif GENERATORS_COMMON_PROJECT
#endif

#if GENERATORS_COMMON_PROJECT
using System.Globalization;
using System.Text;
#endif
Expand All @@ -24,7 +29,7 @@ namespace Common.Extensions;
#endif
public static class StringExtensions
{
#if COMMON_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Defines the casing used in JSON serialization
/// </summary>
Expand All @@ -33,10 +38,11 @@ public enum JsonCasing
Pascal,
Camel
}

#endif
#if COMMON_PROJECT
private static readonly TimeSpan DefaultRegexTimeout = TimeSpan.FromSeconds(10);
#endif
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the <see cref="other" /> is the same as the value (case-insensitive)
/// </summary>
Expand All @@ -45,15 +51,16 @@ public static bool EqualsIgnoreCase(this string value, string other)
return string.Equals(value, other, StringComparison.OrdinalIgnoreCase);
}
#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the <see cref="other" /> is precisely the same as the value (case-sensitive)
/// </summary>
public static bool EqualsOrdinal(this string value, string other)
{
return string.Equals(value, other, StringComparison.Ordinal);
}

#endif
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Formats the <see cref="value" /> with the <see cref="arguments" />
/// </summary>
Expand All @@ -78,7 +85,7 @@ public static string Format(this string value, params object[] arguments)
PropertyNameCaseInsensitive = true
});
}
#elif GENERATORS_WEB_API_PROJECT
#elif GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM
public static TObject FromJson<TObject>(this string json)
where TObject : new()
{
Expand All @@ -87,11 +94,27 @@ public static TObject FromJson<TObject>(this string json)
return new TObject();
}

var serializer = new DataContractJsonSerializer(typeof(TObject), new DataContractJsonSerializerSettings());
var deserialized = FromJson(json, typeof(TObject));
if (deserialized is not null)
{
return (TObject)deserialized;
}

return new TObject();
}

public static object? FromJson(this string json, Type type)
{
if (json.HasNoValue())
{
return null;
}

var serializer = new DataContractJsonSerializer(type, new DataContractJsonSerializerSettings());

using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
return (TObject)serializer.ReadObject(ms);
return serializer.ReadObject(ms);
}
}
#endif
Expand All @@ -112,7 +135,7 @@ public static TObject FromJson<TObject>(this string json)
});
}
#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || GENERATORS_WEB_API_PROJECT || GENERATORS_COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Whether the string value contains no value: it is either: null, empty or only whitespaces
/// </summary>
Expand All @@ -132,22 +155,6 @@ public static bool HasValue([NotNullWhen(true)] this string? value)
{
return !string.IsNullOrEmpty(value) && !string.IsNullOrWhiteSpace(value);
}
#elif GENERATORS_WEB_API_PROJECT || GENERATORS_COMMON_PROJECT
/// <summary>
/// Whether the string value contains no value: it is either: null, empty or only whitespaces
/// </summary>
public static bool HasNoValue(this string? value)
{
return string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value);
}

/// <summary>
/// Whether the string value contains any value except: null, empty or only whitespaces
/// </summary>
public static bool HasValue(this string? value)
{
return !string.IsNullOrEmpty(value) && !string.IsNullOrWhiteSpace(value);
}
#endif
#if COMMON_PROJECT
/// <summary>
Expand Down Expand Up @@ -261,7 +268,7 @@ public static string ToCamelCase(this string value)
return char.ToLowerInvariant(titleCase[0]) + titleCase.Substring(1);
}
#endif
#if COMMON_PROJECT
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Converts the <see cref="value" /> to a integer value
/// </summary>
Expand Down Expand Up @@ -321,8 +328,9 @@ public static int ToIntOrDefault(this string? value, int defaultValue)
: JsonIgnoreCondition.WhenWritingNull
});
}
#elif GENERATORS_WEB_API_PROJECT
public static string? ToJson<TObject>(this TObject? value, bool? prettyPrint = false)
#elif GENERATORS_WEB_API_PROJECT || ANALYZERS_NONPLATFORM || ANALYZERS_NONPLATFORM
public static string? ToJson<TObject>(this TObject? value, bool? prettyPrint = true, JsonCasing casing =
JsonCasing.Pascal)
{
if (value is null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Optional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static Optional<TValue> None<TValue>()
/// </summary>
public static Optional<TValue> Some<TValue>(TValue value)
{
ArgumentNullException.ThrowIfNull(value);
value.ThrowIfNullParameter(nameof(value));

if (value.TryGetContainedValue(out var contained))
{
Expand Down
Loading

0 comments on commit e7d0e8b

Please sign in to comment.