Skip to content

Commit 2889e63

Browse files
committed
refactor: implement _get_path_parts util
1 parent ab767aa commit 2889e63

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

scim2_models/messages/patch_op.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ..urn import _resolve_path_to_target
2323
from ..utils import _extract_field_name
2424
from ..utils import _find_field_name
25+
from ..utils import _get_path_parts
2526
from ..utils import _validate_scim_path_syntax
2627
from .error import Error
2728
from .message import Message
@@ -346,7 +347,7 @@ def _set_value_at_path(
346347
target.__dict__.update(updated_target.__dict__)
347348
return True
348349

349-
path_parts = attr_path.split(".")
350+
path_parts = _get_path_parts(attr_path)
350351
if len(path_parts) == 1:
351352
return cls._set_simple_attribute(target, path_parts[0], value, is_add)
352353

@@ -471,7 +472,7 @@ def _remove_value_at_path(cls, resource: Resource[Any], path: str) -> bool:
471472
if not attr_path or not target:
472473
raise ValueError(Error.make_invalid_path_error().detail)
473474

474-
parent_attr, *path_parts = attr_path.split(".")
475+
parent_attr, *path_parts = _get_path_parts(attr_path)
475476
field_name = _find_field_name(type(target), parent_attr)
476477
if not field_name:
477478
raise ValueError(Error.make_no_target_error().detail)

scim2_models/urn.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Union
55

66
from .base import BaseModel
7+
from .utils import _get_path_parts
78
from .utils import _normalize_attribute_name
89

910
if TYPE_CHECKING:
@@ -52,7 +53,7 @@ def _normalize_path(model: Optional[type["BaseModel"]], path: str) -> tuple[str,
5253

5354
def _validate_model_attribute(model: type["BaseModel"], attribute_base: str) -> None:
5455
"""Validate that an attribute name or a sub-attribute path exist for a given model."""
55-
attribute_name, *sub_attribute_blocks = attribute_base.split(".")
56+
attribute_name, *sub_attribute_blocks = _get_path_parts(attribute_base)
5657
sub_attribute_base = ".".join(sub_attribute_blocks)
5758

5859
aliases = {field.validation_alias for field in model.model_fields.values()}

scim2_models/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,7 @@ def _find_field_name(model_class: type["BaseModel"], attr_name: str) -> Optional
195195
return field_key
196196

197197
return None
198+
199+
200+
def _get_path_parts(path: str) -> list[str]:
201+
return path.split(".")

0 commit comments

Comments
 (0)