Skip to content

Commit 73a555d

Browse files
committed
docs: function extensions [skip ci]
1 parent 32b69c7 commit 73a555d

3 files changed

Lines changed: 109 additions & 4 deletions

File tree

‎docs/advanced.md‎

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,106 @@
1-
# Advanced
1+
# Advanced Usage
22

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:

‎docs/quickstart.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,6 @@ other_users = path.findall(other_data)
135135

136136
## What's Next
137137

138-
Read about user-defined filter functions at [Function Extensions](advanced.md), or see how to make extra data available to filters with [Extra Filter Context](advanced.md).
138+
Read about user-defined filter functions at [Function Extensions](advanced.md#function-extensions), or see how to make extra data available to filters with [Extra Filter Context](advanced.md#extra-filter-context).
139139

140-
`findall`, `finditer` and `compile` are shortcuts that use the default[`JSONPathEnvironment`](api.md#jsonpath.JSONPathEnvironment). `jsonpath.findall(path, data)` is equivalent to `jsonpath.JSONPathEnvironment().compile(path).findall(data)`. If you would like to use a custom environment, see [Advanced Usage](advanced.md).
140+
`findall`, `finditer` and `compile` are shortcuts that use the default[`JSONPathEnvironment`](api.md#jsonpath.JSONPathEnvironment). `jsonpath.findall(path, data)` is equivalent to `jsonpath.JSONPathEnvironment().compile(path).findall(data)`. If you would like to use a custom environment, see [Advanced Usage](advanced.md#custom-environments).

‎jsonpath/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .exceptions import JSONPathNameError
88
from .exceptions import JSONPathSyntaxError
99
from .exceptions import JSONPathTypeError
10+
from .filter import UNDEFINED
1011
from .lex import Lexer
1112
from .match import JSONPathMatch
1213
from .parse import Parser
@@ -29,6 +30,7 @@
2930
"JSONPathTypeError",
3031
"Lexer",
3132
"Parser",
33+
"UNDEFINED",
3234
)
3335

3436

0 commit comments

Comments
 (0)