-
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.
feat: define a route for each output ports (#108)
- Loading branch information
Showing
14 changed files
with
389 additions
and
81 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Streamistry.Fluent; | ||
public class ConvergerBuilder<TNext> : BaseRoutesBuilder | ||
{ | ||
protected interface IRouteBuilder<TIn, TOut> : IRouteBuilder | ||
{ | ||
new IChainablePort<TOut> Build(IDualRoute dual); | ||
} | ||
|
||
public ConvergerBuilder(IBuilder<IDualRoute> upstream) | ||
: base(upstream) | ||
{ } | ||
|
||
public ConvergerBuilder<TNext> Route(Func<IDualRoute, IChainablePort> port, ISegment segment) | ||
{ | ||
Add(port, segment); | ||
return this; | ||
} | ||
|
||
public ConvergerBuilder<TNext> Route<TOutput>(Func<IDualRoute, IChainablePort> port, Func<BasePipeBuilder<TOutput>, BasePipeBuilder<TNext>> path) | ||
{ | ||
Add(port, new Segment<TOutput, TNext>(path)); | ||
return this; | ||
} | ||
|
||
public UnionBuilder<TNext> Union() | ||
=> new(this); | ||
} |
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,34 +1,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Streamistry.Fluent; | ||
public class MapperBuilder<TInput, TOutput> : PipeElementBuilder<TInput, TOutput>, ISafeBuilder<MapperBuilder<TInput, TOutput>> | ||
public class MapperBuilder<TInput, TOutput> : PipeElementBuilder<TInput, TOutput> | ||
{ | ||
protected Func<TInput, TOutput>? Function { get; set; } | ||
private bool IsSafe { get; set; } = false; | ||
|
||
public MapperBuilder(IPipeBuilder<TInput> upstream, Func<TInput, TOutput>? function) | ||
: base(upstream) | ||
=> (Function) = (function); | ||
|
||
public MapperBuilder<TInput, TOutput> Safe() | ||
{ | ||
IsSafe = true; | ||
return this; | ||
} | ||
public SafeMapperBuilder<TInput, TOutput> Safe() | ||
=> new (Upstream, Function); | ||
|
||
public override IChainablePort<TOutput> OnBuildPipeElement() | ||
=> IsSafe | ||
? new ExceptionMapper<TInput, TOutput>( | ||
=> new Mapper<TInput, TOutput>( | ||
Upstream.BuildPipeElement() | ||
, Function ?? throw new InvalidOperationException() | ||
) | ||
: new Mapper<TInput, TOutput>( | ||
); | ||
} | ||
|
||
public class SafeMapperBuilder<TInput, TOutput> : MapperBuilder<TInput, TOutput>, IBuilder<IDualRoute> | ||
{ | ||
protected new IDualRoute? Instance { get; set; } | ||
|
||
public new IDualRoute BuildPipeElement() | ||
=> Instance ??= OnBuildPipeElement(); | ||
|
||
public new IDualRoute OnBuildPipeElement() | ||
=> new SafeMapper<TInput, TOutput>( | ||
Upstream.BuildPipeElement() | ||
, Function ?? throw new InvalidOperationException() | ||
); | ||
|
||
public SafeMapperBuilder(IPipeBuilder<TInput> upstream, Func<TInput, TOutput>? function) | ||
: base(upstream, function) | ||
{ } | ||
|
||
public RoutesBuilder Route<TPort, TNext>(Func<IDualRoute, IChainablePort> port, Segment<TPort, TNext> segment) | ||
{ | ||
var routeBuilder = new RoutesBuilder(this); | ||
routeBuilder.Add(port, segment); | ||
return routeBuilder; | ||
} | ||
|
||
public RoutesBuilder Route<TPort, TNext>(Func<IDualRoute, IChainablePort> port, Func<BasePipeBuilder<TPort>, BasePipeBuilder<TNext>> path) | ||
{ | ||
var routeBuilder = new RoutesBuilder(this); | ||
routeBuilder.Add(port, new Segment<TPort, TNext>(path)); | ||
return routeBuilder; | ||
} | ||
|
||
public ConvergerBuilder<TNext> Converge<TNext>() | ||
=> new(this); | ||
} |
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,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Streamistry.Fluent; | ||
public abstract class BaseRoutesBuilder : IBuilder<IChainablePort[]> | ||
{ | ||
protected interface IRouteBuilder | ||
{ | ||
IChainablePort Build(IDualRoute dual); | ||
} | ||
|
||
protected class RouteBuilder : IRouteBuilder | ||
{ | ||
protected Func<IDualRoute, IChainablePort> Port { get; } | ||
protected ISegment Segment { get; } | ||
|
||
public RouteBuilder(Func<IDualRoute, IChainablePort> port, ISegment segment) | ||
=> (Port, Segment) = (port, segment); | ||
|
||
public IChainablePort Build(IDualRoute dual) | ||
{ | ||
var port = Port(dual); | ||
var (input, output) = Segment.Craft(port.Pipe.Pipeline!); | ||
input.Bind(port); | ||
return output; | ||
} | ||
} | ||
|
||
protected IBuilder<IDualRoute> Upstream { get; } | ||
|
||
protected IChainablePort[]? Instances { get; set; } | ||
protected List<IRouteBuilder> RouteBuilders { get; } = []; | ||
|
||
public BaseRoutesBuilder(IBuilder<IDualRoute> upstream) | ||
=> (Upstream) = (upstream); | ||
|
||
internal void Add(Func<IDualRoute, IChainablePort> port, ISegment segment) | ||
=> RouteBuilders.Add(new RouteBuilder(port, segment)); | ||
|
||
public IChainablePort[] BuildPipeElement() | ||
=> Instances ??= OnBuildPipeElement(); | ||
|
||
public IChainablePort[] OnBuildPipeElement() | ||
{ | ||
var routes = new List<IChainablePort>(); | ||
var upstream = Upstream.BuildPipeElement(); | ||
foreach (var routeBuilder in RouteBuilders) | ||
routes.Add(routeBuilder.Build(upstream)); | ||
return [.. routes]; | ||
} | ||
|
||
public Pipeline Build() | ||
{ | ||
BuildPipeElement(); | ||
return Instances![0].Pipe.Pipeline!; | ||
} | ||
} | ||
public class RoutesBuilder : BaseRoutesBuilder | ||
{ | ||
public RoutesBuilder(IBuilder<IDualRoute> upstream) | ||
: base(upstream) | ||
{ } | ||
|
||
public RoutesBuilder Route<TInput, TOutput>(Func<IDualRoute, IChainablePort> port, Segment<TInput, TOutput> segment) | ||
{ | ||
Add(port, segment); | ||
return this; | ||
} | ||
|
||
public RoutesBuilder Route<TInput, TOutput>(Func<IDualRoute, IChainablePort> port, Func<BasePipeBuilder<TInput>, BasePipeBuilder<TOutput>> path) | ||
{ | ||
Add(port, new Segment<TInput, TOutput>(path)); | ||
return this; | ||
} | ||
|
||
public UnionBuilder<TNext> Union<TNext>() | ||
=> new(this); | ||
} | ||
|
Oops, something went wrong.