Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<!--GLOBAL-->
<PropertyGroup>
<Version>12.1.1</Version>
<PackageVersion>12.1.1</PackageVersion>
<AssemblyVersion>12.1.1</AssemblyVersion>
<Version>12.2.0</Version>
<PackageVersion>12.2.0</PackageVersion>
<AssemblyVersion>12.2.0</AssemblyVersion>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<LangVersion>13</LangVersion>
<Nullable>enable</Nullable>
Expand Down
28 changes: 28 additions & 0 deletions OnixLabs.Core.UnitTests/ObjectExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,32 @@ public async Task ToSuccessAsyncShouldProduceTheExpectedResult()
Success<string> success = Assert.IsType<Success<string>>(result);
Assert.Equal(expected, success.Value);
}

[Fact(DisplayName = "TryGetNonNull should produce the expected result (true)")]
public void TryGetNotNullShouldProduceExpectedResultTrue()
{
// Given
const string? value = "Hello, World!";

// When
bool result = value.TryGetNonNull(out string output);

// Then
Assert.True(result);
Assert.NotNull(output);
}

[Fact(DisplayName = "TryGetNonNull should produce the expected result (false)")]
public void TryGetNotNullShouldProduceExpectedResultFalse()
{
// Given
const string? value = null;

// When
bool result = value.TryGetNonNull(out string? output);

// Then
Assert.False(result);
Assert.Null(output);
}
}
20 changes: 20 additions & 0 deletions OnixLabs.Core/Extensions.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -226,4 +227,23 @@ public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, Ca
/// <returns>Returns a <see cref="Success{T}"/> representation of the current <see cref="Object"/>.</returns>
public static async Task<Success<T>> ToSuccessAsync<T>(this Task<T> value, CancellationToken token = default) =>
Result<T>.Success(await value.WaitAsync(token).ConfigureAwait(false));

/// <summary>
/// Attempts to extract a non-null value from the current nullable <see cref="Object"/>.
/// </summary>
/// <param name="value">The current nullable <see cref="Object"/> to test for nullability.</param>
/// <param name="result">The non-null value when this method returns <see langword="true"/>; otherwise, <see langword="null"/>.</param>
/// <typeparam name="T">The underlying type of the value.</typeparam>
/// <returns>Returns <see langword="true"/> if the current nullable <see cref="Object"/> is not null; otherwise, <see langword="false"/>.</returns>
public static bool TryGetNonNull<T>(this T? value, [NotNullWhen(true)] out T result)
{
if (value is null)
{
result = default!;
return false;
}

result = value;
return true;
}
}