Path: redb-route/redb.Route/Definitions/OfTypeDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/OfTypeDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Processors;
using redb.Route.Serialization;
namespace redb.Route.Definitions;
///
/// Typed scope-opener that converts the exchange body to
/// (via ) and then runs the contained child outputs
/// with the body strongly typed as . Apache Camel parity:
/// .OfType<T>() equivalent of choice().when(body().isInstanceOf(T.class))
/// combined with a transparent unmarshal step.
///
public class OfTypeDefinition : RouteDefinitionBase>
{
internal OfTypeDefinition() { }
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var pipeline = new PipelineProcessor();
// Skip ConvertBody when target type is string and body may not be string —
// ConvertBodyProcessor handles the "already-of-target-type" pass-through itself,
// but we elide it for the trivial string passthrough to match Camel semantics.
if (typeof(T) != typeof(string))
pipeline.Add(new ConvertBodyProcessor(typeof(T), context.GetService()));
foreach (var output in Outputs)
pipeline.Add(NodePipeline.Node(context, output));
return pipeline;
}
// ── Typed overloads ────────────────────────────────────────────────────────
/// Typed Process: invoked with the body cast to .
public OfTypeDefinition Process(Action action)
{
ArgumentNullException.ThrowIfNull(action);
AddOutput(new ProcessActionDefinition(e =>
{
if (e.In.Body is T typed) action(typed);
}));
return this;
}
/// Typed Transform: receives the body as and assigns the result back.
public OfTypeDefinition Transform(Func transform)
{
ArgumentNullException.ThrowIfNull(transform);
AddOutput(new TransformDefinition(e =>
e.In.Body is T typed ? transform(typed) : e.In.Body));
return this;
}
/// Typed inline Filter: keep only exchanges whose body matches the typed predicate.
public OfTypeFilterDefinition Filter(Func predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
var def = new OfTypeFilterDefinition(e => e.In.Body is T typed && predicate(typed));
AddOutput(def);
return def;
}
}
///
/// Typed filter scope used inside an . Re-exposes typed
/// and overloads.
///
public sealed class OfTypeFilterDefinition : FilterDefinition
{
internal OfTypeFilterDefinition(Func predicate) : base(predicate) { }
/// Typed Process inside the filter scope.
public new OfTypeFilterDefinition Process(Action action)
{
ArgumentNullException.ThrowIfNull(action);
AddOutput(new ProcessActionDefinition(e =>
{
if (e.In.Body is T typed) action(typed);
}));
return this;
}
/// Typed Transform inside the filter scope.
public new OfTypeFilterDefinition Transform(Func transform)
{
ArgumentNullException.ThrowIfNull(transform);
AddOutput(new TransformDefinition(e =>
e.In.Body is T typed ? transform(typed) : e.In.Body));
return this;
}
}