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

V9.0.0/task based sceanario relocation #95

Merged
merged 12 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion src/Cuemon.Core/ActionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public ActionFactory(Action<TTuple> method, TTuple tuple) : this(method, tuple,
internal ActionFactory(Action<TTuple> method, TTuple tuple, Delegate originalDelegate) : base(tuple, originalDelegate != null)
{
Method = method;
DelegateInfo = Infrastructure.ResolveDelegateInfo(method, originalDelegate);
DelegateInfo = Decorator.RawEnclose(method).ResolveDelegateInfo(originalDelegate);
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Cuemon.Core/Decorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public static Decorator<T> Enclose<T>(T inner, bool throwIfNull = true)
return new Decorator<T>(inner, throwIfNull);
}

/// <summary>
/// Encloses the specified <paramref name="inner"/> so that it can be extended by non-common extension methods.
/// </summary>
/// <typeparam name="T">The type of the <paramref name="inner"/> to wrap for non-common extension methods.</typeparam>
/// <param name="inner">The object to extend for non-common extension methods.</param>
/// <returns>An instance of <see cref="Decorator{T}"/>.</returns>
/// <remarks>Unlike <see cref="Enclose{T}"/>, this method does not perform a null-check when wrapping the value.</remarks>
public static Decorator<T> RawEnclose<T>(T inner)
{
return Enclose(inner, false);
}

/// <summary>
/// Encloses the specified <paramref name="inner"/> so that it can be extended by both common and non-common extension methods.
/// </summary>
Expand Down
26 changes: 0 additions & 26 deletions src/Cuemon.Core/Extensions/ByteArrayDecoratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Text;

namespace Cuemon
Expand Down Expand Up @@ -44,29 +42,5 @@ public static Stream ToStream(this IDecorator<byte[]> decorator)
return ms;
});
}

/// <summary>
/// Converts the enclosed <see cref="T:byte[]"/> of the specified <paramref name="decorator"/> to its equivalent <see cref="Stream"/> representation.
/// </summary>
/// <param name="decorator">The <see cref="T:IDecorator{byte[]}"/> to extend.</param>
/// <param name="ct">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Stream"/> that is equivalent to the enclosed <see cref="T:byte[]"/> of the specified <paramref name="decorator"/>.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="decorator"/> cannot be null.
/// </exception>
public static Task<Stream> ToStreamAsync(this IDecorator<byte[]> decorator, CancellationToken ct = default)
{
Validator.ThrowIfNull(decorator);
return Patterns.SafeInvokeAsync<Stream>(() => new MemoryStream(decorator.Inner.Length), async (ms, cti) =>
{
#if NETSTANDARD
await ms.WriteAsync(decorator.Inner, 0, decorator.Inner.Length, cti).ConfigureAwait(false);
#else
await ms.WriteAsync(decorator.Inner.AsMemory(0, decorator.Inner.Length), cti).ConfigureAwait(false);
#endif
ms.Position = 0;
return ms;
}, ct);
}
}
}
27 changes: 27 additions & 0 deletions src/Cuemon.Core/Extensions/DelegateDecoratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Reflection;

namespace Cuemon
{
/// <summary>
/// Extension methods for the <see cref="Delegate"/> hidden behind the <see cref="IDecorator{T}"/> interface.
/// </summary>
/// <seealso cref="IDecorator{T}"/>
/// <seealso cref="Decorator{T}"/>
public static class DelegateDecoratorExtensions
{
/// <summary>
/// Resolves the <see cref="MethodInfo"/> of the specified <paramref name="original"/> delegate or the enclosed <see cref="Delegate"/> of the <paramref name="decorator"/>.
/// </summary>
/// <param name="decorator">The <see cref="IDecorator{T}"/> to extend.</param>
/// <param name="original">The original delegate to resolve the method information from.</param>
/// <returns>The <see cref="MethodInfo"/> of the specified <paramref name="original"/> delegate or the enclosed <see cref="Delegate"/> of the <paramref name="decorator"/>; otherwise, <c>null</c> if both are <c>null</c>.</returns>
public static MethodInfo ResolveDelegateInfo(this IDecorator<Delegate> decorator, Delegate original)
{
var wrapper = decorator?.Inner;
if (original != null) { return original.GetMethodInfo(); }
if (wrapper != null) { return wrapper.GetMethodInfo(); }
return null;

Check warning on line 24 in src/Cuemon.Core/Extensions/DelegateDecoratorExtensions.cs

View check run for this annotation

Codecov / codecov/patch

src/Cuemon.Core/Extensions/DelegateDecoratorExtensions.cs#L24

Added line #L24 was not covered by tests
}
Comment on lines +19 to +25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider simplifying the method implementation.

While the current implementation is correct, it could be simplified to improve readability and reduce the number of lines of code.

Consider refactoring the method as follows:

