pydantic-json-patch 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pydantic_json_patch
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Pydantic models for implementing JSON Patch.
5
5
  Author: Jonathan Sharpe
6
6
  Author-email: Jonathan Sharpe <mail@jonrshar.pe>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pydantic_json_patch"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "Pydantic models for implementing JSON Patch."
5
5
  readme = "README.md"
6
6
  authors = [
@@ -1,7 +1,7 @@
1
1
  import re
2
2
  import typing as tp
3
3
  from collections.abc import Sequence
4
- from functools import cached_property
4
+ from functools import cached_property, lru_cache
5
5
 
6
6
  import typing_extensions as tx
7
7
  from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, model_validator
@@ -21,8 +21,7 @@ class _BaseOp(BaseModel):
21
21
  @classmethod
22
22
  def create(cls, *, path: str | Sequence[str], **kwargs) -> tx.Self:
23
23
  (op,) = tp.get_args(cls.model_fields["op"].annotation)
24
- pointer = path if isinstance(path, str) else cls._dump_pointer(path)
25
- return cls(op=op, path=pointer, **kwargs)
24
+ return cls(op=op, path=cls._dump_pointer(path), **kwargs)
26
25
 
27
26
  op: tp.Literal["add", "copy", "move", "remove", "replace", "test"]
28
27
  """The operation being represented."""
@@ -35,18 +34,26 @@ class _BaseOp(BaseModel):
35
34
  """The decoded tokens in the 'path' JSON pointer."""
36
35
  return self._load_pointer(self.path)
37
36
 
37
+ @classmethod
38
+ def _dump_pointer(cls, pointer: Sequence[str]) -> str:
39
+ if isinstance(pointer, str):
40
+ return pointer
41
+ return "/".join(["", *(cls._encode_token(token) for token in pointer)])
42
+
38
43
  @staticmethod
39
- def _dump_pointer(pointer: Sequence[str]) -> str:
40
- return "/".join(
41
- ["", *(token.replace("~", "~0").replace("/", "~1") for token in pointer)]
42
- )
44
+ @lru_cache
45
+ def _decode_token(token: str) -> str:
46
+ return token.replace("~1", "/").replace("~0", "~")
43
47
 
44
48
  @staticmethod
45
- def _load_pointer(pointer: str) -> tuple[str, ...]:
46
- return tuple(
47
- token.replace("~1", "/").replace("~0", "~")
48
- for token in pointer.split("/")[1:]
49
- )
49
+ @lru_cache
50
+ def _encode_token(token: str) -> str:
51
+ return token.replace("~", "~0").replace("/", "~1")
52
+
53
+ @classmethod
54
+ @lru_cache
55
+ def _load_pointer(cls, pointer: str) -> tuple[str, ...]:
56
+ return tuple(cls._decode_token(token) for token in pointer.split("/")[1:])
50
57
 
51
58
 
52
59
  class _FromOp(_BaseOp):