From 2bfda562f4dfcc52552ca59378737c05d71d1a9b Mon Sep 17 00:00:00 2001 From: Asher Glick Date: Sat, 30 Sep 2023 14:01:51 -0500 Subject: [PATCH] Making the nested proto field drilldowns more generic They had unique logic specifically for `trigger.` but that means that they wont work for any other nested proto in the future. --- xml_converter/generators/code_generator.py | 34 ++++++++++++---------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/xml_converter/generators/code_generator.py b/xml_converter/generators/code_generator.py index 23eff229..87a69075 100644 --- a/xml_converter/generators/code_generator.py +++ b/xml_converter/generators/code_generator.py @@ -365,14 +365,7 @@ def generate_cpp_variable_data( xml_fields.append(lowercase(x, delimiter="")) default_xml_field = fieldval['xml_fields'][0] - # TODO: this should handle more then just `trigger.` variants - if fieldval["protobuf_field"].startswith("trigger"): - proto_drilldown_calls = ".trigger()" - # TODO this would not properly catch trigger.something.ourvariable - protobuf_field = fieldval["protobuf_field"].split('.')[1] - else: - proto_drilldown_calls = "" - protobuf_field = fieldval["protobuf_field"] + proto_drilldown_calls, protobuf_field = split_field_into_drilldown(fieldval["protobuf_field"]) if fieldval.get("uses_file_path", False): args.append("base_dir") @@ -471,14 +464,7 @@ def write_attribute(self, output_directory: str) -> None: metadata[filepath] = self.data[filepath].metadata attribute_name = attribute_name_from_markdown_data(metadata[filepath]['name']) - # TODO this should handle more then just `trigger.` variants - if metadata[filepath]["protobuf_field"].startswith("trigger"): - proto_drilldown_calls = ".trigger()" - # TODO this would not properly catch trigger.something.ourvariable - protobuf_field = metadata[filepath]["protobuf_field"].split('.')[1] - else: - proto_drilldown_calls = "" - protobuf_field = metadata[filepath]["protobuf_field"] + proto_drilldown_calls, protobuf_field = split_field_into_drilldown(metadata[filepath]["protobuf_field"]) if metadata[filepath]['type'] == "MultiflagValue": for flag in metadata[filepath]['flags']: @@ -754,6 +740,22 @@ def generate_auto_docs(self, metadata: Dict[str, SchemaType], content: Dict[str, return template.render(field_rows=field_rows), field_rows +################################################################################ +# split_field_into_drilldown +# +# Splits the field string into a cpp drilldown function call stack and the +# final proto field name. +# EG: +# field: "trigger.range" +# returns: (".trigger()", "range") +################################################################################ +def split_field_into_drilldown(field: str) -> Tuple[str, str]: + components = field.split(".") + proto_drilldown_calls = "".join([".{}()".format(x) for x in components[:-1]]) + protobuf_field = components[-1] + return proto_drilldown_calls, protobuf_field + + ############################################################################ # attribute_name_from_markdown_data #