Skip to content

Commit

Permalink
- use global prefix for types used in generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
ax0l0tl committed Mar 15, 2024
1 parent d435fce commit 824464d
Show file tree
Hide file tree
Showing 57 changed files with 2,471 additions and 1,815 deletions.
162 changes: 81 additions & 81 deletions Source/FunicularSwitch.Generators.Templates/ResultType.cs

Large diffs are not rendered by default.

76 changes: 38 additions & 38 deletions Source/FunicularSwitch.Generators.Templates/ResultTypeWithMerge.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable enable
using System.Linq;
using global::System.Linq;
//additional using directives

namespace FunicularSwitch.Generators.Templates
Expand All @@ -12,27 +12,27 @@ public abstract partial class MyResult

public static partial class MyResultExtension
{
public static MyResult<System.Collections.Generic.IReadOnlyCollection<T1>> Map<T, T1>(this System.Collections.Generic.IEnumerable<MyResult<T>> results,
System.Func<T, T1> map) =>
public static MyResult<global::System.Collections.Generic.IReadOnlyCollection<T1>> Map<T, T1>(this global::System.Collections.Generic.IEnumerable<MyResult<T>> results,
global::System.Func<T, T1> map) =>
results.Select(r => r.Map(map)).Aggregate();

public static MyResult<System.Collections.Generic.IReadOnlyCollection<T1>> Bind<T, T1>(this System.Collections.Generic.IEnumerable<MyResult<T>> results,
System.Func<T, MyResult<T1>> bind) =>
public static MyResult<global::System.Collections.Generic.IReadOnlyCollection<T1>> Bind<T, T1>(this global::System.Collections.Generic.IEnumerable<MyResult<T>> results,
global::System.Func<T, MyResult<T1>> bind) =>
results.Select(r => r.Bind(bind)).Aggregate();

public static MyResult<System.Collections.Generic.IReadOnlyCollection<T1>> Bind<T, T1>(this MyResult<T> result,
System.Func<T, System.Collections.Generic.IEnumerable<MyResult<T1>>> bindMany) =>
public static MyResult<global::System.Collections.Generic.IReadOnlyCollection<T1>> Bind<T, T1>(this MyResult<T> result,
global::System.Func<T, global::System.Collections.Generic.IEnumerable<MyResult<T1>>> bindMany) =>
result.Map(ok => bindMany(ok).Aggregate()).Flatten();

public static MyResult<T1> Bind<T, T1>(this System.Collections.Generic.IEnumerable<MyResult<T>> results,
System.Func<System.Collections.Generic.IEnumerable<T>, MyResult<T1>> bind) =>
public static MyResult<T1> Bind<T, T1>(this global::System.Collections.Generic.IEnumerable<MyResult<T>> results,
global::System.Func<global::System.Collections.Generic.IEnumerable<T>, MyResult<T1>> bind) =>
results.Aggregate().Bind(bind);

public static MyResult<System.Collections.Generic.IReadOnlyCollection<T>> Aggregate<T>(this System.Collections.Generic.IEnumerable<MyResult<T>> results)
public static MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>> Aggregate<T>(this global::System.Collections.Generic.IEnumerable<MyResult<T>> results)
{
var isError = false;
MyError aggregated = default!;
var oks = new System.Collections.Generic.List<T>();
var oks = new global::System.Collections.Generic.List<T>();
foreach (var result in results)
{
result.Match(
Expand All @@ -46,31 +46,31 @@ public static MyResult<T1> Bind<T, T1>(this System.Collections.Generic.IEnumerab
}

return isError
? MyResult.Error<System.Collections.Generic.IReadOnlyCollection<T>>(aggregated)
: MyResult.Ok<System.Collections.Generic.IReadOnlyCollection<T>>(oks);
? MyResult.Error<global::System.Collections.Generic.IReadOnlyCollection<T>>(aggregated)
: MyResult.Ok<global::System.Collections.Generic.IReadOnlyCollection<T>>(oks);
}

public static async System.Threading.Tasks.Task<MyResult<System.Collections.Generic.IReadOnlyCollection<T>>> Aggregate<T>(
this System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<MyResult<T>>> results)
public static async global::System.Threading.Tasks.Task<MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>>> Aggregate<T>(
this global::System.Threading.Tasks.Task<global::System.Collections.Generic.IEnumerable<MyResult<T>>> results)
=> (await results.ConfigureAwait(false))
.Aggregate();

public static async System.Threading.Tasks.Task<MyResult<System.Collections.Generic.IReadOnlyCollection<T>>> Aggregate<T>(
this System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<MyResult<T>>> results)
=> (await System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false))
public static async global::System.Threading.Tasks.Task<MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>>> Aggregate<T>(
this global::System.Collections.Generic.IEnumerable<global::System.Threading.Tasks.Task<MyResult<T>>> results)
=> (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false))
.Aggregate();

public static async System.Threading.Tasks.Task<MyResult<System.Collections.Generic.IReadOnlyCollection<T>>> AggregateMany<T>(
this System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<MyResult<T>>>> results)
=> (await System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false))
public static async global::System.Threading.Tasks.Task<MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>>> AggregateMany<T>(
this global::System.Collections.Generic.IEnumerable<global::System.Threading.Tasks.Task<global::System.Collections.Generic.IEnumerable<MyResult<T>>>> results)
=> (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false))
.SelectMany(e => e)
.Aggregate();

