Path: redb-route/redb.Route.TestKit/WeaveTarget.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.TestKit/WeaveTarget.cs
----
using redb.Route.Abstractions;
using redb.Route.Definitions;
namespace redb.Route.TestKit;
///
/// The steps selected by ,
/// or , ready to be replaced, wrapped or removed
/// (Apache Camel weaveById(...).replace() / .before() / .after() / .remove()).
/// New steps are written with the ordinary route DSL: t.Replace(r => r.To("mock://x")).
///
public sealed class WeaveTarget
{
private readonly AdviceWithBuilder _owner;
private readonly IReadOnlyList _slots;
internal WeaveTarget(AdviceWithBuilder owner, IReadOnlyList slots)
{
_owner = owner;
_slots = slots;
}
/// Number of steps selected.
public int Count => _slots.Count;
/// Replaces each selected step with the steps built by .
public AdviceWithBuilder Replace(Action steps)
{
ArgumentNullException.ThrowIfNull(steps);
foreach (var slot in Descending())
DefinitionTree.Splice(slot.Owner, slot.Index, Build(steps));
return _owner;
}
/// Inserts the steps built by before each selected step.
public AdviceWithBuilder Before(Action steps)
{
ArgumentNullException.ThrowIfNull(steps);
foreach (var slot in Descending())
DefinitionTree.InsertAll(slot.Owner, slot.Index, Build(steps));
return _owner;
}
/// Inserts the steps built by after each selected step.
public AdviceWithBuilder After(Action steps)
{
ArgumentNullException.ThrowIfNull(steps);
foreach (var slot in Descending())
DefinitionTree.InsertAll(slot.Owner, slot.Index + 1, Build(steps));
return _owner;
}
/// Removes each selected step.
public AdviceWithBuilder Remove()
{
foreach (var slot in Descending())
slot.Owner.Outputs.RemoveAt(slot.Index);
return _owner;
}
/// Builds steps on a scratch route so the full DSL is available, then hands over its outputs.
internal static IReadOnlyList Build(Action steps)
{
var scratch = new RouteDefinition();
steps(scratch);
return scratch.Outputs.ToArray();
}
// Mutate from the back of each owner list so earlier indices stay valid.
private IEnumerable Descending() => _slots.OrderByDescending(s => s.Index);
}