Condition
Condition is a meta-inspector that evaluates data with a condition (multiple inspectors plus an operator).
This inspector makes it possible to create complex conditions that are otherwise impossible, such as:
- ( foo OR bar ) AND ( baz OR qux )
- ( foo AND bar ) OR ( baz AND qux )
Interpretation Methods
The inspector supports the interpretation methods of the inspectors passed to the configured condition.
Options
The inspector uses any operator for its options.
Use Cases
Chaining OR with AND
The logic ( foo OR bar ) AND ( baz OR qux ) is possible by combining an any-based condition with all-based conditions as inspectors. In this example, the value of foo is copied to bar if either of these conditions is true:
- the value of
foohas a length greater than zero and starts with the substring "x" - the value of
foohas a length less than four and ends with the substring "ar"
local processors = [
sub.interfaces.processor.copy(
settings={ key: 'foo', set_key: 'bar', condition: sub.interfaces.operator.any([
sub.interfaces.inspector.condition(
options=sub.interfaces.operator.all([
sub.patterns.inspector.length.gt_zero(key='foo'),
sub.patterns.inspector.strings.starts_with(key='foo', expression='x'),
]),
),
sub.interfaces.inspector.condition(
options=sub.interfaces.operator.all([
sub.interfaces.inspector.length(
settings={ key: 'foo' },
options={ type: 'less_than', value: 4 }
),
sub.patterns.inspector.strings.ends_with(key='foo', expression='ar'),
]),
),
]) },
)
];
Chaining AND with OR
The logic ( foo OR bar ) AND ( baz OR qux ) is possible by combining an all-based condition with any-based conditions as inspectors. In this example, the value of foo is copied to bar if both of these conditions is true:
- the value of
foostarts with the substring "b" or starts with the substring "x" - the value of
foohas a length greater than two or a length less than five
local processors = [
sub.interfaces.processor.copy(
settings={ key: 'foo', set_key: 'bar', condition: sub.interfaces.operator.all([
sub.interfaces.inspector.condition(
options=sub.interfaces.operator.any([
sub.patterns.inspector.strings.starts_with(key='foo', expression='b'),
sub.patterns.inspector.strings.starts_with(key='foo', expression='x'),
]),
),
sub.interfaces.inspector.condition(
options=sub.interfaces.operator.any([
sub.interfaces.inspector.length(
settings={ key: 'foo' },
options={ type: 'greater_than', value: 2 }
),
sub.interfaces.inspector.length(
settings={ key: 'foo' },
options={ type: 'less_than', value: 5 }
),
]),
),
]) },
)
];
Updated almost 3 years ago
