Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEdwardsWaggleBee committed Nov 20, 2024
1 parent 505b3ad commit 4e83e33
Show file tree
Hide file tree
Showing 29 changed files with 713 additions and 518 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:
- develop

env:
DOTNET_VERSION: '8.0.x'
DOTNET_VERSION: '9.0.x'

jobs:
format:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
env:
BRANCH_NAME: ${{ github.event.release.target_commitish }}
SOLUTION_NAME: ${{ vars.SOLUTION_NAME }}
DOTNET_VERSION: '8.0.x'
DOTNET_VERSION: '9.0.x'
NUGET_SOURCE: 'https://api.nuget.org/v3/index.json'
BUILD_CONFIGURATION: ''
VERSION_SUFFIX: ''
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
SOLUTION_NAME: ${{ vars.SOLUTION_NAME }}
DOTNET_VERSION: '8.0.x'
DOTNET_VERSION: '9.0.x'

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion src/Hyperbee.Pipeline.Auth/Hyperbee.Pipeline.Auth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<PackageReference Include="Microsoft.AspNetCore.Http.Connections.Common" Version="9.0.0-rc.2.24474.3" />
<PackageReference Include="Microsoft.AspNetCore.Http.Connections.Common" Version="9.0.0" />
<ProjectReference Include="..\Hyperbee.Pipeline\Hyperbee.Pipeline.csproj" />
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
55 changes: 26 additions & 29 deletions src/Hyperbee.Pipeline/Binders/Abstractions/Binder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Expressions;
using System.Reflection;
using Hyperbee.Pipeline.Context;
using static System.Linq.Expressions.Expression;
using static Hyperbee.Expressions.ExpressionExtensions;
Expand All @@ -10,25 +11,26 @@ internal abstract class Binder<TInput, TOutput>
protected Expression<FunctionAsync<TInput, TOutput>> Pipeline { get; }
protected Expression<Action<IPipelineContext>> Configure { get; }

private static ConstructorInfo _tupleConstructor = typeof( ValueTuple<TOutput, bool> ).GetConstructor( [typeof( TOutput ), typeof( bool )] )!;

protected Binder( Expression<FunctionAsync<TInput, TOutput>> function, Expression<Action<IPipelineContext>> configure )
{
Pipeline = function;
Configure = configure;
}

// protected virtual 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);
// }

protected virtual Expression ProcessPipelineAsync( ParameterExpression context, ParameterExpression argument )
{
var tupleCtor = typeof( ValueTuple<TOutput, bool> ).GetConstructor( [typeof( TOutput ), typeof( bool )] )!;
/*
{
var result = await Pipeline( context, argument ).ConfigureAwait( false );
var contextControl = (IPipelineContextControl) context;
var canceled = contextControl.HandleCancellationRequested( result );
return (canceled ? default : result, canceled);
}
*/

var result = Variable( typeof( TOutput ), "result" );
var canceled = Variable( typeof( bool ), "canceled" );
Expand All @@ -42,30 +44,25 @@ protected virtual Expression ProcessPipelineAsync( ParameterExpression context,

Condition(
canceled,
New( tupleCtor, Default( typeof( TOutput ) ), canceled ),
New( tupleCtor, result, canceled )
New( _tupleConstructor, Default( typeof( TOutput ) ), canceled ),
New( _tupleConstructor, result, canceled )
)
);
}



/*
public static bool HandleCancellationRequested<TOutput>( this IPipelineContextControl control, TOutput value )
{
if ( !control.CancellationToken.IsCancellationRequested )
return false;
if ( !control.HasCancellationValue )
control.CancellationValue = value;
return true;
}
*/


