Skip to content

Commit 85a76e6

Browse files
committed
Removed non-generic Result (use Result<Unit> instead)
1 parent 798ca85 commit 85a76e6

File tree

15 files changed

+62
-157
lines changed

15 files changed

+62
-157
lines changed

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RFunctionTests/DisposeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void RegisteringFunctionOnDisposedRFunctionsThrowsException()
1919
Should.Throw<ObjectDisposedException>(() =>
2020
_ = rFunctions.RegisterFunc(
2121
"id".ToFlowType(),
22-
(string _) => Succeed.WithoutValue.ToTask()
22+
(string _) => Succeed.WithUnit.ToTask()
2323
)
2424
);
2525
}

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RFunctionTests/DuplicateRegistrationTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public void ReRegistrationRActionSucceedsWhenArgumentsAreIdentical()
4949
using var rFunctions = new FunctionsRegistry(new InMemoryFunctionStore());
5050
_ = rFunctions.RegisterFunc(
5151
"SomeFunctionType",
52-
Task<Result>(string _) => Succeed.WithoutValue.ToTask()
52+
Task<Result<Unit>>(string _) => Succeed.WithUnit.ToTask()
5353
);
5454

5555
_ = rFunctions.RegisterFunc(
5656
"SomeFunctionType",
57-
Task<Result> (string _) => Succeed.WithoutValue.ToTask()
57+
Task<Result<Unit>> (string _) => Succeed.WithUnit.ToTask()
5858
);
5959
}
6060

@@ -64,13 +64,13 @@ public void ReRegistrationRActionWithIncompatibleTypeThrowsException()
6464
using var rFunctions = new FunctionsRegistry(new InMemoryFunctionStore());
6565
_ = rFunctions.RegisterFunc(
6666
"SomeFunctionType",
67-
Task<Result>(string _) => Succeed.WithoutValue.ToTask()
67+
Task<Result<Unit>>(string _) => Succeed.WithUnit.ToTask()
6868
);
6969

7070
Should.Throw<InvalidCastException>(() =>
7171
_ = rFunctions.RegisterFunc(
7272
"SomeFunctionType",
73-
Task<Result>(int _) => Succeed.WithoutValue.ToTask()
73+
Task<Result<Unit>>(int _) => Succeed.WithUnit.ToTask()
7474
)
7575
);
7676
}
@@ -81,13 +81,13 @@ public void ReRegistrationFromFuncToActionThrowsArgumentException()
8181
using var rFunctions = new FunctionsRegistry(new InMemoryFunctionStore());
8282
_ = rFunctions.RegisterFunc(
8383
"SomeFunctionType",
84-
Task<Result>(string _) => Succeed.WithoutValue.ToTask()
84+
Task<Result<Unit>>(string _) => Succeed.WithUnit.ToTask()
8585
);
8686

8787
Should.Throw<InvalidCastException>(() =>
8888
_ = rFunctions.RegisterAction(
8989
"SomeFunctionType",
90-
Task (int _) => Succeed.WithoutValue.ToTask()
90+
Task (int _) => Succeed.WithUnit.ToTask()
9191
)
9292
);
9393
}

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RFunctionTests/InMemorySunshineTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ await ExecuteFunc((functionsRegistry, callback)
4848
=> functionsRegistry.RegisterFunc(
4949
flowType: "",
5050
Task<Result<string>>(string param)
51-
=> Result.SucceedWithValue(callback(param).Result).ToTask()
51+
=> Succeed.WithValue(callback(param).Result).ToTask()
5252
)
5353
);
5454
}
@@ -61,7 +61,7 @@ await ExecuteFunc((rFunctions, callback)
6161
=> rFunctions.RegisterFunc(
6262
flowType: "",
6363
Task<Result<string>> (string param, Workflow _)
64-
=> Result.SucceedWithValue(callback(param).Result).ToTask()
64+
=> Succeed.WithValue(callback(param).Result).ToTask()
6565
)
6666
);
6767
}
@@ -150,7 +150,7 @@ public async Task AsyncActionWithResultSunshineTest()
150150
await ExecuteAction((rFunctions, callback)
151151
=> rFunctions.RegisterAction(
152152
flowType: "",
153-
Task<Result> (string param) => { callback(param); return Task.FromResult(Result.Succeed); }
153+
Task<Result<Unit>> (string param) => { callback(param); return Succeed.WithUnit.ToTask(); }
154154
)
155155
);
156156
}
@@ -162,7 +162,7 @@ public async Task AsyncActionWithWorkflowAndResultSunshineTest()
162162
await ExecuteAction((rFunctions, callback)
163163
=> rFunctions.RegisterAction(
164164
flowType: "",
165-
Task<Result> (string param, Workflow workflow) => { callback(param); return Task.FromResult(Result.Succeed); }
165+
Task<Result<Unit>> (string param, Workflow workflow) => { callback(param); return Succeed.WithUnit.ToTask(); }
166166
)
167167
);
168168
}

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/RFunctionTests/RegisterWithExplicitReturnTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public async Task ActionWithExplicitReturnIsInvokedSuccessfully()
6161
{
6262
await Task.CompletedTask;
6363
syncedParam.Value = param;
64-
return Succeed.WithoutValue;
64+
return Succeed.WithUnit;
6565
})
6666
.Invoke;
6767

