Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking out proto info inside AttributeVariable #308

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf(ProtoWriterState* state) cons
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
if (this->{{attribute_variable.attribute_flag_name}}) {
{% if not attribute_variable.is_proto_field_scalar %}
std::function<void({{attribute_variable.protobuf_cpp_type}}*)> setter = [&proto_{{cpp_class_header}}]({{attribute_variable.protobuf_cpp_type}}* val) { proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.protobuf_field}}(val); };
{% if not attribute_variable.proto_info.is_proto_field_scalar %}
std::function<void({{attribute_variable.proto_info.protobuf_cpp_type}}*)> setter = [&proto_{{cpp_class_header}}]({{attribute_variable.proto_info.protobuf_cpp_type}}* val) { proto_{{cpp_class_header}}.{{attribute_variable.proto_info.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.proto_info.protobuf_field}}(val); };
{% else %}
std::function<void({{attribute_variable.protobuf_cpp_type}})> setter = [&proto_{{cpp_class_header}}]({{attribute_variable.protobuf_cpp_type}} val) { proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(val); };
std::function<void({{attribute_variable.proto_info.protobuf_cpp_type}})> setter = [&proto_{{cpp_class_header}}]({{attribute_variable.proto_info.protobuf_cpp_type}} val) { proto_{{cpp_class_header}}.{{attribute_variable.proto_info.mutable_proto_drilldown_calls}}set_{{attribute_variable.proto_info.protobuf_field}}(val); };
{% endif %}
{{attribute_variable.serialize_proto_function}}(this->{{attribute_variable.attribute_name}}, state, setter{% for side_effect in attribute_variable.serialize_proto_side_effects %}, &(this->{{side_effect}}){% endfor %});
{{attribute_variable.proto_info.serialize_proto_function}}(this->{{attribute_variable.attribute_name}}, state, setter{% for side_effect in attribute_variable.proto_info.serialize_proto_side_effects %}, &(this->{{side_effect}}){% endfor %});
}
{% endif %}
{% endfor %}
Expand All @@ -120,14 +120,14 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf(ProtoWriterState* state) cons
void {{cpp_class}}::parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_header}}, ProtoReaderState* state) {
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
{% if not attribute_variable.is_proto_field_scalar %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.has_{{attribute_variable.protobuf_field}}()) {
{% elif attribute_variable.protobuf_cpp_type == "std::string" %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != "") {
{% if not attribute_variable.proto_info.is_proto_field_scalar %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_info.proto_drilldown_calls}}.has_{{attribute_variable.proto_info.protobuf_field}}()) {
{% elif attribute_variable.proto_info.protobuf_cpp_type == "std::string" %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_info.proto_drilldown_calls}}.{{attribute_variable.proto_info.protobuf_field}}() != "") {
{% else %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
if (proto_{{cpp_class_header}}{{attribute_variable.proto_info.proto_drilldown_calls}}.{{attribute_variable.proto_info.protobuf_field}}() != 0) {
{% endif %}
{{attribute_variable.deserialize_proto_function}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}(), state, &(this->{{attribute_variable.attribute_name}}), &(this->{{attribute_variable.attribute_flag_name}}){% for side_effect in attribute_variable.deserialize_proto_side_effects %}, &(this->{{side_effect}}){% endfor %});
{{attribute_variable.proto_info.deserialize_proto_function}}(proto_{{cpp_class_header}}{{attribute_variable.proto_info.proto_drilldown_calls}}.{{attribute_variable.proto_info.protobuf_field}}(), state, &(this->{{attribute_variable.attribute_name}}), &(this->{{attribute_variable.attribute_flag_name}}){% for side_effect in attribute_variable.proto_info.deserialize_proto_side_effects %}, &(this->{{side_effect}}){% endfor %});
}
{% endif %}
{% endfor %}
Expand Down
97 changes: 56 additions & 41 deletions xml_converter/generators/generate_cpp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass, field
from typing import Set, List, Dict, Optional, Tuple, TypedDict
from jinja2 import FileSystemLoader, Environment, Template
from jinja2 import FileSystemLoader, Environment, StrictUndefined, Template
from metadata import CustomFunction, MetadataType
from jinja_helpers import UnindentBlocks
from util import lowercase, capitalize, normalize, Document
Expand Down Expand Up @@ -46,21 +46,7 @@ class DocumentationTypeData(TypedDict):


@dataclass
class AttributeVariable:
attribute_name: str
attribute_type: str
cpp_type: str
class_name: str
xml_fields: List[str]

# The function name and additional side effect pointers for xml serialization.
serialize_xml_function: str
serialize_xml_side_effects: List[str]

# The function name and additional side effect pointers for xml deserialization.
deserialize_xml_function: str
deserialize_xml_side_effects: List[str]

class AttributeVariableProtoInfo:
# The function name and additional side effect pointers for proto serialization.
serialize_proto_function: str
serialize_proto_side_effects: List[str]
Expand All @@ -78,12 +64,6 @@ class AttributeVariable:
# Protoc cares if a field is scalar or not in the case of "set" vs "set allocated".
is_proto_field_scalar: bool

default_xml_field: str = ""
side_effects: List[str] = field(default_factory=list)
xml_bundled_components: List[str] = field(default_factory=list)
attribute_flag_name: Optional[str] = ""
write_to_xml: bool = True

# The CPP code to inject into the variable getter to drill down to the
# variable we are looking for. eg ".trigger()" or ".one().two()"
proto_drilldown_calls: str = ""
Expand All @@ -92,6 +72,31 @@ class AttributeVariable:
# variable we are looking for. eg ".mutable_trigger()" or "mutable_one()->mutable_two()->"
mutable_proto_drilldown_calls: str = ""


@dataclass
class AttributeVariable:
attribute_name: str
attribute_type: str
cpp_type: str
class_name: str
xml_fields: List[str]

# The function name and additional side effect pointers for xml serialization.
serialize_xml_function: str
serialize_xml_side_effects: List[str]

# The function name and additional side effect pointers for xml deserialization.
deserialize_xml_function: str
deserialize_xml_side_effects: List[str]

proto_info: AttributeVariableProtoInfo

default_xml_field: str = ""
side_effects: List[str] = field(default_factory=list)
xml_bundled_components: List[str] = field(default_factory=list)
attribute_flag_name: Optional[str] = ""
write_to_xml: bool = True

uses_file_path: bool = False
is_component: bool = False

Expand Down Expand Up @@ -158,7 +163,8 @@ def write_cpp_classes(
extensions=[UnindentBlocks],
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True
lstrip_blocks=True,
undefined=StrictUndefined
)
header_template: Template = env.get_template("class_template.hpp")
code_template: Template = env.get_template("class_template.cpp")
Expand Down Expand Up @@ -333,9 +339,6 @@ def generate_cpp_variable_data(
class_name=component_class_name,
xml_fields=component_xml_fields,
default_xml_field=component_default_xml_field,
protobuf_field=component.protobuf_field,
protobuf_cpp_type=get_proto_field_cpp_type(doc_type, fieldval.protobuf_field + "." + component.protobuf_field),
is_proto_field_scalar=is_proto_field_scalar(doc_type, fieldval.protobuf_field + "." + component.protobuf_field),
attribute_flag_name=attribute_name + "_is_set",
write_to_xml=write_to_xml,
is_component=True,
Expand All @@ -344,10 +347,17 @@ def generate_cpp_variable_data(
serialize_xml_side_effects=[],
deserialize_xml_function="xml_attribute_to_" + component_class_name,
deserialize_xml_side_effects=[],
serialize_proto_function="to_proto_" + component_class_name,
serialize_proto_side_effects=[],
deserialize_proto_function="from_proto_" + component_class_name,
deserialize_proto_side_effects=[],

proto_info=AttributeVariableProtoInfo(
protobuf_field=component.protobuf_field,
protobuf_cpp_type=get_proto_field_cpp_type(doc_type, fieldval.protobuf_field + "." + component.protobuf_field),
is_proto_field_scalar=is_proto_field_scalar(doc_type, fieldval.protobuf_field + "." + component.protobuf_field),
serialize_proto_function="to_proto_" + component_class_name,
serialize_proto_side_effects=[],
deserialize_proto_function="from_proto_" + component_class_name,
deserialize_proto_side_effects=[],
),

)
attribute_variables.append(component_attribute_variable)
# If there aren't any components to bundle, we don't want to render the attribute
Expand Down Expand Up @@ -409,11 +419,6 @@ def generate_cpp_variable_data(
class_name=class_name,
xml_fields=xml_fields,
default_xml_field=default_xml_field,
protobuf_field=protobuf_field,
protobuf_cpp_type=get_proto_field_cpp_type(doc_type, fieldval.protobuf_field),
is_proto_field_scalar=is_proto_field_scalar(doc_type, fieldval.protobuf_field),
proto_drilldown_calls=proto_drilldown_calls,
mutable_proto_drilldown_calls=mutable_proto_drilldown_calls,

write_to_xml=write_to_xml,
attribute_flag_name=attribute_name + "_is_set",
Expand All @@ -423,12 +428,21 @@ def generate_cpp_variable_data(
serialize_xml_side_effects=convert_side_effects_to_variable_names(serialize_xml_function.side_effects),
deserialize_xml_function=deserialize_xml_function.function,
deserialize_xml_side_effects=convert_side_effects_to_variable_names(deserialize_xml_function.side_effects),
serialize_proto_function=serialize_proto_function.function,
serialize_proto_side_effects=convert_side_effects_to_variable_names(serialize_proto_function.side_effects),
deserialize_proto_function=deserialize_proto_function.function,
deserialize_proto_side_effects=convert_side_effects_to_variable_names(deserialize_proto_function.side_effects),

uses_file_path=fieldval.uses_file_path if fieldval.variable_type == "Custom" else False
uses_file_path=fieldval.uses_file_path if fieldval.variable_type == "Custom" else False,

proto_info=AttributeVariableProtoInfo(
protobuf_field=protobuf_field,
protobuf_cpp_type=get_proto_field_cpp_type(doc_type, fieldval.protobuf_field),
is_proto_field_scalar=is_proto_field_scalar(doc_type, fieldval.protobuf_field),
proto_drilldown_calls=proto_drilldown_calls,
mutable_proto_drilldown_calls=mutable_proto_drilldown_calls,
serialize_proto_function=serialize_proto_function.function,
serialize_proto_side_effects=convert_side_effects_to_variable_names(serialize_proto_function.side_effects),
deserialize_proto_function=deserialize_proto_function.function,
deserialize_proto_side_effects=convert_side_effects_to_variable_names(deserialize_proto_function.side_effects),
)

)
attribute_variables.append(attribute_variable)

Expand All @@ -451,7 +465,8 @@ def write_attribute(output_directory: str, data: Dict[str, Document]) -> List[st
extensions=[UnindentBlocks],
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True
lstrip_blocks=True,
undefined=StrictUndefined
)
attribute_names: Dict[str, str] = {}
template: Dict[str, Template] = {
Expand Down
Loading