Skip to content
Snippets Groups Projects
Commit 2597fb38 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

ENH: initial implementaiton of deserialization done

parent 5469ff58
No related branches found
No related tags found
2 merge requests!57RELEASE 0.7.3,!52F refactor high level api
Pipeline #20048 canceled
...@@ -605,7 +605,51 @@ class CaosDBPythonEntity(object): ...@@ -605,7 +605,51 @@ class CaosDBPythonEntity(object):
""" """
Deserialize a yaml representation of an entity in high level API form. Deserialize a yaml representation of an entity in high level API form.
""" """
pass
entity = high_level_type_for_role(serialization["role"])()
for parent in serialization["parents"]:
if "unresolved" in parent:
entity.add_parent(CaosDBPythonUnresolvedParent(
id=parent["id"], name=parent["name"]))
else:
raise NotImplementedError()
for baseprop in ("name", "id", "description", "version"):
if baseprop in serialization:
entity.__setattr__(baseprop, serialization[baseprop])
if type(entity) == CaosDBPythonFile:
entity.file = serialization["file"]
entity.path = serialization["path"]
for p in serialization["properties"]:
# The property needs to be set first:
prop = serialization["properties"][p]
if isinstance(prop, dict):
if "unresolved" in prop:
entity.__setattr__(p, CaosDBPythonUnresolvedReference(
id=prop["id"]))
else:
entity.__setattr__(p,
entity.deserialize(prop))
else:
entity.__setattr__(p, prop)
# if there is no metadata in the yaml file just initialize an empty metadata object
if "metadata" in serialization and p in serialization["metadata"]:
metadata = serialization["metadata"][p]
propmeta = entity.get_property_metadata(p)
for f in fields(propmeta):
if f.name in metadata:
propmeta.__setattr__(f.name, metadata[f.name])
else:
raise NotImplementedError()
return entity
def serialize(self, without_metadata: bool = False): def serialize(self, without_metadata: bool = False):
......
...@@ -534,3 +534,36 @@ def test_type_conversion(): ...@@ -534,3 +534,36 @@ def test_type_conversion():
with pytest.raises(RuntimeError, match="Incompatible type."): with pytest.raises(RuntimeError, match="Incompatible type."):
high_level_type_for_standard_type("ajsdkfjasfkj") high_level_type_for_standard_type("ajsdkfjasfkj")
def test_deserialization():
r = db.Record(id=17, name="test")
r.add_parent("bla")
r.add_property(name="a", value=42)
r.add_property(name="b", value="test")
obj = convert_to_python_object(r)
serial = obj.serialize()
obj_des = CaosDBPythonEntity.deserialize(serial)
assert obj_des.name == "test"
assert obj_des.id == 17
assert obj_des.has_parent(CaosDBPythonUnresolvedParent(name="bla"))
print(obj)
print(obj_des)
assert obj.serialize() == obj_des.serialize()
f = db.File()
f.file = "bla.test"
f.path = "/test/n/bla.test"
obj = convert_to_python_object(f)
serial = obj.serialize()
obj_des = CaosDBPythonEntity.deserialize(serial)
assert obj_des.file == "bla.test"
assert obj_des.path == "/test/n/bla.test"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment