Path: redb-route/redb.Route.Templates/PayloadTemplateProcessor.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.Templates/PayloadTemplateProcessor.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Templates;
/// Runtime side of : renders and writes the result to its target.
internal sealed class PayloadTemplateProcessor : IProcessor
{
private readonly CompiledTemplate _compiled;
private readonly MediaType _mediaType;
private readonly TemplateTarget _target;
private readonly string? _targetName;
private readonly TemplateArgs? _args;
private readonly RouteTemplateOptions _options;
public PayloadTemplateProcessor(CompiledTemplate compiled, MediaType mediaType, TemplateTarget target, string? targetName, TemplateArgs? args, RouteTemplateOptions options)
{
_compiled = compiled;
_mediaType = mediaType;
_target = target;
_targetName = targetName;
_args = args;
_options = options;
}
public Task Process(IExchange exchange, CancellationToken ct = default)
{
var text = TemplateEngine.Render(_compiled, exchange, _mediaType, _args, _options);
switch (_target)
{
case TemplateTarget.Header:
exchange.In.Headers[_targetName!] = text;
break;
case TemplateTarget.Property:
exchange.Properties[_targetName!] = text;
break;
default:
exchange.In.Body = text;
exchange.In.ContentType = ContentTypeOf(_mediaType);
break;
}
return Task.CompletedTask;
}
/// Content type written to the message when the target is the body.
public static string ContentTypeOf(MediaType mediaType) => mediaType switch
{
MediaType.Json => "application/json",
MediaType.Xml => "application/xml",
_ => "text/plain",
};
}