• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SlowAPI / fast-json-pointer / 3798008751

pending completion
3798008751

push

github

Tristan Sweeney
Refactor to compile json pointer down to ops

186 of 186 new or added lines in 5 files covered. (100.0%)

270 of 279 relevant lines covered (96.77%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.36
/src/fast_json_pointer/low_verbs.py
1
from dataclasses import dataclass
1✔
2
from typing import *
1✔
3

4
from .exceptions import (CompilationException, EndOfArrayException,
1✔
5
                         ParseException, ResolutionException)
6
from .jsontypes import JsonType
1✔
7
from .resolver import Operation, resolve
1✔
8

9

10
def get(doc: JsonType, path: list[Operation]) -> JsonType:
1✔
11
    result = resolve(doc, path)
1✔
12
    return result.value
1✔
13

14

15
def _set_ref(doc: JsonType, part: str, value: JsonType) -> None:
1✔
16
    match doc:
1✔
17
        case dict():
1✔
18
            doc[part] = value
1✔
19

20
        case list() if part == "-":
1✔
21
            doc.append(value)
1✔
22

23
        case list():
1✔
24
            part_idx = int(part)
1✔
25
            if len(doc) == part_idx:
1✔
26
                doc.append(value)
1✔
27
            else:
28
                doc[part_idx] = value
1✔
29
        case _:
×
30
            raise RuntimeError(f"Unnavigable type {type(doc)}")
×
31

32

33
def add(doc: JsonType, path: list[Operation], value: JsonType) -> None:
1✔
34
    try:
1✔
35
        result = resolve(doc, path)
1✔
36
        if result.is_index_result:
1✔
37
            raise RuntimeError()
×
38
    except ResolutionException as e:
1✔
39
        if len(e.remaining) > 1:
1✔
40
            raise RuntimeError("Can't ")
×
41

42
        if len(e.remaining) == 0:
1✔
43
            raise RuntimeError()
×
44

45
        _set_ref(e.refs[-1].doc, e.remaining[0].step, value)
1✔
46
    else:
47

48
        _set_ref(result.refs[-2].doc, result.refs[-1].operation.step, value)
1✔
49

50

51
def remove(doc: JsonType, path: list[Operation]) -> JsonType:
1✔
52
    result = resolve(doc, path)
1✔
53
    parent, last = result.refs[-2:]
1✔
54

55
    part = last.operation.step
1✔
56

57
    match parent.doc:
1✔
58
        case dict():
1✔
59
            del parent.doc[part]
1✔
60
        case list():
1✔
61
            del parent.doc[int(part)]
1✔
62
        case _:
×
63
            raise RuntimeError()
×
64

65
    return result.value
1✔
66

67

68
def replace(doc: JsonType, path: list[Operation], value: JsonType) -> JsonType:
1✔
69
    result = resolve(doc, path)
1✔
70
    parent, last = result.refs[-2:]
1✔
71

72
    part = last.operation.step
1✔
73

74
    match parent.doc:
1✔
75
        case dict():
1✔
76
            parent.doc[part] = value
1✔
77
        case list():
1✔
78
            parent.doc[int(part)] = value
1✔
79
        case _:
×
80
            raise RuntimeError()
×
81

82
    return result.value
1✔
83

84

85
def move(doc: JsonType, path: list[Operation], from_: list[Operation]) -> None:
1✔
86
    obj = remove(doc, from_)
1✔
87
    add(doc, path, obj)
1✔
88

89

90
def copy(doc: JsonType, path: list[Operation], from_: list[Operation]) -> None:
1✔
91
    obj = get(doc, from_)
1✔
92
    add(doc, path, obj)
1✔
93

94

95
def test(doc: JsonType, path: list[Operation], value: JsonType) -> bool:
1✔
96
    obj = get(doc, path)
1✔
97
    return obj == value
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc