Skip to content

Commit

Permalink
Merge pull request #117 from Encamina/@hramos/FixWarnings/Encamina.En…
Browse files Browse the repository at this point in the history
…marcha.Core

Fixed warnings in Encamina.Enmarcha.Core
  • Loading branch information
HugoRamosEs authored May 10, 2024
2 parents 4640318 + 230c964 commit 00dcf0d
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Previous classification is not required if changes are simple or all belong to t
- `Encamina.Enmarcha.AI`.
- `Encamina.Enmarcha.AspNet`
- `Encamina.Enmarcha.Bot`
- `Encamina.Enmarcha.Core`
- Corrected a typo in the Spanish error message in `ResponseMessages.es.resx` from "ha encontrar" to "ha encontrado".

## [8.1.5]
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<PropertyGroup>
<VersionPrefix>8.1.7</VersionPrefix>
<VersionSuffix>preview-01</VersionSuffix>
<VersionSuffix>preview-02</VersionSuffix>
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public sealed class GuidNotEmptyAttribute : ValidationAttribute
/// <remarks>
/// A null value is accepted as valid since the idea of «Not Empty» doesn't necessarily means «Required».
/// </remarks>
public override bool IsValid(object value) => value == null || (value is Guid s && s != Guid.Empty);
public override bool IsValid(object? value) => value == null || (value is Guid s && s != Guid.Empty);

