MCPcopy Create free account
hub / github.com/apache/fory / generate_union_macros

Method generate_union_macros

compiler/fory_compiler/generators/cpp.py:1095–1146  ·  view source on GitHub ↗

Generate FORY_UNION metadata macros for a union.

(
        self,
        union: Union,
        parent_stack: List[Message],
    )

Source from the content-addressed store, hash-verified

1093 return lines
1094
1095 def generate_union_macros(
1096 self,
1097 union: Union,
1098 parent_stack: List[Message],
1099 ) -> List[str]:
1100 """Generate FORY_UNION metadata macros for a union."""
1101 if not union.fields:
1102 return []
1103
1104 lines: List[str] = []
1105 if len(union.fields) <= 16:
1106 union_type = self.get_namespaced_type_name(union.name, parent_stack)
1107 lines.append(f"FORY_UNION({union_type},")
1108 for index, field in enumerate(union.fields):
1109 case_type = self.generate_namespaced_type(
1110 field.field_type,
1111 False,
1112 field.ref,
1113 field.element_optional,
1114 field.element_ref,
1115 False,
1116 False,
1117 parent_stack,
1118 )
1119 case_ctor = self.to_snake_case(field.name)
1120 meta = self.get_union_field_meta(field)
1121 suffix = "," if index + 1 < len(union.fields) else ""
1122 lines.append(f" ({case_ctor}, {case_type}, {meta}){suffix}")
1123 lines.append(");")
1124 return lines
1125
1126 union_type = self.get_namespaced_type_name(union.name, parent_stack)
1127 case_ids = ", ".join(str(field.number) for field in union.fields)
1128 lines.append(f"FORY_UNION_IDS({union_type}, {case_ids});")
1129 for field in union.fields:
1130 case_type = self.generate_namespaced_type(
1131 field.field_type,
1132 False,
1133 field.ref,
1134 field.element_optional,
1135 field.element_ref,
1136 False,
1137 False,
1138 parent_stack,
1139 )
1140 case_ctor = self.to_snake_case(field.name)
1141 meta = self.get_union_field_meta(field)
1142 lines.append(
1143 f"FORY_UNION_CASE({union_type}, {field.number}, {case_type}, {union_type}::{case_ctor}, {meta});"
1144 )
1145
1146 return lines
1147
1148 def get_union_case_type(self, field: Field, parent_stack: List[Message]) -> str:
1149 """Return the C++ type for a union case."""

Callers 2

generate_headerMethod · 0.95

Calls 5

get_union_field_metaMethod · 0.95
to_snake_caseMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected