Skip to content

Commit

Permalink
Merge pull request #119 from J-Tech-Japan/118-need-optionalvaluefromn…
Browse files Browse the repository at this point in the history
…ullablevalue

Add OptionalValue.FromNullableValue
  • Loading branch information
tomohisa authored Aug 10, 2024
2 parents 39e471c + 4dc9c4e commit 8d1238b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/ResultBoxes/ResultBoxes.Test/ConveyorSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace ResultBoxes.Test;

public class ConveyorSpec(ITestOutputHelper testOutputHelper)
public class ConveyorSpec()
{


[Fact]
public void ConveyorWithNoParam()
{
Expand Down
28 changes: 28 additions & 0 deletions src/ResultBoxes/ResultBoxes.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,31 @@ public async Task Railway3AsyncSpec()
Assert.Equal(12, sut.GetValue());
}
}

public class OptionalValueTests
{
[Fact]
public void OptionalValueEmptyTest()
{
var sut = OptionalValue<string>.Empty;

Assert.False(sut.HasValue);
Assert.Throws<ResultsInvalidOperationException>(() => sut.GetValue());
}

[Fact]
public void OptionalValueCreateSpec()
{
var value = (Guid?) Guid.NewGuid();
var sut = OptionalValue.FromNullableValue(value);
Assert.True(sut.HasValue);
}
[Fact]
public void OptionalValueCreateSpec2()
{
var value = (Guid?) null;
var sut = OptionalValue.FromNullableValue(value);
Assert.False(sut.HasValue);
}

}
19 changes: 11 additions & 8 deletions src/ResultBoxes/ResultBoxes/OptionalValue.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System.Text.Json.Serialization;
namespace ResultBoxes;

public record OptionalValue<TValue>(TValue? Value)
public record OptionalValue<TValue>(TValue? Value, bool HasValue = true)
{
[JsonIgnore]
public bool HasValue => Value is not null;

public static OptionalValue<TValue> Empty => new(default);
public static OptionalValue<TValue> Null => new(default);
public TValue GetValue() => Value ?? throw new ResultsInvalidOperationException("no value");
public static OptionalValue<TValue> Empty => new(default, false);
public static OptionalValue<TValue> Null => new(default, false);
public TValue GetValue() => HasValue && Value is not null ? Value : throw new ResultsInvalidOperationException("no value");
public static implicit operator OptionalValue<TValue>(TValue value) => new(value);
public static OptionalValue<TValue> FromValue(TValue value) => new(value);
}
public static class OptionalValue
{
public static OptionalValue<TValue> FromValue<TValue>(TValue value) where TValue : notnull => new(value);
public static OptionalValue<TValue> FromNullableValue<TValue>(TValue? value) where TValue : notnull => value is null ? OptionalValue<TValue>.Null : new(value);
public static OptionalValue<TValue> FromNullableValue<TValue>(TValue? value) where TValue : struct => value.HasValue ? new(value.Value) : OptionalValue<TValue>.Null;
public static ResultBox<TValue> GetValueResult<TValue>(this OptionalValue<TValue> optional) where TValue : notnull => optional.HasValue && optional.Value is not null ? optional.Value : new ResultsInvalidOperationException("no value");
}
6 changes: 3 additions & 3 deletions src/ResultBoxes/ResultBoxes/ResultBoxes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<RootNamespace>ResultBoxes</RootNamespace>
<LangVersion>preview</LangVersion>
<PackageId>ResultBoxes</PackageId>
<Version>0.3.21</Version>
<Version>0.3.22</Version>
<Authors>J-Tech Group</Authors>
<Company>J-Tech-Japan</Company>
<PackageDescription>ResultBoxes - C# Results Library that focus on Railway Programming.</PackageDescription>
<RepositoryUrl>https://github.com/J-Tech-Japan/ResultBoxes</RepositoryUrl>
<PackageVersion>0.3.21</PackageVersion>
<Description>CheckNullWrapTry - CheckNull exception go through exceptionMapper</Description>
<PackageVersion>0.3.22</PackageVersion>
<Description>Adding OptionalValue.FromNullableValue</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/ConsoleApp2Optional/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static ResultBox<OptionalValue<string>> ConvertStringToHalfLength(string
{
0 => new ApplicationException("Input string is empty"), // Exception
1 => OptionalValue<string>.Empty, // Not error but Empty
_ => OptionalValue<string>.FromValue(input[..^(input.Length / 2)]) // has value
_ => OptionalValue.FromValue(input[..^(input.Length / 2)]) // has value
};
private static void Main(string[] args)
{
Expand Down

0 comments on commit 8d1238b

Please sign in to comment.