@telorun/templating 0.125.1 → 0.126.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -16
- package/dist/engines/ref.d.ts +40 -8
- package/dist/engines/ref.d.ts.map +1 -1
- package/dist/engines/ref.js +74 -8
- package/dist/expansion-tags.d.ts +50 -0
- package/dist/expansion-tags.d.ts.map +1 -0
- package/dist/expansion-tags.js +118 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/sentinel.d.ts +15 -3
- package/dist/sentinel.d.ts.map +1 -1
- package/dist/sentinel.js +8 -1
- package/dist/yaml-tags.d.ts +11 -8
- package/dist/yaml-tags.d.ts.map +1 -1
- package/dist/yaml-tags.js +11 -6
- package/package.json +3 -3
- package/src/engines/ref.ts +97 -8
- package/src/expansion-tags.ts +171 -0
- package/src/index.ts +29 -2
- package/src/sentinel.ts +18 -2
- package/src/yaml-tags.ts +17 -9
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
<h1 align="center">Telo</h1>
|
|
6
6
|
|
|
7
|
-
<p align="center">Runtime for declarative applications —
|
|
7
|
+
<p align="center">Runtime for declarative applications — backend services and the data screens on top of them.</p>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
10
|
<a href="https://github.com/telorun/telo/actions/workflows/test.yml"><img alt="Tests" src="https://github.com/telorun/telo/actions/workflows/test.yml/badge.svg" /></a>
|
|
@@ -17,30 +17,45 @@
|
|
|
17
17
|
<img alt="Changesets" src="https://img.shields.io/badge/maintained%20with-changesets-176de3" />
|
|
18
18
|
</p>
|
|
19
19
|
|
|
20
|
-
Telo
|
|
20
|
+
Telo runs applications described in YAML files instead of written as program code. You declare your web APIs, databases, data screens (tables, forms, create/edit/delete) and AI workflows as small, connected YAML documents, and Telo checks them and runs them. There is no drag-and-drop screen builder: screens are declared in YAML like everything else.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Today you extend Telo in TypeScript (Rust for some native modules); the runtime is designed so other languages can be added.
|
|
23
|
+
|
|
24
|
+
**Where Telo is today:** the core runtime, module system, standard library and checker work, but Telo is pre-1.0 and not yet recommended for production use. See [Status](#status).
|
|
25
|
+
|
|
26
|
+
Deciding whether Telo fits your team? Start with [Compared to low-code platforms](https://telo.run/learn/vs-low-code), [Licensing](https://telo.run/learn/licensing) and the [examples](./examples/INDEX.md).
|
|
23
27
|
|
|
24
28
|
```bash
|
|
25
29
|
# Reconcile your manifest into a running application
|
|
26
30
|
$ telo ./examples/todo-app
|
|
27
31
|
|
|
28
|
-
{"level":
|
|
32
|
+
{"time":"2026-11-01T10:29:16.386Z","level":"INFO","severity":9,"msg":"Listening","resource":{"kind":"HttpServer.Server","name":"server","id":"HttpServer.Server.server"},"attributes":{"server.address":"0.0.0.0","server.port":8077,"url.scheme":"http"},"event_name":"http.server.started"}
|
|
29
33
|
```
|
|
30
34
|
|
|
35
|
+
(On a terminal the same record prints human-readable and coloured.)
|
|
36
|
+
|
|
37
|
+
## Get started
|
|
38
|
+
|
|
39
|
+
Install the CLI — a single executable that carries its own runtime:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
curl -fsSL https://telo.sh/install.sh | sh
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
On Windows, `irm https://telo.sh/install.ps1 | iex`. With Node.js already installed, `npm install -g @telorun/cli` gives the same `telo`.
|
|
46
|
+
|
|
47
|
+
Then follow [**Getting Started**](https://telo.run/learn/getting-started) on [telo.run](https://telo.run) — install, a first manifest, and what the kernel did with it — and [Your first HTTP API](https://telo.run/learn/first-http-api). Modules and their kinds are discoverable on the [hub](https://hub.telo.run).
|
|
48
|
+
|
|
31
49
|
## Why use Telo?
|
|
32
50
|
|
|
33
|
-
- **Open
|
|
34
|
-
- **
|
|
35
|
-
- **
|
|
36
|
-
- **
|
|
51
|
+
- **Open standards:** built on YAML, JSON Schema and CEL (a small expression language) — no proprietary language.
|
|
52
|
+
- **Mistakes caught before the app runs:** wrong references, wrong value types and missing inputs are reported with the exact place to fix, in the editor or in CI.
|
|
53
|
+
- **Everything is a module:** Telo itself knows nothing about HTTP or SQL. Each capability is a module you import and combine, with declared inputs and secrets.
|
|
54
|
+
- **Other runtimes are possible:** the runtime is Node.js today, with a shared YAML contract so other implementations can run the same manifests. A narrower Rust runtime exists and is in progress.
|
|
37
55
|
|
|
38
56
|
## What It Does
|
|
39
57
|
|
|
40
|
-
|
|
41
|
-
- **Resolves** resource dependencies via a multi-pass init loop, handling ordering automatically.
|
|
42
|
-
- **Indexes** resources by Kind and Name for constant-time lookup.
|
|
43
|
-
- **Dispatches** execution to the controller that owns each Kind.
|
|
58
|
+
Telo reads your YAML files, checks them, works out what depends on what, starts everything in the right order, and hands each piece to the code that implements it. For the mechanics, see [How Telo works](https://telo.run/learn/how-telo-works).
|
|
44
59
|
|
|
45
60
|
## Example manifest
|
|
46
61
|
|
|
@@ -48,9 +63,9 @@ See [examples/](./examples/) for a list of working applications.
|
|
|
48
63
|
|
|
49
64
|
## Status
|
|
50
65
|
|
|
51
|
-
Telo is
|
|
66
|
+
Telo is pre-1.0 on purpose. The core runtime, module system, standard library and checker work, but Telo is not yet recommended for production use. Breaking changes ship in **minor** releases: manifest shapes and module definitions can change between versions, so pin your imports and expect to update manifests when you upgrade (`telo migrate` rewrites old spellings for you).
|
|
52
67
|
|
|
53
|
-
**1.0
|
|
68
|
+
**1.0 is planned for when the second, Rust-based runtime can run everything the Node.js one can** — that is the milestone that freezes the manifest format across runtimes.
|
|
54
69
|
|
|
55
70
|
## The Meaning of Telo
|
|
56
71
|
|
|
@@ -76,8 +91,7 @@ At kernel execution time, execution is always routed by **Kind.Name**. The kerne
|
|
|
76
91
|
|
|
77
92
|
## Architecture
|
|
78
93
|
|
|
79
|
-
The architecture is inspired by Kubernetes-style manifests: declarative resources, explicit kinds, and a
|
|
80
|
-
Those manifests were taken to the next level by allowing them to run inside a standalone runtime host.
|
|
94
|
+
The architecture is inspired by Kubernetes-style manifests: declarative resources, explicit kinds, and a runtime that routes work based on those definitions. Unlike Kubernetes, the manifests run inside one standalone process rather than describing a cluster.
|
|
81
95
|
|
|
82
96
|
## See more at
|
|
83
97
|
|
|
@@ -87,6 +101,8 @@ Those manifests were taken to the next level by allowing them to run inside a st
|
|
|
87
101
|
|
|
88
102
|
## License
|
|
89
103
|
|
|
104
|
+
In plain terms: Telo is free to use, including commercially. You can build, host and sell your own products on it. You cannot offer Telo itself to others as a managed service without a commercial licence from CodeNet Sp. z o.o. (contact@codenet.pl). The modules, SDK, examples and docs you build with are MIT. Details: [Licensing](https://telo.run/learn/licensing).
|
|
105
|
+
|
|
90
106
|
Two licenses, split by directory. Each MIT directory carries its own `LICENSE`; everything else falls under the root [LICENSE](https://github.com/telorun/telo/blob/main/LICENSE), the Sustainable Use License.
|
|
91
107
|
|
|
92
108
|
| MIT | Sustainable Use |
|
package/dist/engines/ref.d.ts
CHANGED
|
@@ -1,12 +1,44 @@
|
|
|
1
1
|
import type { TemplatingEngine } from "../engine.js";
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/** One `[<binding>.key]` / `[<binding>.index]` of an indexed `!ref`. */
|
|
3
|
+
export interface RefIndex {
|
|
4
|
+
readonly binding: string;
|
|
5
|
+
readonly member: "key" | "index";
|
|
6
|
+
}
|
|
7
|
+
/** What a `!ref` names: `<name>`, `<Alias>.<name>`, or a family member
|
|
8
|
+
* `<family>[<binding>.key|index]…`. `alias` is kept as written (`Self`
|
|
9
|
+
* included); a reader that treats `Self` as local says so itself. */
|
|
10
|
+
export interface ParsedRef {
|
|
11
|
+
readonly alias?: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly indexes: readonly RefIndex[];
|
|
14
|
+
}
|
|
15
|
+
export declare const REF_MEMBER_INVALID = "REF_MEMBER_INVALID";
|
|
16
|
+
/** A `!ref` text outside the grammar, with the offset of its first inadmissible
|
|
17
|
+
* character. */
|
|
18
|
+
export interface RefRefusal {
|
|
19
|
+
readonly code: typeof REF_MEMBER_INVALID;
|
|
20
|
+
readonly offset: number;
|
|
21
|
+
}
|
|
22
|
+
export type RefVerdict = {
|
|
23
|
+
parsed: ParsedRef;
|
|
24
|
+
} | {
|
|
25
|
+
refusal: RefRefusal;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The `!ref` grammar — its one reader. Three forms: `<name>`, `<Alias>.<name>`,
|
|
29
|
+
* and `<family>` followed by one or more `[<binding>.key]` / `[<binding>.index]`.
|
|
30
|
+
* Every name follows the identifier grammar, no whitespace is allowed and there
|
|
31
|
+
* is no quoting rule inside the text; an alias with an index is refused.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseRefSource(source: string): RefVerdict;
|
|
34
|
+
/** The sentence a refused `!ref` is reported with, naming where it goes wrong. */
|
|
35
|
+
export declare function refRefusalMessage(source: string, refusal: RefRefusal): string;
|
|
36
|
+
/** The `!ref` engine. Marks a tagged scalar as a resource reference; the loader
|
|
37
|
+
* attaches this engine's verdict ({@link parseRefSource}) to the sentinel, and
|
|
38
|
+
* every reader reads that verdict rather than the text.
|
|
5
39
|
*
|
|
6
|
-
* Returns the source string verbatim at compile time. The
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* through the parse/serialize round-trip. Analysis is a no-op; reference
|
|
10
|
-
* resolution is the analyzer's responsibility via the ref-aware walker. */
|
|
40
|
+
* Returns the source string verbatim at compile time. The lookup (sentinel →
|
|
41
|
+
* live ResourceInstance) happens at resource-context init; analysis is a no-op
|
|
42
|
+
* here, since reference resolution is the analyzer's. */
|
|
11
43
|
export declare const refEngine: TemplatingEngine;
|
|
12
44
|
//# sourceMappingURL=ref.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../../src/engines/ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD
|
|
1
|
+
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../../src/engines/ref.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,wEAAwE;AACxE,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,KAAK,GAAG,OAAO,CAAC;CAClC;AAED;;sEAEsE;AACtE,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,SAAS,QAAQ,EAAE,CAAC;CACvC;AAED,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AAEvD;iBACiB;AACjB,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,OAAO,kBAAkB,CAAC;IACzC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,MAAM,UAAU,GAAG;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,UAAU,CAAA;CAAE,CAAC;AAKzE;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAyCzD;AAED,kFAAkF;AAClF,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAG,MAAM,CAU7E;AAED;;;;;;0DAM0D;AAC1D,eAAO,MAAM,SAAS,EAAE,gBAUvB,CAAC"}
|
package/dist/engines/ref.js
CHANGED
|
@@ -1,12 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export const REF_MEMBER_INVALID = "REF_MEMBER_INVALID";
|
|
2
|
+
const IDENT_START = /[A-Za-z_]/;
|
|
3
|
+
const IDENT_PART = /[A-Za-z0-9_]/;
|
|
4
|
+
/**
|
|
5
|
+
* The `!ref` grammar — its one reader. Three forms: `<name>`, `<Alias>.<name>`,
|
|
6
|
+
* and `<family>` followed by one or more `[<binding>.key]` / `[<binding>.index]`.
|
|
7
|
+
* Every name follows the identifier grammar, no whitespace is allowed and there
|
|
8
|
+
* is no quoting rule inside the text; an alias with an index is refused.
|
|
9
|
+
*/
|
|
10
|
+
export function parseRefSource(source) {
|
|
11
|
+
let pos = 0;
|
|
12
|
+
const refuse = (offset) => ({
|
|
13
|
+
refusal: { code: REF_MEMBER_INVALID, offset },
|
|
14
|
+
});
|
|
15
|
+
const identifier = () => {
|
|
16
|
+
if (pos >= source.length || !IDENT_START.test(source[pos]))
|
|
17
|
+
return undefined;
|
|
18
|
+
const start = pos;
|
|
19
|
+
pos++;
|
|
20
|
+
while (pos < source.length && IDENT_PART.test(source[pos]))
|
|
21
|
+
pos++;
|
|
22
|
+
return source.slice(start, pos);
|
|
23
|
+
};
|
|
24
|
+
const head = identifier();
|
|
25
|
+
if (head === undefined)
|
|
26
|
+
return refuse(pos);
|
|
27
|
+
if (pos === source.length)
|
|
28
|
+
return { parsed: { name: head, indexes: [] } };
|
|
29
|
+
if (source[pos] === ".") {
|
|
30
|
+
pos++;
|
|
31
|
+
const name = identifier();
|
|
32
|
+
if (name === undefined)
|
|
33
|
+
return refuse(pos);
|
|
34
|
+
if (pos !== source.length)
|
|
35
|
+
return refuse(pos);
|
|
36
|
+
return { parsed: { alias: head, name, indexes: [] } };
|
|
37
|
+
}
|
|
38
|
+
const indexes = [];
|
|
39
|
+
while (pos < source.length) {
|
|
40
|
+
if (source[pos] !== "[")
|
|
41
|
+
return refuse(pos);
|
|
42
|
+
pos++;
|
|
43
|
+
const binding = identifier();
|
|
44
|
+
if (binding === undefined)
|
|
45
|
+
return refuse(pos);
|
|
46
|
+
if (source[pos] !== ".")
|
|
47
|
+
return refuse(pos);
|
|
48
|
+
pos++;
|
|
49
|
+
const memberStart = pos;
|
|
50
|
+
const member = identifier();
|
|
51
|
+
if (member !== "key" && member !== "index")
|
|
52
|
+
return refuse(memberStart);
|
|
53
|
+
if (source[pos] !== "]")
|
|
54
|
+
return refuse(pos);
|
|
55
|
+
pos++;
|
|
56
|
+
indexes.push({ binding, member });
|
|
57
|
+
}
|
|
58
|
+
return { parsed: { name: head, indexes } };
|
|
59
|
+
}
|
|
60
|
+
/** The sentence a refused `!ref` is reported with, naming where it goes wrong. */
|
|
61
|
+
export function refRefusalMessage(source, refusal) {
|
|
62
|
+
const at = refusal.offset >= source.length
|
|
63
|
+
? "it ends too early"
|
|
64
|
+
: `'${source[refusal.offset]}' at offset ${refusal.offset} is not allowed there`;
|
|
65
|
+
return (`'!ref ${source}' is not a reference: ${at}. A reference is '<name>', '<Alias>.<name>', ` +
|
|
66
|
+
`or '<family>[<binding>.key]' / '<family>[<binding>.index]' (one index per family depth, ` +
|
|
67
|
+
`no alias), with no whitespace.`);
|
|
68
|
+
}
|
|
69
|
+
/** The `!ref` engine. Marks a tagged scalar as a resource reference; the loader
|
|
70
|
+
* attaches this engine's verdict ({@link parseRefSource}) to the sentinel, and
|
|
71
|
+
* every reader reads that verdict rather than the text.
|
|
4
72
|
*
|
|
5
|
-
* Returns the source string verbatim at compile time. The
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* through the parse/serialize round-trip. Analysis is a no-op; reference
|
|
9
|
-
* resolution is the analyzer's responsibility via the ref-aware walker. */
|
|
73
|
+
* Returns the source string verbatim at compile time. The lookup (sentinel →
|
|
74
|
+
* live ResourceInstance) happens at resource-context init; analysis is a no-op
|
|
75
|
+
* here, since reference resolution is the analyzer's. */
|
|
10
76
|
export const refEngine = {
|
|
11
77
|
name: "ref",
|
|
12
78
|
compile(source) {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type CollectionTag, type Document } from "yaml";
|
|
2
|
+
/**
|
|
3
|
+
* The expansion tags — `!if`, `!switch`, `!for-each` — a closed tag family beside
|
|
4
|
+
* the expression engines and outside their contract. Each sits on a mapping and
|
|
5
|
+
* loads as that same mapping, its keys in written order after the marker
|
|
6
|
+
* {@link EXPANSION_MARKER} naming the tag. Position travels by path, so a key
|
|
7
|
+
* inside a branch (`cases.postgres.<typo>`) is a path in the loaded tree as in
|
|
8
|
+
* the YAML.
|
|
9
|
+
*/
|
|
10
|
+
export declare const EXPANSION_TAGS: readonly ["if", "switch", "for-each"];
|
|
11
|
+
export type ExpansionTag = (typeof EXPANSION_TAGS)[number];
|
|
12
|
+
/** The key a loaded expansion mapping carries its tag under. */
|
|
13
|
+
export declare const EXPANSION_MARKER = "__expand";
|
|
14
|
+
/** The mapping key that splices a `!for-each`'s entries into its mapping. A
|
|
15
|
+
* plain key to every loader; its meaning is the analyzer's. */
|
|
16
|
+
export declare const SPLICE_KEY = "...";
|
|
17
|
+
/** An expansion tag's mapping as loaded: the marker plus the written keys. */
|
|
18
|
+
export type ExpansionSentinel = {
|
|
19
|
+
readonly [EXPANSION_MARKER]: ExpansionTag;
|
|
20
|
+
} & Record<string, unknown>;
|
|
21
|
+
export declare function isExpansionTag(name: unknown): name is ExpansionTag;
|
|
22
|
+
/** True when `v` is a loaded expansion mapping — a plain object carrying the
|
|
23
|
+
* marker with one of the three tag names. */
|
|
24
|
+
export declare function isExpansionSentinel(v: unknown): v is ExpansionSentinel;
|
|
25
|
+
/** The tag a value was written behind, or undefined. */
|
|
26
|
+
export declare function expansionTagOf(v: unknown): ExpansionTag | undefined;
|
|
27
|
+
/** The tag as written (`!if`) of an expansion mapping, or undefined. */
|
|
28
|
+
export declare function expansionTagLabel(v: unknown): string | undefined;
|
|
29
|
+
/** The written keys of an expansion mapping, marker removed, in written order. */
|
|
30
|
+
export declare function expansionBody(v: ExpansionSentinel): Record<string, unknown>;
|
|
31
|
+
/** The code a shape refusal of `tag` is reported under. */
|
|
32
|
+
export declare function expansionShapeCode(tag: ExpansionTag): "BRANCH_SHAPE_INVALID" | "REPEAT_SHAPE_INVALID";
|
|
33
|
+
/** The `customTags` entries of the expansion tags: one mapping tag each, whose
|
|
34
|
+
* node class loads the mapping with its marker and writes it back behind its
|
|
35
|
+
* tag. */
|
|
36
|
+
export declare function buildExpansionTags(): CollectionTag[];
|
|
37
|
+
/** An expansion tag the YAML itself refuses: one written on a scalar or a
|
|
38
|
+
* sequence, or a tagged mapping writing the marker key itself. */
|
|
39
|
+
export interface ExpansionTagMisuse {
|
|
40
|
+
readonly tag: ExpansionTag;
|
|
41
|
+
readonly code: "BRANCH_SHAPE_INVALID" | "REPEAT_SHAPE_INVALID";
|
|
42
|
+
readonly message: string;
|
|
43
|
+
/** The node's character range in the parsed text, `[start, end]`. */
|
|
44
|
+
readonly range?: readonly [number, number];
|
|
45
|
+
}
|
|
46
|
+
/** Every misuse of an expansion tag in one parsed document — what no loaded
|
|
47
|
+
* tree can show, since a tag on a scalar or a sequence loads as the bare
|
|
48
|
+
* value. Reported at load by every loader. */
|
|
49
|
+
export declare function expansionTagMisuse(document: Document): ExpansionTagMisuse[];
|
|
50
|
+
//# sourceMappingURL=expansion-tags.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expansion-tags.d.ts","sourceRoot":"","sources":["../src/expansion-tags.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,aAAa,EAClB,KAAK,QAAQ,EAGd,MAAM,MAAM,CAAC;AAGd;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,uCAAwC,CAAC;AACpE,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAE3D,gEAAgE;AAChE,eAAO,MAAM,gBAAgB,aAAa,CAAC;AAE3C;gEACgE;AAChE,eAAO,MAAM,UAAU,QAAQ,CAAC;AAEhC,8EAA8E;AAC9E,MAAM,MAAM,iBAAiB,GAAG;IAAE,QAAQ,CAAC,CAAC,gBAAgB,CAAC,EAAE,YAAY,CAAA;CAAE,GAAG,MAAM,CACpF,MAAM,EACN,OAAO,CACR,CAAC;AAEF,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,YAAY,CAElE;AAED;8CAC8C;AAC9C,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,iBAAiB,CAOtE;AAED,wDAAwD;AACxD,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,GAAG,YAAY,GAAG,SAAS,CAEnE;AAED,wEAAwE;AACxE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAGhE;AAED,kFAAkF;AAClF,wBAAgB,aAAa,CAAC,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE3E;AAED,2DAA2D;AAC3D,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,YAAY,GAAG,sBAAsB,GAAG,sBAAsB,CAErG;AAkCD;;WAEW;AACX,wBAAgB,kBAAkB,IAAI,aAAa,EAAE,CAYpD;AAED;mEACmE;AACnE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,GAAG,EAAE,YAAY,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,sBAAsB,GAAG,sBAAsB,CAAC;IAC/D,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5C;AAED;;+CAE+C;AAC/C,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,GAAG,kBAAkB,EAAE,CAmC3E"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { isMap, isScalar, isSeq, visit, YAMLMap, } from "yaml";
|
|
2
|
+
import { createPair } from "yaml/util";
|
|
3
|
+
/**
|
|
4
|
+
* The expansion tags — `!if`, `!switch`, `!for-each` — a closed tag family beside
|
|
5
|
+
* the expression engines and outside their contract. Each sits on a mapping and
|
|
6
|
+
* loads as that same mapping, its keys in written order after the marker
|
|
7
|
+
* {@link EXPANSION_MARKER} naming the tag. Position travels by path, so a key
|
|
8
|
+
* inside a branch (`cases.postgres.<typo>`) is a path in the loaded tree as in
|
|
9
|
+
* the YAML.
|
|
10
|
+
*/
|
|
11
|
+
export const EXPANSION_TAGS = ["if", "switch", "for-each"];
|
|
12
|
+
/** The key a loaded expansion mapping carries its tag under. */
|
|
13
|
+
export const EXPANSION_MARKER = "__expand";
|
|
14
|
+
/** The mapping key that splices a `!for-each`'s entries into its mapping. A
|
|
15
|
+
* plain key to every loader; its meaning is the analyzer's. */
|
|
16
|
+
export const SPLICE_KEY = "...";
|
|
17
|
+
export function isExpansionTag(name) {
|
|
18
|
+
return typeof name === "string" && EXPANSION_TAGS.includes(name);
|
|
19
|
+
}
|
|
20
|
+
/** True when `v` is a loaded expansion mapping — a plain object carrying the
|
|
21
|
+
* marker with one of the three tag names. */
|
|
22
|
+
export function isExpansionSentinel(v) {
|
|
23
|
+
return (v !== null &&
|
|
24
|
+
typeof v === "object" &&
|
|
25
|
+
!Array.isArray(v) &&
|
|
26
|
+
isExpansionTag(v[EXPANSION_MARKER]));
|
|
27
|
+
}
|
|
28
|
+
/** The tag a value was written behind, or undefined. */
|
|
29
|
+
export function expansionTagOf(v) {
|
|
30
|
+
return isExpansionSentinel(v) ? v[EXPANSION_MARKER] : undefined;
|
|
31
|
+
}
|
|
32
|
+
/** The tag as written (`!if`) of an expansion mapping, or undefined. */
|
|
33
|
+
export function expansionTagLabel(v) {
|
|
34
|
+
const tag = expansionTagOf(v);
|
|
35
|
+
return tag ? `!${tag}` : undefined;
|
|
36
|
+
}
|
|
37
|
+
/** The written keys of an expansion mapping, marker removed, in written order. */
|
|
38
|
+
export function expansionBody(v) {
|
|
39
|
+
return Object.fromEntries(Object.entries(v).filter(([key]) => key !== EXPANSION_MARKER));
|
|
40
|
+
}
|
|
41
|
+
/** The code a shape refusal of `tag` is reported under. */
|
|
42
|
+
export function expansionShapeCode(tag) {
|
|
43
|
+
return tag === "for-each" ? "REPEAT_SHAPE_INVALID" : "BRANCH_SHAPE_INVALID";
|
|
44
|
+
}
|
|
45
|
+
function expansionMapClass(tag) {
|
|
46
|
+
return class ExpansionMap extends YAMLMap {
|
|
47
|
+
// The marker first, then the written keys: the order both loaders produce.
|
|
48
|
+
toJSON(_, ctx, Type) {
|
|
49
|
+
const loaded = super.toJSON(_, ctx, Type);
|
|
50
|
+
if (loaded instanceof Map) {
|
|
51
|
+
return new Map([[EXPANSION_MARKER, tag], ...loaded]);
|
|
52
|
+
}
|
|
53
|
+
return { [EXPANSION_MARKER]: tag, ...loaded };
|
|
54
|
+
}
|
|
55
|
+
// A node built from a loaded mapping writes `metadata` first, then every
|
|
56
|
+
// other written key, and never the marker.
|
|
57
|
+
static from(schema, obj, ctx) {
|
|
58
|
+
const map = new this(schema);
|
|
59
|
+
const body = isExpansionSentinel(obj) ? expansionBody(obj) : obj;
|
|
60
|
+
const keys = Object.keys(body);
|
|
61
|
+
const ordered = keys.includes("metadata")
|
|
62
|
+
? ["metadata", ...keys.filter((k) => k !== "metadata")]
|
|
63
|
+
: keys;
|
|
64
|
+
for (const key of ordered) {
|
|
65
|
+
const value = body[key];
|
|
66
|
+
if (value !== undefined || ctx.keepUndefined)
|
|
67
|
+
map.items.push(createPair(key, value, ctx));
|
|
68
|
+
}
|
|
69
|
+
return map;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** The `customTags` entries of the expansion tags: one mapping tag each, whose
|
|
74
|
+
* node class loads the mapping with its marker and writes it back behind its
|
|
75
|
+
* tag. */
|
|
76
|
+
export function buildExpansionTags() {
|
|
77
|
+
return EXPANSION_TAGS.map((tag) => {
|
|
78
|
+
const nodeClass = expansionMapClass(tag);
|
|
79
|
+
return {
|
|
80
|
+
tag: `!${tag}`,
|
|
81
|
+
collection: "map",
|
|
82
|
+
nodeClass,
|
|
83
|
+
identify: (v) => expansionTagOf(v) === tag,
|
|
84
|
+
createNode: (schema, value, ctx) => nodeClass.from(schema, value, ctx),
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** Every misuse of an expansion tag in one parsed document — what no loaded
|
|
89
|
+
* tree can show, since a tag on a scalar or a sequence loads as the bare
|
|
90
|
+
* value. Reported at load by every loader. */
|
|
91
|
+
export function expansionTagMisuse(document) {
|
|
92
|
+
const found = [];
|
|
93
|
+
const report = (node, tag, message) => {
|
|
94
|
+
found.push({
|
|
95
|
+
tag,
|
|
96
|
+
code: expansionShapeCode(tag),
|
|
97
|
+
message,
|
|
98
|
+
...(node.range ? { range: [node.range[0], node.range[1]] } : {}),
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
visit(document, (_key, node) => {
|
|
102
|
+
if (!isScalar(node) && !isSeq(node) && !isMap(node))
|
|
103
|
+
return;
|
|
104
|
+
const name = node.tag?.startsWith("!") ? node.tag.slice(1) : undefined;
|
|
105
|
+
if (!isExpansionTag(name))
|
|
106
|
+
return;
|
|
107
|
+
if (isMap(node)) {
|
|
108
|
+
const markerKey = node.items.find((pair) => isScalar(pair.key) && pair.key.value === EXPANSION_MARKER);
|
|
109
|
+
if (markerKey) {
|
|
110
|
+
report(node, name, `'!${name}' holds a key '${EXPANSION_MARKER}', which is reserved for the tag's own marker. Remove it.`);
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
report(node, name, `'!${name}' is written on ${isSeq(node) ? "a sequence" : "a scalar"}; it takes a mapping ` +
|
|
115
|
+
`(${name === "for-each" ? "'{ in, as?, emit }'" : name === "if" ? "'{ if, then, else? }'" : "'{ switch, cases, default? }'"}).`);
|
|
116
|
+
});
|
|
117
|
+
return found;
|
|
118
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,11 +11,12 @@ export { includeBytesEngine, includeTextEngine, normalizeIncludePath, normalizeM
|
|
|
11
11
|
export { interpolateEngine } from "./engines/interpolate.js";
|
|
12
12
|
export { literalEngine } from "./engines/literal.js";
|
|
13
13
|
export { modulePathEngine } from "./engines/module-path.js";
|
|
14
|
-
export { refEngine } from "./engines/ref.js";
|
|
14
|
+
export { parseRefSource, REF_MEMBER_INVALID, refEngine, refRefusalMessage, type ParsedRef, type RefIndex, type RefRefusal, type RefVerdict, } from "./engines/ref.js";
|
|
15
|
+
export { buildExpansionTags, EXPANSION_MARKER, EXPANSION_TAGS, expansionBody, expansionShapeCode, expansionTagLabel, expansionTagMisuse, expansionTagOf, isExpansionSentinel, isExpansionTag, SPLICE_KEY, type ExpansionSentinel, type ExpansionTag, type ExpansionTagMisuse, } from "./expansion-tags.js";
|
|
15
16
|
export { sqlEngine, isParameterizedSql, type ParameterizedSql } from "./engines/sql.js";
|
|
16
17
|
export { TemplatingEngineRegistry } from "./registry.js";
|
|
17
18
|
export { builtinEngines, celExpressionsOf, createDefaultRegistry, defaultRegistry, producedTypeOf, repeatableSource, } from "./builtins.js";
|
|
18
19
|
export type { AnalyzeEnv, AnalyzeResult, CallArgument, CallSite, CompileEnv, DiagnosticFix, EngineDiagnostic, EngineFileClaim, ExpressionRegion, TemplatingEngine, } from "./engine.js";
|
|
19
|
-
export { CEL_ENGINE, INCLUDE_BYTES_ENGINE, INCLUDE_ENGINE_NAMES, INCLUDE_TEXT_ENGINE, isIncludeSentinel, isModulePathSentinel, isRefSentinel, isTaggedSentinel, makeTaggedSentinel, MODULE_FILE_ENGINE_NAMES, MODULE_PATH_ENGINE, plainChainOf, type TaggedSentinel, } from "./sentinel.js";
|
|
20
|
-
export { buildCustomTags, defaultCustomTags } from "./yaml-tags.js";
|
|
20
|
+
export { CEL_ENGINE, INCLUDE_BYTES_ENGINE, INCLUDE_ENGINE_NAMES, INCLUDE_TEXT_ENGINE, isIncludeSentinel, isModulePathSentinel, isRefSentinel, isTaggedSentinel, makeTaggedSentinel, MODULE_FILE_ENGINE_NAMES, MODULE_PATH_ENGINE, plainChainOf, REF_ENGINE, type RefSentinel, type TaggedSentinel, } from "./sentinel.js";
|
|
21
|
+
export { buildCustomTags, defaultCustomTags, type ManifestTag } from "./yaml-tags.js";
|
|
21
22
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EACxB,aAAa,EACb,0BAA0B,GAC3B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,KAAK,SAAS,EACd,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,cAAc,EACd,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,GACjB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,qBAAqB,GAC3B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,2BAA2B,EAC3B,mBAAmB,EACnB,kBAAkB,EAClB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,KAAK,cAAc,GACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,wBAAwB,EACxB,aAAa,EACb,0BAA0B,GAC3B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,KAAK,SAAS,EACd,KAAK,eAAe,GACrB,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,eAAe,EACf,YAAY,EACZ,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,GAC5B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EACL,cAAc,EACd,YAAY,EACZ,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,GACjB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,qBAAqB,GAC3B,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,SAAS,EACT,iBAAiB,EACjB,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,UAAU,GAChB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACd,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,UAAU,EACV,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,kBAAkB,GACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAExF,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,eAAe,EACf,cAAc,EACd,gBAAgB,GACjB,MAAM,eAAe,CAAC;AACvB,YAAY,EACV,UAAU,EACV,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,UAAU,EACV,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,YAAY,EACZ,UAAU,EACV,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,KAAK,WAAW,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -20,9 +20,10 @@ export { includeBytesEngine, includeTextEngine, normalizeIncludePath, normalizeM
|
|
|
20
20
|
export { interpolateEngine } from "./engines/interpolate.js";
|
|
21
21
|
export { literalEngine } from "./engines/literal.js";
|
|
22
22
|
export { modulePathEngine } from "./engines/module-path.js";
|
|
23
|
-
export { refEngine } from "./engines/ref.js";
|
|
23
|
+
export { parseRefSource, REF_MEMBER_INVALID, refEngine, refRefusalMessage, } from "./engines/ref.js";
|
|
24
|
+
export { buildExpansionTags, EXPANSION_MARKER, EXPANSION_TAGS, expansionBody, expansionShapeCode, expansionTagLabel, expansionTagMisuse, expansionTagOf, isExpansionSentinel, isExpansionTag, SPLICE_KEY, } from "./expansion-tags.js";
|
|
24
25
|
export { sqlEngine, isParameterizedSql } from "./engines/sql.js";
|
|
25
26
|
export { TemplatingEngineRegistry } from "./registry.js";
|
|
26
27
|
export { builtinEngines, celExpressionsOf, createDefaultRegistry, defaultRegistry, producedTypeOf, repeatableSource, } from "./builtins.js";
|
|
27
|
-
export { CEL_ENGINE, INCLUDE_BYTES_ENGINE, INCLUDE_ENGINE_NAMES, INCLUDE_TEXT_ENGINE, isIncludeSentinel, isModulePathSentinel, isRefSentinel, isTaggedSentinel, makeTaggedSentinel, MODULE_FILE_ENGINE_NAMES, MODULE_PATH_ENGINE, plainChainOf, } from "./sentinel.js";
|
|
28
|
+
export { CEL_ENGINE, INCLUDE_BYTES_ENGINE, INCLUDE_ENGINE_NAMES, INCLUDE_TEXT_ENGINE, isIncludeSentinel, isModulePathSentinel, isRefSentinel, isTaggedSentinel, makeTaggedSentinel, MODULE_FILE_ENGINE_NAMES, MODULE_PATH_ENGINE, plainChainOf, REF_ENGINE, } from "./sentinel.js";
|
|
28
29
|
export { buildCustomTags, defaultCustomTags } from "./yaml-tags.js";
|
package/dist/sentinel.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ParsedRef, type RefRefusal } from "./engines/ref.js";
|
|
1
2
|
/** Sentinel object produced by the YAML loader for a tagged scalar (e.g. `!cel
|
|
2
3
|
* 'variables.port'`). Travels through the manifest tree as the parsed value;
|
|
3
4
|
* precompile and the analyzer key off `__tagged === true` to dispatch to the
|
|
@@ -8,17 +9,28 @@ export interface TaggedSentinel {
|
|
|
8
9
|
readonly __tagged: true;
|
|
9
10
|
readonly engine: string;
|
|
10
11
|
readonly source: string;
|
|
12
|
+
/** `!ref` only: what the text names, by the `!ref` engine's grammar. */
|
|
13
|
+
readonly parsed?: ParsedRef;
|
|
14
|
+
/** `!ref` only: the text is outside the grammar. */
|
|
15
|
+
readonly refusal?: RefRefusal;
|
|
11
16
|
}
|
|
17
|
+
/** A `!ref` sentinel, carrying the engine's verdict on its text — exactly one
|
|
18
|
+
* of `parsed` / `refusal`, attached when the sentinel is made. */
|
|
19
|
+
export type RefSentinel = TaggedSentinel & {
|
|
20
|
+
readonly engine: "ref";
|
|
21
|
+
};
|
|
12
22
|
export declare function isTaggedSentinel(v: unknown): v is TaggedSentinel;
|
|
23
|
+
/** The sentinel a tagged scalar becomes. A `!ref` one carries the `!ref`
|
|
24
|
+
* engine's verdict on its text, so no reader keeps a grammar of its own. */
|
|
13
25
|
export declare function makeTaggedSentinel(engine: string, source: string): TaggedSentinel;
|
|
26
|
+
/** The `!ref` engine's name. */
|
|
27
|
+
export declare const REF_ENGINE = "ref";
|
|
14
28
|
/** True when `v` is a `!ref <name>` sentinel — i.e. a resource reference
|
|
15
29
|
* marked at parse time. Downstream walkers (analyzer ref validation,
|
|
16
30
|
* inline normalization, dependency graph, kernel resource resolution)
|
|
17
31
|
* use this to tell a reference from an inline definition without
|
|
18
32
|
* inferring intent from field presence. */
|
|
19
|
-
export declare function isRefSentinel(v: unknown): v is
|
|
20
|
-
engine: "ref";
|
|
21
|
-
};
|
|
33
|
+
export declare function isRefSentinel(v: unknown): v is RefSentinel;
|
|
22
34
|
/** The CEL engine's name. Beside the other sentinel predicates for the same
|
|
23
35
|
* reason they are: a consumer that spells an engine name inline is a second
|
|
24
36
|
* place the registry's key is written down. */
|
package/dist/sentinel.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sentinel.d.ts","sourceRoot":"","sources":["../src/sentinel.ts"],"names":[],"mappings":"AAAA;;;;;YAKY;AACZ,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"sentinel.d.ts","sourceRoot":"","sources":["../src/sentinel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,SAAS,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnF;;;;;YAKY;AACZ,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,wEAAwE;IACxE,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;IAC5B,oDAAoD;IACpD,QAAQ,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC;CAC/B;AAED;mEACmE;AACnE,MAAM,MAAM,WAAW,GAAG,cAAc,GAAG;IAAE,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAA;CAAE,CAAC;AAEtE,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,cAAc,CAQhE;AAED;6EAC6E;AAC7E,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,CAGjF;AAED,gCAAgC;AAChC,eAAO,MAAM,UAAU,QAAQ,CAAC;AAEhC;;;;4CAI4C;AAC5C,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,WAAW,CAE1D;AAED;;gDAEgD;AAChD,eAAO,MAAM,UAAU,QAAQ,CAAC;AAEhC;;wCAEwC;AACxC,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAClD,eAAO,MAAM,oBAAoB,kBAAkB,CAAC;AAEpD;wDACwD;AACxD,eAAO,MAAM,kBAAkB,gBAAgB,CAAC;AAEhD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAM/D;AAED;mEACmE;AACnE,eAAO,MAAM,oBAAoB,EAAE,WAAW,CAAC,MAAM,CAGnD,CAAC;AAEH;;;;;;;qCAOqC;AACrC,wBAAgB,iBAAiB,CAC/B,CAAC,EAAE,OAAO,GACT,CAAC,IAAI,cAAc,GAAG;IAAE,MAAM,EAAE,OAAO,mBAAmB,GAAG,OAAO,oBAAoB,CAAA;CAAE,CAI5F;AAED;;6CAE6C;AAC7C,wBAAgB,oBAAoB,CAClC,CAAC,EAAE,OAAO,GACT,CAAC,IAAI,cAAc,GAAG;IAAE,MAAM,EAAE,OAAO,kBAAkB,CAAA;CAAE,CAE7D;AAED;qEACqE;AACrE,eAAO,MAAM,wBAAwB,EAAE,WAAW,CAAC,MAAM,CAIvD,CAAC"}
|
package/dist/sentinel.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseRefSource } from "./engines/ref.js";
|
|
1
2
|
export function isTaggedSentinel(v) {
|
|
2
3
|
return (v !== null &&
|
|
3
4
|
typeof v === "object" &&
|
|
@@ -5,16 +6,22 @@ export function isTaggedSentinel(v) {
|
|
|
5
6
|
typeof v.engine === "string" &&
|
|
6
7
|
typeof v.source === "string");
|
|
7
8
|
}
|
|
9
|
+
/** The sentinel a tagged scalar becomes. A `!ref` one carries the `!ref`
|
|
10
|
+
* engine's verdict on its text, so no reader keeps a grammar of its own. */
|
|
8
11
|
export function makeTaggedSentinel(engine, source) {
|
|
12
|
+
if (engine === REF_ENGINE)
|
|
13
|
+
return { __tagged: true, engine, source, ...parseRefSource(source) };
|
|
9
14
|
return { __tagged: true, engine, source };
|
|
10
15
|
}
|
|
16
|
+
/** The `!ref` engine's name. */
|
|
17
|
+
export const REF_ENGINE = "ref";
|
|
11
18
|
/** True when `v` is a `!ref <name>` sentinel — i.e. a resource reference
|
|
12
19
|
* marked at parse time. Downstream walkers (analyzer ref validation,
|
|
13
20
|
* inline normalization, dependency graph, kernel resource resolution)
|
|
14
21
|
* use this to tell a reference from an inline definition without
|
|
15
22
|
* inferring intent from field presence. */
|
|
16
23
|
export function isRefSentinel(v) {
|
|
17
|
-
return isTaggedSentinel(v) && v.engine ===
|
|
24
|
+
return isTaggedSentinel(v) && v.engine === REF_ENGINE;
|
|
18
25
|
}
|
|
19
26
|
/** The CEL engine's name. Beside the other sentinel predicates for the same
|
|
20
27
|
* reason they are: a consumer that spells an engine name inline is a second
|
package/dist/yaml-tags.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import type { ScalarTag } from "yaml";
|
|
1
|
+
import type { CollectionTag, ScalarTag } from "yaml";
|
|
2
2
|
import type { TemplatingEngineRegistry } from "./registry.js";
|
|
3
|
+
/** One YAML tag the manifest parsers recognise. */
|
|
4
|
+
export type ManifestTag = ScalarTag | CollectionTag;
|
|
3
5
|
/** Build the `customTags` array passed to `yaml`'s `parseAllDocuments` /
|
|
4
|
-
* `Document` from the registered engines. Each engine
|
|
5
|
-
* ScalarTag whose `resolve` produces a `TaggedSentinel`, and
|
|
6
|
-
* `identify` + `stringify` round-trip the sentinel back to its original
|
|
7
|
-
* `!<engine> "<source>"` form when the document is re-serialized
|
|
6
|
+
* `Document` from the registered engines plus the expansion tags. Each engine
|
|
7
|
+
* contributes one ScalarTag whose `resolve` produces a `TaggedSentinel`, and
|
|
8
|
+
* whose `identify` + `stringify` round-trip the sentinel back to its original
|
|
9
|
+
* `!<engine> "<source>"` form when the document is re-serialized; each
|
|
10
|
+
* expansion tag a mapping tag (`expansion-tags.ts`).
|
|
8
11
|
*
|
|
9
12
|
* Without `stringify`, `Document.toString()` would emit the sentinel as a
|
|
10
13
|
* YAML mapping (`{__tagged: true, engine: cel, source: ...}`), corrupting
|
|
@@ -17,12 +20,12 @@ import type { TemplatingEngineRegistry } from "./registry.js";
|
|
|
17
20
|
* hosts. Each host passes its own registry (in practice always
|
|
18
21
|
* `createDefaultRegistry()`), keeping the door open for future
|
|
19
22
|
* test-only registries. */
|
|
20
|
-
export declare function buildCustomTags(registry: TemplatingEngineRegistry):
|
|
23
|
+
export declare function buildCustomTags(registry: TemplatingEngineRegistry): ManifestTag[];
|
|
21
24
|
/** Returns `customTags` built from the default registry, freshly each call.
|
|
22
25
|
* Every `parseAllDocuments` call site in the repo calls this so they all
|
|
23
26
|
* parse the same set of tags. Built from `defaultRegistry()` (the same
|
|
24
27
|
* singleton precompile + the analyzer use) so registering a new engine on
|
|
25
28
|
* the default registry propagates to YAML parsing on the next call. The
|
|
26
|
-
* rebuild cost is negligible (one array of N small
|
|
27
|
-
export declare function defaultCustomTags():
|
|
29
|
+
* rebuild cost is negligible (one array of N small tag objects). */
|
|
30
|
+
export declare function defaultCustomTags(): ManifestTag[];
|
|
28
31
|
//# sourceMappingURL=yaml-tags.d.ts.map
|
package/dist/yaml-tags.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"yaml-tags.d.ts","sourceRoot":"","sources":["../src/yaml-tags.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"yaml-tags.d.ts","sourceRoot":"","sources":["../src/yaml-tags.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAKrD,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAE9D,mDAAmD;AACnD,MAAM,MAAM,WAAW,GAAG,SAAS,GAAG,aAAa,CAAC;AAEpD;;;;;;;;;;;;;;;;;4BAiB4B;AAC5B,wBAAgB,eAAe,CAAC,QAAQ,EAAE,wBAAwB,GAAG,WAAW,EAAE,CAKjF;AAED;;;;;qEAKqE;AACrE,wBAAgB,iBAAiB,IAAI,WAAW,EAAE,CAEjD"}
|
package/dist/yaml-tags.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { stringifyString } from "yaml/util";
|
|
2
2
|
import { defaultRegistry } from "./builtins.js";
|
|
3
|
+
import { buildExpansionTags } from "./expansion-tags.js";
|
|
3
4
|
import { isTaggedSentinel, makeTaggedSentinel } from "./sentinel.js";
|
|
4
5
|
/** Build the `customTags` array passed to `yaml`'s `parseAllDocuments` /
|
|
5
|
-
* `Document` from the registered engines. Each engine
|
|
6
|
-
* ScalarTag whose `resolve` produces a `TaggedSentinel`, and
|
|
7
|
-
* `identify` + `stringify` round-trip the sentinel back to its original
|
|
8
|
-
* `!<engine> "<source>"` form when the document is re-serialized
|
|
6
|
+
* `Document` from the registered engines plus the expansion tags. Each engine
|
|
7
|
+
* contributes one ScalarTag whose `resolve` produces a `TaggedSentinel`, and
|
|
8
|
+
* whose `identify` + `stringify` round-trip the sentinel back to its original
|
|
9
|
+
* `!<engine> "<source>"` form when the document is re-serialized; each
|
|
10
|
+
* expansion tag a mapping tag (`expansion-tags.ts`).
|
|
9
11
|
*
|
|
10
12
|
* Without `stringify`, `Document.toString()` would emit the sentinel as a
|
|
11
13
|
* YAML mapping (`{__tagged: true, engine: cel, source: ...}`), corrupting
|
|
@@ -19,14 +21,17 @@ import { isTaggedSentinel, makeTaggedSentinel } from "./sentinel.js";
|
|
|
19
21
|
* `createDefaultRegistry()`), keeping the door open for future
|
|
20
22
|
* test-only registries. */
|
|
21
23
|
export function buildCustomTags(registry) {
|
|
22
|
-
return
|
|
24
|
+
return [
|
|
25
|
+
...registry.list().map((engine) => buildTagForEngine(engine.name)),
|
|
26
|
+
...buildExpansionTags(),
|
|
27
|
+
];
|
|
23
28
|
}
|
|
24
29
|
/** Returns `customTags` built from the default registry, freshly each call.
|
|
25
30
|
* Every `parseAllDocuments` call site in the repo calls this so they all
|
|
26
31
|
* parse the same set of tags. Built from `defaultRegistry()` (the same
|
|
27
32
|
* singleton precompile + the analyzer use) so registering a new engine on
|
|
28
33
|
* the default registry propagates to YAML parsing on the next call. The
|
|
29
|
-
* rebuild cost is negligible (one array of N small
|
|
34
|
+
* rebuild cost is negligible (one array of N small tag objects). */
|
|
30
35
|
export function defaultCustomTags() {
|
|
31
36
|
return buildCustomTags(defaultRegistry());
|
|
32
37
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/templating",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.126.0",
|
|
4
4
|
"description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"re2js": "2.8.3",
|
|
40
40
|
"uuid": "14.0.1",
|
|
41
41
|
"yaml": "^2.8.3",
|
|
42
|
-
"@telorun/cel": "0.
|
|
42
|
+
"@telorun/cel": "0.126.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/d3-format": "^3.0.4",
|
|
46
46
|
"@types/node": "^20.0.0",
|
|
47
47
|
"typescript": "^5.0.0",
|
|
48
48
|
"vitest": "^2.1.8",
|
|
49
|
-
"@telorun/sdk": "0.
|
|
49
|
+
"@telorun/sdk": "0.126.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@telorun/sdk": "*"
|
package/src/engines/ref.ts
CHANGED
|
@@ -1,14 +1,103 @@
|
|
|
1
1
|
import type { TemplatingEngine } from "../engine.js";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/** One `[<binding>.key]` / `[<binding>.index]` of an indexed `!ref`. */
|
|
4
|
+
export interface RefIndex {
|
|
5
|
+
readonly binding: string;
|
|
6
|
+
readonly member: "key" | "index";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** What a `!ref` names: `<name>`, `<Alias>.<name>`, or a family member
|
|
10
|
+
* `<family>[<binding>.key|index]…`. `alias` is kept as written (`Self`
|
|
11
|
+
* included); a reader that treats `Self` as local says so itself. */
|
|
12
|
+
export interface ParsedRef {
|
|
13
|
+
readonly alias?: string;
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly indexes: readonly RefIndex[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const REF_MEMBER_INVALID = "REF_MEMBER_INVALID";
|
|
19
|
+
|
|
20
|
+
/** A `!ref` text outside the grammar, with the offset of its first inadmissible
|
|
21
|
+
* character. */
|
|
22
|
+
export interface RefRefusal {
|
|
23
|
+
readonly code: typeof REF_MEMBER_INVALID;
|
|
24
|
+
readonly offset: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type RefVerdict = { parsed: ParsedRef } | { refusal: RefRefusal };
|
|
28
|
+
|
|
29
|
+
const IDENT_START = /[A-Za-z_]/;
|
|
30
|
+
const IDENT_PART = /[A-Za-z0-9_]/;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The `!ref` grammar — its one reader. Three forms: `<name>`, `<Alias>.<name>`,
|
|
34
|
+
* and `<family>` followed by one or more `[<binding>.key]` / `[<binding>.index]`.
|
|
35
|
+
* Every name follows the identifier grammar, no whitespace is allowed and there
|
|
36
|
+
* is no quoting rule inside the text; an alias with an index is refused.
|
|
37
|
+
*/
|
|
38
|
+
export function parseRefSource(source: string): RefVerdict {
|
|
39
|
+
let pos = 0;
|
|
40
|
+
const refuse = (offset: number): RefVerdict => ({
|
|
41
|
+
refusal: { code: REF_MEMBER_INVALID, offset },
|
|
42
|
+
});
|
|
43
|
+
const identifier = (): string | undefined => {
|
|
44
|
+
if (pos >= source.length || !IDENT_START.test(source[pos]!)) return undefined;
|
|
45
|
+
const start = pos;
|
|
46
|
+
pos++;
|
|
47
|
+
while (pos < source.length && IDENT_PART.test(source[pos]!)) pos++;
|
|
48
|
+
return source.slice(start, pos);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const head = identifier();
|
|
52
|
+
if (head === undefined) return refuse(pos);
|
|
53
|
+
if (pos === source.length) return { parsed: { name: head, indexes: [] } };
|
|
54
|
+
|
|
55
|
+
if (source[pos] === ".") {
|
|
56
|
+
pos++;
|
|
57
|
+
const name = identifier();
|
|
58
|
+
if (name === undefined) return refuse(pos);
|
|
59
|
+
if (pos !== source.length) return refuse(pos);
|
|
60
|
+
return { parsed: { alias: head, name, indexes: [] } };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const indexes: RefIndex[] = [];
|
|
64
|
+
while (pos < source.length) {
|
|
65
|
+
if (source[pos] !== "[") return refuse(pos);
|
|
66
|
+
pos++;
|
|
67
|
+
const binding = identifier();
|
|
68
|
+
if (binding === undefined) return refuse(pos);
|
|
69
|
+
if (source[pos] !== ".") return refuse(pos);
|
|
70
|
+
pos++;
|
|
71
|
+
const memberStart = pos;
|
|
72
|
+
const member = identifier();
|
|
73
|
+
if (member !== "key" && member !== "index") return refuse(memberStart);
|
|
74
|
+
if (source[pos] !== "]") return refuse(pos);
|
|
75
|
+
pos++;
|
|
76
|
+
indexes.push({ binding, member });
|
|
77
|
+
}
|
|
78
|
+
return { parsed: { name: head, indexes } };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The sentence a refused `!ref` is reported with, naming where it goes wrong. */
|
|
82
|
+
export function refRefusalMessage(source: string, refusal: RefRefusal): string {
|
|
83
|
+
const at =
|
|
84
|
+
refusal.offset >= source.length
|
|
85
|
+
? "it ends too early"
|
|
86
|
+
: `'${source[refusal.offset]}' at offset ${refusal.offset} is not allowed there`;
|
|
87
|
+
return (
|
|
88
|
+
`'!ref ${source}' is not a reference: ${at}. A reference is '<name>', '<Alias>.<name>', ` +
|
|
89
|
+
`or '<family>[<binding>.key]' / '<family>[<binding>.index]' (one index per family depth, ` +
|
|
90
|
+
`no alias), with no whitespace.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The `!ref` engine. Marks a tagged scalar as a resource reference; the loader
|
|
95
|
+
* attaches this engine's verdict ({@link parseRefSource}) to the sentinel, and
|
|
96
|
+
* every reader reads that verdict rather than the text.
|
|
6
97
|
*
|
|
7
|
-
* Returns the source string verbatim at compile time. The
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* through the parse/serialize round-trip. Analysis is a no-op; reference
|
|
11
|
-
* resolution is the analyzer's responsibility via the ref-aware walker. */
|
|
98
|
+
* Returns the source string verbatim at compile time. The lookup (sentinel →
|
|
99
|
+
* live ResourceInstance) happens at resource-context init; analysis is a no-op
|
|
100
|
+
* here, since reference resolution is the analyzer's. */
|
|
12
101
|
export const refEngine: TemplatingEngine = {
|
|
13
102
|
name: "ref",
|
|
14
103
|
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isMap,
|
|
3
|
+
isScalar,
|
|
4
|
+
isSeq,
|
|
5
|
+
visit,
|
|
6
|
+
YAMLMap,
|
|
7
|
+
type CollectionTag,
|
|
8
|
+
type Document,
|
|
9
|
+
type Node,
|
|
10
|
+
type Schema,
|
|
11
|
+
} from "yaml";
|
|
12
|
+
import { createPair, type CreateNodeContext, type ToJSContext } from "yaml/util";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The expansion tags — `!if`, `!switch`, `!for-each` — a closed tag family beside
|
|
16
|
+
* the expression engines and outside their contract. Each sits on a mapping and
|
|
17
|
+
* loads as that same mapping, its keys in written order after the marker
|
|
18
|
+
* {@link EXPANSION_MARKER} naming the tag. Position travels by path, so a key
|
|
19
|
+
* inside a branch (`cases.postgres.<typo>`) is a path in the loaded tree as in
|
|
20
|
+
* the YAML.
|
|
21
|
+
*/
|
|
22
|
+
export const EXPANSION_TAGS = ["if", "switch", "for-each"] as const;
|
|
23
|
+
export type ExpansionTag = (typeof EXPANSION_TAGS)[number];
|
|
24
|
+
|
|
25
|
+
/** The key a loaded expansion mapping carries its tag under. */
|
|
26
|
+
export const EXPANSION_MARKER = "__expand";
|
|
27
|
+
|
|
28
|
+
/** The mapping key that splices a `!for-each`'s entries into its mapping. A
|
|
29
|
+
* plain key to every loader; its meaning is the analyzer's. */
|
|
30
|
+
export const SPLICE_KEY = "...";
|
|
31
|
+
|
|
32
|
+
/** An expansion tag's mapping as loaded: the marker plus the written keys. */
|
|
33
|
+
export type ExpansionSentinel = { readonly [EXPANSION_MARKER]: ExpansionTag } & Record<
|
|
34
|
+
string,
|
|
35
|
+
unknown
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
export function isExpansionTag(name: unknown): name is ExpansionTag {
|
|
39
|
+
return typeof name === "string" && (EXPANSION_TAGS as readonly string[]).includes(name);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** True when `v` is a loaded expansion mapping — a plain object carrying the
|
|
43
|
+
* marker with one of the three tag names. */
|
|
44
|
+
export function isExpansionSentinel(v: unknown): v is ExpansionSentinel {
|
|
45
|
+
return (
|
|
46
|
+
v !== null &&
|
|
47
|
+
typeof v === "object" &&
|
|
48
|
+
!Array.isArray(v) &&
|
|
49
|
+
isExpansionTag((v as Record<string, unknown>)[EXPANSION_MARKER])
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The tag a value was written behind, or undefined. */
|
|
54
|
+
export function expansionTagOf(v: unknown): ExpansionTag | undefined {
|
|
55
|
+
return isExpansionSentinel(v) ? v[EXPANSION_MARKER] : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The tag as written (`!if`) of an expansion mapping, or undefined. */
|
|
59
|
+
export function expansionTagLabel(v: unknown): string | undefined {
|
|
60
|
+
const tag = expansionTagOf(v);
|
|
61
|
+
return tag ? `!${tag}` : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The written keys of an expansion mapping, marker removed, in written order. */
|
|
65
|
+
export function expansionBody(v: ExpansionSentinel): Record<string, unknown> {
|
|
66
|
+
return Object.fromEntries(Object.entries(v).filter(([key]) => key !== EXPANSION_MARKER));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The code a shape refusal of `tag` is reported under. */
|
|
70
|
+
export function expansionShapeCode(tag: ExpansionTag): "BRANCH_SHAPE_INVALID" | "REPEAT_SHAPE_INVALID" {
|
|
71
|
+
return tag === "for-each" ? "REPEAT_SHAPE_INVALID" : "BRANCH_SHAPE_INVALID";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function expansionMapClass(tag: ExpansionTag): {
|
|
75
|
+
new (schema?: Schema): YAMLMap;
|
|
76
|
+
from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLMap;
|
|
77
|
+
} {
|
|
78
|
+
return class ExpansionMap extends YAMLMap {
|
|
79
|
+
// The marker first, then the written keys: the order both loaders produce.
|
|
80
|
+
override toJSON(_?: unknown, ctx?: ToJSContext, Type?: { new (): unknown }): any {
|
|
81
|
+
const loaded = super.toJSON(_, ctx, Type as never) as Record<string, unknown> | Map<unknown, unknown>;
|
|
82
|
+
if (loaded instanceof Map) {
|
|
83
|
+
return new Map<unknown, unknown>([[EXPANSION_MARKER, tag], ...loaded]);
|
|
84
|
+
}
|
|
85
|
+
return { [EXPANSION_MARKER]: tag, ...loaded };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A node built from a loaded mapping writes `metadata` first, then every
|
|
89
|
+
// other written key, and never the marker.
|
|
90
|
+
static override from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLMap {
|
|
91
|
+
const map = new this(schema);
|
|
92
|
+
const body = isExpansionSentinel(obj) ? expansionBody(obj) : (obj as Record<string, unknown>);
|
|
93
|
+
const keys = Object.keys(body);
|
|
94
|
+
const ordered = keys.includes("metadata")
|
|
95
|
+
? ["metadata", ...keys.filter((k) => k !== "metadata")]
|
|
96
|
+
: keys;
|
|
97
|
+
for (const key of ordered) {
|
|
98
|
+
const value = body[key];
|
|
99
|
+
if (value !== undefined || ctx.keepUndefined) map.items.push(createPair(key, value, ctx));
|
|
100
|
+
}
|
|
101
|
+
return map;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The `customTags` entries of the expansion tags: one mapping tag each, whose
|
|
107
|
+
* node class loads the mapping with its marker and writes it back behind its
|
|
108
|
+
* tag. */
|
|
109
|
+
export function buildExpansionTags(): CollectionTag[] {
|
|
110
|
+
return EXPANSION_TAGS.map((tag) => {
|
|
111
|
+
const nodeClass = expansionMapClass(tag);
|
|
112
|
+
return {
|
|
113
|
+
tag: `!${tag}`,
|
|
114
|
+
collection: "map",
|
|
115
|
+
nodeClass,
|
|
116
|
+
identify: (v: unknown) => expansionTagOf(v) === tag,
|
|
117
|
+
createNode: (schema: Schema, value: unknown, ctx: CreateNodeContext) =>
|
|
118
|
+
nodeClass.from(schema, value, ctx),
|
|
119
|
+
} satisfies CollectionTag;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** An expansion tag the YAML itself refuses: one written on a scalar or a
|
|
124
|
+
* sequence, or a tagged mapping writing the marker key itself. */
|
|
125
|
+
export interface ExpansionTagMisuse {
|
|
126
|
+
readonly tag: ExpansionTag;
|
|
127
|
+
readonly code: "BRANCH_SHAPE_INVALID" | "REPEAT_SHAPE_INVALID";
|
|
128
|
+
readonly message: string;
|
|
129
|
+
/** The node's character range in the parsed text, `[start, end]`. */
|
|
130
|
+
readonly range?: readonly [number, number];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Every misuse of an expansion tag in one parsed document — what no loaded
|
|
134
|
+
* tree can show, since a tag on a scalar or a sequence loads as the bare
|
|
135
|
+
* value. Reported at load by every loader. */
|
|
136
|
+
export function expansionTagMisuse(document: Document): ExpansionTagMisuse[] {
|
|
137
|
+
const found: ExpansionTagMisuse[] = [];
|
|
138
|
+
const report = (node: Node, tag: ExpansionTag, message: string) => {
|
|
139
|
+
found.push({
|
|
140
|
+
tag,
|
|
141
|
+
code: expansionShapeCode(tag),
|
|
142
|
+
message,
|
|
143
|
+
...(node.range ? { range: [node.range[0], node.range[1]] as const } : {}),
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
visit(document, (_key, node) => {
|
|
147
|
+
if (!isScalar(node) && !isSeq(node) && !isMap(node)) return;
|
|
148
|
+
const name = node.tag?.startsWith("!") ? node.tag.slice(1) : undefined;
|
|
149
|
+
if (!isExpansionTag(name)) return;
|
|
150
|
+
if (isMap(node)) {
|
|
151
|
+
const markerKey = node.items.find(
|
|
152
|
+
(pair) => isScalar(pair.key) && pair.key.value === EXPANSION_MARKER,
|
|
153
|
+
);
|
|
154
|
+
if (markerKey) {
|
|
155
|
+
report(
|
|
156
|
+
node,
|
|
157
|
+
name,
|
|
158
|
+
`'!${name}' holds a key '${EXPANSION_MARKER}', which is reserved for the tag's own marker. Remove it.`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
report(
|
|
164
|
+
node,
|
|
165
|
+
name,
|
|
166
|
+
`'!${name}' is written on ${isSeq(node) ? "a sequence" : "a scalar"}; it takes a mapping ` +
|
|
167
|
+
`(${name === "for-each" ? "'{ in, as?, emit }'" : name === "if" ? "'{ if, then, else? }'" : "'{ switch, cases, default? }'"}).`,
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
return found;
|
|
171
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -71,7 +71,32 @@ export {
|
|
|
71
71
|
export { interpolateEngine } from "./engines/interpolate.js";
|
|
72
72
|
export { literalEngine } from "./engines/literal.js";
|
|
73
73
|
export { modulePathEngine } from "./engines/module-path.js";
|
|
74
|
-
export {
|
|
74
|
+
export {
|
|
75
|
+
parseRefSource,
|
|
76
|
+
REF_MEMBER_INVALID,
|
|
77
|
+
refEngine,
|
|
78
|
+
refRefusalMessage,
|
|
79
|
+
type ParsedRef,
|
|
80
|
+
type RefIndex,
|
|
81
|
+
type RefRefusal,
|
|
82
|
+
type RefVerdict,
|
|
83
|
+
} from "./engines/ref.js";
|
|
84
|
+
export {
|
|
85
|
+
buildExpansionTags,
|
|
86
|
+
EXPANSION_MARKER,
|
|
87
|
+
EXPANSION_TAGS,
|
|
88
|
+
expansionBody,
|
|
89
|
+
expansionShapeCode,
|
|
90
|
+
expansionTagLabel,
|
|
91
|
+
expansionTagMisuse,
|
|
92
|
+
expansionTagOf,
|
|
93
|
+
isExpansionSentinel,
|
|
94
|
+
isExpansionTag,
|
|
95
|
+
SPLICE_KEY,
|
|
96
|
+
type ExpansionSentinel,
|
|
97
|
+
type ExpansionTag,
|
|
98
|
+
type ExpansionTagMisuse,
|
|
99
|
+
} from "./expansion-tags.js";
|
|
75
100
|
export { sqlEngine, isParameterizedSql, type ParameterizedSql } from "./engines/sql.js";
|
|
76
101
|
|
|
77
102
|
export { TemplatingEngineRegistry } from "./registry.js";
|
|
@@ -109,6 +134,8 @@ export {
|
|
|
109
134
|
MODULE_FILE_ENGINE_NAMES,
|
|
110
135
|
MODULE_PATH_ENGINE,
|
|
111
136
|
plainChainOf,
|
|
137
|
+
REF_ENGINE,
|
|
138
|
+
type RefSentinel,
|
|
112
139
|
type TaggedSentinel,
|
|
113
140
|
} from "./sentinel.js";
|
|
114
|
-
export { buildCustomTags, defaultCustomTags } from "./yaml-tags.js";
|
|
141
|
+
export { buildCustomTags, defaultCustomTags, type ManifestTag } from "./yaml-tags.js";
|
package/src/sentinel.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { parseRefSource, type ParsedRef, type RefRefusal } from "./engines/ref.js";
|
|
2
|
+
|
|
1
3
|
/** Sentinel object produced by the YAML loader for a tagged scalar (e.g. `!cel
|
|
2
4
|
* 'variables.port'`). Travels through the manifest tree as the parsed value;
|
|
3
5
|
* precompile and the analyzer key off `__tagged === true` to dispatch to the
|
|
@@ -8,8 +10,16 @@ export interface TaggedSentinel {
|
|
|
8
10
|
readonly __tagged: true;
|
|
9
11
|
readonly engine: string;
|
|
10
12
|
readonly source: string;
|
|
13
|
+
/** `!ref` only: what the text names, by the `!ref` engine's grammar. */
|
|
14
|
+
readonly parsed?: ParsedRef;
|
|
15
|
+
/** `!ref` only: the text is outside the grammar. */
|
|
16
|
+
readonly refusal?: RefRefusal;
|
|
11
17
|
}
|
|
12
18
|
|
|
19
|
+
/** A `!ref` sentinel, carrying the engine's verdict on its text — exactly one
|
|
20
|
+
* of `parsed` / `refusal`, attached when the sentinel is made. */
|
|
21
|
+
export type RefSentinel = TaggedSentinel & { readonly engine: "ref" };
|
|
22
|
+
|
|
13
23
|
export function isTaggedSentinel(v: unknown): v is TaggedSentinel {
|
|
14
24
|
return (
|
|
15
25
|
v !== null &&
|
|
@@ -20,17 +30,23 @@ export function isTaggedSentinel(v: unknown): v is TaggedSentinel {
|
|
|
20
30
|
);
|
|
21
31
|
}
|
|
22
32
|
|
|
33
|
+
/** The sentinel a tagged scalar becomes. A `!ref` one carries the `!ref`
|
|
34
|
+
* engine's verdict on its text, so no reader keeps a grammar of its own. */
|
|
23
35
|
export function makeTaggedSentinel(engine: string, source: string): TaggedSentinel {
|
|
36
|
+
if (engine === REF_ENGINE) return { __tagged: true, engine, source, ...parseRefSource(source) };
|
|
24
37
|
return { __tagged: true, engine, source };
|
|
25
38
|
}
|
|
26
39
|
|
|
40
|
+
/** The `!ref` engine's name. */
|
|
41
|
+
export const REF_ENGINE = "ref";
|
|
42
|
+
|
|
27
43
|
/** True when `v` is a `!ref <name>` sentinel — i.e. a resource reference
|
|
28
44
|
* marked at parse time. Downstream walkers (analyzer ref validation,
|
|
29
45
|
* inline normalization, dependency graph, kernel resource resolution)
|
|
30
46
|
* use this to tell a reference from an inline definition without
|
|
31
47
|
* inferring intent from field presence. */
|
|
32
|
-
export function isRefSentinel(v: unknown): v is
|
|
33
|
-
return isTaggedSentinel(v) && v.engine ===
|
|
48
|
+
export function isRefSentinel(v: unknown): v is RefSentinel {
|
|
49
|
+
return isTaggedSentinel(v) && v.engine === REF_ENGINE;
|
|
34
50
|
}
|
|
35
51
|
|
|
36
52
|
/** The CEL engine's name. Beside the other sentinel predicates for the same
|
package/src/yaml-tags.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import type { ScalarTag } from "yaml";
|
|
1
|
+
import type { CollectionTag, ScalarTag } from "yaml";
|
|
2
2
|
import { stringifyString } from "yaml/util";
|
|
3
3
|
import { defaultRegistry } from "./builtins.js";
|
|
4
|
+
import { buildExpansionTags } from "./expansion-tags.js";
|
|
4
5
|
import { isTaggedSentinel, makeTaggedSentinel, type TaggedSentinel } from "./sentinel.js";
|
|
5
6
|
import type { TemplatingEngineRegistry } from "./registry.js";
|
|
6
7
|
|
|
8
|
+
/** One YAML tag the manifest parsers recognise. */
|
|
9
|
+
export type ManifestTag = ScalarTag | CollectionTag;
|
|
10
|
+
|
|
7
11
|
/** Build the `customTags` array passed to `yaml`'s `parseAllDocuments` /
|
|
8
|
-
* `Document` from the registered engines. Each engine
|
|
9
|
-
* ScalarTag whose `resolve` produces a `TaggedSentinel`, and
|
|
10
|
-
* `identify` + `stringify` round-trip the sentinel back to its original
|
|
11
|
-
* `!<engine> "<source>"` form when the document is re-serialized
|
|
12
|
+
* `Document` from the registered engines plus the expansion tags. Each engine
|
|
13
|
+
* contributes one ScalarTag whose `resolve` produces a `TaggedSentinel`, and
|
|
14
|
+
* whose `identify` + `stringify` round-trip the sentinel back to its original
|
|
15
|
+
* `!<engine> "<source>"` form when the document is re-serialized; each
|
|
16
|
+
* expansion tag a mapping tag (`expansion-tags.ts`).
|
|
12
17
|
*
|
|
13
18
|
* Without `stringify`, `Document.toString()` would emit the sentinel as a
|
|
14
19
|
* YAML mapping (`{__tagged: true, engine: cel, source: ...}`), corrupting
|
|
@@ -21,8 +26,11 @@ import type { TemplatingEngineRegistry } from "./registry.js";
|
|
|
21
26
|
* hosts. Each host passes its own registry (in practice always
|
|
22
27
|
* `createDefaultRegistry()`), keeping the door open for future
|
|
23
28
|
* test-only registries. */
|
|
24
|
-
export function buildCustomTags(registry: TemplatingEngineRegistry):
|
|
25
|
-
return
|
|
29
|
+
export function buildCustomTags(registry: TemplatingEngineRegistry): ManifestTag[] {
|
|
30
|
+
return [
|
|
31
|
+
...registry.list().map((engine) => buildTagForEngine(engine.name)),
|
|
32
|
+
...buildExpansionTags(),
|
|
33
|
+
];
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
/** Returns `customTags` built from the default registry, freshly each call.
|
|
@@ -30,8 +38,8 @@ export function buildCustomTags(registry: TemplatingEngineRegistry): ScalarTag[]
|
|
|
30
38
|
* parse the same set of tags. Built from `defaultRegistry()` (the same
|
|
31
39
|
* singleton precompile + the analyzer use) so registering a new engine on
|
|
32
40
|
* the default registry propagates to YAML parsing on the next call. The
|
|
33
|
-
* rebuild cost is negligible (one array of N small
|
|
34
|
-
export function defaultCustomTags():
|
|
41
|
+
* rebuild cost is negligible (one array of N small tag objects). */
|
|
42
|
+
export function defaultCustomTags(): ManifestTag[] {
|
|
35
43
|
return buildCustomTags(defaultRegistry());
|
|
36
44
|
}
|
|
37
45
|
|