This repository was archived by the owner on Jul 2, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Document package constants #413
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| title: Add constants | ||
| --- | ||
|
|
||
| This guide shows you how to define package-wide constants in a `constants.tql` | ||
| file and reference them as `pkg::$name` from the package's own operators and | ||
| pipelines, as well as from any pipeline that uses the package. You'll learn the | ||
| `let` syntax, how bindings build on one another, the rules each binding must | ||
| satisfy, and when to use a constant instead of an input. | ||
|
|
||
| ## Define constants | ||
|
|
||
| Place a `constants.tql` file at the root of your package. Each `let` binding | ||
| defines a named constant that Tenzir evaluates once when it loads the package: | ||
|
|
||
| ```tql title="constants.tql" | ||
| let $threshold = 8 | ||
| let $severities = {low: 2, medium: 3, high: 4, critical: 5} | ||
| ``` | ||
|
|
||
| A constant can be any valueβa number, string, list, or recordβwhich makes | ||
| `constants.tql` a natural home for the lookup tables and magic numbers that | ||
| would otherwise be copy-pasted across your operators. | ||
|
|
||
| ## Reference constants | ||
|
|
||
| Reference a constant from anywhere with `<package>::$name`, using your package's | ||
| ID as the prefix. In a package with ID `acme`, the bindings above become | ||
| `acme::$threshold` and `acme::$severities`. | ||
|
|
||
| ### From the package's own operators | ||
|
|
||
| Inside the package, operators and pipelines reference constants so the same | ||
| value lives in exactly one place: | ||
|
|
||
| ```tql title="operators/ocsf/map.tql" | ||
| ocsf.severity_id = acme::$severities[severity] | ||
| ``` | ||
|
|
||
| ### From external pipelines | ||
|
|
||
| Once the `acme` package is available, any pipeline can reference its constants: | ||
|
|
||
| ```tql | ||
| from {severity: 9}, {severity: 3} | ||
| where severity >= acme::$threshold | ||
| ``` | ||
|
|
||
| ```tql | ||
| {severity: 9} | ||
| ``` | ||
|
|
||
| This lets a package publish named thresholds and enumerations that consumers | ||
| reuse instead of hardcoding their own copies. | ||
|
|
||
| ## Build on earlier constants | ||
|
|
||
| A binding may reference any constant declared before it, so you can derive one | ||
| constant from another: | ||
|
|
||
| ```tql title="constants.tql" | ||
| let $high_severity = 8 | ||
| let $threshold = $high_severity + 1 | ||
| ``` | ||
|
|
||
| References resolve in order. A binding that refers to a constant declared later | ||
| in the file fails to load the package. | ||
|
|
||
| ## Binding rules | ||
|
|
||
| Each binding must evaluate to a deterministic constant value, because Tenzir | ||
| computes it once when the package loads and folds the result into every | ||
| reference. Tenzir rejects a binding that is: | ||
|
|
||
| - **Non-deterministic**, such as `now()` or `random()`. | ||
| - A **pipeline** rather than a value. | ||
| - A **function** (lambda). | ||
| - A **duplicate name**, since all constants share one flat namespace. | ||
|
|
||
| When a binding violates these rules, the package fails to load with a diagnostic | ||
| that points at the offending `let`. | ||
|
|
||
| ## Constants versus inputs | ||
|
|
||
| Constants and [inputs](/guides/packages/configure-inputs) both parameterize a | ||
| package, but they sit on opposite sides of the install boundary: | ||
|
|
||
| | | Constants | Inputs | | ||
| | ------------- | ---------------------------------------------------- | ----------------------------------------------------------------------- | | ||
| | Defined in | `constants.tql` | `package.yaml` | | ||
| | Set by | the package author (fixed) | the person installing the package | | ||
| | Referenced as | `pkg::$name` | `{{ inputs.name }}` | | ||
| | Resolved | const-evaluated when the package loads | substituted at install time | | ||
| | Use for | shared lookup tables, enumerations, fixed thresholds | endpoints, credentials, intervals, and other deployment-specific values | | ||
|
|
||
| Reach for a constant when the package author owns the value and wants to share | ||
| it. Reach for an input when the person installing the package must supply it. | ||
|
|
||
| ## See also | ||
|
|
||
| - <Guide>packages/add-operators</Guide> | ||
| - <Guide>packages/configure-inputs</Guide> | ||
| - <Guide>packages/create-a-package</Guide> | ||
| - <Tutorial>write-a-package</Tutorial> | ||
| - <Explanation>packages</Explanation> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.