@promptctl/go-template-js 0.1.0 → 0.1.1
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 +16 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,13 +9,13 @@ Go template syntax + Sprig subset, generic over output type, in TypeScript.
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
pnpm add go-template-js
|
|
12
|
+
pnpm add @promptctl/go-template-js
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Quickstart (string output)
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
import { createEngine } from "go-template-js";
|
|
18
|
+
import { createEngine } from "@promptctl/go-template-js";
|
|
19
19
|
|
|
20
20
|
const engine = createEngine<string>({ fromString: (s) => s });
|
|
21
21
|
const tpl = engine.parse("Hello, {{ .name }}!");
|
|
@@ -36,7 +36,7 @@ greet({ name: "ada" }).join(""); // "hi ada"
|
|
|
36
36
|
The engine is parameterised over its output fragment type `T`. Consumers pick a fragment shape (e.g. styled text, AST nodes, layout primitives) and the engine emits a `T[]` that integrates into their downstream pipeline:
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
|
-
import { createEngine, type TemplateFunc } from "go-template-js";
|
|
39
|
+
import { createEngine, type TemplateFunc } from "@promptctl/go-template-js";
|
|
40
40
|
|
|
41
41
|
interface Frag { color: string; text: string }
|
|
42
42
|
|
|
@@ -62,9 +62,14 @@ tpl.evaluate({ label: "warn", value: "ALERT" });
|
|
|
62
62
|
|
|
63
63
|
Functions have optional `argTypes`. The engine uses them to enforce one architectural commitment: **a non-string `T` value will never silently flatten into a `string` parameter**. If you try to pipe a styled fragment into a function declared with `argTypes: ["string"]`, you get a `TypeMismatchError` naming the function, the argument index, and a suggestion to call your `unstyled` (or equivalent) flatten helper.
|
|
64
64
|
|
|
65
|
-
###
|
|
65
|
+
### Lifting and stringification: the boundary bridges
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
`fromString` and `toString` are the engine's two boundary bridges between text and `T`. Each one has an `ArgType` that uses it:
|
|
68
|
+
|
|
69
|
+
| Direction | Bridge | ArgType | Where it runs |
|
|
70
|
+
| --- | --- | --- | --- |
|
|
71
|
+
| `string` → `T` | `fromString` (required) | `"liftable"` | a string at a `"liftable"` slot is replaced with `fromString(s)` before the body sees it |
|
|
72
|
+
| `T` → `string` | `toString` (optional) | `"stringifiable"` | a non-string at a `"stringifiable"` slot is flattened via `toString` (e.g. `print`, `printf "%s"`) |
|
|
68
73
|
|
|
69
74
|
```ts
|
|
70
75
|
const engine = createEngine<Frag>({
|
|
@@ -73,7 +78,9 @@ const engine = createEngine<Frag>({
|
|
|
73
78
|
});
|
|
74
79
|
```
|
|
75
80
|
|
|
76
|
-
|
|
81
|
+
`fromString` is mandatory in `EngineConfig`: every engine knows how to lift, so `"liftable"` is always servable. `toString` is optional: without it, typed-`T` values reaching a `"stringifiable"` slot throw `TypeMismatchError` directing you to configure it. The default `toString` passes strings through unchanged and flattens numeric/boolean/nil primitives, so `T = string` engines work out of the box.
|
|
82
|
+
|
|
83
|
+
Use `"liftable"` for any consumer-defined function whose slot should accept either a `T` value or a string literal. The body, by contract, only ever receives `T` — the gate owns the lift, so the body has no string-vs-`T` discrimination to do.
|
|
77
84
|
|
|
78
85
|
### `ArgType` reference
|
|
79
86
|
|
|
@@ -91,6 +98,7 @@ Every entry in `argTypes` is one of:
|
|
|
91
98
|
| `"dict"` | plain object only (not `Map`, not class instance) | `get`, `keys`, `values`, `pluck`, `merge` |
|
|
92
99
|
| `"sized"` | `string` ∪ array ∪ `Map` ∪ `Set` ∪ plain object | `len` |
|
|
93
100
|
| `"stringifiable"` | `string`, or any value the engine's `toString` can flatten | `print`, `println`, `printf "%s"`, `printf "%q"` |
|
|
101
|
+
| `"liftable"` | `T`, or `string` (lifted to `T` via the engine's `fromString` before the body runs) | consumer-defined funcs that accept either a `T` fragment or a bare text literal at a slot |
|
|
94
102
|
| `"callable"` | `typeof === "function"` | `call` (first slot) |
|
|
95
103
|
| `"collection"` | `string` ∪ array ∪ `Map` ∪ plain object | `index` (receiver slot) |
|
|
96
104
|
| `"index-key"` | `string`, `number`, or `bigint` | `index` (key slots) |
|
|
@@ -133,7 +141,7 @@ All of Go template's runtime built-ins:
|
|
|
133
141
|
|
|
134
142
|
### Sprig subset
|
|
135
143
|
|
|
136
|
-
Imported from
|
|
144
|
+
Imported from `@promptctl/go-template-js` as category-scoped `FuncMap` factories:
|
|
137
145
|
|
|
138
146
|
| Category | Helper | Functions |
|
|
139
147
|
| --- | --- | --- |
|
|
@@ -209,7 +217,7 @@ Every error carries `pos`, `source`, and a `kind` discriminator. `.toString()` p
|
|
|
209
217
|
|
|
210
218
|
### Stable public API surface
|
|
211
219
|
|
|
212
|
-
The package exports exactly the following from `"go-template-js"` — anything else is internal and may change at any time:
|
|
220
|
+
The package exports exactly the following from `"@promptctl/go-template-js"` — anything else is internal and may change at any time:
|
|
213
221
|
|
|
214
222
|
- Engine: `createEngine`, `Engine`, `Template`, `EngineConfig`, `FuncMap`, `TemplateFunc`, `ArgType`.
|
|
215
223
|
- Errors: `TemplateError`, `ParseError`, `EvalError`, `FuncNotFoundError`, `TypeMismatchError`, `MissingFieldError`, `ErrorKind`.
|
package/dist/index.d.ts
CHANGED
|
@@ -136,7 +136,7 @@ declare class MissingFieldError extends EvalError {
|
|
|
136
136
|
* `"callable"`). The history for this decision lives in epic
|
|
137
137
|
* template-laws-3gt.
|
|
138
138
|
*/
|
|
139
|
-
type ArgType = "string" | "number" | "bool" | "T" | "ordered" | "list" | "dict" | "sized" | "comparable" | "stringifiable" | "callable" | "collection" | "index-key" | "sliceable" | "truthy" | "reflective" | "value" | "serializable";
|
|
139
|
+
type ArgType = "string" | "number" | "bool" | "T" | "ordered" | "list" | "dict" | "sized" | "comparable" | "stringifiable" | "liftable" | "callable" | "collection" | "index-key" | "sliceable" | "truthy" | "reflective" | "value" | "serializable";
|
|
140
140
|
interface TemplateFunc {
|
|
141
141
|
/**
|
|
142
142
|
* The function body. Parameter types are *contravariant-bottom*
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/parser/pos.ts","../src/errors.ts","../src/evaluator/evaluator.ts","../src/sprig/conversions/index.ts","../src/sprig/defaults/index.ts","../src/sprig/dicts/index.ts","../src/sprig/lists/index.ts","../src/sprig/math/index.ts","../src/sprig/regex/index.ts","../src/sprig/strings/index.ts","../src/sprig/types/index.ts"],"mappings":";;AAUA;;;;;;;;;UAAiB,GAAA;EAAA,SACN,IAAA;EAAA,SACA,MAAA;EAAA,SACA,MAAA;AAAA;;;KCSC,SAAA;AAAA,UAOK,oBAAA;EAAA,SACN,MAAA;AAAA;AAAA,cAOE,aAAA,SAAsB,KAAA;EAAA,SACf,IAAA;EAAA,SACT,IAAA,EAAM,SAAA;EAAA,SACN,GAAA,EAAK,GAAA;EAAA,SACL,MAAA;cAEG,OAAA,UAAiB,GAAA,EAAK,GAAA,EAAK,GAAA,GAAK,oBAAA;EANX;EAAA,IAa7B,aAAA,CAAA;EAIK,QAAA,CAAA;AAAA;AAAA,UAWM,iBAAA,SAA0B,oBAAA;EAAA,SAChC,QAAA;EAAA,SACA,KAAA;AAAA;AAAA,cAGE,UAAA,SAAmB,aAAA;EAAA,SACZ,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,QAAA;EAAA,SACA,KAAA;cAEG,OAAA,UAAiB,GAAA,EAAK,GAAA,EAAK,GAAA,GAAK,iBAAA;AAAA;AAAA,cAWjC,SAAA,SAAkB,aAAA;EAAA,SACX,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;AAAA;;AAxB1B;;;;cAgCa,iBAAA,SAA0B,SAAA;EAAA,SACnB,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,QAAA;EAAA,SACA,WAAA;cAGP,QAAA,UACA,GAAA,EAAK,GAAA,EACL,GAAA,GAAK,oBAAA;IAAyB,SAAA;EAAA;AAAA;AAAA,cAUrB,iBAAA,SAA0B,SAAA;EAAA,SACnB,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,QAAA;EAjDkC;EAAA,SAmDlC,QAAA;EAAA,SACA,QAAA;EAAA,SACA,eAAA;cAGP,QAAA,UACA,QAAA,UACA,QAAA,UACA,eAAA,UACA,GAAA,EAAK,GAAA,EACL,GAAA,GAAK,oBAAA;AAAA;AAAA,cAgBI,iBAAA,SAA0B,SAAA;EAAA,SACnB,IAAA;EAAA,SACA,IAAA,EAAM,SAAA;EAAA,SACf,IAAA;cAEG,IAAA,qBAAyB,GAAA,EAAK,GAAA,EAAK,GAAA,GAAK,oBAAA;AAAA;;;AA3HtD;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;AAzCA,KCqEY,OAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/parser/pos.ts","../src/errors.ts","../src/evaluator/evaluator.ts","../src/sprig/conversions/index.ts","../src/sprig/defaults/index.ts","../src/sprig/dicts/index.ts","../src/sprig/lists/index.ts","../src/sprig/math/index.ts","../src/sprig/regex/index.ts","../src/sprig/strings/index.ts","../src/sprig/types/index.ts"],"mappings":";;AAUA;;;;;;;;;UAAiB,GAAA;EAAA,SACN,IAAA;EAAA,SACA,MAAA;EAAA,SACA,MAAA;AAAA;;;KCSC,SAAA;AAAA,UAOK,oBAAA;EAAA,SACN,MAAA;AAAA;AAAA,cAOE,aAAA,SAAsB,KAAA;EAAA,SACf,IAAA;EAAA,SACT,IAAA,EAAM,SAAA;EAAA,SACN,GAAA,EAAK,GAAA;EAAA,SACL,MAAA;cAEG,OAAA,UAAiB,GAAA,EAAK,GAAA,EAAK,G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IAAK,MAAA,CAAO,CAAA;;;iBCpsBjD,gBAAA,CAAA,GAAoB,OAAA;;;iBChBpB,aAAA,CAAA,GAAiB,OAAA;;;iBCDjB,UAAA,CAAA,GAAc,OAAA;;;iBCqBd,UAAA,CAAA,GAAc,OAAA;;;iBCnBd,SAAA,CAAA,GAAa,OAAA;;;iBCDb,UAAA,CAAA,GAAc,OAAA;;;iBCkEd,YAAA,CAAA,GAAgB,OAAA;;;iBCxEhB,UAAA,CAAA,GAAc,OAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1779,7 +1779,7 @@ var Engine = class {
|
|
|
1779
1779
|
const v = piped.value;
|
|
1780
1780
|
args.push(lazy ? () => v : v);
|
|
1781
1781
|
}
|
|
1782
|
-
enforceArgTypes(head.ident, fn.argTypes, args, cmd.pos, ctx.source, this.toString, fn.argTypePattern);
|
|
1782
|
+
enforceArgTypes(head.ident, fn.argTypes, args, cmd.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString);
|
|
1783
1783
|
try {
|
|
1784
1784
|
return fn.fn(...args);
|
|
1785
1785
|
} catch (e) {
|
|
@@ -1802,7 +1802,7 @@ var Engine = class {
|
|
|
1802
1802
|
source: ctx.source,
|
|
1803
1803
|
available: Object.keys(this.funcs)
|
|
1804
1804
|
});
|
|
1805
|
-
enforceArgTypes(node.ident, fn.argTypes, [], node.pos, ctx.source, this.toString, fn.argTypePattern);
|
|
1805
|
+
enforceArgTypes(node.ident, fn.argTypes, [], node.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString);
|
|
1806
1806
|
return fn.fn();
|
|
1807
1807
|
}
|
|
1808
1808
|
case "Chain": return this.resolveFieldChain(this.evalPrimary(node.node, scope, ctx), node.fields, node.pos, ctx);
|
|
@@ -1867,7 +1867,7 @@ function formatScalarLikeGo(v) {
|
|
|
1867
1867
|
function createEngine(config) {
|
|
1868
1868
|
return new Engine(config);
|
|
1869
1869
|
}
|
|
1870
|
-
function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaultToString, pattern) {
|
|
1870
|
+
function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaultToString, pattern, fromString = defaultFromString) {
|
|
1871
1871
|
const lookup = makeSlotLookup(argTypes, pattern);
|
|
1872
1872
|
let firstOrdered = -1;
|
|
1873
1873
|
let firstComparable = -1;
|
|
@@ -1875,6 +1875,7 @@ function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaul
|
|
|
1875
1875
|
const declared = lookup(i);
|
|
1876
1876
|
const value = values[i];
|
|
1877
1877
|
if (!matchesArgType(declared, value, toString)) throw new TypeMismatchError(funcName, i + 1, humanArgType(declared), describeValue(value), pos, { source: src });
|
|
1878
|
+
if (declared === "liftable" && typeof value === "string") values[i] = fromString(value);
|
|
1878
1879
|
if (declared === "ordered") {
|
|
1879
1880
|
if (firstOrdered === -1) firstOrdered = i;
|
|
1880
1881
|
else if (!sameOrderedKind(values[firstOrdered], value)) throw new TypeMismatchError(funcName, i + 1, `${humanArgType("ordered")} of the same kind as ${describeValue(values[firstOrdered])}`, describeValue(value), pos, { source: src });
|
|
@@ -1920,9 +1921,13 @@ function matchesArgType(declared, value, toString) {
|
|
|
1920
1921
|
} catch {
|
|
1921
1922
|
return false;
|
|
1922
1923
|
}
|
|
1924
|
+
case "liftable": return typeof value === "string" || value !== null && value !== void 0 && typeof value !== "number" && typeof value !== "boolean" && typeof value !== "bigint" && typeof value !== "symbol";
|
|
1923
1925
|
case "serializable": return isJsonSerializable(value);
|
|
1924
1926
|
}
|
|
1925
1927
|
}
|
|
1928
|
+
function defaultFromString(s) {
|
|
1929
|
+
return s;
|
|
1930
|
+
}
|
|
1926
1931
|
function defaultToString(value) {
|
|
1927
1932
|
if (typeof value === "string") return value;
|
|
1928
1933
|
if (value === null || value === void 0) return "<nil>";
|
|
@@ -1978,6 +1983,7 @@ function humanArgType(t) {
|
|
|
1978
1983
|
case "sized": return "sized value (string, list, map, set, or dict)";
|
|
1979
1984
|
case "comparable": return "comparable value (orderable primitive, nil, list, dict, Map, or Set)";
|
|
1980
1985
|
case "stringifiable": return "stringifiable value (string or convertible via the engine's toString)";
|
|
1986
|
+
case "liftable": return "liftable value (T or string, the latter lifted via the engine's fromString)";
|
|
1981
1987
|
case "callable": return "callable (function value)";
|
|
1982
1988
|
case "collection": return "collection (string, array, Map, or dict)";
|
|
1983
1989
|
case "index-key": return "index key (number, bigint, or string)";
|