Skip to content

Commit

Permalink
WIP: mostly working
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEdwardsWaggleBee committed Oct 31, 2024
1 parent c2873e6 commit 821c37d
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 61 deletions.
2 changes: 0 additions & 2 deletions src/Hyperbee.Pipeline/Binders/Abstractions/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ protected virtual Expression ProcessPipelineAsync( ParameterExpression context,
Assign( result, Await( Invoke( Pipeline, context, argument ), configureAwait: false ) ),
Assign( canceled, HandleCancellationRequested( contextControl, result ) ),

Invoke( LoggerExpression.Log( "Binder.ProcessPipelineAsync" + Random.Shared.Next( 0, 1000 ) ), Convert( result, typeof( object ) ) ),

Condition(
canceled,
New( tupleCtor, Default( typeof( TOutput ) ), canceled ),
Expand Down
37 changes: 26 additions & 11 deletions src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected virtual Expression ProcessStatementAsync<TNext>( Expression<FunctionAs
// return await nextFunction( context, nextArgument ).ConfigureAwait( false );
if ( Middleware == null )
{
return Invoke( nextFunction, context, nextArgument ); //, configureAwait: false );
return Invoke( nextFunction, context, nextArgument );

//using var _ = contextControl.CreateFrame( context, Configure, frameName );
// return CreateFrameExpression(
Expand Down Expand Up @@ -69,23 +69,38 @@ protected virtual Expression ProcessStatementAsync<TNext>( Expression<FunctionAs
// nextArgument,
// middlewareNext
// ).ConfigureAwait( false );
return
//using var _ = contextControl.CreateFrame( context, Configure, frameName );
// CreateFrameExpression(
// Convert( Constant( context ), typeof(IPipelineContextControl) ),
// context,
// Configure,
BlockAsync(
Convert(
//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,
nextArgument,
Convert( nextArgument, typeof(object)),
middlewareNext
),
configureAwait: false ),
typeof( TNext ) ) ); //,
typeof( TNext ) ) )

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

, returnResult
); //,


// frameName ); //);

return b;
}

public static Expression CreateFrameExpression(
Expand Down
26 changes: 15 additions & 11 deletions src/Hyperbee.Pipeline/Binders/CallBlockBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,33 @@ public Expression<FunctionAsync<TInput, TOutput>> Bind( Expression<FunctionAsync
var argument = Parameter( typeof( TInput ), "argument" );

var awaitedResult = Variable( typeof( (TOutput, bool) ), "awaitedResult" );
var result = Variable( typeof( TOutput ), "result" );

var returnValue = Label( typeof( TOutput ) );
var nextArgument = Field( awaitedResult, "Item1" );
var canceled = Field( awaitedResult, "Item2" );

return Lambda<FunctionAsync<TInput, TOutput>>(
BlockAsync(
[awaitedResult],
Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ) ) ),

Invoke( LoggerExpression.Log( "CallBlockBinder.Bind" + Random.Shared.Next( 0, 1000 ) ), Convert( nextArgument, typeof( object ) ) ),

Condition( canceled,
Default( typeof( TOutput ) ),
// TODO: Think there is a bug here, we shouldn't need a child state machine.
[awaitedResult, result],
Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ), configureAwait: false ) ),

IfThenElse( canceled,
Block(
Assign( result, Default( typeof(TOutput) ) )//,
//Return( returnValue, result )
),
Block(
Await(
ProcessBlockAsync( next, context, nextArgument ),
configureAwait: false
),
nextArgument
Assign( result, nextArgument )//,
//Return( returnValue, result )
)

)
),
result
//Label( returnValue, result )
),
parameters: [context, argument]
);
Expand Down
28 changes: 11 additions & 17 deletions src/Hyperbee.Pipeline/Binders/CallStatementBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public Expression<FunctionAsync<TInput, TOutput>> Bind( Expression<ProcedureAsyn
var argument = Parameter( typeof( TInput ), "argument" );

var awaitedResult = Variable( typeof( (TOutput, bool) ), "awaitedResult" );

var nextArgument = Field( awaitedResult, "Item1" );
var canceled = Field( awaitedResult, "Item2" );

Expand All @@ -51,35 +52,28 @@ public Expression<FunctionAsync<TInput, TOutput>> Bind( Expression<ProcedureAsyn
var nextExpression = Lambda<FunctionAsync<TOutput, TInput>>(
BlockAsync(
Await( Invoke( next, ctx, arg ), configureAwait: false ),
argument
arg
),
parameters: [ctx, arg]
);

return Lambda<FunctionAsync<TInput, TOutput>>(
var l = Lambda<FunctionAsync<TInput, TOutput>>(
BlockAsync(
[awaitedResult],
Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ), configureAwait: false ) ),

Condition( canceled,
Default( typeof( TOutput ) ),
// TODO: Think there is a bug here, we shouldn't need a child state machine.
// Await(
// BlockAsync(
// [awaitedResult],
Block(
// [awaitedResult],
Await(
ProcessStatementAsync( nextExpression, context, nextArgument, defaultName ),
configureAwait: false
),
nextArgument
)
// )
// )
Default( typeof(TOutput) ),
Await(
ProcessStatementAsync( nextExpression, context, nextArgument, defaultName ),
configureAwait: false
)
)
),
parameters: [context, argument]
);

