Skip to content

Commit

Permalink
Merge pull request #421 from ProphetLamb/inline_payload_retrieval
Browse files Browse the repository at this point in the history
Inline payload retrieval
  • Loading branch information
vkhorikov authored Jul 11, 2022
2 parents dc277d5 + aa959c2 commit c0555ec
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 3 deletions.
44 changes: 44 additions & 0 deletions CSharpFunctionalExtensions.Tests/MaybeTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,50 @@ public void Maybe_None_doesnt_throw_on_Deconstruct()
act.Should().NotThrow();
}

[Fact]
public void TryGetValue_returns_false_if_source_is_empty()
{
var maybe = Maybe<int>.None;

bool result = maybe.TryGetValue(out int value);

result.Should().BeFalse();
value.Should().Be(default);
}

[Fact]
public void TryGetValue_returns_true_if_source_has_value()
{
var maybe = Maybe.From(5);

bool result = maybe.TryGetValue(out int value);

result.Should().BeTrue();
value.Should().Be(5);
}

[Fact]
public void TryGetValue_returns_false_if_source_is_empty_and_out_parameter_is_null()
{
var maybe = Maybe<int?>.None;

bool result = maybe.TryGetValue(out int? value);

result.Should().BeFalse();
value.Should().BeNull();
}

[Fact]
public void TryGetValue_returns_true_if_source_has_value_and_out_parameter_is_null()
{
var maybe = Maybe<int?>.From(5);

bool result = maybe.TryGetValue(out int? value);

result.Should().BeTrue();
value.Should().Be(5);
}

[Fact]
public void Maybe_struct_default_is_none()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;

using FluentAssertions;
using Xunit;

namespace CSharpFunctionalExtensions.Tests.ResultTests.Methods
{
public class TryGetTests
{
public const string ErrorMessage = "Error from result";

[Fact]
public void Simple_result_tryGetError_is_false_Success_value_expected()
{
Result result = Result.Success(ErrorMessage);
result.TryGetError(out string error).Should().BeFalse();
error.Should().BeNull();
}

[Fact]
public void Simple_result_tryGetError_is_true_Failure_value_expected()
{
Result result = Result.Failure(ErrorMessage);
result.TryGetError(out string error).Should().BeTrue();
error.Should().Be(ErrorMessage);
}

[Fact]
public void Generic_result_tryGetError_is_false_Success_value_expected()
{
Result<string> result = Result.Success("Success");
result.TryGetError(out string error).Should().BeFalse();
error.Should().BeNull();
}

[Fact]
public void Generic_result_tryGetError_is_true_Failure_value_expected()
{
Result<string> result = Result.Failure<string>(ErrorMessage);
result.TryGetError(out string error).Should().BeTrue();
error.Should().Be(ErrorMessage);
}

[Fact]
public void Generic_result_tryGetSuccess_is_false_Failure_value_expected()
{
Result<string> result = Result.Failure<string>(ErrorMessage);
result.TryGetValue(out string value).Should().BeFalse();
value.Should().BeNull();
}

[Fact]
public void Generic_result_tryGetSuccess_is_true_Success_value_expected()
{
Result<string> result = Result.Success("Success");
result.TryGetValue(out string value).Should().BeTrue();
value.Should().Be("Success");
}

[Fact]
public void Value_Error_Generic_result_tryGetError_is_false_Success_value_expected()
{
Result<string, string> result = Result.Success<string, string>("Success");
result.TryGetError(out string error).Should().BeFalse();
error.Should().BeNull();
}

[Fact]
public void Value_Error_Generic_result_tryGetError_is_true_Failure_value_expected()
{
Result<string, string> result = Result.Failure<string, string>(ErrorMessage);
result.TryGetError(out string error).Should().BeTrue();
error.Should().Be(ErrorMessage);
}

[Fact]
public void Value_Error_Generic_result_tryGetSuccess_is_false_Failure_value_expected()
{
Result<string, string> result = Result.Failure<string, string>(ErrorMessage);
result.TryGetValue(out string value).Should().BeFalse();
value.Should().BeNull();
}

[Fact]
public void Value_Error_Generic_result_tryGetSuccess_is_true_Success_value_expected()
{
Result<string, string> result = Result.Success<string, string>("Success");
result.TryGetValue(out string value).Should().BeTrue();
value.Should().Be("Success");
}
}
}
4 changes: 2 additions & 2 deletions CSharpFunctionalExtensions/CSharpFunctionalExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<PackageReference Include="Microsoft.Bcl.Async" Version="1.0.168" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net40' OR '$(TargetFramework)'=='net45' OR '$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net472' OR '$(TargetFramework)'=='netstandard2.0'">
<ItemGroup Condition="!$(DefineConstants.Contains('NET5_0_OR_GREATER')) and !$(DefineConstants.Contains('NETCORE'))">
<Compile Remove="Result\Json\Serialization\**" />
<EmbeddedResource Remove="Result\Json\Serialization\**" />
<None Remove="Result\Json\Serialization\**" />
Expand Down
25 changes: 24 additions & 1 deletion CSharpFunctionalExtensions/Maybe/Maybe.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System;
using System.Collections.Generic;
#if NETCORE || NETSTANDARD || NET45_OR_GREATER
using System.Runtime.CompilerServices;
#endif
#if NET_5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace CSharpFunctionalExtensions
{
Expand Down Expand Up @@ -29,6 +35,23 @@ public T GetValueOrDefault(T defaultValue = default)

return _value;
}

