-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [FEATURE]: Binder base (#31) * Implement abstract binders * Refactor existing binders to use abstract base --------- Co-authored-by: Brenton Farmer <brent.farmer@wagglebee.net> * [FEATURE]: Implement dedicated builder and binder for CallIfBlock and PipeIfBlock (#33) * Factor CallIfBlock* and PipeIfBlock* into their own builders and binders. --------- Co-authored-by: Brenton Farmer <brent.farmer@wagglebee.net> * [FEATURE]: Improve builder pattern (#35) * Refactor builders to eliminate reliance on partial interfaces and classes --------- Co-authored-by: Brenton Farmer <brent.farmer@wagglebee.net> * Fix footer * Previous version was 'v1.1.6'. Version now 'v1.2.0'. --------- Co-authored-by: Brenton Farmer <brent.farmer@wagglebee.net>
- Loading branch information
Showing
37 changed files
with
766 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<footer class="site-footer"> | ||
Hyperbee Json Docs | ||
Hyperbee Pipeline Docs | ||
</footer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Hyperbee.Pipeline.Context; | ||
using Hyperbee.Pipeline.Extensions.Implementation; | ||
|
||
namespace Hyperbee.Pipeline.Binders.Abstractions; | ||
|
||
internal abstract class Binder<TInput, TOutput> | ||
{ | ||
protected FunctionAsync<TInput, TOutput> Pipeline { get; } | ||
protected Action<IPipelineContext> Configure { get; } | ||
|
||
protected Binder( FunctionAsync<TInput, TOutput> function, Action<IPipelineContext> configure ) | ||
{ | ||
Pipeline = function; | ||
Configure = configure; | ||
} | ||
|
||
protected virtual async Task<(TOutput Result, bool Canceled)> ProcessPipelineAsync( IPipelineContext context, TInput argument ) | ||
{ | ||
var result = await Pipeline( context, argument ).ConfigureAwait( false ); | ||
|
||
var contextControl = (IPipelineContextControl) context; | ||
var canceled = contextControl.HandleCancellationRequested( result ); | ||
|
||
return (canceled ? default : result, canceled); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Hyperbee.Pipeline.Context; | ||
|
||
namespace Hyperbee.Pipeline.Binders.Abstractions; | ||
|
||
internal abstract class BlockBinder<TInput, TOutput> : Binder<TInput, TOutput> | ||
{ | ||
protected BlockBinder( FunctionAsync<TInput, TOutput> function, Action<IPipelineContext> configure ) | ||
: base( function, configure ) | ||
{ | ||
} | ||
|
||
// Using TArgument instead of TOutput allows more capabilities for special | ||
// use cases where the next argument is not the same as the output type | ||
// like ReduceBlockBinder and ForEachBlockBinder | ||
|
||
protected virtual async Task<TNext> ProcessBlockAsync<TArgument, TNext>( FunctionAsync<TArgument, TNext> blockFunction, IPipelineContext context, TArgument nextArgument ) | ||
{ | ||
return await blockFunction( context, nextArgument ).ConfigureAwait( false ); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Hyperbee.Pipeline/Binders/Abstractions/ConditionalBlockBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Runtime.CompilerServices; | ||
using Hyperbee.Pipeline.Context; | ||
|
||
namespace Hyperbee.Pipeline.Binders.Abstractions; | ||
|
||
internal abstract class ConditionalBlockBinder<TInput, TOutput> : BlockBinder<TInput, TOutput> | ||
{ | ||
protected Function<TOutput, bool> Condition { get; } | ||
|
||
protected ConditionalBlockBinder( Function<TOutput, bool> condition, FunctionAsync<TInput, TOutput> function, Action<IPipelineContext> configure ) | ||
: base( function, configure ) | ||
{ | ||
Condition = condition; | ||
} | ||
|
||
protected override async Task<TNext> ProcessBlockAsync<TArgument, TNext>( FunctionAsync<TArgument, TNext> blockFunction, IPipelineContext context, TArgument nextArgument ) | ||
{ | ||
if ( Condition != null && !Condition( context, CastTypeArg<TArgument, TOutput>( nextArgument ) ) ) | ||
{ | ||
return CastTypeArg<TArgument, TNext>( nextArgument ); | ||
} | ||
|
||
return await base.ProcessBlockAsync( blockFunction, context, nextArgument ).ConfigureAwait( false ); | ||
} | ||
|
||
[MethodImpl( MethodImplOptions.AggressiveInlining )] | ||
private static TResult CastTypeArg<TType, TResult>( TType input ) | ||
{ | ||
return (TResult) (object) input; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Hyperbee.Pipeline.Context; | ||
using Hyperbee.Pipeline.Extensions.Implementation; | ||
|
||
namespace Hyperbee.Pipeline.Binders.Abstractions; | ||
|
||
internal abstract class StatementBinder<TInput, TOutput> : Binder<TInput, TOutput> | ||
{ | ||
protected MiddlewareAsync<object, object> Middleware { get; } | ||
|
||
protected StatementBinder( FunctionAsync<TInput, TOutput> function, MiddlewareAsync<object, object> middleware, Action<IPipelineContext> configure ) | ||
: base( function, configure ) | ||
{ | ||
Middleware = middleware; | ||
} | ||
|
||
protected virtual async Task<TNext> ProcessStatementAsync<TNext>( FunctionAsync<TOutput, TNext> nextFunction, IPipelineContext context, TOutput nextArgument, string frameName ) | ||
{ | ||
var contextControl = (IPipelineContextControl) context; | ||
|
||
using var _ = contextControl.CreateFrame( context, Configure, frameName ); | ||
|
||
if ( Middleware == null ) | ||
return await nextFunction( context, nextArgument ).ConfigureAwait( false ); | ||
|
||
return (TNext) await Middleware( | ||
context, | ||
nextArgument, | ||
async ( context1, argument1 ) => await nextFunction( context1, (TOutput) argument1 ).ConfigureAwait( false ) | ||
).ConfigureAwait( false ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Hyperbee.Pipeline.Binders.Abstractions; | ||
|
||
namespace Hyperbee.Pipeline.Binders; | ||
|
||
internal class CallIfBlockBinder<TInput, TOutput> : ConditionalBlockBinder<TInput, TOutput> | ||
{ | ||
public CallIfBlockBinder( Function<TOutput, bool> condition, FunctionAsync<TInput, TOutput> function ) | ||
: base( condition, function, default ) | ||
{ | ||
} | ||
|
||
public FunctionAsync<TInput, TOutput> Bind( FunctionAsync<TOutput, object> next ) | ||
{ | ||
return async ( context, argument ) => | ||
{ | ||
var (nextArgument, canceled) = await ProcessPipelineAsync( context, argument ).ConfigureAwait( false ); | ||
if ( canceled ) | ||
return default; | ||
await ProcessBlockAsync( next, context, nextArgument ).ConfigureAwait( false ); | ||
return nextArgument; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
namespace Hyperbee.Pipeline.Binders; | ||
using Hyperbee.Pipeline.Binders.Abstractions; | ||
|
||
internal class ForEachBlockBinder<TInput, TOutput, TElement> | ||
{ | ||
private FunctionAsync<TInput, TOutput> Pipeline { get; } | ||
namespace Hyperbee.Pipeline.Binders; | ||
|
||
internal class ForEachBlockBinder<TInput, TOutput, TElement> : BlockBinder<TInput, TOutput> | ||
{ | ||
public ForEachBlockBinder( FunctionAsync<TInput, TOutput> function ) | ||
: base( function, default ) | ||
{ | ||
Pipeline = function; | ||
} | ||
|
||
public FunctionAsync<TInput, TOutput> Bind( FunctionAsync<TElement, object> next ) | ||
|
||
{ | ||
return async ( context, argument ) => | ||
{ | ||
var nextArgument = await Pipeline( context, argument ).ConfigureAwait( false ); | ||
var (nextArgument, canceled) = await ProcessPipelineAsync( context, argument ).ConfigureAwait( false ); | ||
if ( canceled ) | ||
return default; | ||
var nextArguments = (IEnumerable<TElement>) nextArgument; | ||
foreach ( var elementArgument in nextArguments ) | ||
{ | ||
await next( context, elementArgument ).ConfigureAwait( false ); | ||
await ProcessBlockAsync( next, context, elementArgument ).ConfigureAwait( false ); | ||
} | ||
return nextArgument; | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.