@@ -81,7 +81,7 @@ public async Task ActionWithStateAndExplicitReturnIsInvokedSuccessfully()
8181
{
8282
await Task.CompletedTask;
8383
syncedParam.Value = param;
84-
return Succeed.WithoutValue;
84+
return Succeed.WithUnit;
8585
})
8686
.Invoke;
8787

Core/Cleipnir.ResilientFunctions.Tests/InMemoryTests/ShutdownCoordinationTests/RFunctionsShutdownTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public async Task RFunctionsShutdownTaskThrowsExceptionWhenWaitThresholdIsExceed
7575
(string _) =>
7676
{
7777
insideRFuncFlag.Raise();
78-
return NeverCompletingTask.OfType<Result>();
78+
return NeverCompletingTask.OfType<Result<Unit>>();
7979
}
8080
).Invoke;
8181

@@ -163,7 +163,7 @@ public async Task RFunctionsShutdownTaskOnlyCompletesAfterPostponedWatchDogsRFun
163163
{
164164
insideRFuncFlag.Raise();
165165
await completeRFuncFlag.WaitForRaised();
166-
return Succeed.WithoutValue;
166+
return Succeed.WithUnit;
167167
}
168168
);
169169

@@ -216,10 +216,10 @@ public async Task RFunctionsShutdownTaskCompletesWhenInvokedRFuncIsPostponed()
216216

217217
var registration = functionsRegistry.RegisterAction(
218218
flowType,
219-
Task<Result> (string _) =>
219+
Task<Result<Unit>> (string _) =>
220220
{
221221
counter.Increment();
222-
return Postpone.For(500).ToResult().ToTask();
222+
return Postpone.For(500).ToUnitResult.ToTask();
223223
}
224224
);
225225
var rAction = registration.Invoke;

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/RFunctionTests/FailedTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private async Task ExceptionThrowingActionWithStateIsNotCompletedByWatchDog(
288288
(string _) =>
289289
{
290290
flag.Raise();
291-
return Succeed.WithoutValue.ToTask();
291+
return Succeed.WithUnit.ToTask();
292292
}
293293
);
294294

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/RFunctionTests/PostponedTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ protected async Task PostponedActionIsCompletedByWatchDog(Task<IFunctionStore> s
164164
)
165165
);
166166
var rAction = functionsRegistry.RegisterAction(
167-
flowType,
168-
(string _) => Postpone.Until(DateTime.UtcNow.AddMilliseconds(1_000)).ToResult().ToTask()
167+
flowType,
168+
Task<Result<Unit>> (string _) => Postpone.Until(DateTime.UtcNow.AddMilliseconds(1_000)).ToUnitResult.ToTask()
169169
).Invoke;
170170

171171
await Should.ThrowAsync<InvocationPostponedException>(() => rAction(flowInstance.Value, param));
@@ -211,7 +211,7 @@ protected async Task PostponedActionWithStateIsCompletedByWatchDog(Task<IFunctio
211211
);
212212
var rAction = functionsRegistry.RegisterAction(
213213
flowType,
214-
(string _, Workflow _) => Postpone.For(1_000).ToResult().ToTask()
214+
Task<Result<Unit>> (string _, Workflow _) => Postpone.For(1_000).ToUnitResult.ToTask()
215215
).Invoke;
216216

217217
await Should.ThrowAsync<InvocationPostponedException>(() =>
@@ -270,7 +270,7 @@ protected async Task PostponedActionIsCompletedByWatchDogAfterCrash(Task<IFuncti
270270
)
271271
);
272272
var rFunc = functionsRegistry
273-
.RegisterAction(functionId.Type, (string _) => Postpone.For(1_000).ToResult().ToTask())
273+
.RegisterAction(functionId.Type, Task<Result<Unit>> (string _) => Postpone.For(1_000).ToUnitResult.ToTask())
274274
.Invoke;
275275

