Just like any other programming language, In Mule4, dataweave provides matchfunction to achieve the functionality of if-else statements.
A match expression contains a list of case statements that can optionally have an else statement. Each case statement consists of a conditional selector expression that must evaluate to either true or false.
Pattern matching is another method of flow control, but it does quite a bit more under the hood than the if/else expression does, and the syntax is a little more complicated. Like the if/else expression, pattern matching also returns a single value. Here’s a simplification of how pattern matching expressions are formatted:
Let’s consider a scenario where you need to update certain fields of incoming payload keeping the rest of the fields as it is, you can use a match and case.
match Statement Syntax:
<input_expression> match {
case <condition> -> <execute_if_condition_pass>
case <condition> -> <execute_if_condition_pass>
else -> <execute_if_no_condition_pass>
}
inputExpression match {
case <condition> -> <routing expression>
case <condition> -> <routing expression>
else -> <default routing expression>
}
The easiest way to understand basic pattern matching it to show an example:
{
"action": "buy"
}
%dw 2.0
output json
---
payload.action match {
case "buy" -> “Buy at market price"
case "sell" -> "Sell at market price"
case "hold" -> “Hold asset"
else -> "Invalid input"
}
"Buy at market price"
Follow Me If you like my post please follow me to read my latest post on programming and technology. Instagram Facebook
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…