diff --git a/xml_converter/generators/code_generator.py b/xml_converter/generators/code_generator.py index c51eeeff..f5edbf98 100644 --- a/xml_converter/generators/code_generator.py +++ b/xml_converter/generators/code_generator.py @@ -1,7 +1,7 @@ from jsonschema import validate # type:ignore from jsonschema.exceptions import ValidationError # type:ignore import frontmatter # type:ignore -from typing import Any, Dict, List, Tuple, Set, Optional, Final +from typing import Any, Dict, List, Tuple, Set, Optional, Final, TypedDict import os import markdown from dataclasses import dataclass, field @@ -104,17 +104,20 @@ class FieldRow: description: str -# TODO: Eventually replace all references to `doc_type_to_cpp_type` with -# references to `documentation_type_data` because they contain the same data -doc_type_to_cpp_type: Dict[str, str] = { - "Fixed32": "int", - "Int32": "int", - "Boolean": "bool", - "Float32": "float", - "String": "std::string", -} +################################################################################ +# DocumentationTypeData +# +# A type definition to indicate what information should be included in the +# documentation_type_data variable. +################################################################################ +class DocumentationTypeData(TypedDict): + class_name: str + cpp_type: str + -documentation_type_data = { +# A map between the documentation types, and useful class name info related to +# that type. +documentation_type_data: Dict[str, DocumentationTypeData] = { "Fixed32": { "class_name": "int", "cpp_type": "int", @@ -391,7 +394,7 @@ def generate_cpp_variable_data( component_attribute_variable = AttributeVariable( attribute_name=attribute_name + "." + component_name, attribute_type="CompoundValue", - cpp_type=doc_type_to_cpp_type[component['type']], + cpp_type=documentation_type_data[component['type']]["cpp_type"], class_name=component_class_name, xml_fields=component_xml_fields, default_xml_field=component_default_xml_field, @@ -490,7 +493,7 @@ def write_attribute(self, output_directory: str) -> None: elif metadata[filepath]['type'] == "CompoundValue": for component in metadata[filepath]['components']: xml_fields = [] - if component['type'] not in doc_type_to_cpp_type: + if component['type'] not in documentation_type_data: raise ValueError("Unexpected type for component. Look at markdown file {attribute_name}".format( attribute_name=attribute_name )) @@ -502,7 +505,7 @@ def write_attribute(self, output_directory: str) -> None: attribute_variable = AttributeVariable( attribute_name=component_attribute_name, attribute_type=metadata[filepath]['type'], - cpp_type=doc_type_to_cpp_type[component['type']], + cpp_type=documentation_type_data[component['type']]["cpp_type"], class_name=attribute_name, xml_fields=xml_fields, protobuf_field=component["protobuf_field"],