276276
var instanceId = functionId.Instance.ToString();

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/RFunctionTests/ScheduledInvocationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected async Task ScheduledFunctionIsInvokedAfterActionWithStateHasBeenPersis
7373
using var functionsRegistry = new FunctionsRegistry(store, new Settings(unhandledExceptionCatcher.Catch));
7474
var reg = functionsRegistry.RegisterAction(
7575
flowType,
76-
(string _) => NeverCompletingTask.OfType<Result>()
76+
(string _) => NeverCompletingTask.OfType<Result<Unit>>()
7777
);
7878
var schedule = reg.Schedule;
7979

@@ -99,7 +99,7 @@ protected async Task ScheduledFunctionIsInvokedAfterActionStateHasBeenPersisted(
9999
using var functionsRegistry = new FunctionsRegistry(store, new Settings(unhandledExceptionCatcher.Catch));
100100
var reg = functionsRegistry.RegisterFunc(
101101
flowType,
102-
(string _) => NeverCompletingTask.OfType<Result>()
102+
(string _) => NeverCompletingTask.OfType<Result<Unit>>()
103103
);
104104
var schedule = reg.Schedule;
105105

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/RFunctionTests/SunshineTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public async Task SunshineScenarioParamlessWithResultReturnType(Task<IFunctionSt
101101
inner: () =>
102102
{
103103
flag.Raise();
104-
return Result.Succeed.ToTask();
104+
return Succeed.WithUnit.ToTask();
105105
});
106106
var invoke = reg.Invoke;
107107

@@ -357,11 +357,11 @@ protected async Task InstancesCanBeFetched(Task<IFunctionStore> storeTask)
357357

358358
var registration = functionsRegistry.RegisterAction(
359359
flowType,
360-
inner: Task<Result> (bool postpone) =>
360+
inner: Task<Result<Unit>> (bool postpone) =>
361361
Task.FromResult(
362362
postpone
363363
? Postpone.For(TimeSpan.FromHours(1))
364-
: Succeed.WithoutValue
364+
: Succeed.WithUnit
365365
)
366366
);
367367

@@ -546,11 +546,11 @@ public async Task FlowIdCanBeExtractedFromAmbientStateAfterSuspension(Task<IFunc
546546
if (!postponed)
547547
{
548548
postponed = true;
549-
return Postpone.For(100).ToResult().ToTask();
549+
return Postpone.For(100).ToUnitResult.ToTask();
550550
}
551551

552552
storedId = CurrentFlow.StoredId;
553-
return Result.Succeed.ToTask();
553+
return Succeed.WithUnit.ToTask();
554554
}
555555
);
556556
await reg.Schedule(instance);

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/RFunctionTests/SuspensionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected async Task ActionCanBeSuspended(Task<IFunctionStore> storeTask)
3333

3434
var rAction = functionsRegistry.RegisterAction(
3535
flowType,
36-
Task<Result> (string _) => throw new SuspendInvocationException()
36+
Task<Result<Unit>> (string _) => throw new SuspendInvocationException()
3737
);
3838

3939
await Should.ThrowAsync<InvocationSuspendedException>(
@@ -112,7 +112,7 @@ protected async Task DetectionOfEligibleSuspendedFunctionSucceedsAfterEventAdded
112112
}
113113

114114
invocations++;
115-
return Result.SucceedWithValue("completed").ToTask();
115+
return Succeed.WithValue("completed").ToTask();
116116
});
117117

118118
await Should.ThrowAsync<InvocationSuspendedException>(
@@ -155,7 +155,7 @@ protected async Task PostponedFunctionIsResumedAfterEventIsAppendedToMessages(Ta
155155
}
156156

157157
invocations++;
158-
return Result.SucceedWithValue("completed").ToTask();
158+
return Succeed.WithValue("completed").ToTask();
159159
});
160160

161161
await Should.ThrowAsync<InvocationPostponedException>(
@@ -192,7 +192,7 @@ protected async Task EligibleSuspendedFunctionIsPickedUpByWatchdog(Task<IFunctio
192192
flowType,
193193
Task<Result<string>> (_) =>
194194
{
195-
if (flag.IsRaised) return Result.SucceedWithValue("success").ToTask();
195+
if (flag.IsRaised) return Succeed.WithValue("success").ToTask();
196196
flag.Raise();
197197
return Suspend.Invocation.ToResult<string>().ToTask();
198198
});

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/WatchDogsTests/WatchdogCompoundTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public async Task ActionCompoundTest(Task<IFunctionStore> storeTask)
319319
inner: (Param p) =>
320320
{
321321
Task.Run(() => paramTcs.TrySetResult(p));
322-
return Postpone.For(100).ToResult().ToTask();
322+
return Postpone.For(100).ToUnitResult.ToTask();
323323
}
324324
);
325325

