Path: redb-route/redb.Route/Definitions/NormalizerDefinition.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/NormalizerDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Definition for a Normalizer step. Collects when-clauses and builds a
/// with transform branches at route-build time.
///
public sealed class NormalizerDefinition : ProcessorDefinition, INormalizerDefinition
{
private readonly List<(Func Predicate, Func Transform)> _clauses = new();
private Func? _otherwise;
///
public INormalizerDefinition When(
Func predicate,
Func transform)
{
ArgumentNullException.ThrowIfNull(predicate);
ArgumentNullException.ThrowIfNull(transform);
_clauses.Add((predicate, transform));
return this;
}
///
public INormalizerDefinition WhenContentType(
string contentType,
Func transform)
{
ArgumentNullException.ThrowIfNull(contentType);
ArgumentNullException.ThrowIfNull(transform);
Func predicate = e => string.Equals(
e.In.Headers.TryGetValue("ContentType", out var ct) ? ct?.ToString() : null,
contentType,
StringComparison.OrdinalIgnoreCase);
_clauses.Add((predicate, transform));
return this;
}
///
public INormalizerDefinition Otherwise(Func transform)
{
ArgumentNullException.ThrowIfNull(transform);
_otherwise = transform;
return this;
}
/// True when no When-clauses have been added yet (used for eager validation in v2 DSL).
internal bool IsEmpty => _clauses.Count == 0;
///
public override IProcessor CreateProcessor(IRouteContext context)
{
if (_clauses.Count == 0)
throw new InvalidOperationException("Normalizer requires at least one When clause.");
var choice = new ChoiceProcessor();
foreach (var (predicate, transform) in _clauses)
{
var t = transform;
choice.When(predicate, new DelegateProcessor(e => { e.In.Body = t(e); }));
}
if (_otherwise is not null)
{
var otherwise = _otherwise;
choice.SetOtherwise(new DelegateProcessor(e => { e.In.Body = otherwise(e); }));
}
return choice;
}
}