Skip to content

Commit ad16124

Browse files
committed
Fail value
1 parent ecf04bb commit ad16124

5 files changed

Lines changed: 89 additions & 35 deletions

File tree

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717
<Product>Managed Code - Communication</Product>
18-
<Version>1.0.1</Version>
19-
<PackageVersion>1.0.1</PackageVersion>
18+
<Version>1.0.2</Version>
19+
<PackageVersion>1.0.2</PackageVersion>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2222
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>

ManagedCode.Communication/BaseResult.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected BaseResult(List<Error<TErrorCode>> errors)
2222
IsSuccess = false;
2323
Errors = errors;
2424
}
25-
25+
2626
public bool IsSuccess { get; }
2727
public bool IsFail => !IsSuccess;
2828
public Error<TErrorCode>? Error => Errors?.FirstOrDefault();
@@ -39,14 +39,29 @@ protected BaseResult(T value) : base(true)
3939
protected BaseResult(bool isSuccess) : base(isSuccess)
4040
{
4141
}
42+
43+
protected BaseResult(bool isSuccess, T value) : base(isSuccess)
44+
{
45+
Value = value;
46+
}
4247

4348
protected BaseResult(Error<TErrorCode> error) : base(error)
4449
{
4550
}
51+
52+
protected BaseResult(Error<TErrorCode> error, T value) : base(error)
53+
{
54+
Value = value;
55+
}
4656

4757
protected BaseResult(List<Error<TErrorCode>> errors) : base(errors)
4858
{
4959
}
60+
61+
protected BaseResult(List<Error<TErrorCode>> errors, T value) : base(errors)
62+
{
63+
Value = value;
64+
}
5065

5166
public T? Value { get; }
5267

ManagedCode.Communication/ManagedCode.Communication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1"/>
19+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
2020
</ItemGroup>
2121

2222
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ManagedCode.Communication;
5+
6+
7+
public sealed partial class Result<T> : BaseResult<T, ErrorCode>
8+
{
9+
public static Result<T> Fail()
10+
{
11+
return new Result<T>(false);
12+
}
13+
14+
public static Result<T> Fail(T result)
15+
{
16+
return new Result<T>(false);
17+
}
18+
19+
public static Result<T> Fail(Error<ErrorCode> error)
20+
{
21+
return new Result<T>(error);
22+
}
23+
24+
public static Result<T> Fail(Error<ErrorCode> error, T value)
25+
{
26+
return new Result<T>(error, value);
27+
}
28+
29+
public static Result<T> Fail(List<Error<ErrorCode>> errors)
30+
{
31+
return new Result<T>(errors);
32+
}
33+
34+
public static Result<T> Fail(List<Error<ErrorCode>> errors, T value)
35+
{
36+
return new Result<T>(errors, value);
37+
}
38+
39+
public static Result<T> Fail(Exception? exception)
40+
{
41+
return new Result<T>(Error<ErrorCode>.FromException(exception));
42+
}
43+
44+
public static Result<T> Fail(Exception? exception, T value)
45+
{
46+
return new Result<T>(Error<ErrorCode>.FromException(exception), value);
47+
}
48+
49+
public Result<T> WithError(Error<ErrorCode> error)
50+
{
51+
if (IsSuccess)
52+
{
53+
throw new InvalidOperationException("Cannot add error to success result");
54+
}
55+
56+
Errors!.Add(error);
57+
return this;
58+
}
59+
}

ManagedCode.Communication/Result/Result.cs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,25 @@ internal Result(T value) : base(value)
6767
internal Result(bool isSuccess) : base(isSuccess)
6868
{
6969
}
70+
internal Result(bool isSuccess, T value) : base(isSuccess, value)
71+
{
72+
}
7073

7174
internal Result(Error<ErrorCode> error) : base(error)
7275
{
7376
}
77+
78+
internal Result(Error<ErrorCode> error, T value) : base(error, value)
79+
{
80+
}
7481

7582
internal Result(List<Error<ErrorCode>> errors) : base(errors)
7683
{
7784
}
85+
86+
internal Result(List<Error<ErrorCode>> errors, T value) : base(errors, value)
87+
{
88+
}
7889

7990
public static implicit operator Result<T>(T value)
8091
{
@@ -100,35 +111,4 @@ public static Result<T> Succeed(T value)
100111
{
101112
return new Result<T>(value);
102113
}
103-
104-
public static Result<T> Fail()
105-
{
106-
return new Result<T>(false);
107-
}
108-
109-
public static Result<T> Fail(Error<ErrorCode> error)
110-
{
111-
return new Result<T>(error);
112-
}
113-
114-
public static Result<T> Fail(List<Error<ErrorCode>> errors)
115-
{
116-
return new Result<T>(errors);
117-
}
118-
119-
public static Result<T> Fail(Exception? exception)
120-
{
121-
return new Result<T>(Error<ErrorCode>.FromException(exception));
122-
}
123-
124-
public Result<T> WithError(Error<ErrorCode> error)
125-
{
126-
if (IsSuccess)
127-
{
128-
throw new InvalidOperationException("Cannot add error to success result");
129-
}
130-
131-
Errors!.Add(error);
132-
return this;
133-
}
134114
}

0 commit comments

Comments
 (0)