return l;
}
}

6 changes: 3 additions & 3 deletions src/Hyperbee.Pipeline/Binders/HookBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public Expression<MiddlewareAsync<TInput, TOutput>> Bind( Expression<MiddlewareA
var ctx = Parameter( typeof( IPipelineContext ), "ctx" );
var arg = Parameter( typeof( TInput ), "arg" );
var nextExpression = Lambda<FunctionAsync<TOutput, TInput>>(
BlockAsync(
Await( Invoke( Middleware, ctx, arg, function ), configureAwait: false ),
argument
BlockAsync(
[function],
Await( Invoke( Middleware, ctx, arg, function ), configureAwait: false )
),
parameters: [ctx, arg]
);
Expand Down
4 changes: 4 additions & 0 deletions src/Hyperbee.Pipeline/Binders/PipeBlockBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public Expression<FunctionAsync<TInput, TNext>> Bind<TNext>( Expression<Function
BlockAsync(
[awaitedResult],
Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ) ) ),

Invoke( LoggerExpression.Log( "PipeBlockBinder.Bind" + Random.Shared.Next( 0, 1000 ) ),
Convert( nextArgument, typeof( object ) ) ),

Condition( canceled,
Default( typeof( TNext ) ),
Await( ProcessBlockAsync( next, context, nextArgument ), configureAwait: false )
Expand Down
2 changes: 0 additions & 2 deletions src/Hyperbee.Pipeline/Binders/PipeStatementBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public Expression<FunctionAsync<TInput, TNext>> Bind<TNext>( Expression<Function
[awaitedResult],
Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ) ) ),

Invoke( LoggerExpression.Log( "PipeStatementBinder.Bind" + Random.Shared.Next( 0, 1000 ) ), Convert( nextArgument, typeof( object ) ) ),

Condition( canceled,
Default( typeof( TNext ) ),
Await( ProcessStatementAsync( next, context, nextArgument, defaultName ), configureAwait: false )
Expand Down
13 changes: 11 additions & 2 deletions src/Hyperbee.Pipeline/Builders/PipeStatementBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public static IPipelineBuilder<TInput, TNext> Pipe<TNext>(

var (parentFunction, parentMiddleware) = parent.GetPipelineFunction();

Expression<FunctionAsync<TOutput, TNext>> nextExpression = ( ctx, arg ) => Task.FromResult( next( ctx, arg ) );
Expression<FunctionAsync<TOutput, TNext>> nextExpression = ( ctx, arg ) => AsyncNext( next, ctx, arg );
Expression<Action<IPipelineContext>> configExpression = config == null
? null
: ctx => config( ctx );

return new PipelineBuilder<TInput, TNext>
{
Function = new PipeStatementBinder<TInput, TOutput>( parentFunction, parentMiddleware, configExpression ).Bind( nextExpression ).Reduce() as Expression<FunctionAsync<TInput, TNext>>,
Function = new PipeStatementBinder<TInput, TOutput>( parentFunction, parentMiddleware, configExpression ).Bind( nextExpression ),
Middleware = parentMiddleware
};
}
Expand All @@ -89,4 +89,13 @@ public static IPipelineBuilder<TInput, TNext> PipeAsync<TNext>(
Middleware = parentMiddleware
};
}

private static async Task<TNext> AsyncNext<TNext>( Function<TOutput, TNext> next, IPipelineContext ctx, TOutput arg )
{
var result = next( ctx, arg );
await Task.CompletedTask.ConfigureAwait( false );

return result;
}

}
2 changes: 1 addition & 1 deletion src/Hyperbee.Pipeline/Hyperbee.Pipeline.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Hyperbee.Expressions" Version="1.0.0-develop.241025145716" />
<PackageReference Include="Hyperbee.Expressions" Version="1.0.0-develop.241031173234" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0-rc.2.24473.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0-rc.2.24473.5" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Hyperbee.Pipeline/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ Expression<FunctionAsync<TIn, TOut>> IPipelineBuilder.CastExpression<TIn, TOut>(

return Lambda<FunctionAsync<TIn, TOut>>(
BlockAsync(
[context, argument],
Convert( Await( Invoke( Function, context, Convert( argument, typeof( TInput ) ) ), configureAwait: false ), typeof( TOut ) )
),
parameters: [context, argument]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.2" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions test/Hyperbee.Pipeline.Tests/Hyperbee.Pipeline.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Hyperbee.Pipeline\Hyperbee.Pipeline.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion test/Hyperbee.Pipeline.Tests/PipelineEnumerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task Pipeline_should_enumerate()

await command( new PipelineContext(), "e f" );

Assert.AreEqual( count, 25 );
Assert.AreEqual( 25, count );
}

[TestMethod]
Expand Down
2 changes: 0 additions & 2 deletions test/Hyperbee.Pipeline.Tests/PipelineExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public async Task Pipeline_should_execute_function()
var command = PipelineFactory
.Start<string>()
.Pipe( ( ctx, arg ) => int.Parse( arg ) )
.Pipe( ( ctx, arg ) => arg )
//.Pipe( ( ctx, arg ) => arg )
.Build();

var result = await command( new PipelineContext(), "5" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0-rc.2.24473.5" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.2" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 821c37d

Please sign in to comment.