Skip to content

Commit

Permalink
feat: add async IfNone extension for Maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Aug 28, 2024
1 parent f832baf commit b2bb7cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 17 additions & 1 deletion Vonage.Test/Common/Monads/MaybeExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Threading.Tasks;
#region
using System.Threading.Tasks;
using FluentAssertions;
using Vonage.Common.Monads;
using Vonage.Test.Common.Extensions;
using Xunit;
#endregion

namespace Vonage.Test.Common.Monads;

Expand Down Expand Up @@ -77,4 +79,18 @@ public async Task Match_ShouldReturnSome_GivenValueIsSome()
var result = await MaybeBehaviors.CreateSomeAsync(10).Match(_ => "some", () => "none");
result.Should().Be("some");
}

[Fact]
public async Task IfNone_Value_ShouldReturnSpecifiedValue_GivenValueIsNone()
{
var result = await MaybeBehaviors.CreateNoneAsync<int>().IfNone(5);
result.Should().Be(5);
}

[Fact]
public async Task IfNone_Value_ShouldReturnValue_GivenValueIsSome()
{
var result = await MaybeBehaviors.CreateSomeAsync(10).IfNone(5);
result.Should().Be(10);
}
}
24 changes: 19 additions & 5 deletions Vonage/Common/Monads/MaybeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#region
using System;
using System.Threading.Tasks;
#endregion

namespace Vonage.Common.Monads;

Expand All @@ -17,7 +19,7 @@ public static Maybe<string> From(string value) =>
string.IsNullOrWhiteSpace(value)
? Maybe<string>.None
: Maybe<string>.Some(value);

/// <summary>
/// Monadic bind operation.
/// </summary>
Expand All @@ -29,7 +31,7 @@ public static async Task<Maybe<TDestination>> Bind<TSource, TDestination>(this T
var result = await task.ConfigureAwait(false);
return result.Bind(bind);
}

/// <summary>
/// Monadic bind operation.
/// </summary>
Expand All @@ -41,7 +43,7 @@ public static async Task<Maybe<TDestination>> BindAsync<TSource, TDestination>(t
var result = await task.ConfigureAwait(false);
return await result.BindAsync(bind).ConfigureAwait(false);
}

/// <summary>
/// Projects from one value to another.
/// </summary>
Expand All @@ -55,7 +57,7 @@ public static async Task<Maybe<TDestination>> Map<TSource, TDestination>(this Ta
var result = await task.ConfigureAwait(false);
return result.Map(map);
}

/// <summary>
/// Projects from one value to another.
/// </summary>
Expand All @@ -69,7 +71,7 @@ public static async Task<Maybe<TDestination>> MapAsync<TSource, TDestination>(th
var result = await task.ConfigureAwait(false);
return await result.MapAsync(map).ConfigureAwait(false);
}

/// <summary>
/// Match the two states of the Maybe and return a non-null TDestination.
/// </summary>
Expand All @@ -85,4 +87,16 @@ public static async Task<TDestination> Match<TSource, TDestination>(this Task<Ma
var result = await task.ConfigureAwait(false);
return result.Match(some, none);
}

/// <summary>
/// Returns the specified value if Maybe is in the None state, the Some value otherwise.
/// </summary>
/// <param name="task">Initial Maybe.</param>
/// <param name="noneValue">The value to return if in None state.</param>
/// <returns>A value.</returns>
public static async Task<TSource> IfNone<TSource>(this Task<Maybe<TSource>> task, TSource noneValue)
{
var result = await task.ConfigureAwait(false);
return result.IfNone(noneValue);
}
}

0 comments on commit b2bb7cc

Please sign in to comment.