Repository navigation
Expressions Blocks Basics
All the expressions begins with the at symbol @ in the key pro the name of the property.
Example:
{
"@return":50
}
Result
50
If the key doesn't have an @ symbol then is not a priest expression, it's a regular JSON value.
Example:
{
"return":50
}
Result
{
"return":50
}
Any expression has at least 3 parts.
- Key: Name of the expression.
- hint: (Optional) A random string that can be used as a simple hint for the expression to execute.
- Input: (Optional) Object used as arguments of the expression.
In the following example set is the name of the expression, variable1 is the hint and 2 is the input.
{
"@set(variable1)": 2
}
The syntax would be like this:
{
"@<expression-name>(<hint>)": <input>
}
When a expression key is present in a object definition that object becomes an expression-block and no JSON regular keys are allowed anymore.
The following expression-block is illegal, it contains both expressions and regular JSON keys at the same level of the document:
{
"@set(name)": "Name",
"regularJSONNumber": 2000 // This is illegal, @set already converted this object into a expression-block.
}
The previous code should be formatted as follows:
{
"@set(name)": "Name",
"@return":{
"regularJSONNumber": 2000
}
}
And the result would be:
{
"regularJSONNumber": 2000
}
The expression block only returns the last result.
Example:
{
"@return": 2,
" @return": 5,
}
The result is:
5
JSON keys must be unique, if there is a repeated key the parser will ignore it.
Example, the following priest.js is a valid JSON Document but only one @set expression will reach the compiler:
{
"@set(x)": 23,
"@set(x)": 25
"@return": {
"@get(x)": null
}
}
The result will be usually 23 since this is an error of the JSON Document and the parser is free to take any decision. To solve this situation the priest.js compiler will ignore any whitespace before the @ symbol, so for JSON you actually have different keys but for the compiler is a different instruction:
Example:
{
"@set(x)": 23,
" @set(x)": 25
"@return": {
"@get(x)": null
}
}
You can use as many white-spaces as you need.
Since the input can be any JSON value it can also become another priest.js expression.
In the following example you can see how the input of the high level @return is actually the result of nested @return Example:
{
"@return": {
"@return": {
"point": {
"x": 22.4,
"y": 45.8
}
}
}
}
The result:
{
"point": {
"x": 22.4,
"y": 45.8
}
}
Depending of how the expression works the input can be ignored or processed. Conditional expressions like @if and @else uses this technique.
Example:
{
"@set(chuckNorrisIsHere)": false,
"@if(chuckNorrisIsHere)": "Chuck is here, You are dead!",
"@else": "It's never here!, you are safe!"
}
The result is:
"It's never here!, you are safe!"