Skip to content

Commit

Permalink
Properly support dicts during conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Frydrych <[email protected]>
  • Loading branch information
fholger committed Nov 17, 2023
1 parent 9bfc89a commit 7fce562
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dev/gen_model_to_rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@
prop_node = URIRef("{prop_id}")
graph.add((node, prop_node, {prop_conversion_code}))"""

PROP_DICT_CONVERTER_BODY = """
for key, value in obj.{prop_name}.items():
dict_node = BNode()
graph.add((dict_node, RDF.type, URIRef("https://spdx.org/rdf/v3/Core/DictionaryEntry")))
key_node = URIRef("https://spdx.org/rdf/v3/Core/key")
graph.add((dict_node, key_node, Literal(key, datatype="http://www.w3.org/2001/XMLSchema#string")))
if value is not None:
value_node = URIRef("https://spdx.org/rdf/v3/Core/value")
graph.add((dict_node, value_node, Literal(value, datatype="http://www.w3.org/2001/XMLSchema#string")))
prop_node = URIRef("{prop_id}")
graph.add((node, prop_node, dict_node))"""

VOCAB_CONVERTER_FUNC_BODY = """def {type_name}_to_rdf(obj, graph: Graph) -> Identifier:
from .converter import enum_value_to_str
name = enum_value_to_str(obj)
Expand Down Expand Up @@ -257,7 +269,12 @@ def handle_class(self, output_file: IO[str], clazz: dict, namespace_name: str, m
_, _, prop_name = full_prop_name.partition("/")
is_list = prop.get("maxCount") != "1"
prop_conversion_code = self.prop_conversion_code(prop["type"], namespace_name, model)
if is_list:
if "DictionaryEntry" in prop["type"]:
prop_code += PROP_DICT_CONVERTER_BODY.format(
prop_name=prop_name_to_python(prop_name),
prop_id=self.prop_name_to_id[full_prop_name],
)
elif is_list:
prop_code += PROP_LIST_CONVERTER_BODY.format(
prop_name=prop_name_to_python(prop_name),
prop_id=self.prop_name_to_id[full_prop_name],
Expand Down

0 comments on commit 7fce562

Please sign in to comment.