jsontmpl

package
v1.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jsontmpl is a dependency-free evaluator for the bounded subset of the Adaptive Card Templating language used by Registry-backed JSON card templates (roadmap E1). It intentionally implements only the syntax exercised by the shipped handoff templates — single-identifier field bindings, `$index`, `if($index == N, a, b)`, string interpolation, and typed whole-value binding. Anything outside that subset is a hard error, never a silent passthrough (see the package brief, decision D4: freeze the subset, extend via L0 PR).

The package holds no cardtmpl dependency: the markdown escaper is injected as an EscapeFunc so the trust boundary (template text is trusted, caller-supplied data is escaped) is enforced by the caller that owns the escaping authority.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvalValue

func EvalValue(raw string, sc Scope, escape EscapeFunc) (any, error)

EvalValue evaluates a JSON string value that may embed ${...} expressions:

  • no ${...} → the literal string, unchanged
  • exactly one ${expr} (no other text) → the typed result of expr; a bool or number stays native, a string result is escaped
  • literal text mixed with ${expr}(s) → string interpolation: each expr result is stringified and escaped, literals are emitted verbatim (so trusted template punctuation is never mangled, only data is escaped)

func Expand

func Expand(ctx context.Context, node any, sc Scope, escape EscapeFunc) (any, error)

Expand walks a parsed Adaptive Card template (map/slice/scalar tree) and returns a new tree with every ${...} expression resolved against sc. Objects carrying a `$data` directive are repeated once per array element (with a loop scope exposing `$index`); the directive key itself is dropped. String leaves go through EvalValue, so a whole-value `"${bool}"` becomes a native boolean and caller data is escaped. The input tree is never mutated.

ctx is honored between nodes so a caller cancel/deadline interrupts a large expansion (R2-7 context propagation); the total emitted nodes are bounded by maxExpandNodes (see the const). A nil ctx is treated as context.Background().

func ValidateToggleTargets

func ValidateToggleTargets(node any) error

ValidateToggleTargets statically checks that every Action.ToggleVisibility target in the template refers to an element id that exists in the same template. It walks the raw (pre-expansion) AST: element ids and toggle targets in these cards are template-authored constants, so a dangling target is an authoring bug catchable at register time rather than a silent no-op button at render time. Data-driven ids/targets (`${...}`) are skipped — they cannot be resolved statically.

Types

type EscapeFunc

type EscapeFunc func(string) string

EscapeFunc escapes a caller-supplied string before it lands in card text. The expander injects cardtmpl's markdown escaper here.

type Scope

type Scope struct {
	Data   map[string]any
	Index  int
	InLoop bool
}

Scope is the data frame an expression is evaluated against. Data is the current object (root object, or the current element inside a $data loop); Index/InLoop carry the iteration position for `$index`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址