Commit 571ff2bb authored by Nicolas Chauvat's avatar Nicolas Chauvat
Browse files

feat: make serialization work with multiple inheritance

It is not clear wether yams supports multiple inheritance or not
but in some cases it seems to work, so fix serialize to be
able to use it.
parent 3454508d7828
......@@ -32,6 +32,40 @@ class MyTests(testlib.TestCase):
),
)
def test_yams_serialize_multiple_inheritance(self):
# declaration order is important, we want to test ordered_nodes()
# so add entity Ent1 before Base0 and Base1
s = schema.Schema("Unknown")
register_base_types(s)
e1 = EntityType("Ent1")
e1.specialized_type = "Base0, Base1"
s.add_entity_type(e1)
b0 = EntityType("Base0")
s.add_entity_type(b0)
b1 = EntityType("Base1")
s.add_entity_type(b1)
s.add_relation_type(RelationType("attr1"))
s.add_relation_def(RelationDefinition("Ent1", "attr1", "String"))
out = serialize.serialize_to_python(s)
self.assertMultiLineEqual(
out,
"\n".join(
[
"from yams.buildobjs import *",
"",
"class Base1(EntityType):",
" pass",
"",
"class Base0(EntityType):",
" pass",
"",
"class Ent1(Base0, Base1):",
" attr1 = String()",
"\n",
]
),
)
if __name__ == "__main__":
testlib.unittest_main()
......@@ -14,7 +14,16 @@ def serialize_to_python(schema: yams_types.Schema) -> str:
graph: Dict = {}
for entity in schema.entities():
graph.setdefault(entity, [])
targets = graph.setdefault(entity, set())
if entity.final:
continue
base = entity._specialized_type
if base is None:
continue
if isinstance(base, str) and ", " in base:
targets |= set(base.split(", "))
else:
targets.add(base)
for e in reversed(ordered_nodes(graph)):
if not e.final:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment