File: value.py

package info (click to toggle)
python-jsonpath 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,028 kB
  • sloc: python: 9,473; makefile: 6
file content (24 lines) | stat: -rw-r--r-- 721 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""The standard `value` function extension."""
from __future__ import annotations

from typing import TYPE_CHECKING

from jsonpath.filter import UNDEFINED
from jsonpath.function_extensions import ExpressionType
from jsonpath.function_extensions import FilterFunction

if TYPE_CHECKING:
    from jsonpath.match import NodeList


class Value(FilterFunction):
    """A type-aware implementation of the standard `value` function."""

    arg_types = [ExpressionType.NODES]
    return_type = ExpressionType.VALUE

    def __call__(self, nodes: NodeList) -> object:
        """Return the first node in a node list if it has only one item."""
        if len(nodes) == 1:
            return nodes[0].obj
        return UNDEFINED