Skip to content

Commit

Permalink
v3.23.3 (#112)
Browse files Browse the repository at this point in the history
- *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.
  • Loading branch information
chullybun authored Aug 1, 2024
1 parent c374a00 commit 1ff751d
Show file tree
Hide file tree
Showing 4 changed files with 46 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.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.

## v3.23.2
- *Fixed:* `DatabaseExtendedExtensions.DeleteWithResultAsync` corrected to return a `Task<Result>`.`

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.2</Version>
<Version>3.23.3</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
15 changes: 15 additions & 0 deletions src/CoreEx/Results/ResultsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public static async Task<Result<U>> AsResult<T, U>(this Task<Result<T>> result)
public static Result<T> ThrowIfNull<T>(this Result<T> result, string? name = null)
=> result.IsSuccess && result.Value == null ? throw new ArgumentNullException(name ?? Validation.Validation.ValueNameDefault) : 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 Result<T> Adjusts<T>(this Result<T> result, Action<T> adjuster)
{
if (result.IsSuccess)
result.Value.Adjust(adjuster);

return result;
}

/// <summary>
/// Checks whether the user has the required <paramref name="permission"/> (see <see cref="ExecutionContext.UserIsAuthorized(string)"/>).
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions tests/CoreEx.Test/Framework/Results/ResultTTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,32 @@ public void Failure_Value()
var ir = (IResult)Result<int>.Fail("On no!");
Assert.Throws<BusinessException>(() => _ = ir.Value);
}

[Test]
public void Adjusts()
{
var r = Result<Person>.Ok(new Person());
Assert.Multiple(() =>
{
Assert.That(r.IsSuccess, Is.True);
Assert.That(r.Value.Id, Is.EqualTo(0));
});

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

r = Result<Person>.Fail(new BusinessException());
r2 = r.Adjusts(v => v.Id = 2);
Assert.That(r2.IsSuccess, Is.False);
}

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

0 comments on commit 1ff751d

Please sign in to comment.