You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: LANGUAGE.md
+152-7Lines changed: 152 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,14 @@ In the example above it is an object with the name `"router"` (the value of the
18
18
19
19
When this simple script is evaluated its value will be the resolved value that the executor returns. See [Synchronous and asynchronous values](#synchronous-and-asynchronous-values).
20
20
21
+
The full syntax above allows all properties to be evaluated - executor name and methods can be the result of some other scripts. For the majority of cases when these are constants the short syntax can be used:
22
+
23
+
```json
24
+
{ "$$router.get": { "path": "/resource/1" } }
25
+
```
26
+
27
+
This is achieved via [macros](#macros) support.
28
+
21
29
JSONScript core includes other instructions that will be shown later and the interpreter may allow to define your own custom instructions.
22
30
23
31
With JSONScript you can combine instructions in three ways:
@@ -50,11 +58,20 @@ JSONScript can include several instructions that will be executed sequentially:
"Sequential" means that the next script begins evaluating only after the script in the previous item has evaluated (and if the result is an asynchronous value this value has resolved into synchronous).
59
76
60
77
The example above will retrive the value of some resource and once it was retrieved it will update it. The sequential execution guarantees that it will be the value before the update.
@@ -76,7 +93,7 @@ For example, this script does the same as the script above for two resources:
The result of this script evaluation is the array of two arrays containing two items each, the items being the results of evaluation of individual instructions.
98
131
99
132
@@ -116,6 +149,15 @@ JSONScript can include several instructions that will be executed in parallel:
The meaning of "parallel" depends on the environment in which the script executes and on the interpreter implementation. The implementation should not guarantee any order in which evaluation of individual scripts will start, and instructions don't wait until another instruction evaluates (and resolves) to begin executing.
120
162
121
163
The example above retrieves the values fo two resources. The result of the evaluation of the whole script above is a single asynchronous value (assuming that instructions return asynchronous values) that resolves into the object with the results of both instructions available in properties `"res1"` and `"res2"`.
@@ -137,7 +179,7 @@ For example, the script below is similar to the example in the previous section
This example combines parallel and sequential evaluation. Each resource update only starts after the current value is retrieved, but the update of `"/resource/2"` does not need to wait until the `"/resource/1"` finished updating.
159
216
160
217
The result of this script evaluation is the object with two properties `"res1"` and `"res2"` each containing two items with the results of individual instructions.
@@ -183,6 +240,15 @@ During the evaluation the script can use the data instance passed to the interpe
The result of the evaluation of the script in `$if` keyword should be a boolean value, otherwise the whole script will fail to evailuate (no type coercion is made).
268
344
269
345
If the condition is `true` then the script in `$then` keyword will be evaluted and its result will be the result of `$if` instruction, otherwise the script in `$else` will be evaluated and `$if` evaluate to its result.
The evaluation result will be the same as without `$delay` istruction, but the second "$exec" instruction will start executing at least 50 milliseconds later than the first.
332
420
333
421
This instruction can also be used to create asynchronous value from synchronous value. For example if some executor expects an asynchronous value as an argument and you want to pass a constant, you can use `$delay`:
@@ -343,6 +431,17 @@ This instruction can also be used to create asynchronous value from synchronous
343
431
}
344
432
```
345
433
434
+
or with short syntax:
435
+
436
+
```json
437
+
{
438
+
"$$logger.resolve": {
439
+
"message": "Resolved",
440
+
"asyncValue": { "$delay": "test", "$wait": 1000 }
441
+
}
442
+
}
443
+
```
444
+
346
445
In the example above a hypothetical logger logs message when asynchronous value is resolved. `$delay` instruction result is an asynchrnous value that resolves 1 second after its evaluation with the value `"test"`.
347
446
348
447
`$wait` keyword is optional, the default is 0. It means that the interpreter should schedule the script evaluation as soon as possible but do not execute it immediately.
@@ -378,6 +477,27 @@ Anonymous or named function can be defined in the script to be passed to executo
In the example above the same function `getRes` is used three times, being called by name and using $ref with absolute and relative JSON-pointers. Arguments can be passed to function as array, as an object (property names should match parameters declarations, otherwise an exception will be thrown) and as a scalar value if there is only one parameter.
382
502
383
503
Functions can be used as parameters in the executors:
@@ -406,6 +526,26 @@ Functions can be used as parameters in the executors:
406
526
}
407
527
```
408
528
529
+
or with short syntax:
530
+
531
+
```json
532
+
{
533
+
"$$array.map": {
534
+
"data": [
535
+
"/resource/1",
536
+
"/resource/2",
537
+
"/resource/3"
538
+
],
539
+
"iterator": {
540
+
"$func": {
541
+
"$$router1.get": { "path": { "$data": "/path" } }
542
+
},
543
+
"$args": ["path"]
544
+
}
545
+
}
546
+
}
547
+
```
548
+
409
549
If the function was previously defined it can be passed either using `"$ref"` with an absolute or relative JSON-pointer or `{ "$func": "myfunc" }. The latter always evaluates as the reference to the existing function rather than the function that always returns string "myfunc", to define the function that always returns the same string you can use "$quote".
410
550
411
551
@@ -421,7 +561,7 @@ To insert an object that contains properties that start with `"$"` that normally
421
561
}
422
562
```
423
563
424
-
evaluates as: `{ "$exec": "myExec" }`.
564
+
evaluates as: `{ "$exec": "myExec" }` and the executor is not called.
425
565
426
566
`$quote` can also be used to define the function that always returns the same string:
The anonymous function defined above always returns the string `"foo"`. Without `$quote` it would be the reference to the function with the name `foo`.
574
+
The anonymous function defined above always returns the string `"foo"`. Without `$quote` it would have been the reference to the function with the name `foo`.
575
+
576
+
577
+
## Macros
578
+
579
+
JSONScript defines several core macros to support short syntax for executor and function calls and for calculations. The interpreter may allow to define your own custom macros.
Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,9 @@ JSONScript is created to manage scripted execution in remote systems to avoid th
12
12
13
13
JSONScript:
14
14
15
-
- uses JSON as its representaion format for both data and control structures, being similar to lisp (homoiconic)
15
+
- uses JSON as its representaion format for both data and control structures, being similar to lisp (homoiconic).
16
16
- defines simple control structures.
17
-
- is asynchronous and concurrent without the need to use any special keywords
17
+
- is asynchronous and concurrent without the need to use any special keywords.
18
18
- actual processing in the remote system is done synchronously or asynchronously by the functions and objects supplied to JSONScript interpreter by the host environment.
JSONScript uses JSON-Schema standard both for the validation schemas and for the schema that defines evaluation process.
3
+
JSONScript uses JSON-Schema standard both for the validation schemas and for the schemas that define macro expansion and evaluation process.
4
4
5
-
[JSONScript schema](http://www.json-script.com/schema/schema.json#) the schema that does not validate scalar keywords in instructions (keyword values can be scripts and have to be validated when the script is evaluated).
5
+
[JSONScript schema](http://www.json-script.com/schema/schema.json#)- the schema for JSONScript that does not validate scalar keywords in instructions (keyword values can be scripts and have to be validated when the script is evaluated).
6
6
7
-
[JSONScript strict schema](http://www.json-script.com/schema/schema_strict.json#) the schema that validates scalar keywords in instructions.
7
+
[JSONScript strict schema](http://www.json-script.com/schema/schema_strict.json#)- the schema for JSONScript that validates scalar keywords in instructions.
8
8
9
-
[JSONScript evaluation schema](http://www.json-script.com/schema/evaluate.json#) this schema defines evalution process. It can be used by implementations to evaluate scripts. It contains non-standard keywords.
9
+
[Macro expansion schema](http://www.json-script.com/schema/expand_macros.json#)- this schema defines macro expansion process. It can be used by implementations to expand macros in the scripts before their evaluation. It contains non-standard keyword `expandJsMacro`.
10
10
11
-
[Instruction definition schema](http://www.json-script.com/schema/instruction.json#) the schema for instruction defnitions. The definitions of both standard and user-defined instructions should be valid according to this schema.
11
+
[Evaluation schema](http://www.json-script.com/schema/evaluate.json#) - this schema defines evalution process. It can be used by implementations to evaluate scripts. It contains non-standard keywords.
12
+
13
+
[Instruction definition schema](http://www.json-script.com/schema/instruction.json#) - the schema for instruction defnitions. The definitions of both standard and user-defined instructions should be valid according to this schema.
14
+
15
+
[Macro definition schema](http://www.json-script.com/schema/macro.json#) - the schema for macro definition. The definitions of both standard and user-defined macros should be valid according to this schema.
0 commit comments