Walk a local JSON Pointer (``#/a/b/0``) against ``root``. Returns a tagged result so the caller can distinguish a malformed pointer from a structurally valid pointer whose target is absent.
(root: t.Dict[str, t.Any], pointer: str)
| 78 | |
| 79 | |
| 80 | def _try_resolve_pointer(root: t.Dict[str, t.Any], pointer: str) -> _Resolution: |
| 81 | """Walk a local JSON Pointer (``#/a/b/0``) against ``root``. |
| 82 | |
| 83 | Returns a tagged result so the caller can distinguish a malformed pointer |
| 84 | from a structurally valid pointer whose target is absent. |
| 85 | """ |
| 86 | if pointer in ("#", ""): |
| 87 | return _Resolution(ok=True, value=root) |
| 88 | if not pointer.startswith("#/"): |
| 89 | return _Resolution(ok=False, reason="malformed-pointer") |
| 90 | |
| 91 | cursor: t.Any = root |
| 92 | for raw_segment in pointer[2:].split("/"): |
| 93 | segment = _decode_pointer_segment(raw_segment) |
| 94 | if isinstance(cursor, list): |
| 95 | try: |
| 96 | index = int(segment) |
| 97 | except ValueError: |
| 98 | return _Resolution(ok=False, reason="missing-target", failed_at=segment) |
| 99 | if index < 0 or index >= len(cursor): |
| 100 | return _Resolution(ok=False, reason="missing-target", failed_at=segment) |
| 101 | cursor = cursor[index] |
| 102 | elif isinstance(cursor, dict): |
| 103 | if segment not in cursor: |
| 104 | return _Resolution(ok=False, reason="missing-target", failed_at=segment) |
| 105 | cursor = cursor[segment] |
| 106 | else: |
| 107 | return _Resolution(ok=False, reason="missing-target", failed_at=segment) |
| 108 | return _Resolution(ok=True, value=cursor) |
| 109 | |
| 110 | |
| 111 | def _raise_resolution_error(pointer: str, resolution: _Resolution) -> t.NoReturn: |
no test coverage detected
searching dependent graphs…