expressions

package
v1.1.2-rc.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EvaluateJSONTemplate

func EvaluateJSONTemplate(jsonBytes []byte, env map[string]any, exprOpts ...expr.Option) ([]byte, error)

EvaluateJSONTemplate evaluates a JSON byte slice, which is presumed to be a template containing expr-lang expressions offset by ${{ and }}, using the provided environment as context. The evaluated JSON is returned as a new byte slice, ready for unmarshaling.

Only expressions contained within values are evaluated. i.e. Any expressions within keys are NOT evaluated.

Since the template itself must be valid JSON, all expressions MUST be enclosed in quotes.

If, after evaluating all expressions in a single value (multiples are permitted), the result can be parsed as a bool, float64, or other valid non-string JSON, it will be treated as such. This ensures the possibility of expressions being used to construct any valid JSON value, despite the fact that expressions must, themselves, be contained within a string value. This does mean that for expressions which may evaluate as something resembling a valid non-string JSON value, the user must take care to ensure that the expression evaluates to a string enclosed in quotes. e.g. ${{ true }} will evaluated as a bool, but ${{ quote(true) }} will be evaluated as a string. This behavior should be intuitive to anyone familiar with YAML.

func EvaluateTemplate

func EvaluateTemplate(template string, env map[string]any, exprOpts ...expr.Option) (any, error)

EvaluateTemplate evaluates a single template string with the provided environment. Note that a single template string can contain multiple expressions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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