pub struct Template { /* private fields */ }Expand description
A config field whose authored JSON is a JSONLogic expression.
Deserializes from any JSON value and keeps it verbatim; the expression is
compiled once at engine construction (see crate::AsyncFunctionHandler::compile_input)
and evaluated per message on the worker thread’s pooled arena — unless it
folded to a constant, in which case the value is computed once at
construction and handed back directly.
§Literals and the $ escape
A JSON scalar or array authored here is a literal: "data.out" resolves to
the string data.out, 30000 to the number. An object is where care is
needed, because the engine evaluates in templating mode: a single-key object
whose key matches an operator name is that operator. {"cat": ["a", "b"]}
resolves to "ab", not to the object.
Prefix the key with Engine::template_key_escape
($) to force the literal reading: {"$cat": ["a", "b"]} resolves to the
object {"cat": ["a", "b"]}. One prefix is stripped from every template key,
so a genuinely $-prefixed key doubles up — {"$$oid": …} emits $oid.
Before that escape existed a literal object with a colliding key was
inexpressible, which is why this type used to be documented as opt-in per
field. It no longer is: any config field may be a Template.
Implementations§
Source§impl Template
impl Template
Sourcepub fn compile(&mut self, c: &TemplateCompiler, label: &str) -> Result<()>
pub fn compile(&mut self, c: &TemplateCompiler, label: &str) -> Result<()>
Compile the expression. Called once at engine construction via
crate::AsyncFunctionHandler::compile_input or its receiver-taking
twin crate::AsyncFunctionHandler::compile_input_with. label is used only in
the error message, matching LogicCompiler’s
"<what> for task <id> in workflow <id>" convention.
§Errors
DataflowError::LogicEvaluation if the expression fails to compile,
with label prefixed onto the message.
Sourcepub fn is_constant(&self) -> bool
pub fn is_constant(&self) -> bool
Whether the expression folded to a compile-time constant, so every
resolve_* call returns a cached value instead of evaluating.
True for the static spelling of any parameter — a scalar, an array, or
an object template with no var in it. False once anything reads the
message. Callers that precompute a derived form (a split write path, for
instance) branch on this.
Meaningless before Self::compile: an uncompiled Template reports
false because nothing has been folded yet, not because the expression
is dynamic.
Sourcepub fn constant_string(&self) -> Option<String>
pub fn constant_string(&self) -> Option<String>
The folded constant coerced to a plain string, when the expression folded to one.
Lets a caller do at compile time what Self::resolve_string would
otherwise defer to the first message — which is how PathTemplate
precomputes a static write path.
Sourcepub fn resolve(&self, ctx: &TaskContext<'_>) -> Result<OwnedDataValue>
pub fn resolve(&self, ctx: &TaskContext<'_>) -> Result<OwnedDataValue>
The parameter’s value for this message: the cached constant when the expression folded, otherwise a fresh evaluation.
This is the sanctioned read for a config parameter. Self::eval is
the same thing without the constant cache, kept for handlers that hold
a Template they compiled themselves.
§Errors
As Self::eval.
Sourcepub fn resolve_string(&self, ctx: &TaskContext<'_>) -> Result<String>
pub fn resolve_string(&self, ctx: &TaskContext<'_>) -> Result<String>
As Self::resolve, coerced to a plain string — a string result
yields its contents, anything else its compact JSON form. Use this
wherever the value becomes a URL path, a header value, a topic name or a
write path, where JSON quoting would be wrong.
§Errors
As Self::eval.
Sourcepub fn resolve_u64(&self, ctx: &TaskContext<'_>, label: &str) -> Result<u64>
pub fn resolve_u64(&self, ctx: &TaskContext<'_>, label: &str) -> Result<u64>
As Self::resolve, as a u64 — for parameters like timeout_ms.
§Errors
As Self::eval, plus DataflowError::Validation when the result is
not a number that fits a u64. A timeout that evaluated to null
because its path was missing is a configuration error worth reporting,
not something to silently default.
Sourcepub fn eval(&self, ctx: &TaskContext<'_>) -> Result<OwnedDataValue>
pub fn eval(&self, ctx: &TaskContext<'_>) -> Result<OwnedDataValue>
Evaluate against the message context, on the worker thread’s pooled bump arena.
§Errors
DataflowError::LogicEvaluation if Self::compile was never called —
naming the field is the caller’s job via label, since this type has no
field name of its own to report — or if evaluation itself fails.
Sourcepub fn eval_into<T: DeserializeOwned>(&self, ctx: &TaskContext<'_>) -> Result<T>
pub fn eval_into<T: DeserializeOwned>(&self, ctx: &TaskContext<'_>) -> Result<T>
As Self::eval, deserialized into T.
Routes through serde_json::Value — TaskContext::eval_json then
serde_json::from_value — so it costs one extra walk and rebuild past
Self::eval. Prefer eval when T is OwnedDataValue or when you
only need to inspect the result, not deserialize it into a caller type.
§Errors
As Self::eval, plus a deserialization error if the evaluated JSON does
not fit T.
Sourcepub fn eval_to_plain_string(&self, ctx: &TaskContext<'_>) -> Result<String>
pub fn eval_to_plain_string(&self, ctx: &TaskContext<'_>) -> Result<String>
As Self::eval, coerced to a plain string via
TaskContext::eval_to_plain_string — a JSON string result yields its
contents, anything else its compact JSON form. Use this when the result
is going into a URL path or a message key, where JSON quoting would be
wrong.
§Errors
As Self::eval.
Sourcepub fn as_json(&self) -> &Value
pub fn as_json(&self) -> &Value
The authored JSON, unchanged. For handlers that need to report or re-serialize their own config.
Sourcepub fn is_compiled(&self) -> bool
pub fn is_compiled(&self) -> bool
Whether Self::compile has run. Mainly for tests and for callers that
want to assert the build pass reached them.