Skip to content

Commit 5780f74

Browse files
committed
* more playground updates for jsonata
*
1 parent 6d2ba3f commit 5780f74

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

examples/showcase/18-js-operators.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
// JavaScript Operator & Method Mapping
22
//
3-
// The same logic as 17-intrinsics.ts, but written using standard
4-
// JavaScript operators and methods. The compiler recognizes these
5-
// patterns and maps them to the exact same ASL intrinsic functions.
3+
// Write standard JavaScript — the compiler maps operators and methods
4+
// to the appropriate ASL representation for your chosen query language.
65
//
7-
// Compare the generated ASL output — it's identical!
6+
// JSONata mode (default):
7+
// a + b → native JSONata addition
8+
// `${a} text ${b}` → JSONata string concatenation with &
9+
// str.split(delim) → $split(str, delim)
10+
// JSON.parse(str) → $eval(str)
11+
// JSON.stringify(obj) → $string(obj)
12+
// arr.includes(val) → val in arr
813
//
9-
// See also 34-37 for JSONata-only methods (string, math, array,
10-
// and higher-order functions like map/filter/reduce).
11-
//
12-
// Mapping table:
14+
// JSONPath mode (--query-language jsonpath):
1315
// a + b → States.MathAdd(a, b)
14-
// a - 5 → States.MathAdd(a, -5)
1516
// `${a} text ${b}` → States.Format('{} text {}', a, b)
1617
// str.split(delim) → States.StringSplit(str, delim)
1718
// JSON.parse(str) → States.StringToJson(str)
1819
// JSON.stringify(obj) → States.JsonToString(obj)
1920
// arr.includes(val) → States.ArrayContains(arr, val)
21+
//
22+
// The TypeScript source is identical — only the ASL output differs.
2023

2124
import { Steps, SimpleStepContext } from '../../packages/core/src/runtime/index';
2225

