Skip to content

Commit

Permalink
v3.23.4 (#113)
Browse files Browse the repository at this point in the history
- *Fixed:* Added `Result<T>.AdjustsAsync` to support asynchronous adjustments.
  • Loading branch information
chullybun authored Aug 1, 2024
1 parent 1ff751d commit f8dfa4c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v3.23.4
- *Fixed:* Added `Result<T>.AdjustsAsync` to support asynchronous adjustments.

## v3.23.3
- *Fixed:* Added `Result<T>.Adjusts` as wrapper for `ObjectExtensions.Adjust` to simplify support and resolve issue where the compiler sees the adjustment otherwise as a implicit cast resulting in an errant outcome.

Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.23.3</Version>
<Version>3.23.4</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
41 changes: 41 additions & 0 deletions src/CoreEx/Results/ResultsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,47 @@ public static Result<T> Adjusts<T>(this Result<T> result, Action<T> adjuster)
return result;
}

/// <summary>
/// Enables adjustment (changes) to a <see cref="Result{T}.Value"/> via an <paramref name="adjuster"/> action where the <paramref name="result"/> is <see cref="Result{T}.IsSuccess"/>
/// </summary>
/// <typeparam name="T">The <see cref="Result{T}.Value"/> <see cref="Type"/>.</typeparam>
/// <param name="result">The <see cref="Result{T}"/>.</param>
/// <param name="adjuster">The adjusting action (invoked only where the underlying <see cref="Result{T}.Value"/> is not <c>null</c>).</param>
/// <returns>The resulting <see cref="Result{T}"/>.</returns>
public static async Task<Result<T>> Adjusts<T>(this Task<Result<T>> result, Action<T> adjuster)
{
var r = await result.ConfigureAwait(false);
return r.Adjusts(adjuster);
}

/// <summary>
/// Enables adjustment (changes) to a <see cref="Result{T}.Value"/> via an <paramref name="adjuster"/> action where the <paramref name="result"/> is <see cref="Result{T}.IsSuccess"/>
/// </summary>
/// <typeparam name="T">The <see cref="Result{T}.Value"/> <see cref="Type"/>.</typeparam>
/// <param name="result">The <see cref="Result{T}"/>.</param>
/// <param name="adjuster">The adjusting action (invoked only where the underlying <see cref="Result{T}.Value"/> is not <c>null</c>).</param>
/// <returns>The resulting <see cref="Result{T}"/>.</returns>
public static async Task<Result<T>> AdjustsAsync<T>(this Result<T> result, Func<T, Task> adjuster)
{
if (result.IsSuccess && result.Value is not null)
await adjuster(result.Value).ConfigureAwait(false);

return result;
}

/// <summary>
/// Enables adjustment (changes) to a <see cref="Result{T}.Value"/> via an <paramref name="adjuster"/> action where the <paramref name="result"/> is <see cref="Result{T}.IsSuccess"/>
/// </summary>
/// <typeparam name="T">The <see cref="Result{T}.Value"/> <see cref="Type"/>.</typeparam>
/// <param name="result">The <see cref="Result{T}"/>.</param>
/// <param name="adjuster">The adjusting action (invoked only where the underlying <see cref="Result{T}.Value"/> is not <c>null</c>).</param>
/// <returns>The resulting <see cref="Result{T}"/>.</returns>
public static async Task<Result<T>> AdjustsAsync<T>(this Task<Result<T>> result, Func<T, Task> adjuster)
{
var r = await result.ConfigureAwait(false);
return await r.AdjustsAsync(adjuster).ConfigureAwait(false);
}

/// <summary>
/// Checks whether the user has the required <paramref name="permission"/> (see <see cref="ExecutionContext.UserIsAuthorized(string)"/>).
/// </summary>
Expand Down
58 changes: 58 additions & 0 deletions tests/CoreEx.Test/Framework/Results/ResultTTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,64 @@ public void Adjusts()
Assert.That(r2.IsSuccess, Is.False);
}

[Test]
public async Task AdjustsAsync()
{
var r = Result<Person>.Ok(new Person());
var r2 = await r.AdjustsAsync(async v =>
{
await Task.CompletedTask;
v.Id = 2;
});

Assert.Multiple(() =>
{
Assert.That(r2.IsSuccess, Is.True);
Assert.That(r2.Value.Id, Is.EqualTo(2));
});
}

[Test]
public async Task Adjusts2Async()
{
var r = Result.GoAsync(async () =>
{
await Task.CompletedTask;
return new Person();
});

var r2 = await r.AdjustsAsync(async v =>
{
await Task.CompletedTask;
v.Id = 2;
});

Assert.Multiple(() =>
{
Assert.That(r2.IsSuccess, Is.True);
Assert.That(r2.Value.Id, Is.EqualTo(2));
});
}

[Test]
public async Task Adjusts2AsyncTightLoop()
{
for (int i = 0; i < 10000; i++)
{
var r = Result.GoAsync(async () =>
{
await Task.CompletedTask;
return new Person();
});

var r2 = await r.AdjustsAsync(async v =>
{
await Task.CompletedTask;
v.Id = 2;
});
}
}

public class Person
{
public int Id { get; set; }
Expand Down

0 comments on commit f8dfa4c

Please sign in to comment.