In this section, various conditional functions will be explored, which are available for use in PubSubDB mapping rules. The functions are designed to facilitate the manipulation and transformation of data using conditional operations during the mapping process. These functions are adapted from the ConditionalHandler
class, and are implemented as a functional system referred to as @pipes.
Table of Contents
- conditional.ternary
- conditional.equality
- conditional.strict_equality
- conditional.greater_than
- conditional.less_than
- conditional.greater_than_or_equal
- conditional.less_than_or_equal
The conditional.ternary
function evaluates a condition and returns one of the two provided values based on the result of the condition. It takes three parameters: the condition to evaluate, the value to return if the condition is true, and the value to return if the condition is false.
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"isActive": true,
"activeLabel": "Active",
"inactiveLabel": "Inactive"
}
}
The goal is to create a new object with the label based on the isActive
value. The conditional.ternary
function can be used in the mapping rules as follows:
status_label:
"@pipe":
- ["{a.data.isActive}", "{a.data.activeLabel}", "{a.data.inactiveLabel}"]
- ["{@conditional.ternary}"]
After executing the mapping rules, the resulting JSON object will be:
{
"status_label": "Active"
}
The conditional.equality
function checks whether two values are equal, using non-strict equality (==). It takes two parameters: the first value (value1
) and the second value (value2
).
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"value1": 42,
"value2": "42"
}
}
The goal is to create a new object with a boolean value indicating whether value1
and value2
are equal. The conditional.equality
function can be used in the mapping rules as follows:
are_values_equal:
"@pipe":
- ["{a.data.value1}", "{a.data.value2}"]
- ["{@conditional.equality}"]
After executing the mapping rules, the resulting JSON object will be:
{
"are_values_equal": true
}
The conditional.strict_equality
function checks whether two values are equal, using strict equality (===). It takes two parameters: the first value (value1
) and the second value (value2
).
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"value1": 42,
"value2": "42"
}
}
The goal is to create a new object with a boolean value indicating whether value1
and value2
are strictly equal. The conditional.strict_equality
function can be used in the mapping rules as follows:
are_values_strictly_equal:
"@pipe":
- ["{a.data.value1}", "{a.data.value2}"]
- ["{@conditional.strict_equality}"]
After executing the mapping rules, the resulting JSON object will be:
{
"are_values_strictly_equal": false
}
The conditional.greater_than
function checks whether the first value is greater than the second value. It takes two parameters: the first number (value1
) and the second number (value2
).
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"value1": 42,
"value2": 30
}
}
The goal is to create a new object with a boolean value indicating whether value1
is greater than value2
. The conditional.greater_than
function can be used in the mapping rules as follows:
is_value1_greater_than_value2:
"@pipe":
- ["{a.data.value1}", "{a.data.value2}"]
- ["{@conditional.greater_than}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_value1_greater_than_value2": true
}
The conditional.less_than
function checks whether the first value is less than the second value. It takes two parameters: the first number (value1
) and the second number (value2
).
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"value1": 42,
"value2": 50
}
}
The goal is to create a new object with a boolean value indicating whether value1
is less than value2
. The conditional.less_than
function can be used in the mapping rules as follows:
is_value1_less_than_value2:
"@pipe":
- ["{a.data.value1}", "{a.data.value2}"]
- ["{@conditional.less_than}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_value1_less_than_value2": true
}
The conditional.greater_than_or_equal
function checks whether the first value is greater than or equal to the second value. It takes two parameters: the first number (value1
) and the second number (value2
).
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"value1": 42,
"value2": 42
}
}
The goal is to create a new object with a boolean value indicating whether value1
is greater than or equal to value2
. The conditional.greater_than_or_equal
function can be used in the mapping rules as follows:
is_value1_greater_than_or_equal_value2:
"@pipe":
- ["{a.data.value1}", "{a.data.value2}"]
- ["{@conditional.greater_than_or_equal}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_value1_greater_than_or_equal_value2": true
}
The conditional.less_than_or_equal
function checks whether the first value is less than or equal to the second value. It takes two parameters: the first number (value1
) and the second number (value2
).
Suppose there are the following input JSON objects:
Object A:
{
"data": {
"value1": 42,
"value2": 42
}
}
The goal is to create a new object with a boolean value indicating whether value1
is less than or equal to value2
. The conditional.less_than_or_equal
function can be used in the mapping rules as follows:
is_value1_less_than_or_equal_value2:
"@pipe":
- ["{a.data.value1}", "{a.data.value2}"]
- ["{@conditional.less_than_or_equal}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_value1_less_than_or_equal_value2": true
}