Skip to content

Commit

Permalink
simplifying genrator code with sane default value setting
Browse files Browse the repository at this point in the history
  • Loading branch information
AsherGlick committed Oct 9, 2023
1 parent 5b72f62 commit 7e8cd83
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions xml_converter/generators/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ class AttributeVariable:
is_component: bool = False

# A flag to override the type that should be used when writing or reading from a protobuf
ptotobuf_type: Optional[str] = None
ptotobuf_type: str = ""

def __post_init__(self) -> None:
if self.ptotobuf_type == "":
self.ptotobuf_type = self.attribute_type


XML_ATTRIBUTE_PARSER_DEFAULT_ARGUMENTS: Final[List[str]] = ["attribute", "errors"]
Expand Down Expand Up @@ -417,7 +421,7 @@ def generate_cpp_variable_data(
if fieldval['xml_bundled_components'] == []:
write_to_xml = False

ptotobuf_type = None
ptotobuf_type = ""
if "ptotobuf_type" in fieldval:
ptotobuf_type = fieldval["ptotobuf_type"]

Expand Down
8 changes: 4 additions & 4 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
if (this->{{attribute_variable.attribute_flag_name}}) {
{% if (attribute_variable.ptotobuf_type or attribute_variable.attribute_type) in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"] %}
{% if attribute_variable.ptotobuf_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"] %}
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}}));
{% else %}
proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}}));
Expand All @@ -112,11 +112,11 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
void {{cpp_class}}::parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_header}}) {
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
{% if (attribute_variable.ptotobuf_type or attribute_variable.attribute_type) in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"] %}
{% if attribute_variable.ptotobuf_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"] %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.has_{{attribute_variable.protobuf_field}}()) {
{% elif (attribute_variable.ptotobuf_type or attribute_variable.attribute_type) == "String" %}
{% elif attribute_variable.ptotobuf_type == "String" %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != "") {
{% elif (attribute_variable.ptotobuf_type or attribute_variable.attribute_type) == "Enum" %}
{% elif attribute_variable.ptotobuf_type == "Enum" %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
{% else %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
Expand Down

0 comments on commit 7e8cd83

Please sign in to comment.