private Expression HandleCancellationRequested( Expression contextControl, Expression resultVariable )
{
/*
{
if ( !control.CancellationToken.IsCancellationRequested )
return false;
if ( !control.HasCancellationValue )
control.CancellationValue = value;
return true;
}
*/
var hasCancellationValue = Property( contextControl, "HasCancellationValue" );
var cancellationTokenProperty = Property( contextControl, "CancellationToken" );
var cancellationValueProperty = Property( contextControl, "CancellationValue" );
Expand Down
8 changes: 4 additions & 4 deletions src/Hyperbee.Pipeline/Binders/Abstractions/BlockBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ protected BlockBinder( Expression<FunctionAsync<TInput, TOutput>> function, Expr
// 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>( Expression<FunctionAsync<TArgument, TNext>> blockFunction, IPipelineContext context, TArgument nextArgument )
// {
// return await blockFunction( context, nextArgument ).ConfigureAwait( false );
// }
protected virtual Expression ProcessBlockAsync<TArgument, TNext>(
Expression<FunctionAsync<TArgument, TNext>> blockFunction,
ParameterExpression context,
Expression nextArgument )
{
/*
return blockFunction( context, nextArgument );
*/

return Invoke( blockFunction, context, nextArgument );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,31 @@ protected ConditionalBlockBinder( Expression<Function<TOutput, bool>> condition,
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;
// }

protected override Expression ProcessBlockAsync<TArgument, TNext>(
Expression<FunctionAsync<TArgument, TNext>> blockFunction,
ParameterExpression context,
Expression nextArgument )
{
/*
{
if (Condition != null && !Condition( context, CastTypeArg<TArgument, TOutput>(nextArgument ) ) )
{
return CastTypeArg<TArgument, TNext>(nextArgument );
}
return await base.ProcessBlockAsync( blockFunction, context, nextArgument ).ConfigureAwait( false );
}
*/

if ( Condition == null )
return base.ProcessBlockAsync( blockFunction, context, nextArgument );

// TODO: improve casting
return Condition(
Not( Invoke(
Condition,
context,
Convert( Convert( nextArgument, typeof( object ) ), typeof( TOutput ) )
Convert( Convert( nextArgument, typeof( object ) ), typeof( TOutput ) ) //(TResult) (object) input;
) ),
Convert( Convert( nextArgument, typeof( object ) ), typeof( TNext ) ),
Await( base.ProcessBlockAsync( blockFunction, context, nextArgument ) )
Expand Down
141 changes: 39 additions & 102 deletions src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq.Expressions;
using System.Linq.Expressions;
using Hyperbee.Pipeline.Context;
using Hyperbee.Pipeline.Extensions.Implementation;
using static System.Linq.Expressions.Expression;
using static Hyperbee.Expressions.ExpressionExtensions;

Expand All @@ -15,39 +16,33 @@ protected StatementBinder( Expression<FunctionAsync<TInput, TOutput>> function,
Middleware = middleware;
}

// protected MiddlewareAsync<object, object> Middleware1 { get; }
// 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 ( Middleware1 == null )
// return await nextFunction( context, nextArgument ).ConfigureAwait( false );
//
// return (TNext) await Middleware1(
// context,
// nextArgument,
// async ( context1, argument1 ) => await nextFunction( context1, (TOutput) argument1 ).ConfigureAwait( false )
// ).ConfigureAwait( false );
// }

protected virtual Expression ProcessStatementAsync<TNext>( Expression<FunctionAsync<TOutput, TNext>> nextFunction,
ParameterExpression context, Expression nextArgument, string frameName )
{
// if ( Middleware == null )
// return await nextFunction( context, nextArgument ).ConfigureAwait( false );
if ( Middleware == null )
/*
{
return Invoke( nextFunction, context, nextArgument );
var contextControl = (IPipelineContextControl) context;
using var _ = contextControl.CreateFrame( context, Configure, frameName );
//using var _ = contextControl.CreateFrame( context, Configure, frameName );
// return CreateFrameExpression(
// Convert( context, typeof(IPipelineContextControl) ),
// context,
// Configure,
// Await( Invoke( nextFunction, context, nextArgument ), configureAwait: false ),
// frameName );
if ( Middleware1 == null )
return await nextFunction( context, nextArgument ).ConfigureAwait( false );
return (TNext) await Middleware1(
context,
nextArgument,
async ( context1, argument1 ) => await nextFunction( context1, (TOutput) argument1 ).ConfigureAwait( false )
).ConfigureAwait( false );
}
*/

if ( Middleware == null )
{
return BlockAsync(
Using( //using var _ = contextControl.CreateFrame( context, Configure, frameName );
ContextImplExtensions.CreateFrameExpression( context, Configure, frameName ),
Await( Invoke( nextFunction, context, nextArgument ) )
) );
}

// async ( context1, argument1 ) => await nextFunction( context1, (TOutput) argument1 ).ConfigureAwait( false )
Expand All @@ -56,84 +51,26 @@ protected virtual Expression ProcessStatementAsync<TNext>( Expression<FunctionAs

var middlewareNext = Lambda<FunctionAsync<object, object>>(
BlockAsync(
Convert( Await(
Invoke( nextFunction, context1, Convert( argument1, typeof( TOutput ) ) ),
configureAwait: false ),
Convert(
Await( Invoke( nextFunction, context1, Convert( argument1, typeof( TOutput ) ) ), configureAwait: false ),
typeof( object ) )
),
parameters: [context1, argument1]
);

// return (TNext) await Middleware(
// context,
// nextArgument,
// middlewareNext
// ).ConfigureAwait( false );
//return
//using var _ = contextControl.CreateFrame( context, Configure, frameName );
// CreateFrameExpression(
// Convert( Constant( context ), typeof(IPipelineContextControl) ),
// context,
// Configure,

var returnResult = Variable( typeof( TNext ), "returnResult" );

var b = BlockAsync(
[returnResult],
//Invoke( LoggerExpression.Log( "StatementBinder.ProcessStatementAsync" + Random.Shared.Next( 0, 1000 ) ), Convert( nextArgument, typeof( object ) ) ),

Assign( returnResult, Convert(
Await(
Invoke( Middleware,
context,
Convert( nextArgument, typeof( object ) ),
middlewareNext
),
configureAwait: false ),
typeof( TNext ) ) )

//Invoke( LoggerExpression.Log( "StatementBinder.ProcessStatementAsync-" + Random.Shared.Next( 0, 1000 ) ), Convert( returnResult, typeof( object ) ) )

, returnResult
); //,


// frameName ); //);

return b;
}

public static Expression CreateFrameExpression(
Expression controlParam,
Expression contextParam,
Expression<Action<IPipelineContext>> config,
Expression body,
string defaultName = null
)
{
var nameVariable = Variable( typeof( string ), "originalName" );
var idVariable = Variable( typeof( int ), "originalId" );

var idProperty = Property( controlParam, "Id" );
var nameProperty = Property( controlParam, "Name" );

return BlockAsync(
[nameVariable, idVariable],
Assign( idVariable, idProperty ),
Assign( nameVariable, nameProperty ),
TryFinally(
Block(
Assign( idProperty, Call( controlParam, "GetNextId", Type.EmptyTypes ) ),
Assign( nameProperty, Constant( defaultName ) ),
config != null
? Invoke( config, contextParam )
: Empty(),
body
),
Block(
Assign( idProperty, idVariable ),
Assign( nameProperty, nameVariable )
) )
);
Using( //using var _ = contextControl.CreateFrame( context, Configure, frameName );
ContextImplExtensions.CreateFrameExpression( context, Configure, frameName ),
Convert(
Await(
Invoke( Middleware,
context,
Convert( nextArgument, typeof( object ) ),
middlewareNext
),
configureAwait: false ),
typeof( TNext ) )
)
);
}
}
Loading

0 comments on commit 4e83e33

Please sign in to comment.