From 827c34d2c6a7fd5a29124cff3f9f012f0abe97a9 Mon Sep 17 00:00:00 2001 From: Matthew Layton Date: Wed, 5 Jun 2024 15:40:47 +0100 Subject: [PATCH] Added `Optional.Of` to handle nullable structs. --- OnixLabs.Core.UnitTests/OptionalTests.cs | 32 +++++++++++++++++++ OnixLabs.Core/OnixLabs.Core.csproj | 2 +- OnixLabs.Core/Optional.cs | 17 +++++++++- OnixLabs.Numerics/OnixLabs.Numerics.csproj | 2 +- .../OnixLabs.Security.Cryptography.csproj | 2 +- 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/OnixLabs.Core.UnitTests/OptionalTests.cs b/OnixLabs.Core.UnitTests/OptionalTests.cs index b91a3ed..6621a34 100644 --- a/OnixLabs.Core.UnitTests/OptionalTests.cs +++ b/OnixLabs.Core.UnitTests/OptionalTests.cs @@ -87,6 +87,38 @@ public void OptionalOfShouldProduceExpectedResultForExplicitNonDefaultValues() Assert.Equal("abc", text); } + [Fact(DisplayName = "Optional.Of should produce the expected result for null nullable struct values.")] + public void OptionalOfShouldProduceExpectedResultForNullNullableStructValues() + { + // Given / When + Optional number = Optional.Of((int?)null); + Optional identifier = Optional.Of((Guid?)default); + + // Then + Assert.False(number.HasValue); + Assert.IsType>(number); + + Assert.False(identifier.HasValue); + Assert.IsType>(identifier); + } + + [Fact(DisplayName = "Optional.Of should produce the expected result for non-null nullable struct values.")] + public void OptionalOfShouldProduceExpectedResultForNonNullNullableStructValues() + { + // Given / When + Optional number = Optional.Of((int?)123); + Optional identifier = Optional.Of((Guid?)Guid.Empty); + + // Then + Assert.True(number.HasValue); + Assert.IsType>(number); + Assert.Equal(123, number); + + Assert.True(identifier.HasValue); + Assert.IsType>(identifier); + Assert.Equal(Guid.Empty, identifier); + } + [Fact(DisplayName = "Optional.Some should produce the expected result.")] public void OptionalSomeShouldProduceExpectedResult() { diff --git a/OnixLabs.Core/OnixLabs.Core.csproj b/OnixLabs.Core/OnixLabs.Core.csproj index 282e8a5..121cbbe 100644 --- a/OnixLabs.Core/OnixLabs.Core.csproj +++ b/OnixLabs.Core/OnixLabs.Core.csproj @@ -11,7 +11,7 @@ en Copyright © ONIXLabs 2020 https://github.com/onix-labs/onixlabs-dotnet - 8.5.0 + 8.6.0 diff --git a/OnixLabs.Core/Optional.cs b/OnixLabs.Core/Optional.cs index 2a34952..58d132b 100644 --- a/OnixLabs.Core/Optional.cs +++ b/OnixLabs.Core/Optional.cs @@ -53,7 +53,22 @@ internal Optional() /// Returns a new instance of the class, where the underlying value is present if /// the specified value is not ; otherwise, the underlying value is . /// - public static Optional Of(T? value) => value is null || EqualityComparer.Default.Equals(value, default) ? None : Some(value); + public static Optional Of(T? value) => value is not null && !EqualityComparer.Default.Equals(value, default) + ? Some(value) + : None; + + /// + /// Creates a new instance of the class, where the underlying value is present if + /// the specified value is not ; otherwise, the underlying value is . + /// + /// The underlying optional value. + /// + /// Returns a new instance of the class, where the underlying value is present if + /// the specified value is not ; otherwise, the underlying value is . + /// + public static Optional Of(TStruct? value) where TStruct : struct => value.HasValue + ? Optional.Some(value.Value) + : Optional.None; /// /// Creates a new instance of the class, where the underlying value is present if diff --git a/OnixLabs.Numerics/OnixLabs.Numerics.csproj b/OnixLabs.Numerics/OnixLabs.Numerics.csproj index 0284c71..543ac47 100644 --- a/OnixLabs.Numerics/OnixLabs.Numerics.csproj +++ b/OnixLabs.Numerics/OnixLabs.Numerics.csproj @@ -10,7 +10,7 @@ true Copyright © ONIXLabs 2020 https://github.com/onix-labs/onixlabs-dotnet - 8.5.0 + 8.6.0 12 diff --git a/OnixLabs.Security.Cryptography/OnixLabs.Security.Cryptography.csproj b/OnixLabs.Security.Cryptography/OnixLabs.Security.Cryptography.csproj index ccb9af2..83ef887 100644 --- a/OnixLabs.Security.Cryptography/OnixLabs.Security.Cryptography.csproj +++ b/OnixLabs.Security.Cryptography/OnixLabs.Security.Cryptography.csproj @@ -10,7 +10,7 @@ true Copyright © ONIXLabs 2020 https://github.com/onix-labs/onixlabs-dotnet - 8.5.0 + 8.6.0 12