 public static MethodInfo ResolveDelegateInfo(this IDecorator<Delegate> decorator, Delegate original)
 {
-    var wrapper = decorator?.Inner;
-    if (original != null) { return original.GetMethodInfo(); }
-    if (wrapper != null) { return wrapper.GetMethodInfo(); }
-    return null;
+    return original?.GetMethodInfo() ?? decorator?.Inner?.GetMethodInfo();
 }

This refactored version uses the null-coalescing operator to achieve the same logic in a more concise manner. It first attempts to get the MethodInfo from the original delegate, and if that's null, it tries to get it from the decorator's inner delegate.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static MethodInfo ResolveDelegateInfo(this IDecorator<Delegate> decorator, Delegate original)
{
var wrapper = decorator?.Inner;
if (original != null) { return original.GetMethodInfo(); }
if (wrapper != null) { return wrapper.GetMethodInfo(); }
return null;
}
public static MethodInfo ResolveDelegateInfo(this IDecorator<Delegate> decorator, Delegate original)
{
return original?.GetMethodInfo() ?? decorator?.Inner?.GetMethodInfo();
}

}
}
31 changes: 0 additions & 31 deletions src/Cuemon.Core/Extensions/StringDecoratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cuemon.Text;

namespace Cuemon
Expand Down Expand Up @@ -149,36 +148,6 @@ public static Stream ToStream(this IDecorator<string> decorator, Action<Encoding
});
}

/// <summary>
/// Converts the enclosed <see cref="string"/> of the specified <paramref name="decorator"/> to a <see cref="Stream"/>.
/// </summary>
/// <param name="decorator">The <see cref="IDecorator{String}"/> to extend.</param>
/// <param name="setup">The <see cref="AsyncEncodingOptions"/> which may be configured.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Stream"/> containing the result of the enclosed <see cref="string"/> of the specified <paramref name="decorator"/>.</returns>
/// <remarks><see cref="IEncodingOptions"/> will be initialized with <see cref="EncodingOptions.DefaultPreambleSequence"/> and <see cref="EncodingOptions.DefaultEncoding"/>.</remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="decorator"/> cannot be null.
/// </exception>
/// <exception cref="InvalidEnumArgumentException">
/// <paramref name="setup"/> was initialized with an invalid <see cref="EncodingOptions.Preamble"/>.
/// </exception>
public static Task<Stream> ToStreamAsync(this IDecorator<string> decorator, Action<AsyncEncodingOptions> setup = null)
{
Validator.ThrowIfNull(decorator);
var options = Patterns.Configure(setup);
return Patterns.SafeInvokeAsync<Stream>(() => new MemoryStream(), async (ms, token) =>
{
var bytes = Convertible.GetBytes(decorator.Inner, Patterns.ConfigureExchange<AsyncEncodingOptions, EncodingOptions>(setup));
#if NETSTANDARD
await ms.WriteAsync(bytes, 0, bytes.Length, token).ConfigureAwait(false);
#else
await ms.WriteAsync(bytes.AsMemory(0, bytes.Length), token).ConfigureAwait(false);
#endif
ms.Position = 0;
return ms;
}, options.CancellationToken);
}

/// <summary>
/// Converts the enclosed <see cref="string"/> of the specified <paramref name="decorator"/> to its equivalent <see cref="Uri"/> representation.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Cuemon.Core/FuncFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public FuncFactory(Func<TTuple, TResult> method, TTuple tuple) : this(method, tu
internal FuncFactory(Func<TTuple, TResult> method, TTuple tuple, Delegate originalDelegate) : base(tuple, originalDelegate != null)
{
Method = method;
DelegateInfo = Infrastructure.ResolveDelegateInfo(method, originalDelegate);
DelegateInfo = Decorator.RawEnclose(method).ResolveDelegateInfo(originalDelegate);
}

/// <summary>
Expand Down
Loading
Loading