//generated aggregate extension methods

public static MyResult<T> FirstOk<T>(this System.Collections.Generic.IEnumerable<MyResult<T>> results, System.Func<MyError> onEmpty)
public static MyResult<T> FirstOk<T>(this global::System.Collections.Generic.IEnumerable<MyResult<T>> results, global::System.Func<MyError> onEmpty)
{
var errors = new System.Collections.Generic.List<MyError>();
var errors = new global::System.Collections.Generic.List<MyError>();
foreach (var result in results)
{
if (result is MyResult<T>.Error_ e)
Expand All @@ -85,24 +85,24 @@ public static MyResult<T> FirstOk<T>(this System.Collections.Generic.IEnumerable
return MyResult.Error<T>(MergeErrors(errors));
}

public static async System.Threading.Tasks.Task<MyResult<System.Collections.Generic.IReadOnlyCollection<T>>> Aggregate<T>(
this System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<MyResult<T>>> results,
public static async global::System.Threading.Tasks.Task<MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>>> Aggregate<T>(
this global::System.Collections.Generic.IEnumerable<global::System.Threading.Tasks.Task<MyResult<T>>> results,
int maxDegreeOfParallelism)
=> (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false))
.Aggregate();

public static async System.Threading.Tasks.Task<MyResult<System.Collections.Generic.IReadOnlyCollection<T>>> AggregateMany<T>(
this System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<MyResult<T>>>> results,
public static async global::System.Threading.Tasks.Task<MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>>> AggregateMany<T>(
this global::System.Collections.Generic.IEnumerable<global::System.Threading.Tasks.Task<global::System.Collections.Generic.IEnumerable<MyResult<T>>>> results,
int maxDegreeOfParallelism)
=> (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false))
.SelectMany(e => e)
.Aggregate();

static async System.Threading.Tasks.Task<TOut[]> SelectAsync<T, TOut>(this System.Collections.Generic.IEnumerable<T> items, System.Func<T, System.Threading.Tasks.Task<TOut>> selector, int maxDegreeOfParallelism)
static async global::System.Threading.Tasks.Task<TOut[]> SelectAsync<T, TOut>(this global::System.Collections.Generic.IEnumerable<T> items, global::System.Func<T, global::System.Threading.Tasks.Task<TOut>> selector, int maxDegreeOfParallelism)
{
using (var throttler = new System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism))
using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism))
{
return await System.Threading.Tasks.Task.WhenAll(items.Select(async item =>
return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item =>
{
// ReSharper disable once AccessToDisposedClosure
await throttler.WaitAsync().ConfigureAwait(false);
Expand All @@ -119,19 +119,19 @@ static async System.Threading.Tasks.Task<TOut[]> SelectAsync<T, TOut>(this Syste
}
}

public static MyResult<System.Collections.Generic.IReadOnlyCollection<T>> AllOk<T>(this System.Collections.Generic.IEnumerable<T> candidates, System.Func<T, System.Collections.Generic.IEnumerable<MyError>> validate) =>
public static MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>> AllOk<T>(this global::System.Collections.Generic.IEnumerable<T> candidates, global::System.Func<T, global::System.Collections.Generic.IEnumerable<MyError>> validate) =>
candidates
.Select(c => c.Validate(validate))
.Aggregate();

public static MyResult<System.Collections.Generic.IReadOnlyCollection<T>> AllOk<T>(this System.Collections.Generic.IEnumerable<MyResult<T>> candidates,
System.Func<T, System.Collections.Generic.IEnumerable<MyError>> validate) =>
public static MyResult<global::System.Collections.Generic.IReadOnlyCollection<T>> AllOk<T>(this global::System.Collections.Generic.IEnumerable<MyResult<T>> candidates,
global::System.Func<T, global::System.Collections.Generic.IEnumerable<MyError>> validate) =>
candidates
.Bind(items => items.AllOk(validate));

public static MyResult<T> Validate<T>(this MyResult<T> item, System.Func<T, System.Collections.Generic.IEnumerable<MyError>> validate) => item.Bind(i => i.Validate(validate));
public static MyResult<T> Validate<T>(this MyResult<T> item, global::System.Func<T, global::System.Collections.Generic.IEnumerable<MyError>> validate) => item.Bind(i => i.Validate(validate));

public static MyResult<T> Validate<T>(this T item, System.Func<T, System.Collections.Generic.IEnumerable<MyError>> validate)
public static MyResult<T> Validate<T>(this T item, global::System.Func<T, global::System.Collections.Generic.IEnumerable<MyError>> validate)
{
try
{
Expand All @@ -140,21 +140,21 @@ public static MyResult<T> Validate<T>(this T item, System.Func<T, System.Collect
}
// ReSharper disable once RedundantCatchClause
#pragma warning disable CS0168 // Variable is declared but never used
catch (System.Exception e)
catch (global::System.Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
{
throw; //createGenericErrorResult
}
}

public static MyResult<T> FirstOk<T>(this System.Collections.Generic.IEnumerable<T> candidates, System.Func<T, System.Collections.Generic.IEnumerable<MyError>> validate, System.Func<MyError> onEmpty) =>
public static MyResult<T> FirstOk<T>(this global::System.Collections.Generic.IEnumerable<T> candidates, global::System.Func<T, global::System.Collections.Generic.IEnumerable<MyError>> validate, global::System.Func<MyError> onEmpty) =>
candidates
.Select(r => r.Validate(validate))
.FirstOk(onEmpty);

#region helpers

static MyError MergeErrors(System.Collections.Generic.IEnumerable<MyError> errors)
static MyError MergeErrors(global::System.Collections.Generic.IEnumerable<MyError> errors)
{
var first = true;
MyError aggregated = default!;
Expand Down
18 changes: 9 additions & 9 deletions Source/FunicularSwitch.Generators/EnumType/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ void BlankLine()
{
using (builder.StaticPartialClass($"{enumTypeSchema.TypeName.Replace(".", "_")}MatchExtension", enumTypeSchema.IsInternal ? "internal" : "public"))
{
var thisTaskParameter = ThisParameter(enumTypeSchema, $"System.Threading.Tasks.Task<{enumTypeSchema.FullTypeName}>");
var thisTaskParameter = ThisParameter(enumTypeSchema, $"global::System.Threading.Tasks.Task<{enumTypeSchema.FullTypeName}>");
var caseParameters = enumTypeSchema.Cases.Select(c => c.ParameterName).ToSeparatedString();
void WriteBodyForTaskExtension(string matchMethodName) => builder.WriteLine($"(await {thisTaskParameter.Name}.ConfigureAwait(false)).{matchMethodName}({caseParameters});");
void WriteBodyForAsyncTaskExtension(string matchMethodName) => builder.WriteLine($"await (await {thisTaskParameter.Name}.ConfigureAwait(false)).{matchMethodName}({caseParameters}).ConfigureAwait(false);");

GenerateMatchMethod(builder, enumTypeSchema, "T");
BlankLine();
GenerateMatchMethod(builder, enumTypeSchema, "System.Threading.Tasks.Task<T>");
GenerateMatchMethod(builder, enumTypeSchema, "global::System.Threading.Tasks.Task<T>");
BlankLine();

WriteMatchSignature(builder, enumTypeSchema, thisTaskParameter, "System.Threading.Tasks.Task<T>", "T", "public static async");
WriteMatchSignature(builder, enumTypeSchema, thisTaskParameter, "global::System.Threading.Tasks.Task<T>", "T", "public static async");
WriteBodyForTaskExtension(MatchMethodName);
BlankLine();
WriteMatchSignature(builder, enumTypeSchema, thisTaskParameter, "System.Threading.Tasks.Task<T>", handlerReturnType: "System.Threading.Tasks.Task<T>", "public static async");
WriteMatchSignature(builder, enumTypeSchema, thisTaskParameter, "global::System.Threading.Tasks.Task<T>", handlerReturnType: "global::System.Threading.Tasks.Task<T>", "public static async");
WriteBodyForAsyncTaskExtension(MatchMethodName);
BlankLine();

Expand Down Expand Up @@ -72,7 +72,7 @@ static void GenerateMatchMethod(CSharpBuilder builder, EnumTypeSchema enumTypeSc
}

builder.WriteLine(
$"_ => throw new System.ArgumentException($\"Unknown enum value from {enumTypeSchema.FullTypeName}: {{{thisParameterName}.GetType().Name}}\")");
$"_ => throw new global::System.ArgumentException($\"Unknown enum value from {enumTypeSchema.FullTypeName}: {{{thisParameterName}.GetType().Name}}\")");
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ static void GenerateSwitchMethod(CSharpBuilder builder, EnumTypeSchema enumTypeS
builder.WriteLine("default:");
using (builder.Indent())
{
builder.WriteLine($"throw new System.ArgumentException($\"Unknown enum value from {enumTypeSchema.FullTypeName}: {{{thisParameterName}.GetType().Name}}\");");
builder.WriteLine($"throw new global::System.ArgumentException($\"Unknown enum value from {enumTypeSchema.FullTypeName}: {{{thisParameterName}.GetType().Name}}\");");
}
}
}
Expand All @@ -116,7 +116,7 @@ static void WriteMatchSignature(CSharpBuilder builder, EnumTypeSchema enumTypeSc
{
handlerReturnType ??= returnType;
var handlerParameters = enumTypeSchema.Cases
.Select(c => new Parameter($"System.Func<{handlerReturnType}>", c.ParameterName));
.Select(c => new Parameter($"global::System.Func<{handlerReturnType}>", c.ParameterName));

builder.WriteMethodSignature(
modifiers: modifiers,
Expand All @@ -128,9 +128,9 @@ static void WriteMatchSignature(CSharpBuilder builder, EnumTypeSchema enumTypeSc
static void WriteSwitchSignature(CSharpBuilder builder, EnumTypeSchema enumTypeSchema,
Parameter thisParameter, bool isAsync, bool? asyncReturn = null, bool lambda = false)
{
var returnType = asyncReturn ?? isAsync ? "async System.Threading.Tasks.Task" : "void";
var returnType = asyncReturn ?? isAsync ? "async global::System.Threading.Tasks.Task" : "void";
var handlerParameters = enumTypeSchema.Cases
.Select(c => new Parameter(isAsync ? "System.Func<System.Threading.Tasks.Task>" : "System.Action", c.ParameterName));
.Select(c => new Parameter(isAsync ? "global::System.Func<global::System.Threading.Tasks.Task>" : "global::System.Action", c.ParameterName));

string modifiers = "public static";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<!--#region adapt versions here-->
<MajorVersion>3</MajorVersion>
<MinorAndPatchVersion>3.1</MinorAndPatchVersion>
<MinorAndPatchVersion>3.2</MinorAndPatchVersion>
<!--#endregion-->

<AssemblyVersion>$(MajorVersion).0.0</AssemblyVersion>
Expand Down
1 change: 0 additions & 1 deletion Source/FunicularSwitch.Generators/GeneratorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FunicularSwitch.Generators.Common;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using FunicularSwitch.Generators.EnumType;

namespace FunicularSwitch.Generators;

Expand Down
Loading

0 comments on commit 824464d

Please sign in to comment.