fixes add_prefix_to_full_access_path, updates tests

The prefix does not contain a "." anymore. This will be added by the
function itself (to be able to distinguish empty full access paths).
This commit is contained in:
Mose Müller 2024-10-01 10:57:13 +02:00
parent 0d5cef1537
commit e52a019d5e
2 changed files with 65 additions and 22 deletions

View File

@ -333,7 +333,7 @@ class Serializer:
cls, obj: ProxyClass, access_path: str = "" cls, obj: ProxyClass, access_path: str = ""
) -> SerializedDataService: ) -> SerializedDataService:
# Get serialization value from the remote service and adapt the full_access_path # Get serialization value from the remote service and adapt the full_access_path
return add_prefix_to_full_access_path(obj.serialize(), access_path + ".") return add_prefix_to_full_access_path(obj.serialize(), access_path)
def dump(obj: Any) -> SerializedObject: def dump(obj: Any) -> SerializedObject:
@ -603,7 +603,7 @@ def add_prefix_to_full_access_path(
Example: Example:
```python ```python
>>> data = { >>> data = {
... "full_access_path": "some_path", ... "full_access_path": "",
... "value": { ... "value": {
... "item": { ... "item": {
... "full_access_path": "some_item_path", ... "full_access_path": "some_item_path",
@ -612,34 +612,30 @@ def add_prefix_to_full_access_path(
... } ... }
... } ... }
... ...
... modified_data = add_prefix_to_full_access_path(data, 'prefix.') ... modified_data = add_prefix_to_full_access_path(data, 'prefix')
{"full_access_path": "prefix.some_path", "value": {"item": {"full_access_path": {"full_access_path": "prefix", "value": {"item": {"full_access_path":
"prefix.some_item_path", "value": 1.0}}} "prefix.some_item_path", "value": 1.0}}}
``` ```
""" """
try: try:
serialized_obj["full_access_path"] = prefix + serialized_obj["full_access_path"] if serialized_obj.get("full_access_path", None) is not None:
serialized_obj["full_access_path"] = (
prefix + "." + serialized_obj["full_access_path"]
if serialized_obj["full_access_path"] != ""
else prefix
)
if isinstance(serialized_obj["value"], list): if isinstance(serialized_obj["value"], list):
for value in serialized_obj["value"]: for value in serialized_obj["value"]:
value["full_access_path"] = prefix + value["full_access_path"] add_prefix_to_full_access_path(cast(SerializedObject, value), prefix)
add_prefix_to_full_access_path(
cast(SerializedObject, value["value"]), prefix
)
elif isinstance(serialized_obj["value"], dict): elif isinstance(serialized_obj["value"], dict):
for value in cast( for value in cast(
dict[str, SerializedObject], serialized_obj["value"] dict[str, SerializedObject], serialized_obj["value"]
).values(): ).values():
value["full_access_path"] = prefix + value["full_access_path"] add_prefix_to_full_access_path(cast(SerializedObject, value), prefix)
add_prefix_to_full_access_path( except (TypeError, KeyError, AttributeError):
cast(SerializedObject, value["value"]), prefix
)
except TypeError:
# passed object is not a dict (cannot use __get__ on it)
pass
except KeyError:
# passed dictionary is not a serialized object # passed dictionary is not a serialized object
pass pass
return serialized_obj return serialized_obj

View File

@ -1086,7 +1086,7 @@ def test_generate_serialized_data_paths(obj: Any, expected: list[str]) -> None:
} }
}, },
}, },
"prefix.", "prefix",
{ {
"full_access_path": "prefix.new_attr", "full_access_path": "prefix.new_attr",
"value": { "value": {
@ -1107,7 +1107,7 @@ def test_generate_serialized_data_paths(obj: Any, expected: list[str]) -> None:
} }
], ],
}, },
"prefix.", "prefix",
{ {
"full_access_path": "prefix.new_attr", "full_access_path": "prefix.new_attr",
"value": [ "value": [
@ -1128,7 +1128,7 @@ def test_generate_serialized_data_paths(obj: Any, expected: list[str]) -> None:
} }
}, },
}, },
"prefix.", "prefix",
{ {
"full_access_path": "prefix.new_attr", "full_access_path": "prefix.new_attr",
"value": { "value": {
@ -1144,7 +1144,7 @@ def test_generate_serialized_data_paths(obj: Any, expected: list[str]) -> None:
"full_access_path": "new_attr", "full_access_path": "new_attr",
"value": {"magnitude": 10, "unit": "meter"}, "value": {"magnitude": 10, "unit": "meter"},
}, },
"prefix.", "prefix",
{ {
"full_access_path": "prefix.new_attr", "full_access_path": "prefix.new_attr",
"value": {"magnitude": 10, "unit": "meter"}, "value": {"magnitude": 10, "unit": "meter"},
@ -1160,7 +1160,7 @@ def test_generate_serialized_data_paths(obj: Any, expected: list[str]) -> None:
} }
], ],
}, },
"prefix.", "prefix",
{ {
"full_access_path": "prefix.quantity_list", "full_access_path": "prefix.quantity_list",
"value": [ "value": [
@ -1171,6 +1171,53 @@ def test_generate_serialized_data_paths(obj: Any, expected: list[str]) -> None:
], ],
}, },
), ),
(
{
"full_access_path": "",
"value": {
"dict_attr": {
"type": "dict",
"full_access_path": "dict_attr",
"value": {
"foo": {
"full_access_path": 'dict_attr["foo"]',
"type": "dict",
"value": {
"some_int": {
"full_access_path": 'dict_attr["foo"].some_int',
"type": "int",
"value": 1,
},
},
},
},
}
},
},
"prefix",
{
"full_access_path": "prefix",
"value": {
"dict_attr": {
"type": "dict",
"full_access_path": "prefix.dict_attr",
"value": {
"foo": {
"full_access_path": 'prefix.dict_attr["foo"]',
"type": "dict",
"value": {
"some_int": {
"full_access_path": 'prefix.dict_attr["foo"].some_int',
"type": "int",
"value": 1,
},
},
},
},
}
},
},
),
], ],
) )
def test_add_prefix_to_full_access_path( def test_add_prefix_to_full_access_path(