/// <inheritdoc/>
/// <remarks>
/// A null value is accepted as valid since the idea of «Not Empty» doesn't necessarily means «Required».
/// </remarks>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
return IsValid(value)
? ValidationResult.Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace Encamina.Enmarcha.Core.DataAnnotations;
public sealed class NotEmptyOrWhitespaceAttribute : ValidationAttribute
{
/// <inheritdoc/>
public override bool IsValid(object value) => value == null || (value is string s && !string.IsNullOrWhiteSpace(s));
public override bool IsValid(object? value) => value == null || (value is string s && !string.IsNullOrWhiteSpace(s));

/// <inheritdoc/>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
return IsValid(value)
? ValidationResult.Success
: new ValidationResult(ValidationResultMessages.ResourceManager.GetFormattedStringByCurrentCulture(nameof(ValidationResultMessages.ValueIsEmptyOrWhiteSpace), validationContext.MemberName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public RequireWhenOtherPropertyNotNullAttribute(string otherPropertyName)
public string OtherPropertyName { get; }

/// <inheritdoc/>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var otherPropertyValue = validationContext.ObjectInstance
.GetType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public RequiredIfAttribute(string conditionalPropertyName, object conditionalVal
public object ConditionalValue { get; }

/// <inheritdoc/>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var conditionalValue = validationContext.ObjectInstance
.GetType()
Expand Down
4 changes: 2 additions & 2 deletions src/Encamina.Enmarcha.Core/DataAnnotations/UriAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public UriAttribute() : base(DataType.Url)
public bool AllowsNull { get; set; } = true;

/// <inheritdoc/>
public override bool IsValid(object value) => (AllowsNull && value == null) || (value is Uri uri && uri.IsWellFormedOriginalString());
public override bool IsValid(object? value) => (AllowsNull && value == null) || (value is Uri uri && uri.IsWellFormedOriginalString());

/// <inheritdoc/>
/// <remarks>
/// A null value is accepted as valid since the idea of «Not Empty» doesn't necessarily means «Required».
/// </remarks>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
return IsValid(value)
? ValidationResult.Success
Expand Down
6 changes: 3 additions & 3 deletions src/Encamina.Enmarcha.Core/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class EnumExtensions
/// <returns>
/// The description for the enumeration value as set with the <see cref="DescriptionAttribute"/>. If not, returns <see langword="null"/>.
/// </returns>
public static string GetEnumDescription(this Enum enumValue)
public static string? GetEnumDescription(this Enum enumValue)
{
return enumValue.GetEnumDescription(false);
}
Expand All @@ -28,12 +28,12 @@ public static string GetEnumDescription(this Enum enumValue)
/// </param>
/// <returns>
/// The description for the enumeration value as set with the <see cref="DescriptionAttribute"/>. If not, returns <see langword="null"/> unless
/// the <paramref name="throwIfNoDescriptionFound"/> is <see langword="true"/>, in which case this methods trows a <see cref="ArgumentException"/>.
/// the <paramref name="throwIfNoDescriptionFound"/> is <see langword="true"/>, in which case this methods throws a <see cref="ArgumentException"/>.
/// </returns>
/// <exception cref="ArgumentException">
/// If <paramref name="throwIfNoDescriptionFound"/> is <see langword="true"/> and the enumeration value does not have a description set using the <see cref="DescriptionAttribute"/>.
/// </exception>
public static string GetEnumDescription(this Enum enumValue, bool throwIfNoDescriptionFound)
public static string? GetEnumDescription(this Enum enumValue, bool throwIfNoDescriptionFound)
{
var field = enumValue.GetType().GetField(enumValue.ToString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class IConfigurationExtensions
/// Checks that two instances of <see cref="IConfiguration"/> are the same configurations in terms of their settings,
/// regardless if they are or not the same instance.
/// </summary>
/// <remarks>This method might be usefull for testing scenarios.</remarks>
/// <remarks>This method might be useful for testing scenarios.</remarks>
/// <param name="first">First instance of <see cref="IConfiguration"/> to check.</param>
/// <param name="actualConfiguration">Second instance of <see cref="IConfiguration"/> to check.</param>
/// <returns>
Expand All @@ -26,23 +26,23 @@ public static bool IsSame(this IConfiguration first, IConfiguration actualConfig
/// Checks that an instance of <see cref="IConfiguration"/> contains the same settings as a given dictionary.
/// </summary>
/// <remarks>
/// Under the hood, an instance of <see cref="IConfiguration"/> is like a dictionary. This method might be usefull for testing scenarios.
/// Under the hood, an instance of <see cref="IConfiguration"/> is like a dictionary. This method might be useful for testing scenarios.
/// </remarks>
/// <param name="configuration">The instance of <see cref="IConfiguration"/> to check.</param>
/// <param name="settings">The instance of a dictionary with the settings to check the configuration.</param>
/// <returns>
/// Returns <see langword="true"/> if the instance of <see cref="IConfiguration"/> has as settings the values from the given dictionary; otherwise returns <see langword="false"/>.
/// </returns>
public static bool IsSame(this IConfiguration configuration, IDictionary<string, string> settings)
public static bool IsSame(this IConfiguration configuration, IDictionary<string, string?> settings)
{
return IsSame(configuration, settings.AsEnumerable());
}

private static bool IsSame(IConfiguration expectedConfiguration, IEnumerable<KeyValuePair<string, string>> settings)
private static bool IsSame(IConfiguration expectedConfiguration, IEnumerable<KeyValuePair<string, string?>> settings)
{
foreach (var setting in settings)
{
if (!expectedConfiguration[setting.Key].Equals(setting.Value, StringComparison.Ordinal))
if (!expectedConfiguration[setting.Key]!.Equals(setting.Value, StringComparison.Ordinal))
{
return false;
}
Expand Down
30 changes: 17 additions & 13 deletions src/Encamina.Enmarcha.Core/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IDictionary<string, object> ToPropertyDictionary(this object @obje
/// <returns>
/// A dictionary with values from <paramref name="object"/>'s properties.
/// </returns>
public static IDictionary<string, object> ToPropertyDictionary(this object @object, string keyPrefix)
public static IDictionary<string, object> ToPropertyDictionary(this object @object, string? keyPrefix)
{
var dictionary = new Dictionary<string, object>();

Expand All @@ -47,29 +47,33 @@ public static IDictionary<string, object> ToPropertyDictionary(this object @obje
/// Creates a dictionary from values in an <paramref name="object"/>'s properties.
/// Each dictionary's key will be prefixed with the name of <typeparamref name="T"/>.
/// </summary>
/// <remarks>This extension method is quite usefull to create in-memory configurations.</remarks>
/// <remarks>This extension method is quite useful to create in-memory configurations.</remarks>
/// <typeparam name="T">The type the object from which the dictionary will be created.</typeparam>
/// <param name="object">The object.</param>
/// <returns>
/// A dictionary with values from <paramref name="object"/>'s properties as strings.
/// </returns>
public static IDictionary<string, string> ToPropertyDictionary<T>(this T @object)
public static IDictionary<string, string?> ToPropertyDictionary<T>(this T @object)
{
var dictionary = new Dictionary<string, string>();
var propertiesDictionary = @object.ToPropertyDictionary($@"{typeof(T).Name}:");
var dictionary = new Dictionary<string, string?>();

foreach (var property in propertiesDictionary)
if (@object is not null)
{
if (property.Value is IDictionary<string, string> innerProperties)
var propertiesDictionary = @object.ToPropertyDictionary($@"{typeof(T).Name}:");

foreach (var property in propertiesDictionary)
{
foreach (var innerProperty in innerProperties)
if (property.Value is IDictionary<string, string> innerProperties)
{
dictionary.Add($@"{property.Key}:{innerProperty.Key}", innerProperty.Value);
foreach (var innerProperty in innerProperties)
{
dictionary.Add($@"{property.Key}:{innerProperty.Key}", innerProperty.Value);
}
}
else
{
dictionary.Add(property.Key, property.Value?.ToString());
}
}
else
{
dictionary.Add(property.Key, property.Value?.ToString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static string GetStringByInvariantCulture(this ResourceManager resourceMa
/// The value of the resource localized from <see cref="CultureInfo.CurrentCulture"/> and formatted with replaced
/// items from the given array of objects, or <see langword="null"/> if it cannot be found in a resource set.
/// </returns>
public static string GetFormattedStringByCurrentCulture(this ResourceManager resourceManager, string resourceName, params object[] args)
public static string GetFormattedStringByCurrentCulture(this ResourceManager resourceManager, string resourceName, params object?[] args)
{
return string.Format(CultureInfo.CurrentCulture, resourceManager.GetStringByCurrentCulture(resourceName), args);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Encamina.Enmarcha.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Encamina.Enmarcha.Core.Extensions;
public static class StringExtensions
{
/// <summary>
/// Normalizes a given string, removing diacritics, plus some unwanted charaters, and replacing others.
/// Normalizes a given string, removing diacritics, plus some unwanted characters, and replacing others.
/// </summary>
/// <param name="value">The <see cref="string"/> value to normalize.</param>
/// <param name="removeCharacters">A collection of characters to remove from <paramref name="value"/>.</param>
Expand Down Expand Up @@ -53,7 +53,7 @@ public static string NormalizeDiacritics(this string value, IEnumerable<char> re
/// Removes the <c>Async</c> suffix from a string, if present.
/// </summary>
/// <remarks>
/// This extension method is usefull when manipulating a an asynchronous method or function name.
/// This extension method is useful when manipulating a an asynchronous method or function name.
/// </remarks>
/// <param name="value">The string value to remove the <c>Async</c> suffix from.</param>
/// <returns>The value with the <c>Async</c> suffix removed, if it was present.</returns>
Expand Down Expand Up @@ -89,7 +89,7 @@ public static string RemoveCharacters(this string value, IEnumerable<char> remov
/// <returns>
/// Returns the trimmed string, or <see langword="null"/> if the trimming result is an empty string, or if <paramref name="value"/> is <see langword="null"/> or empty as well.
/// </returns>
public static string TrimAndAsNullIfEmpty(this string value)
public static string? TrimAndAsNullIfEmpty(this string value)
{
var result = string.IsNullOrWhiteSpace(value) ? null : value.Trim();

Expand All @@ -104,7 +104,7 @@ public static string TrimAndAsNullIfEmpty(this string value)
/// </remarks>
/// <typeparam name="T">The specific type of <paramref name="object"/>.</typeparam>
/// <param name="template">The string to use as template.</param>
/// <param name="object">The object whose properties'values will be used as values for the template.</param>
/// <param name="object">The object whose properties values will be used as values for the template.</param>
/// <returns>
/// <para>
/// A string value from the <paramref name="template"/> with values or tokens replaced with values from <paramref name="object"/>'s properties.
Expand All @@ -126,7 +126,7 @@ public static string TemplateStringFormatter<T>(this string template, T @object)
/// <param name="templateObjectPropertiesPrefix">
/// A value that identifies any token or parameter in the template to be replaced with values from <paramref name="object"/>'s properties is prefixed with the '<c>$</c>' character.
/// </param>
/// <param name="object">The object whose properties'values will be used as values for the template.</param>
/// <param name="object">The object whose properties values will be used as values for the template.</param>
/// <returns>
/// <para>
/// A string value from the <paramref name="template"/> with values or tokens replaced with values from <paramref name="object"/>'s properties.
Expand All @@ -144,7 +144,7 @@ public static string TemplateStringFormatter<T>(this string template, string tem
/// Formats a string as template with tokens or parameters provided from the values in <paramref name="templateValues"/>.
/// </summary>
/// <param name="template">The string to use as template.</param>
/// <param name="templateValues">A dictionalry with values to use as replacements for tokens or parameters in <paramref name="template"/>.</param>
/// <param name="templateValues">A dictionary with values to use as replacements for tokens or parameters in <paramref name="template"/>.</param>
/// <returns>
/// <para>
/// A string value from the <paramref name="template"/> with values or tokens replaced with values from <paramref name="templateValues"/>'s properties.
Expand All @@ -153,7 +153,7 @@ public static string TemplateStringFormatter<T>(this string template, string tem
/// If <paramref name="templateValues"/> is <see langword="null"/>, then this method returns the same value as <paramref name="template"/> without any token or parameter replacement.
/// </para>
/// </returns>
public static string TemplateStringFormatterWithValues(this string template, IDictionary<string, object> templateValues)
public static string TemplateStringFormatterWithValues(this string template, IDictionary<string, object>? templateValues)
{
return templateValues == null
? template
Expand Down
12 changes: 6 additions & 6 deletions src/Encamina.Enmarcha.Core/JsonUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json;
#pragma warning disable IDE0060 // Remove unused parameter

using System.Text.Json;

namespace Encamina.Enmarcha.Core;

Expand All @@ -7,20 +9,18 @@ namespace Encamina.Enmarcha.Core;
/// </summary>
public static class JsonUtils
{
#pragma warning disable IDE0060 // Remove unused parameter

/// <summary>
/// When using the <see cref="System.Text.Json"/> library, this method helps deserializing anonymous types from a JSON string.
/// </summary>
/// <remarks>
/// The <paramref name="anonymousType"/> is required to identify the type of the anonymous type that is going to be deserialized.
/// </remarks>
/// <typeparam name="T">The anonyumous type.</typeparam>
/// <typeparam name="T">The anonymous type.</typeparam>
/// <param name="json">The JSON string to deserialize as an instance of an anonymous type represented by <typeparamref name="T"/>.</param>
/// <param name="anonymousType">The anonymous type that identifies the type to deserialize from a JSON string.</param>
/// <param name="options">A valid instance of <see cref="JsonSerializerOptions"/> with options for the deserialization.</param>
/// <returns>A valid instance of <typeparamref name="T"/> obtained from the JSON deserialization.</returns>
public static T DeserializeAnonymousType<T>(string json, T anonymousType, JsonSerializerOptions options = default) => JsonSerializer.Deserialize<T>(json, options);
public static T? DeserializeAnonymousType<T>(string json, T anonymousType, JsonSerializerOptions? options = default) => JsonSerializer.Deserialize<T>(json, options);

Check warning on line 23 in src/Encamina.Enmarcha.Core/JsonUtils.cs

View workflow job for this annotation

GitHub Actions / CI

Use the overloading mechanism instead of the optional parameters. (https://rules.sonarsource.com/csharp/RSPEC-2360)

Check warning on line 23 in src/Encamina.Enmarcha.Core/JsonUtils.cs

View workflow job for this annotation

GitHub Actions / CI

Use the overloading mechanism instead of the optional parameters. (https://rules.sonarsource.com/csharp/RSPEC-2360)
}

#pragma warning restore IDE0060
}

0 comments on commit 00dcf0d

Please sign in to comment.