Macro Examples

The following examples show the structure and syntax of macros in rules.

Here’s an example macro showing the use of attributes, parameters, and “path” statements.

1"macros":
2 [

3        {

4            "namespace": "myMacroNamespace",
5 
           "definitions": [

6                {

7                    "name": "multiplyTwoNumbers",
8 
                   "parameters": [

9                        "firstNumber",
10 
                       "secondNumber"

11                    ],
12 
                   "returns": "${p.firstNumber * p.secondNumber}"

13                },
14 
                {

15                    "name": "deleteWidget",
16 
                   "description": "Deletes a widget and any references to the widget.",
17 
                   "parameters": [

18                        "widgetName"

19                    ],
20 
                   "actions": [

21                        {

22                            "action": "eval",
23 
                           "key": "results",
24 
                           "path": "${json:searchPaths(\”$.state.widget['p.widgetName']\”)}”

25                       },
26 
                        {

27                            "condition": "${!empty results}",
28 
                           "action": "delete",
29 
                           "path": "$.state.widgets['${p.widgetName}']"

30                        },
31 
                        {

32                            "condition": "${!empty results}",
33 
                           "action": "delete",
34 
                           "path": "$.state.gridLayouts..pages..widgets[?(@.name=='${p.widgetName}')]"

35                        }

36                    ]

37                }

38            ]

39        }

40    ],
41 
    "constants": [...],
42 
    "rules": [...]
}

The following example calls a macro with namespace 'myNS' and macro name 'getSomething' defined with no parameters. Results of the macro are stored in the template context, and can be referenced using this expression: ${Rules.Eval.macroResults}.

1{
2  "action": "eval",
3  "key": "macroResults",
4  "value": "${myNS:getSomething()}"
5}

This example sets the JSON node at path $.path.to.json.node with the result of macro myNS:getJsonValue defined with no parameters. This action will only be called if the macro myNS:shouldExecute (defined with one parameter) returns true.

1{
2  "action": "set",
3  "condition": "${myNS:shouldExecute(true)}",
4  "path": "$.path.to.json.node",
5  "value": "${myNS:getJsonValue()}"
6}