Skip to content

Commit

Permalink
Remove enum MessageType in ProtoBuf (#484)
Browse files Browse the repository at this point in the history
The ProtoBuf generator introduced an additonal enum called
`MessageType`. It was intended to be used as identification of
the concrete type of message that is used in a proto3 `oneof` construct.
However, when compiling `*.proto` files, some identification mechanism
is automatically introduced by the proto compiler. Thus, this enum is
not required anymore.
  • Loading branch information
TomGneuss authored May 8, 2024
1 parent 51c824e commit 13c671b
Showing 1 changed file with 4 additions and 41 deletions.
45 changes: 4 additions & 41 deletions aas_core_codegen/protobuf/structure/_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def _generate_class(
):
# -> must create a new message (choice object) since "oneof"
# and "repeated" do not go together
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 2};"))
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 1};"))
required_choice_object.append(prop.type_annotation.items.our_type)

# optional lists of our types where our types are abstract/interfaces
Expand All @@ -369,7 +369,7 @@ def _generate_class(
)
):
# -> same as the case before
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 2};"))
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 1};"))
required_choice_object.append(prop.type_annotation.value.items.our_type)

# our types where our types are abstract/interfaces
Expand All @@ -380,20 +380,15 @@ def _generate_class(
(intermediate.Interface, intermediate.AbstractClass),
):
# -> must use "oneof"
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 2};"))
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 1};"))
required_choice_object.append(prop.type_annotation.our_type)

else:
# just a normal property with type
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 2};"))
prop_blocks.append(Stripped(f"{prop_type} {prop_name} = {i + 1};"))

blocks.append(Stripped("\n".join(prop_blocks)))

# one additional property indicating the concrete class type (in case multiple
# inherit from the same interface) when instantiating a class of this proto,
# the field must be set (ideally in the constructor)
blocks.append(Stripped("MessageType message_type = 1;"))

# endregion

name = proto_naming.class_name(cls.name)
Expand Down Expand Up @@ -433,34 +428,6 @@ def _generate_class(
return Stripped(writer.getvalue()), None, required_choice_object


def _generate_message_type_enum(
symbol_table: VerifiedIntermediateSymbolTable,
) -> Tuple[Stripped, Optional[Error]]:
writer = io.StringIO()

name = "MessageType"

# write enum and its name
writer.write(f"enum {name} {{\n")
# write at least the unspecified enum entry
writer.write(textwrap.indent(f"{name}_UNSPECIFIED = 0;", I))

# generate one enum entry for each concrete class
for i, cls in enumerate(symbol_table.concrete_classes):
writer.write("\n\n")

writer.write(
textwrap.indent(
f"{name}_{proto_naming.enum_literal_name(cls.name)} = {i + 1};",
I,
)
)

writer.write("\n}")

return Stripped(writer.getvalue()), None


def _generate_choice_class(cls: intermediate.AbstractClass) -> str:
msg_header = f"message {proto_naming.class_name(cls.name)} {{\n"
msg_body = f"{I}oneof value {{\n"
Expand Down Expand Up @@ -546,10 +513,6 @@ def generate(
for cls in required_choice_objects:
code_blocks.append(Stripped(_generate_choice_class(cls)))

code, error = _generate_message_type_enum(symbol_table)
if error is None:
code_blocks.append(code)

if len(errors) > 0:
return None, errors

Expand Down

0 comments on commit 13c671b

Please sign in to comment.