Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Async #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
49 changes: 48 additions & 1 deletion Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ string GetContent(bool isStruct, int i) {

sb.Append(@$"using System;
using static OneOf.Functions;
#if NETSTANDARD2_0 || NET40_OR_GREATER
using System.Threading.Tasks;
#endif

namespace OneOf
{{
Expand Down Expand Up @@ -100,6 +103,19 @@ public void Switch({RangeJoined(", ", e => $"Action<T{e}> f{e}")})
throw new InvalidOperationException();
}}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task SwitchAsync({RangeJoined(", ", e => $"Func<T{e},Task> f{e}")})
{{
{RangeJoined(@"
", j => @$"if (_index == {j} && f{j} != null)
{{
await f{j}(_value{j});
return;
}}")}
throw new InvalidOperationException();
}}
#endif

public TResult Match<TResult>({RangeJoined(", ", e => $"Func<T{e}, TResult> f{e}")})
{{
{RangeJoined(@"
Expand All @@ -110,6 +126,18 @@ public TResult Match<TResult>({RangeJoined(", ", e => $"Func<T{e}, TResult> f{e}
throw new InvalidOperationException();
}}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task<TResult> MatchAsync<TResult>({RangeJoined(", ", e => $"Func<T{e}, Task<TResult>> f{e}")})
{{
{RangeJoined(@"
", j => $@"if (_index == {j} && f{j} != null)
{{
return await f{j}(_value{j});
}}")}
throw new InvalidOperationException();
}}
#endif

{IfStruct(genericArgs.Joined(@"
", bindToType => $@"public static OneOf<{genericArgs.Joined(", ")}> From{bindToType}({bindToType} input) => input;"))}

Expand All @@ -134,7 +162,26 @@ public TResult Match<TResult>({RangeJoined(", ", e => $"Func<T{e}, TResult> f{e}
$"{k} => As{x},")}
_ => throw new InvalidOperationException()
}};
}}";
}}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task<OneOf<{resultArgsPrinted}>> Map{bindToType}Async<TResult>(Func<{bindToType}, Task<TResult>> mapFunc)
{{
if (mapFunc == null)
{{
throw new ArgumentNullException(nameof(mapFunc));
}}
return _index switch
{{
{genericArgs.Joined(@"
", (x, k) =>
x == bindToType ?
$"{k} => await mapFunc(As{x})," :
$"{k} => As{x},")}
_ => throw new InvalidOperationException()
}};
}}
#endif";
}))}
");

Expand Down
116 changes: 116 additions & 0 deletions OneOf.Extended/OneOfBaseT10.generated.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using static OneOf.Functions;
#if NETSTANDARD2_0 || NET40_OR_GREATER
using System.Threading.Tasks;
#endif

namespace OneOf
{
Expand Down Expand Up @@ -176,6 +179,68 @@ public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2, Action<T3> f3, A
throw new InvalidOperationException();
}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task SwitchAsync(Func<T0,Task> f0, Func<T1,Task> f1, Func<T2,Task> f2, Func<T3,Task> f3, Func<T4,Task> f4, Func<T5,Task> f5, Func<T6,Task> f6, Func<T7,Task> f7, Func<T8,Task> f8, Func<T9,Task> f9, Func<T10,Task> f10)
{
if (_index == 0 && f0 != null)
{
await f0(_value0);
return;
}
if (_index == 1 && f1 != null)
{
await f1(_value1);
return;
}
if (_index == 2 && f2 != null)
{
await f2(_value2);
return;
}
if (_index == 3 && f3 != null)
{
await f3(_value3);
return;
}
if (_index == 4 && f4 != null)
{
await f4(_value4);
return;
}
if (_index == 5 && f5 != null)
{
await f5(_value5);
return;
}
if (_index == 6 && f6 != null)
{
await f6(_value6);
return;
}
if (_index == 7 && f7 != null)
{
await f7(_value7);
return;
}
if (_index == 8 && f8 != null)
{
await f8(_value8);
return;
}
if (_index == 9 && f9 != null)
{
await f9(_value9);
return;
}
if (_index == 10 && f10 != null)
{
await f10(_value10);
return;
}
throw new InvalidOperationException();
}
#endif

public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2, Func<T3, TResult> f3, Func<T4, TResult> f4, Func<T5, TResult> f5, Func<T6, TResult> f6, Func<T7, TResult> f7, Func<T8, TResult> f8, Func<T9, TResult> f9, Func<T10, TResult> f10)
{
if (_index == 0 && f0 != null)
Expand Down Expand Up @@ -225,6 +290,57 @@ public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T
throw new InvalidOperationException();
}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task<TResult> MatchAsync<TResult>(Func<T0, Task<TResult>> f0, Func<T1, Task<TResult>> f1, Func<T2, Task<TResult>> f2, Func<T3, Task<TResult>> f3, Func<T4, Task<TResult>> f4, Func<T5, Task<TResult>> f5, Func<T6, Task<TResult>> f6, Func<T7, Task<TResult>> f7, Func<T8, Task<TResult>> f8, Func<T9, Task<TResult>> f9, Func<T10, Task<TResult>> f10)
{
if (_index == 0 && f0 != null)
{
return await f0(_value0);
}
if (_index == 1 && f1 != null)
{
return await f1(_value1);
}
if (_index == 2 && f2 != null)
{
return await f2(_value2);
}
if (_index == 3 && f3 != null)
{
return await f3(_value3);
}
if (_index == 4 && f4 != null)
{
return await f4(_value4);
}
if (_index == 5 && f5 != null)
{
return await f5(_value5);
}
if (_index == 6 && f6 != null)
{
return await f6(_value6);
}
if (_index == 7 && f7 != null)
{
return await f7(_value7);
}
if (_index == 8 && f8 != null)
{
return await f8(_value8);
}
if (_index == 9 && f9 != null)
{
return await f9(_value9);
}
if (_index == 10 && f10 != null)
{
return await f10(_value10);
}
throw new InvalidOperationException();
}
#endif




Expand Down
125 changes: 125 additions & 0 deletions OneOf.Extended/OneOfBaseT11.generated.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using static OneOf.Functions;
#if NETSTANDARD2_0 || NET40_OR_GREATER
using System.Threading.Tasks;
#endif

namespace OneOf
{
Expand Down Expand Up @@ -189,6 +192,73 @@ public void Switch(Action<T0> f0, Action<T1> f1, Action<T2> f2, Action<T3> f3, A
throw new InvalidOperationException();
}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task SwitchAsync(Func<T0,Task> f0, Func<T1,Task> f1, Func<T2,Task> f2, Func<T3,Task> f3, Func<T4,Task> f4, Func<T5,Task> f5, Func<T6,Task> f6, Func<T7,Task> f7, Func<T8,Task> f8, Func<T9,Task> f9, Func<T10,Task> f10, Func<T11,Task> f11)
{
if (_index == 0 && f0 != null)
{
await f0(_value0);
return;
}
if (_index == 1 && f1 != null)
{
await f1(_value1);
return;
}
if (_index == 2 && f2 != null)
{
await f2(_value2);
return;
}
if (_index == 3 && f3 != null)
{
await f3(_value3);
return;
}
if (_index == 4 && f4 != null)
{
await f4(_value4);
return;
}
if (_index == 5 && f5 != null)
{
await f5(_value5);
return;
}
if (_index == 6 && f6 != null)
{
await f6(_value6);
return;
}
if (_index == 7 && f7 != null)
{
await f7(_value7);
return;
}
if (_index == 8 && f8 != null)
{
await f8(_value8);
return;
}
if (_index == 9 && f9 != null)
{
await f9(_value9);
return;
}
if (_index == 10 && f10 != null)
{
await f10(_value10);
return;
}
if (_index == 11 && f11 != null)
{
await f11(_value11);
return;
}
throw new InvalidOperationException();
}
#endif

public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T2, TResult> f2, Func<T3, TResult> f3, Func<T4, TResult> f4, Func<T5, TResult> f5, Func<T6, TResult> f6, Func<T7, TResult> f7, Func<T8, TResult> f8, Func<T9, TResult> f9, Func<T10, TResult> f10, Func<T11, TResult> f11)
{
if (_index == 0 && f0 != null)
Expand Down Expand Up @@ -242,6 +312,61 @@ public TResult Match<TResult>(Func<T0, TResult> f0, Func<T1, TResult> f1, Func<T
throw new InvalidOperationException();
}

#if NETSTANDARD2_0 || NET40_OR_GREATER
public async Task<TResult> MatchAsync<TResult>(Func<T0, Task<TResult>> f0, Func<T1, Task<TResult>> f1, Func<T2, Task<TResult>> f2, Func<T3, Task<TResult>> f3, Func<T4, Task<TResult>> f4, Func<T5, Task<TResult>> f5, Func<T6, Task<TResult>> f6, Func<T7, Task<TResult>> f7, Func<T8, Task<TResult>> f8, Func<T9, Task<TResult>> f9, Func<T10, Task<TResult>> f10, Func<T11, Task<TResult>> f11)
{
if (_index == 0 && f0 != null)
{
return await f0(_value0);
}
if (_index == 1 && f1 != null)
{
return await f1(_value1);
}
if (_index == 2 && f2 != null)
{
return await f2(_value2);
}
if (_index == 3 && f3 != null)
{
return await f3(_value3);
}
if (_index == 4 && f4 != null)
{
return await f4(_value4);
}
if (_index == 5 && f5 != null)
{
return await f5(_value5);
}
if (_index == 6 && f6 != null)
{
return await f6(_value6);
}
if (_index == 7 && f7 != null)
{
return await f7(_value7);
}
if (_index == 8 && f8 != null)
{
return await f8(_value8);
}
if (_index == 9 && f9 != null)
{
return await f9(_value9);
}
if (_index == 10 && f10 != null)
{
return await f10(_value10);
}
if (_index == 11 && f11 != null)
{
return await f11(_value11);
}
throw new InvalidOperationException();
}
#endif




Expand Down
Loading