Skip to content

Commit

Permalink
Use _choice suffix consistently in Protobuf (#534)
Browse files Browse the repository at this point in the history
In #531 the `_choice` suffix has been introduced for concrete types with
concrete classes as subtypes. Since Protobuf does not support 
inheritance, this was necessary so that we can distinguish between
the concrete class and a choice (union) class with quasi-polymorphism. 
For the sake of consistency, this suffix shall be used for all types 
that have concrete descendants, *i.e.* for all the choice (union) 
classes, including the abstract classes.
  • Loading branch information
TomGneuss authored Oct 28, 2024
1 parent 3cacd0b commit 39a2259
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 43 deletions.
14 changes: 6 additions & 8 deletions aas_core_codegen/protobuf/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,12 @@ def generate_type(type_annotation: intermediate.TypeAnnotationUnion) -> Stripped

elif isinstance(our_type, intermediate.Class):
message_name = proto_naming.class_name(our_type.name)
if (
isinstance(our_type, intermediate.ConcreteClass)
and len(our_type.concrete_descendants) > 0
):
# NOTE (mristin):
# We have to add the suffix ``_choice`` since this field points
# to one of the concrete descendants of the class as well as
# the concrete class itself.
if len(our_type.concrete_descendants) > 0:
# NOTE (TomGneuss):
# We add the suffix "_choice" to the type name because this type
# (either abstract or concrete) has concrete subtypes.
# Thus, a choice-object (with that suffix) will be generated and must
# be used here.
message_name = Identifier(message_name + "_choice")

return Stripped(message_name)
Expand Down
15 changes: 1 addition & 14 deletions aas_core_codegen/protobuf/structure/_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,7 @@ def _generate_choice_class(cls: intermediate.ClassUnion) -> Stripped:
subtype_name = proto_naming.property_name(subtype.name)
fields.append(Stripped(f"{subtype_type} {subtype_name} = {j + 1};"))

if isinstance(cls, intermediate.AbstractClass):
message_name = proto_naming.class_name(cls.name)
elif isinstance(cls, intermediate.ConcreteClass):
# NOTE (mristin):
# We have to append the ``_choice`` suffix for concrete classes with descendants
# to distinguish between the concrete classes and one-of choice classes.
# Protocol Buffers do not support inheritance, so we have to work around that
# circumstance.

message_name = Identifier(proto_naming.class_name(cls.name) + "_choice")

else:
assert_never(cls)
raise AssertionError("Unexpected execution path")
message_name = Identifier(proto_naming.class_name(cls.name) + "_choice")

fields_joined = "\n".join(fields)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ message Submodel {
/// <summary>
/// A submodel consists of zero or more submodel elements.
/// </summary>
repeated SubmodelElement submodel_elements = 13;
repeated SubmodelElement_choice submodel_elements = 13;
}

/// <summary>
Expand Down Expand Up @@ -1028,7 +1028,7 @@ message SubmodelElementList {
/// <remarks>
/// The list is ordered.
/// </remarks>
repeated SubmodelElement value = 14;
repeated SubmodelElement_choice value = 14;
}

/// <summary>
Expand Down Expand Up @@ -1135,7 +1135,7 @@ message SubmodelElementCollection {
/// <summary>
/// Submodel element contained in the collection.
/// </summary>
repeated SubmodelElement value = 10;
repeated SubmodelElement_choice value = 10;
}

/// <summary>
Expand Down Expand Up @@ -1992,7 +1992,7 @@ message AnnotatedRelationshipElement {
/// A data element that represents an annotation that holds for the relationship
/// between the two elements
/// </summary>
repeated DataElement annotations = 12;
repeated DataElement_choice annotations = 12;
}

/// <summary>
Expand Down Expand Up @@ -2112,7 +2112,7 @@ message Entity {
/// Describes statements applicable to the entity by a set of submodel elements,
/// typically with a qualified value.
/// </summary>
repeated SubmodelElement statements = 10;
repeated SubmodelElement_choice statements = 10;

/// <summary>
/// Describes whether the entity is a co-managed entity or a self-managed entity.
Expand Down Expand Up @@ -2590,7 +2590,7 @@ message OperationVariable {
/// <summary>
/// Describes an argument or result of an operation via a submodel element
/// </summary>
SubmodelElement value = 1;
SubmodelElement_choice value = 1;
}

/// <summary>
Expand Down Expand Up @@ -3284,7 +3284,7 @@ message EmbeddedDataSpecification {
/// <summary>
/// Actual content of the data specification
/// </summary>
DataSpecificationContent data_specification_content = 1;
DataSpecificationContent_choice data_specification_content = 1;

/// <summary>
/// Reference to the data specification
Expand Down Expand Up @@ -3738,7 +3738,7 @@ message DataSpecificationIec61360 {
optional LevelType level_type = 12;
}

message HasSemantics {
message HasSemantics_choice {
oneof value {
RelationshipElement relationship_element = 1;
AnnotatedRelationshipElement annotated_relationship_element = 2;
Expand All @@ -3761,7 +3761,7 @@ message HasSemantics {
}
}

message HasExtensions {
message HasExtensions_choice {
oneof value {
RelationshipElement relationship_element = 1;
AnnotatedRelationshipElement annotated_relationship_element = 2;
Expand All @@ -3783,7 +3783,7 @@ message HasExtensions {
}
}

message Referable {
message Referable_choice {
oneof value {
RelationshipElement relationship_element = 1;
AnnotatedRelationshipElement annotated_relationship_element = 2;
Expand All @@ -3805,21 +3805,21 @@ message Referable {
}
}

message Identifiable {
message Identifiable_choice {
oneof value {
AssetAdministrationShell asset_administration_shell = 1;
ConceptDescription concept_description = 2;
Submodel submodel = 3;
}
}

message HasKind {
message HasKind_choice {
oneof value {
Submodel submodel = 1;
}
}

message HasDataSpecification {
message HasDataSpecification_choice {
oneof value {
AdministrativeInformation administrative_information = 1;
RelationshipElement relationship_element = 2;
Expand All @@ -3842,7 +3842,7 @@ message HasDataSpecification {
}
}

message Qualifiable {
message Qualifiable_choice {
oneof value {
RelationshipElement relationship_element = 1;
AnnotatedRelationshipElement annotated_relationship_element = 2;
Expand All @@ -3862,7 +3862,7 @@ message Qualifiable {
}
}

message SubmodelElement {
message SubmodelElement_choice {
oneof value {
RelationshipElement relationship_element = 1;
AnnotatedRelationshipElement annotated_relationship_element = 2;
Expand All @@ -3888,7 +3888,7 @@ message RelationshipElement_choice {
}
}

message DataElement {
message DataElement_choice {
oneof value {
Blob blob = 1;
File file = 2;
Expand All @@ -3899,13 +3899,13 @@ message DataElement {
}
}

message EventElement {
message EventElement_choice {
oneof value {
BasicEventElement basic_event_element = 1;
}
}

message AbstractLangString {
message AbstractLangString_choice {
oneof value {
LangStringDefinitionTypeIec61360 lang_string_definition_type_iec_61360 = 1;
LangStringNameType lang_string_name_type = 2;
Expand All @@ -3915,7 +3915,7 @@ message AbstractLangString {
}
}

message DataSpecificationContent {
message DataSpecificationContent_choice {
oneof value {
DataSpecificationIec61360 data_specification_iec_61360 = 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ message AnotherConcrete {
}

message Container {
SomethingAbstract something_abstract = 1;
SomethingAbstract_choice something_abstract = 1;
}

message SomethingAbstract {
message SomethingAbstract_choice {
oneof value {
AnotherConcrete another_concrete = 1;
SomethingConcrete something_concrete = 2;
Expand Down

0 comments on commit 39a2259

Please sign in to comment.