@@ -435,7 +435,7 @@ public async Task ActionWithStateCompoundTest(Task<IFunctionStore> storeTask)
435435
_ = functionsRegistry
436436
.RegisterAction(
437437
flowType,
438-
async Task<Result> (Param p, Workflow workflow) =>
438+
async Task<Result<Unit>> (Param p, Workflow workflow) =>
439439
{
440440
_ = Task.Run(() => paramTcs.TrySetResult(p));
441441
var state = await workflow.States.CreateOrGet<ListState>("Scraps");

Core/Cleipnir.ResilientFunctions/Domain/Result.cs

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,19 @@
11
using System;
2-
using Cleipnir.ResilientFunctions.CoreRuntime;
3-
using Cleipnir.ResilientFunctions.Domain.Exceptions;
42
using Cleipnir.ResilientFunctions.Domain.Exceptions.Commands;
53
using Cleipnir.ResilientFunctions.Helpers;
64

75
namespace Cleipnir.ResilientFunctions.Domain;
86

9-
public class Result
7+
public static class Result
108
{
11-
public Outcome Outcome { get; }
12-
public Postpone? Postpone { get; }
13-
public FatalWorkflowException? Fail { get; }
14-
public Suspend? Suspend { get; }
15-
16-
public Result() : this(succeed: true, postpone: null, fail: null, suspend: null) {}
17-
public Result(Postpone postpone) : this(succeed: false, postpone, fail: null, suspend: null) {}
18-
public Result(FatalWorkflowException exception) : this(succeed: false, postpone: null, fail: exception, suspend: null) {}
19-
public Result(Suspend suspend) : this(succeed: false, postpone: null, fail: null, suspend) {}
20-
21-
private Result(bool succeed, Postpone? postpone, FatalWorkflowException? fail, Suspend? suspend)
22-
{
23-
if (succeed)
24-
Outcome = Outcome.Succeed;
25-
else if (postpone != null)
26-
Outcome = Outcome.Postpone;
27-
else if (fail != null)
28-
Outcome = Outcome.Fail;
29-
else
30-
Outcome = Outcome.Suspend;
31-
32-
Postpone = postpone;
33-
Fail = fail;
34-
Postpone = postpone;
35-
Suspend = suspend;
36-
}
37-
38-
public static Result Succeed { get; } = new Result();
39-
public static implicit operator Result(Fail fail) => new Result(fail.Exception);
40-
public static implicit operator Result(Postpone postpone) => new Result(postpone);
41-
public static implicit operator Result(Suspend suspend) => new Result(suspend);
429
public static Result<T> SucceedWithValue<T>(T value) => new(value);
43-
44-
public Result<Unit> ToUnit() => Outcome switch
45-
{
46-
Outcome.Succeed => new Result<Unit>(Unit.Instance),
47-
Outcome.Postpone => new Result<Unit>(Postpone!),
48-
Outcome.Fail => new Result<Unit>(Fail!),
49-
Outcome.Suspend => new Result<Unit>(Suspend!),
50-
_ => throw new ArgumentOutOfRangeException()
51-
};
10+
public static Result<Unit> SucceedWithUnit { get; } = new Result<Unit>(Unit.Instance);
5211
}
5312

5413
public static class Succeed
5514
{
56-
public static Result WithoutValue { get; } = new Result();
5715
public static Result<T> WithValue<T>(T value) => new Result<T>(value);
16+
public static Result<Unit> WithUnit { get; } = new Result<Unit>(Unit.Instance);
5817
}
5918

6019
public class Fail
@@ -75,7 +34,6 @@ public Fail(Exception exception) =>
7534
public static Fail WithException(Exception exception) => new Fail(exception);
7635
public static Fail WithException(FatalWorkflowException exception, FlowId flowId) => new Fail(flowId, exception);
7736

78-
public Result ToResult() => new(Exception);
7937
public Result<T> ToResult<T>() => new(Exception);
8038
}
8139

@@ -85,8 +43,8 @@ public class Postpone
8543

8644
private Postpone(DateTime dateTime) => DateTime = dateTime;
8745

88-
public Result ToResult() => new(this);
8946
public Result<T> ToResult<T>() => new(this);
47+
public Result<Unit> ToUnitResult => ToResult<Unit>();
9048

9149
public static Postpone Until(DateTime dateTime) => new(dateTime.ToUniversalTime());
9250

0 commit comments

Comments
 (0)