|
1 | | -# Advanced |
| 1 | +# Advanced Usage |
2 | 2 |
|
3 | | -TODO |
| 3 | +## Function Extensions |
| 4 | + |
| 5 | +Add, remove or replace [filter functions](functions.md) by updating [`JSONPathEnvironment.function_extensions`](api.md#jsonpath.env.JSONPathEnvironment.function_extensions). It is a regular Python dictionary mapping the name of the function to any [Callable](https://docs.python.org/3/library/typing.html#typing.Callable), like a function or class with a `__call__` method. |
| 6 | + |
| 7 | +### Example |
| 8 | + |
| 9 | +As an example, we'll add a `min()` filter function, which will return the minimum of a sequence of values. If any of the values are not comparable, we'll return the special `undefined` value instead. |
| 10 | + |
| 11 | +```python |
| 12 | +from typing import Iterable |
| 13 | +import jsonpath |
| 14 | + |
| 15 | + |
| 16 | +def min_filter(obj: object) -> object: |
| 17 | + if not isinstance(obj, Iterable): |
| 18 | + return jsonpath.UNDEFINED |
| 19 | + |
| 20 | + try: |
| 21 | + return min(obj) |
| 22 | + except TypeError: |
| 23 | + return jsonpath.UNDEFINED |
| 24 | + |
| 25 | + |
| 26 | +env = jsonpath.JSONPathEnvironment() |
| 27 | +env.function_extensions["min"] = min_filter |
| 28 | +``` |
| 29 | + |
| 30 | +Now, when we use `env.finall()`, `env.finditer()` or `env.compile()`, our `min` function will be available for use in filter expressions. |
| 31 | + |
| 32 | +```text |
| 33 | +$..products[[email protected] == min($..products.price)] |
| 34 | +``` |
| 35 | + |
| 36 | +### Built-in Functions |
| 37 | + |
| 38 | +The [built-in functions](functions.md) can be removed from a `JSONPathEnvironment` by deleting the entry from `function_extensions`. |
| 39 | + |
| 40 | +```python |
| 41 | +import jsonpath |
| 42 | + |
| 43 | +env = jsonpath.JSONPathEnvironment() |
| 44 | +del env.function_extensions["keys"] |
| 45 | +``` |
| 46 | + |
| 47 | +Or aliased with an additional entry. |
| 48 | + |
| 49 | +```python |
| 50 | +import jsonpath |
| 51 | + |
| 52 | +env = jsonpath.JSONPathEnvironment() |
| 53 | +env.function_extensions["properties"] = env.function_extensions["keys"] |
| 54 | +``` |
| 55 | + |
| 56 | +Alternatively, you could subclass `JSONPathEnvironment` and override the `setup_function_extensions` method. |
| 57 | + |
| 58 | +```python |
| 59 | +from typing import Iterable |
| 60 | +import jsonpath |
| 61 | + |
| 62 | +class MyEnv(jsonpath.JSONPathEnvironment): |
| 63 | + def setup_function_extensions(self) -> None: |
| 64 | + super().setup_function_extensions() |
| 65 | + self.function_extensions["properties"] = self.function_extensions["keys"] |
| 66 | + self.function_extensions["min"] = min_filter |
| 67 | + |
| 68 | + |
| 69 | +def min_filter(obj: object) -> object: |
| 70 | + if not isinstance(obj, Iterable): |
| 71 | + return jsonpath.UNDEFINED |
| 72 | + |
| 73 | + try: |
| 74 | + return min(obj) |
| 75 | + except TypeError: |
| 76 | + return jsonpath.UNDEFINED |
| 77 | + |
| 78 | +env = MyEnv() |
| 79 | +``` |
| 80 | + |
| 81 | +### Compile Time Validation |
| 82 | + |
| 83 | +A function extension's arguments can be validated at compile time by implementing the function as a class with a `__call__` method, and a `validate` method. `validate` will be called after parsing the function, giving you the opportunity to inspect its arguments and raise a `JSONPathTypeError` should any arguments be unacceptable. If defined, `validate` must take a reference to the current environment, an argument list and the token pointing to the start of the function call. |
| 84 | + |
| 85 | +```python |
| 86 | +def validate( |
| 87 | + self, |
| 88 | + env: JSONPathEnvironment, |
| 89 | + args: List[FilterExpression], |
| 90 | + token: Token, |
| 91 | +) -> List[FilterExpression]: |
| 92 | +``` |
| 93 | + |
| 94 | +It should return an argument list, either the same as the input argument list, or a modified version of it. See the implementation of the built-in [`match` function](https://github.com/jg-rp/python-jsonpath/blob/main/jsonpath/function_extensions/match.py) for an example. |
| 95 | + |
| 96 | +## Extra Filter Context |
| 97 | + |
| 98 | +TODO: |
| 99 | + |
| 100 | +## Custom Environments |
| 101 | + |
| 102 | +TODO: |
| 103 | + |
| 104 | +## Undefined |
| 105 | + |
| 106 | +TODO: |
0 commit comments