Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Route Templates via JSON Tree Substitution

Route templates let users define parameterized route blueprints once and instantiate them multiple times with different parameter values. Instead of repeating nearly identical YAML route definitions, a template declares {{param}} placeholders in its route body, and each templated_route instantiation provides concrete values. The materializer substitutes placeholders via a JSON tree walk before parsing the result into a DeclarativeRoute — this pre-parse substitution means placeholders work in any string field, including numeric or boolean contexts like delay_ms: "{{delay}}".

The key architectural decision is where the materializer lives. camel-dsl already depends on camel-core, so camel-core cannot depend on camel-dsl without a circular dependency. The solution splits responsibilities: CamelContext stores only data (template specs and instance records) in a TemplateRegistry, while the materializer and materialize_and_compile helper live in camel-dsl. Discovery uses a two-pass approach — Pass 1 collects templates from all files into a HashMap, Pass 2 materializes each templated_route by looking up its template and calling materialize_and_compile. This enables cross-file template references: a template defined in base-routes.yaml can be instantiated in prod-routes.yaml.

An alternative would have been typed substitution on DeclarativeRoute (walking all 26 step variants and substituting string fields), but that approach is fragile — every new step variant requires updating the materializer, and it cannot substitute into non-string fields without special handling. JSON tree substitution is simpler, more future-proof, and leverages the existing parse pipeline unchanged. The camel-api crate holds shared types (RouteTemplateSpec, TemplatedRouteSpec, TemplateError) to avoid circular dependencies, and camel-dsl provides the placeholder engine, JSON tree materializer, and YAML/JSON parsing extensions with #[serde(default)] for backward compatibility.