mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
Improve documentation and testing for conditions
This commit is contained in:
parent
839afe5ac0
commit
dcececf9c0
@ -23,14 +23,47 @@ A condition is expressed as a JSON object with the following keys:
|
|||||||
* `in`: Is present within a list of values
|
* `in`: Is present within a list of values
|
||||||
* `contains`: Contains the specified value
|
* `contains`: Contains the specified value
|
||||||
|
|
||||||
|
### Accessing Nested Keys
|
||||||
|
|
||||||
|
To access nested keys, use dots to denote the path to the desired attribute. For example, assume the following data:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"a": {
|
||||||
|
"b": {
|
||||||
|
"c": 123
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The following condition will evaluate as true:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"attr": "a.b.c",
|
||||||
|
"value": 123
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
`name` equals "foobar":
|
`name` equals "foo":
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"attr": "name",
|
"attr": "name",
|
||||||
"value": "foobar"
|
"value": "foo"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`name` does not equal "foo"
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"attr": "name",
|
||||||
|
"value": "foo",
|
||||||
|
"negate": true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -64,7 +64,11 @@ class Condition:
|
|||||||
"""
|
"""
|
||||||
Evaluate the provided data to determine whether it matches the condition.
|
Evaluate the provided data to determine whether it matches the condition.
|
||||||
"""
|
"""
|
||||||
value = functools.reduce(dict.get, self.attr.split('.'), data)
|
try:
|
||||||
|
value = functools.reduce(dict.get, self.attr.split('.'), data)
|
||||||
|
except TypeError:
|
||||||
|
# Invalid key path
|
||||||
|
value = None
|
||||||
result = self.eval_func(value)
|
result = self.eval_func(value)
|
||||||
|
|
||||||
if self.negate:
|
if self.negate:
|
||||||
|
@ -35,6 +35,16 @@ class ConditionTestCase(TestCase):
|
|||||||
# 'gt' supports only numeric values
|
# 'gt' supports only numeric values
|
||||||
Condition('x', 'foo', 'gt')
|
Condition('x', 'foo', 'gt')
|
||||||
|
|
||||||
|
#
|
||||||
|
# Nested attrs tests
|
||||||
|
#
|
||||||
|
|
||||||
|
def test_nested(self):
|
||||||
|
c = Condition('x.y.z', 1)
|
||||||
|
self.assertTrue(c.eval({'x': {'y': {'z': 1}}}))
|
||||||
|
self.assertFalse(c.eval({'x': {'y': {'z': 2}}}))
|
||||||
|
self.assertFalse(c.eval({'a': {'b': {'c': 1}}}))
|
||||||
|
|
||||||
#
|
#
|
||||||
# Operator tests
|
# Operator tests
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user