/// <summary>
/// Indicates whether the inner value is present and returns the value if it is.
/// </summary>
/// <param name="value">The inner value, if present; otherwise `default`</param>
#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetValue(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out T value)
{
value = _value;
return _isValueSet;
}

/// <summary>
/// Try to use GetValueOrThrow() or GetValueOrDefault() instead for better explicitness.
Expand Down Expand Up @@ -160,7 +183,7 @@ public readonly struct Maybe
/// <summary>
/// Useful in scenarios where you need to determine if a value is Maybe or not
/// </summary>
public interface IMaybe<T>
public interface IMaybe<out T>
{
T Value { get; }
bool HasValue { get; }
Expand Down
176 changes: 176 additions & 0 deletions CSharpFunctionalExtensions/Result/Methods/TryGet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#if NETCORE || NETSTANDARD || NET45_OR_GREATER
using System.Runtime.CompilerServices;
#endif
#if NET_5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace CSharpFunctionalExtensions
{
partial struct Result
{
#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetError(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out string error)
{
error = _error;
return IsFailure;
}
}

partial struct Result<T>
{
#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetValue(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out T value)
{
value = _value;
return IsSuccess;
}

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetError(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out string error)
{
error = _error;
return IsFailure;
}

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetValue(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out T value,
#if NET_5_0_OR_GREATER
[NotNullWhen(false), MaybeNullWhen(true)]
#endif
out string error
)
{
value = _value;
error = _error;
return IsSuccess;
}

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetError(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out string error,
#if NET_5_0_OR_GREATER
[NotNullWhen(false), MaybeNullWhen(true)]
#endif
out T value
)
{
value = _value;
error = _error;
return IsFailure;
}
}

partial struct Result<T, E>
{

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetValue(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out T value)
{
value = _value;
return IsSuccess;
}

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetError(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out E error)
{
error = _error;
return IsFailure;
}

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetValue(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out T value,
#if NET_5_0_OR_GREATER
[NotNullWhen(false), MaybeNullWhen(true)]
#endif
out E error
)
{
value = _value;
error = _error;
return IsSuccess;
}

#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetError(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out E error,
#if NET_5_0_OR_GREATER
[NotNullWhen(false), MaybeNullWhen(true)]
#endif
out T value
)
{
value = _value;
error = _error;
return IsFailure;
}
}

partial struct UnitResult<E>
{
#if NETCORE || NETSTANDARD || NET45_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public bool TryGetError(
#if NET_5_0_OR_GREATER
[NotNullWhen(true), MaybeNullWhen(false)]
#endif
out E error)
{
error = _error;
return IsFailure;
}
}
}

0 comments on commit c0555ec

Please sign in to comment.