2326
export const jsOperators = Steps.createFunction(
2427
async (context: SimpleStepContext, input: { orderId: string; price: number; tax: number; metadata: string }) => {
25-
// Arithmetic using the + operator (same as Steps.add)
28+
// Arithmetic → JSONata: native +, JSONPath: States.MathAdd
2629
const total = input.price + input.tax;
2730

28-
// String formatting using template literals (compiles to States.Format)
31+
// Template literal → JSONata: & concat, JSONPath: States.Format
2932
const message = `Order ${input.orderId} confirmed, total: ${total}`;
3033

31-
// Generate a unique ID (no JS equivalent — Steps.uuid() is the only way)
34+
// UUID — always States.UUID() in both backends
3235
const trackingId = Steps.uuid();
3336

34-
// Parse a JSON string using JSON.parse (same as Steps.jsonParse)
37+
// JSON.parse → JSONata: $eval(), JSONPath: States.StringToJson
3538
const meta = JSON.parse(input.metadata);
3639

3740
return {

examples/showcase/20-string-interpolation.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// String Interpolation with Template Literals
22
//
3-
// Write natural template strings — the compiler maps them to
4-
// States.Format intrinsic functions automatically.
3+
// Write natural template strings — the compiler maps them to the
4+
// appropriate ASL representation for your chosen query language.
55
//
6-
// Mapping:
6+
// JSONata mode (default):
7+
// `Hello ${name}` → "Hello " & $name
8+
// `${a} and ${b}` → $a & " and " & $b
9+
// `Total: ${a + b}` → "Total: " & ($a + $b)
10+
//
11+
// JSONPath mode (--query-language jsonpath):
712
// `Hello ${name}` → States.Format('Hello {}', name)
813
// `${a} and ${b}` → States.Format('{} and {}', a, b)
914
// `Total: ${a + b}` → States.Format('Total: {}', States.MathAdd(a, b))

examples/showcase/22-js-patterns.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// Complete JavaScript Pattern Reference
22
//
33
// Every JS pattern the compiler maps to ASL, in one place.
4+
// The TypeScript source is identical regardless of backend —
5+
// only the ASL output differs.
46
//
5-
// See also 34-37 for JSONata-only methods (string, math, array,
6-
// and higher-order functions like map/filter/reduce).
7+
// JSONata mode (default):
8+
// a + b (numbers) → native JSONata addition
9+
// a - 5 → native JSONata subtraction
10+
// a * b, a / b, a % b → native JSONata operators (JSONata only)
11+
// `${a} text ${b}` → JSONata & concatenation
12+
// str.split(delim) → $split(str, delim)
13+
// JSON.parse(str) → $eval(str)
14+
// JSON.stringify(obj) → $string(obj)
15+
// arr.includes(val) → val in arr
16+
// arr.length → $count(arr)
17+
// Steps.uuid() → States.UUID() (intrinsic in both)
718
//
8-
// Mapping table:
19+
// JSONPath mode (--query-language jsonpath):
920
// a + b (numbers) → States.MathAdd(a, b)
1021
// a - 5 (literal right) → States.MathAdd(a, -5)
1122
// `${a} text ${b}` → States.Format('{} text {}', a, b)
@@ -15,6 +26,9 @@
1526
// arr.includes(val) → States.ArrayContains(arr, val)
1627
// arr.length → States.ArrayLength(arr)
1728
// Steps.uuid() → States.UUID()
29+
//
30+
// See also 34-37 for JSONata-only methods (string, math, array,
31+
// and higher-order functions like map/filter/reduce).
1832

1933
import { Steps, SimpleStepContext } from '../../packages/core/src/runtime/index';
2034

@@ -23,31 +37,31 @@ export const jsPatterns = Steps.createFunction(
2337
items: string[]; csv: string; data: string;
2438
price: number; tax: number; name: string
2539
}) => {
26-
// Arithmetic: + → States.MathAdd
40+
// Arithmetic: JSONata: native +, JSONPath: States.MathAdd
2741
const total = input.price + input.tax;
2842

29-
// Subtraction: - literal → States.MathAdd(a, -literal)
43+
// Subtraction: JSONata: native -, JSONPath: States.MathAdd(a, -literal)
3044
const discounted = input.price - 5;
3145

32-
// Template literals States.Format
46+
// Template literals: JSONata: & concat, JSONPath: States.Format
3347
const message = `Hello ${input.name}, your total is ${total}`;
3448

35-
// String split States.StringSplit
49+
// String split: JSONata: $split(), JSONPath: States.StringSplit
3650
const parts = input.csv.split(',');
3751

38-
// JSON.parse States.StringToJson
52+
// JSON.parse: JSONata: $eval(), JSONPath: States.StringToJson
3953
const parsed = JSON.parse(input.data);
4054

41-
// JSON.stringify States.JsonToString
55+
// JSON.stringify: JSONata: $string(), JSONPath: States.JsonToString
4256
const serialized = JSON.stringify(parsed);
4357

44-
// Array includes States.ArrayContains
58+
// Array includes: JSONata: `in` operator, JSONPath: States.ArrayContains
4559
const hasItem = input.items.includes('special');
4660

47-
// Array length States.ArrayLength
61+
// Array length: JSONata: $count(), JSONPath: States.ArrayLength
4862
const count = input.items.length;
4963

50-
// Steps.uuidStates.UUID
64+
// Steps.uuid: States.UUID() in both backends
5165
const id = Steps.uuid();
5266

5367
return { total, discounted, message, parts, parsed, serialized, hasItem, count, id };

examples/starters/cdk/lib/batch-processing-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class BatchProcessingStack extends cdk.Stack {
6767

6868
const resultKey = Steps.format('results/{}/output.json', input.batchId);
6969

70-
// Body simplified — JSON.stringify() is not supported by the compiler
70+
// Body simplified — full JSON.stringify() compiles to $string() in JSONata mode
7171
await bucket.putObject({
7272
Key: resultKey,
7373
Body: input.batchId,

packages/core/src/compiler/generation/pathDialect.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
export interface PathDialect {
1616
/**
1717
* The QueryLanguage value for the top-level ASL definition.
18-
* undefined means the default (JSONPath — no field emitted).
18+
* undefined means no explicit QueryLanguage field (JSONPath mode).
19+
* Note: JSONata is the compiler default; JSONPath is used when explicitly requested.
1920
*/
2021
readonly queryLanguage: 'JSONPath' | 'JSONata' | undefined;
2122

@@ -146,7 +147,7 @@ export interface PathDialect {
146147
}
147148

148149
// ---------------------------------------------------------------------------
149-
// JSONPath dialect (default)
150+
// JSONPath dialect (used when queryLanguage: 'JSONPath' is explicitly set)
150151
// ---------------------------------------------------------------------------
151152

152153
export class JsonPathDialect implements PathDialect {
@@ -362,7 +363,7 @@ export class JsonataDialect implements PathDialect {
362363
}
363364
}
364365

365-
/** Singleton instance for the default JSONPath dialect. */
366+
/** Singleton instance for the JSONPath dialect. */
366367
export const JSON_PATH_DIALECT: PathDialect = new JsonPathDialect();
367368

368369
/** Singleton instance for the JSONata dialect. */

0 commit comments

Comments
 (0)