diff --git a/README.rst b/README.rst index 77ccd28b..cdc8063d 100644 --- a/README.rst +++ b/README.rst @@ -139,7 +139,7 @@ Call the generator with the appropriate target: usage: aas-core-codegen [-h] --model_path MODEL_PATH --snippets_dir SNIPPETS_DIR --output_dir OUTPUT_DIR --target - {csharp,cpp,golang,java,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context,protobuf} + {csharp,cpp,golang,java,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context,protobuf,python_protobuf} [--version] Generate implementations and schemas based on an AAS meta-model. @@ -153,7 +153,7 @@ Call the generator with the appropriate target: specific code snippets --output_dir OUTPUT_DIR path to the generated code - --target {csharp,cpp,golang,java,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context,protobuf} + --target {csharp,cpp,golang,java,jsonschema,python,typescript,rdf_shacl,xsd,jsonld_context,protobuf,python_protobuf} target language or schema --version show the current version and exit diff --git a/aas_core_codegen/main.py b/aas_core_codegen/main.py index 73117142..b945592c 100644 --- a/aas_core_codegen/main.py +++ b/aas_core_codegen/main.py @@ -18,6 +18,7 @@ import aas_core_codegen.xsd.main as xsd_main import aas_core_codegen.jsonld.main as jsonld_main import aas_core_codegen.protobuf.main as protobuf_main +import aas_core_codegen.python_protobuf.main as python_protobuf_main from aas_core_codegen import run, specific_implementations from aas_core_codegen.common import LinenoColumner, assert_never @@ -38,6 +39,7 @@ class Target(enum.Enum): XSD = "xsd" JSONLD_CONTEXT = "jsonld_context" PROTOBUF = "protobuf" + PYTHON_PROTOBUF = "python_protobuf" class Parameters: @@ -169,6 +171,9 @@ def execute(params: Parameters, stdout: TextIO, stderr: TextIO) -> int: elif params.target is Target.PROTOBUF: return protobuf_main.execute(run_context, stdout=stdout, stderr=stderr) + elif params.target is Target.PYTHON_PROTOBUF: + return python_protobuf_main.execute(run_context, stdout=stdout, stderr=stderr) + else: assert_never(params.target) diff --git a/aas_core_codegen/python_protobuf/__init__.py b/aas_core_codegen/python_protobuf/__init__.py new file mode 100644 index 00000000..a3a4ab56 --- /dev/null +++ b/aas_core_codegen/python_protobuf/__init__.py @@ -0,0 +1 @@ +"""Generate the code for conversion from and to Protocol Buffers.""" diff --git a/aas_core_codegen/python_protobuf/main.py b/aas_core_codegen/python_protobuf/main.py new file mode 100644 index 00000000..9e5c3aec --- /dev/null +++ b/aas_core_codegen/python_protobuf/main.py @@ -0,0 +1,1424 @@ +"""Generate the code for conversion from and to Protocol Buffers.""" + +import io +import os +import pathlib +from typing import Tuple, Optional, List, TextIO + +from icontract import ensure, require + +import aas_core_codegen.python.common as python_common +import aas_core_codegen.python.description as python_description +import aas_core_codegen.python.naming as python_naming +import aas_core_codegen.python_protobuf +from aas_core_codegen import intermediate, run, specific_implementations +from aas_core_codegen.common import ( + Error, + Stripped, + assert_never, + Identifier, + indent_but_first_line, +) +from aas_core_codegen.python.common import INDENT as I, INDENT2 as II, INDENT3 as III +from aas_core_codegen.python_protobuf import naming as python_protobuf_naming + +assert aas_core_codegen.python_protobuf.__doc__ == __doc__ + + +# region From-protobuf + + +def _generate_from_pb_for_enum( + enum: intermediate.Enumeration, aas_pb_module: python_common.QualifiedModuleName +) -> List[Stripped]: + """ + Generate the code to convert an enum back from a protocol buffer. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + literal_to_literal_items = [] # type: List[str] + + py_enum_name = python_naming.enum_name(enum.name) + pb_enum_name = python_protobuf_naming.enum_name(enum.name) + + for literal in enum.literals: + py_literal_name = python_naming.enum_literal_name(literal.name) + pb_constant_name = python_protobuf_naming.enum_literal_constant_name( + enumeration_name=enum.name, literal_name=literal.name + ) + + literal_to_literal_items.append( + f"types_pb.{pb_enum_name}.{pb_constant_name}:\n" + f"{I}types.{py_enum_name}.{py_literal_name}" + ) + + literal_to_literal_items_joined = ",\n".join(literal_to_literal_items) + + map_name = python_naming.private_constant_name( + Identifier(f"{enum.name}_from_pb_map") + ) + + func_name = python_naming.function_name(Identifier(f"{enum.name}_from_pb")) + + if len(enum.literals) > 0: + first_literal = enum.literals[0] + first_literal_pb_constant = python_protobuf_naming.enum_literal_constant_name( + enumeration_name=enum.name, literal_name=first_literal.name + ) + first_literal_py_name = python_naming.enum_literal_name(first_literal.name) + + docstring = Stripped( + f"""\ +Parse ``that`` enum back from its Protocol Buffer representation. + +>>> import {aas_pb_module}.types_pb2 as types_pb +>>> from {aas_pb_module}.pbization import {func_name} +>>> {func_name}( +... types_pb.{pb_enum_name}.{first_literal_pb_constant} +... ) +<{py_enum_name}.{first_literal_py_name}: {first_literal.value!r}>""" + ) + else: + docstring = Stripped( + "Parse ``that`` enum back from its Protocol Buffer representation." + ) + + quoted_docstring = python_description.docstring(docstring) + + return [ + Stripped( + f"""\ +# fmt: off +{map_name} = {{ +{I}{indent_but_first_line(literal_to_literal_items_joined, I)} +}} # type: Mapping[types_pb.{pb_enum_name}, types.{py_enum_name}] +# fmt: on""" + ), + Stripped( + f"""\ +def {func_name}( +{I}that: types_pb.{pb_enum_name} +) -> types.{py_enum_name}: +{I}{indent_but_first_line(quoted_docstring, I)} +{I}return {map_name}[that]""" + ), + ] + + +def _determine_function_name_from_pb_for_class( + cls: intermediate.ClassUnion, +) -> Identifier: + """Determine from-pb function to use to parse the Protocol Buffer to a class.""" + if len(cls.concrete_descendants) > 0: + convert_func_name = python_naming.function_name( + Identifier(f"{cls.name}_from_pb_choice") + ) + else: + convert_func_name = python_naming.function_name( + Identifier(f"{cls.name}_from_pb") + ) + + return convert_func_name + + +def _generate_concrete_from_pb_for_class( + cls: intermediate.ConcreteClass, aas_pb_module: python_common.QualifiedModuleName +) -> Stripped: + """ + Generate the function to convert an instance of ``cls`` back from a Protocol Buffer. + + This function performs explicitly no dispatch and directly converts back properties + from the Protocol Buffer fields. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + constructor_kwargs = [] # type: List[Stripped] + + assert set(cls.properties_by_name.keys()) == set( + cls.constructor.arguments_by_name.keys() + ), "(mristin) We assume that the properties match the constructor arguments." + + for prop in cls.properties: + type_anno = intermediate.beneath_optional(prop.type_annotation) + + is_optional = isinstance( + prop.type_annotation, intermediate.OptionalTypeAnnotation + ) + + pb_prop_name = python_protobuf_naming.property_name(prop.name) + py_prop_name = python_naming.property_name(prop.name) + + primitive_type = intermediate.try_primitive_type(type_anno) + if primitive_type is not None: + if ( + primitive_type is intermediate.PrimitiveType.BOOL + or primitive_type is intermediate.PrimitiveType.INT + or primitive_type is intermediate.PrimitiveType.FLOAT + or primitive_type is intermediate.PrimitiveType.STR + ): + constructor_kwargs.append( + Stripped( + f"""\ +{py_prop_name}=( +{I}that.{pb_prop_name} +{I}if that.HasField({pb_prop_name!r}) +{I}else None +)""" + ) + if is_optional + else Stripped(f"{py_prop_name}=that.{pb_prop_name}") + ) + elif primitive_type is intermediate.PrimitiveType.BYTEARRAY: + constructor_kwargs.append( + Stripped( + f"""\ +{py_prop_name}=( +{I}bytearray(that.{pb_prop_name}) +{I}if that.HasField({pb_prop_name!r}) +{I}else None +)""" + ) + if is_optional + else Stripped(f"{py_prop_name}=bytearray(that.{pb_prop_name})") + ) + else: + assert_never(primitive_type) + + else: + if isinstance(type_anno, intermediate.PrimitiveTypeAnnotation): + raise AssertionError("Expected to be handled before") + + elif isinstance(type_anno, intermediate.OurTypeAnnotation): + if isinstance(type_anno.our_type, intermediate.Enumeration): + convert_func_name = python_naming.function_name( + Identifier(f"{type_anno.our_type.name}_from_pb") + ) + + constructor_kwargs.append( + Stripped( + f"""\ +{py_prop_name}=( +{I}{convert_func_name}( +{II}that.{pb_prop_name} +{I}) +{I}if that.HasField({pb_prop_name!r}) +{I}else None +)""" + ) + if is_optional + else Stripped( + f"""\ +{py_prop_name}={convert_func_name}( +{I}that.{pb_prop_name} +)""" + ) + ) + + elif isinstance(type_anno.our_type, intermediate.ConstrainedPrimitive): + raise AssertionError("Expected to be handled before") + + elif isinstance( + type_anno.our_type, + (intermediate.AbstractClass, intermediate.ConcreteClass), + ): + convert_func_name = _determine_function_name_from_pb_for_class( + cls=type_anno.our_type + ) + + constructor_kwargs.append( + Stripped( + f"""\ +{py_prop_name}=( +{I}{convert_func_name}( +{II}that.{pb_prop_name} +{I}) +{I}if that.HasField({pb_prop_name!r}) +{I}else None +)""" + ) + if is_optional + else Stripped( + f"""\ +{py_prop_name}={convert_func_name}( +{I}that.{pb_prop_name} +)""" + ) + ) + + else: + assert_never(type_anno.our_type) + + elif isinstance(type_anno, intermediate.ListTypeAnnotation): + assert isinstance( + type_anno.items, intermediate.OurTypeAnnotation + ) and isinstance( + type_anno.items.our_type, + (intermediate.AbstractClass, intermediate.ConcreteClass), + ), ( + f"NOTE (mristin): We expect only lists of classes " + f"at the moment, but you specified {type_anno}. " + f"Please contact the developers if you need this feature." + ) + + convert_func_name = _determine_function_name_from_pb_for_class( + cls=type_anno.items.our_type + ) + + # NOTE (mristin): + # Protocol Buffers 3 do not support ``HasField`` on repeated fields, + # see: https://github.com/protocolbuffers/protobuf/issues/10489 + # + # We decide here to interpret empty repeated list containers as None + # if the model property is optional, and as an empty list if the model + # property is required. + constructor_kwargs.append( + Stripped( + f"""\ +{py_prop_name}=( +{I}list(map( +{II}{convert_func_name}, +{II}that.{pb_prop_name} +{I})) +{I}if len(that.{pb_prop_name}) > 0 +{I}else None +)""" + ) + if is_optional + else Stripped( + f"""\ +{py_prop_name}=list(map( +{I}{convert_func_name}, +{I}that.{pb_prop_name} +))""" + ) + ) + else: + assert_never(type_anno) + + func_name = python_naming.function_name(Identifier(f"{cls.name}_from_pb")) + + pb_cls_name = python_protobuf_naming.class_name(cls.name) + py_cls_name = python_naming.class_name(cls.name) + + constructor_kwargs_joined = ",\n".join(constructor_kwargs) + + py_var = python_naming.variable_name(cls.name) + pb_var = python_naming.variable_name(Identifier(cls.name + "_pb")) + + return Stripped( + f'''\ +def {func_name}( +{I}that: types_pb.{pb_cls_name} +) -> types.{py_cls_name}: +{I}""" +{I}Parse ``that`` Protocol Buffer to an instance of a concrete class. + +{I}Example usage: + +{I}.. code-block:: + +{II}import {aas_pb_module}.types_pb2 as types_pb +{II}from {aas_pb_module}.pbization import {func_name} + +{II}some_bytes = b'... serialized types_pb.{pb_cls_name} ...' +{II}{pb_var} = types_pb.{pb_cls_name}() +{II}{pb_var}.FromString( +{III}some_bytes +{II}) + +{II}{py_var} = {func_name}( +{III}{pb_var} +{II}) +{II}# Do something with the {py_var}... + +{I}""" +{I}return types.{py_cls_name}( +{II}{indent_but_first_line(constructor_kwargs_joined, II)} +{I})''' + ) + + +@require( + lambda cls: len(cls.concrete_descendants) > 0, + "Dispatch only possible with concrete descendants.", +) +def _generate_from_pb_for_class_choice( + cls: intermediate.ClassUnion, aas_pb_module: python_common.QualifiedModuleName +) -> List[Stripped]: + """ + Generate the function to parse an instance of ``cls`` where a dispatch is necessary. + + If the class has concrete descendants, the generated function needs to determine + the concrete runtime type of the Protocol Buffer, and dispatch it to + the corresponding concrete from-pb function. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + blocks = [] # type: List[Stripped] + + # region Dispatch map + items = [] # type: List[Stripped] + + concrete_classes = [] # type: List[intermediate.ConcreteClass] + if isinstance(cls, intermediate.ConcreteClass): + concrete_classes.append(cls) + + concrete_classes.extend(cls.concrete_descendants) + + for concrete_cls in concrete_classes: + pb_which_one_of = python_protobuf_naming.property_name(concrete_cls.name) + + concrete_from_pb_name = python_naming.function_name( + Identifier(f"{concrete_cls.name}_from_pb") + ) + + property_name = python_protobuf_naming.property_name(concrete_cls.name) + + items.append( + Stripped( + f"""\ +{pb_which_one_of!r}: +{I}lambda that: {concrete_from_pb_name}( +{II}that.{property_name} +{I})""" + ) + ) + + map_name = python_naming.private_constant_name( + Identifier(f"{cls.name}_from_pb_choice_map") + ) + + items_joined = ",\n".join(items) + blocks.append( + Stripped( + f"""\ +# fmt: off +{map_name} = {{ +{I}{indent_but_first_line(items_joined, I)} +}} +# fmt: on""" + ) + ) + + # endregion Dispatch map + + # region Function + + pb_cls_name = python_protobuf_naming.choice_class_name(cls.name) + + func_name = python_naming.function_name(Identifier(f"{cls.name}_from_pb_choice")) + + py_cls_name = python_naming.class_name(cls.name) + + pb_var = python_naming.variable_name(Identifier(cls.name + "_choice_pb")) + py_var = python_naming.variable_name(cls.name) + + blocks.append( + Stripped( + f'''\ +def {func_name}( +{I}that: types_pb.{pb_cls_name} +) -> types.{py_cls_name}: +{I}""" +{I}Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + +{I}Example usage: + +{I}.. code-block:: + +{II}import {aas_pb_module}.types_pb2 as types_pb +{II}from {aas_pb_module}.pbization import {func_name} + +{II}some_bytes = b'... serialized types_pb.{pb_cls_name} ...' +{II}{pb_var} = types_pb.{pb_cls_name}() +{II}{pb_var}.FromString( +{III}some_bytes +{II}) + +{II}{py_var} = {func_name}( +{III}{pb_var} +{II}) +{II}# Do something with the {py_var}... +{I}""" +{I}get_concrete_instance_from_pb = ( +{II}{map_name}[ +{III}that.WhichOneof("value") +{II}] +{I}) + +{I}result = get_concrete_instance_from_pb(that) # type: ignore + +{I}assert isinstance(result, types.{py_cls_name}) +{I}return result''' + ) + ) + + return blocks + + +def _generate_general_from_pb( + symbol_table: intermediate.SymbolTable, + aas_pb_module: python_common.QualifiedModuleName, +) -> List[Stripped]: + """ + Generate the parsing from a Protocol Buffer based on its runtime type. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + map_items = [] # type: List[Stripped] + for cls in symbol_table.classes: + if len(cls.concrete_descendants) > 0: + pb_cls_name = python_protobuf_naming.choice_class_name(cls.name) + + from_pb_func = python_naming.function_name( + Identifier(f"{cls.name}_from_pb_choice") + ) + else: + pb_cls_name = python_protobuf_naming.class_name(cls.name) + + from_pb_func = python_naming.function_name( + Identifier(f"{cls.name}_from_pb") + ) + + map_items.append( + Stripped( + f"""\ +types_pb.{pb_cls_name}: +{I}{from_pb_func}""" + ) + ) + + map_items_joined = ",\n".join(map_items) + + # NOTE (mristin): + # We had to put this message outside since black formatter struggled with it. This + # is most probably a bug in the black formatter. + key_class_not_found_message = '''\ +f"We do not know how to parse the protocol buffer " +f"of type {that.__class__} into a model instance."''' + + first_cls_name = symbol_table.concrete_classes[0].name + pb_first_cls_name = python_protobuf_naming.class_name(first_cls_name) + + return [ + Stripped( + f"""\ +# fmt: off +_FROM_PB_MAP = {{ +{I}{indent_but_first_line(map_items_joined, I)} +}} +# fmt: on""" + ), + Stripped( + f'''\ +def from_pb( +{I}that: google.protobuf.message.Message +) -> types.Class: +{I}""" +{I}Parse ``that`` Protocol Buffer into a model instance. + +{I}The concrete parsing is determined based on the runtime type of ``that`` +{I}Protocol Buffer. It is assumed that ``that`` is an instance of a message +{I}coming from the Protocol Buffer definitions corresponding to the meta-model. + +{I}Example usage: + +{I}.. code-block:: + +{II}import {aas_pb_module}.types_pb2 as types_pb +{II}from {aas_pb_module}.pbization import from_pb + +{II}some_bytes = b'... serialized types_pb.{pb_first_cls_name} ...' +{II}instance_pb = types_pb.{pb_first_cls_name}() +{II}instance_pb.FromString( +{III}some_bytes +{II}) + +{II}instance = from_pb( +{III}instance_pb +{II}) +{II}# Do something with the instance... +{I}""" +{I}from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + +{I}if from_pb_func is None: +{II}raise ValueError( +{III}{indent_but_first_line(key_class_not_found_message, III)} +{II}) + +{I}result = from_pb_func(that) # type: ignore +{I}assert isinstance(result, types.Class) +{I}return result''' + ), + ] + + +@ensure(lambda result: (result[0] is not None) ^ (result[1] is not None)) +def _generate_from_pb( + symbol_table: intermediate.SymbolTable, + aas_pb_module: python_common.QualifiedModuleName, +) -> Tuple[Optional[List[Stripped]], Optional[List[Error]]]: + """ + Generate all the code for the conversion from protocol buffers. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + blocks = [] # type: List[Stripped] + errors = [] # type: List[Error] + + for our_type in symbol_table.our_types: + if isinstance(our_type, intermediate.Enumeration): + blocks.extend( + _generate_from_pb_for_enum(enum=our_type, aas_pb_module=aas_pb_module) + ) + + elif isinstance(our_type, intermediate.ConstrainedPrimitive): + # NOTE (mristin): + # We do not represent constrained primitives in Protocol Buffer types. + pass + + elif isinstance( + our_type, (intermediate.AbstractClass, intermediate.ConcreteClass) + ): + if len(our_type.concrete_descendants) > 0: + blocks.extend( + _generate_from_pb_for_class_choice( + cls=our_type, aas_pb_module=aas_pb_module + ) + ) + + if isinstance(our_type, intermediate.ConcreteClass): + blocks.append( + _generate_concrete_from_pb_for_class( + cls=our_type, aas_pb_module=aas_pb_module + ) + ) + + else: + assert_never(our_type) + + blocks.extend( + _generate_general_from_pb( + symbol_table=symbol_table, aas_pb_module=aas_pb_module + ) + ) + + if len(errors) > 0: + return None, errors + + return blocks, None + + +# endregion + + +# region To-protobuf + + +def _generate_partial_visitor(symbol_table: intermediate.SymbolTable) -> Stripped: + """Generate the transformer where all methods return assertion errors.""" + methods = [] # type: List[Stripped] + + for cls in symbol_table.concrete_classes: + method_name = python_naming.method_name( + Identifier(f"visit_{cls.name}_with_context") + ) + + cls_name = python_naming.class_name(cls.name) + + methods.append( + Stripped( + f"""\ +def {method_name}( +{I}self, +{I}that: types.{cls_name}, +{I}context: T +) -> None: +{I}raise AssertionError(f"Unexpected visitation of {{that.__class__}}")""" + ) + ) + body = "\n\n".join(methods) + return Stripped( + f'''\ +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): +{I}""" +{I}Visit instances in context with double-dispatch. + +{I}This class is meant to be inherited from. If you do not override a method, +{I}it will raise an exception. This is a partial visitor, meaning that some +{I}visits are unexpected by design. +{I}""" +{I}# pylint: disable=missing-docstring + +{I}{indent_but_first_line(body, I)}''' + ) + + +def _generate_to_pb_for_enum( + enum: intermediate.Enumeration, + aas_module: python_common.QualifiedModuleName, + aas_pb_module: python_common.QualifiedModuleName, +) -> List[Stripped]: + """ + Generate the code to convert an enum to a protocol buffer. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + literal_to_literal_items = [] # type: List[str] + + py_enum_name = python_naming.enum_name(enum.name) + pb_enum_name = python_protobuf_naming.enum_name(enum.name) + + for literal in enum.literals: + py_literal_name = python_naming.enum_literal_name(literal.name) + pb_constant_name = python_protobuf_naming.enum_literal_constant_name( + enumeration_name=enum.name, literal_name=literal.name + ) + + literal_to_literal_items.append( + f"types.{py_enum_name}.{py_literal_name}:\n" + f"{I}types_pb.{pb_enum_name}.{pb_constant_name}" + ) + + literal_to_literal_items_joined = ",\n".join(literal_to_literal_items) + + map_name = python_naming.private_constant_name(Identifier(f"{enum.name}_to_pb_map")) + + func_name = python_naming.function_name(Identifier(f"{enum.name}_to_pb")) + + if len(enum.literals) > 0: + first_literal = enum.literals[0] + first_literal_py_name = python_naming.enum_literal_name(first_literal.name) + + docstring = Stripped( + f"""\ +Convert ``that`` enum to its Protocol Buffer representation. + +>>> from {aas_pb_module}.pbization import {func_name} +>>> import {aas_module}.types as types +>>> {func_name}( +... types.{py_enum_name}.{first_literal_py_name} +... ) +1""" + ) + else: + docstring = Stripped( + "Convert ``that`` enum to its Protocol Buffer representation." + ) + + quoted_docstring = python_description.docstring(docstring) + + return [ + Stripped( + f"""\ +# fmt: off +{map_name} = {{ +{I}{indent_but_first_line(literal_to_literal_items_joined, I)} +}} # type: Mapping[types.{py_enum_name}, types_pb.{pb_enum_name}] +# fmt: on""" + ), + Stripped( + f"""\ +def {func_name}( +{I}that: types.{py_enum_name} +) -> types_pb.{pb_enum_name}: +{I}{indent_but_first_line(quoted_docstring, I)} +{I}return {map_name}[that]""" + ), + ] + + +def _generate_concrete_to_pb_for_class( + cls: intermediate.ConcreteClass, + aas_module: python_common.QualifiedModuleName, + aas_pb_module: python_common.QualifiedModuleName, +) -> Stripped: + """ + Generate the code to convert an instance to a protocol buffer without dispatch. + + There are two situations when this generated function will be called: + + 1) ``cls`` is a concrete class without descendants (no dispatch necessary), and + 2) ``cls`` is a concrete class with descendants. In this case, we will dispatch + to this generated function if the runtime type equals exactly ``cls``. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + set_blocks = [] # type: List[Stripped] + + for prop in cls.properties: + type_anno = intermediate.beneath_optional(prop.type_annotation) + + py_prop_name = python_naming.property_name(prop.name) + pb_prop_name = python_protobuf_naming.property_name(prop.name) + + set_block: Stripped + + primitive_type = intermediate.try_primitive_type(type_anno) + if primitive_type is not None: + if ( + primitive_type is intermediate.PrimitiveType.BOOL + or primitive_type is intermediate.PrimitiveType.INT + or primitive_type is intermediate.PrimitiveType.FLOAT + or primitive_type is intermediate.PrimitiveType.STR + ): + set_block = Stripped(f"target.{pb_prop_name} = that.{py_prop_name}") + + elif primitive_type is intermediate.PrimitiveType.BYTEARRAY: + set_block = Stripped( + f"target.{pb_prop_name} = bytes(that.{py_prop_name})" + ) + + else: + assert_never(primitive_type) + else: + if isinstance(type_anno, intermediate.PrimitiveTypeAnnotation): + raise AssertionError("Expected to be handled before") + + elif isinstance(type_anno, intermediate.OurTypeAnnotation): + if isinstance(type_anno.our_type, intermediate.Enumeration): + to_pb_func_for_enum = python_naming.function_name( + Identifier(f"{type_anno.our_type.name}_to_pb") + ) + + set_block = Stripped( + f"""\ +target.{pb_prop_name} = {to_pb_func_for_enum}( +{I}that.{py_prop_name} +)""" + ) + + elif isinstance(type_anno.our_type, intermediate.ConstrainedPrimitive): + raise AssertionError("Expected to be handled before") + + elif isinstance( + type_anno.our_type, + (intermediate.AbstractClass, intermediate.ConcreteClass), + ): + if len(type_anno.our_type.concrete_descendants) > 0: + to_pb_choice_func = python_naming.function_name( + Identifier(f"{type_anno.our_type.name}_to_pb_choice") + ) + + set_block = Stripped( + f"""\ +{to_pb_choice_func}( +{I}that.{py_prop_name}, +{I}target.{pb_prop_name} +)""" + ) + else: + to_pb_func = python_naming.function_name( + Identifier(f"{type_anno.our_type.name}_to_pb") + ) + + set_block = Stripped( + f"""\ +# We clear so that the field is set even if all the properties are None. +target.{pb_prop_name}.Clear() + +{to_pb_func}( +{I}that.{py_prop_name}, +{I}target.{pb_prop_name} +)""" + ) + + else: + assert_never(type_anno.our_type) + + elif isinstance(type_anno, intermediate.ListTypeAnnotation): + assert isinstance( + type_anno.items, intermediate.OurTypeAnnotation + ) and isinstance( + type_anno.items.our_type, + (intermediate.AbstractClass, intermediate.ConcreteClass), + ), ( + f"NOTE (mristin): We expect only lists of classes " + f"at the moment, but you specified {type_anno}. " + f"Please contact the developers if you need this feature." + ) + + if len(type_anno.items.our_type.concrete_descendants) > 0: + to_pb_func_for_items_cls = python_naming.function_name( + Identifier(f"{type_anno.items.our_type.name}_to_pb_choice") + ) + else: + to_pb_func_for_items_cls = python_naming.function_name( + Identifier(f"{type_anno.items.our_type.name}_to_pb") + ) + + item_var = python_naming.variable_name(Identifier(f"{prop.name}_item")) + set_block = Stripped( + f"""\ +for {item_var} in that.{py_prop_name}: +{I}{item_var}_pb = target.{pb_prop_name}.add() +{I}{to_pb_func_for_items_cls}( +{II}{item_var}, +{II}{item_var}_pb)""" + ) + + else: + assert_never(type_anno) + + if isinstance(prop.type_annotation, intermediate.OptionalTypeAnnotation): + set_block = Stripped( + f"""\ +if that.{py_prop_name} is not None: +{I}{indent_but_first_line(set_block, I)}""" + ) + + set_blocks.append(set_block) + + func_name = python_naming.function_name(Identifier(f"{cls.name}_to_pb")) + + py_cls_name = python_naming.class_name(cls.name) + pb_cls_name = python_protobuf_naming.class_name(cls.name) + + set_blocks_joined = "\n\n".join(set_blocks) + + pb_var = python_naming.variable_name(Identifier(cls.name + "_pb")) + py_var = python_naming.variable_name(cls.name) + + return Stripped( + f'''\ +def {func_name}( +{I}that: types.{py_cls_name}, +{I}target: types_pb.{pb_cls_name} +) -> None: +{I}""" +{I}Set fields in ``target`` based on ``that`` instance. + +{I}Example usage: + +{I}.. code-block:: + +{II}import {aas_module}.types as types + +{II}import {aas_pb_module}.types_pb2 as types_pb +{II}from {aas_pb_module}.pbization import {func_name} + +{II}{py_var} = types.{py_cls_name}( +{III}... # some constructor arguments +{II}) + +{II}{pb_var} = types_pb.{pb_cls_name}() +{II}{func_name}( +{III}{py_var}, +{III}{pb_var} +{II}) + +{II}some_bytes = {pb_var}.SerializeToString() +{II}# Do something with some_bytes. For example, transfer them +{II}# over the wire. +{I}""" +{I}{indent_but_first_line(set_blocks_joined, I)}''' + ) + + +@require( + lambda cls: len(cls.concrete_descendants) > 0, + "Dispatch needs concrete descendants.", +) +def _generate_to_pb_choice( + cls: intermediate.ClassUnion, + aas_module: python_common.QualifiedModuleName, + aas_pb_module: python_common.QualifiedModuleName, +) -> List[Stripped]: + """ + Generate the code to dispatch the serialization to a concrete serializer. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + blocks = [] # type: List[Stripped] + + pb_choice_cls_name = python_protobuf_naming.choice_class_name(cls.name) + + # region Visitor + + methods = [] # type: List[Stripped] + + concrete_classes = [] # type: List[intermediate.ConcreteClass] + if isinstance(cls, intermediate.ConcreteClass): + concrete_classes.append(cls) + + concrete_classes.extend(cls.concrete_descendants) + + for concrete_cls in concrete_classes: + py_concrete_cls_name = python_naming.class_name(concrete_cls.name) + pb_property = python_protobuf_naming.property_name(concrete_cls.name) + + conversion_func = python_naming.function_name( + Identifier(f"{concrete_cls.name}_to_pb") + ) + + method_name = python_naming.method_name( + Identifier(f"visit_{concrete_cls.name}_with_context") + ) + + methods.append( + Stripped( + f'''\ +def {method_name}( +{I}self, +{I}that: types.{py_concrete_cls_name}, +{I}context: types_pb.{pb_choice_cls_name} +) -> None: +{I}""" +{I}Set the fields of ``context.{pb_property}`` +{I}according to ``that`` instance. +{I}""" +{I}{conversion_func}( +{II}that, +{II}context.{pb_property} +{I})''' + ) + ) + + visitor_class_name = python_naming.private_class_name( + Identifier(f"{cls.name}_to_pb_choice") + ) + body = "\n\n".join(methods) + blocks.append( + Stripped( + f'''\ +class {visitor_class_name}( +{I}_PartialVisitorWithContext[ +{II}types_pb.{pb_choice_cls_name} +{I}] +): +{I}"""Set the fields of the corresponding one-of value.""" +{I}{indent_but_first_line(body, I)}''' + ) + ) + + visitor_name = python_naming.private_constant_name( + Identifier(f"{cls.name}_to_pb_choice") + ) + blocks.append(Stripped(f"{visitor_name} = {visitor_class_name}()")) + + # endregion Visitor + + func_name = python_naming.function_name(Identifier(f"{cls.name}_to_pb_choice")) + + py_cls_name = python_naming.class_name(cls.name) + + py_concrete_descendant_cls_name = python_naming.class_name( + cls.concrete_descendants[0].name + ) + + py_var = python_naming.variable_name(cls.name) + pb_var = python_naming.variable_name(Identifier(cls.name + "_choice_pb")) + + blocks.append( + Stripped( + f'''\ +def {func_name}( +{I}that: types.{py_cls_name}, +{I}target: types_pb.{pb_choice_cls_name} +) -> None: +{I}""" +{I}Set the chosen value in ``target`` based on ``that`` instance. + +{I}The chosen value in ``target`` is determined based on the runtime type of ``that`` +{I}instance. All the fields of the value are recursively set according to ``that`` +{I}instance. + +{I}Example usage: + +{I}.. code-block:: + +{II}import {aas_module}.types as types + +{II}import {aas_pb_module}.types_pb2 as types_pb +{II}from {aas_pb_module}.pbization import {func_name} + +{II}{py_var} = types.{py_concrete_descendant_cls_name}( +{III}... # some constructor arguments +{II}) + +{II}{pb_var} = types_pb.{pb_choice_cls_name}() +{II}{func_name}( +{III}{py_var}, +{III}{pb_var} +{II}) + +{II}some_bytes = {pb_var}.SerializeToString() +{II}# Do something with some_bytes. For example, transfer them +{II}# over the wire. + +{I}""" +{I}{visitor_name}.visit_with_context( +{II}that, +{II}target +{I})''' + ) + ) + + return blocks + + +@require( + lambda symbol_table: len(symbol_table.concrete_classes) > 0, + "At least one concrete class in the meta-model expected if you generate " + "a general to-pb function", +) +def _generate_general_to_pb( + symbol_table: intermediate.SymbolTable, + aas_module: python_common.QualifiedModuleName, + aas_pb_module: python_common.QualifiedModuleName, +) -> List[Stripped]: + """ + Generate a dispatch to the concrete to-pb function. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + methods = [] # type: List[Stripped] + + for cls in symbol_table.concrete_classes: + if len(cls.concrete_descendants) > 0: + conversion_func_name = python_naming.function_name( + Identifier(f"{cls.name}_to_pb_choice") + ) + pb_cls_name = python_protobuf_naming.choice_class_name(cls.name) + else: + conversion_func_name = python_naming.function_name( + Identifier(f"{cls.name}_to_pb") + ) + pb_cls_name = python_protobuf_naming.class_name(cls.name) + + method_name = python_naming.function_name(Identifier(f"transform_{cls.name}")) + + py_cls_name = python_naming.class_name(cls.name) + + methods.append( + Stripped( + f'''\ +def {method_name}( +{I}self, +{I}that: types.{py_cls_name} +) -> google.protobuf.message.Message: +{I}""" +{I}Convert ``that`` instance +{I}to a :py:class:`types_pb.{pb_cls_name}`. +{I}""" +{I}result = types_pb.{pb_cls_name}() +{I}{conversion_func_name}(that, result) +{I}return result''' + ) + ) + + body = "\n\n".join(methods) + + first_cls = symbol_table.concrete_classes[0] + py_first_cls_name = python_naming.class_name(first_cls.name) + + return [ + Stripped( + f'''\ +class _ToPbTransformer( +{I}types.AbstractTransformer[google.protobuf.message.Message] +): +{I}""" +{I}Dispatch to-pb conversion to the concrete functions. + +{I}The classes with descendants (i.e., subtypes) are always going to be converted +{I}to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer +{I}class. We made this decision with the compactness of messages in mind. +{I}""" +{I}{indent_but_first_line(body, I)}''' + ), + Stripped("_TO_PB_TRANSFORMER = _ToPbTransformer()"), + Stripped( + f'''\ +def to_pb( +{I}that: types.Class, +) -> google.protobuf.message.Message: +{I}""" +{I}Dispatch to-pb conversion to the concrete functions. + +{I}The classes with descendants (i.e., subtypes) are always going to be converted +{I}to their concrete Protocol Buffer message type instead of the choice (union) type. +{I}We made this decision with the compactness of messages in mind as choice types +{I}would occupy a tiny bit more space. + +{I}Example usage: + +{I}.. code-block:: + +{II}import {aas_module}.types as types + +{II}from {aas_pb_module}.pbization import to_pb + +{II}instance = types.{py_first_cls_name}( +{III}... # some constructor arguments +{II}) + +{II}instance_pb = to_pb( +{III}instance +{II}) + +{II}some_bytes = instance_pb.SerializeToString() +{II}# Do something with some_bytes. For example, transfer them +{II}# over the wire. +{I}""" +{I}return _TO_PB_TRANSFORMER.transform(that)''' + ), + ] + + +@ensure(lambda result: (result[0] is not None) ^ (result[1] is not None)) +def _generate_to_pb( + symbol_table: intermediate.SymbolTable, + aas_module: python_common.QualifiedModuleName, + aas_pb_module: python_common.QualifiedModuleName, +) -> Tuple[Optional[List[Stripped]], Optional[List[Error]]]: + """ + Generate all the code for the conversion to protocol buffers. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + blocks = [ + Stripped('T = TypeVar("T")'), + _generate_partial_visitor(symbol_table=symbol_table), + ] # type: List[Stripped] + errors = [] # type: List[Error] + + for our_type in symbol_table.our_types: + if isinstance(our_type, intermediate.Enumeration): + enum_blocks = _generate_to_pb_for_enum( + enum=our_type, aas_module=aas_module, aas_pb_module=aas_pb_module + ) + blocks.extend(enum_blocks) + elif isinstance(our_type, intermediate.ConstrainedPrimitive): + # NOTE (mristin): + # We do not represent constrained primitives in Python types. + pass + elif isinstance( + our_type, (intermediate.AbstractClass, intermediate.ConcreteClass) + ): + if len(our_type.concrete_descendants) > 0: + blocks.extend( + _generate_to_pb_choice( + cls=our_type, aas_module=aas_module, aas_pb_module=aas_pb_module + ) + ) + + if isinstance(our_type, intermediate.ConcreteClass): + blocks.append( + _generate_concrete_to_pb_for_class( + cls=our_type, aas_module=aas_module, aas_pb_module=aas_pb_module + ) + ) + else: + assert_never(our_type) + + blocks.extend( + _generate_general_to_pb( + symbol_table=symbol_table, + aas_module=aas_module, + aas_pb_module=aas_pb_module, + ) + ) + + if len(errors) > 0: + return None, errors + + return blocks, None + + +# endregion + +_THIS_PATH = pathlib.Path(os.path.realpath(__file__)) + + +@ensure(lambda result: not (result[0] is not None) or (result[0].endswith("\n"))) +@ensure(lambda result: (result[0] is not None) ^ (result[1] is not None)) +def _generate( + symbol_table: intermediate.SymbolTable, + spec_impls: specific_implementations.SpecificImplementations, +) -> Tuple[Optional[str], Optional[List[Error]]]: + """ + Generate the content of the pbization module. + + The ``aas_module`` indicates the fully-qualified name of the base SDK module. + + The ``aas_pb_module`` indicates the fully-qualified name of this base module. + """ + aas_module_key = specific_implementations.ImplementationKey( + "base_qualified_module_name.txt" + ) + + aas_module_text = spec_impls.get(aas_module_key, None) + if aas_module_text is None: + return None, [ + Error( + None, + f"The implementation snippet for the base qualified module name " + f"is missing: {aas_module_key}", + ) + ] + + if not python_common.QUALIFIED_MODULE_NAME_RE.fullmatch(aas_module_text): + return None, [ + Error( + None, + f"The text from the snippet {aas_module_key} " + f"is not a valid qualified module name: {aas_module_text!r}\n", + ) + ] + + aas_module = python_common.QualifiedModuleName(aas_module_text) + + aas_pb_module_key = specific_implementations.ImplementationKey( + "qualified_module_name_for_protobuf_library.txt" + ) + + aas_pb_module_text = spec_impls.get(aas_pb_module_key, None) + if aas_pb_module_text is None: + return None, [ + Error( + None, + f"The implementation snippet for the qualified name of the protobuf " + f"library is missing: {aas_pb_module_key}", + ) + ] + + if not python_common.QUALIFIED_MODULE_NAME_RE.fullmatch(aas_pb_module_text): + return None, [ + Error( + None, + f"The text from the snippet {aas_pb_module_key} " + f"is not a valid qualified module name: {aas_pb_module_text!r}\n", + ) + ] + + aas_pb_module = python_common.QualifiedModuleName(aas_pb_module_text) + + warning = Stripped( + f"""\ +# Automatically generated with {_THIS_PATH.parent.name}/{_THIS_PATH.name}. +# Do NOT edit or append.""" + ) + + blocks = [ + warning, + Stripped('"""Convert instances from and to Protocol Buffers."""'), + Stripped( + f"""\ +from typing import Mapping, TypeVar + +import google.protobuf.message + +from {aas_module} import types +import {aas_pb_module}.types_pb2 as types_pb""", + ), + Stripped("# region From Protocol Buffers"), + ] # type: List[Stripped] + + errors = [] # type: List[Error] + + from_pb_blocks, from_pb_errors = _generate_from_pb( + symbol_table=symbol_table, aas_pb_module=aas_pb_module + ) + if from_pb_errors is not None: + errors.append( + Error( + None, + "Failed to generate one or more from-protobuf conversion", + underlying=errors, + ) + ) + else: + assert from_pb_blocks is not None + blocks.extend(from_pb_blocks) + + blocks.extend( + [ + Stripped("# endregion From Protocol Buffers"), + Stripped("# region To Protocol Buffers"), + ] + ) + + to_pb_blocks, to_pb_errors = _generate_to_pb( + symbol_table=symbol_table, aas_module=aas_module, aas_pb_module=aas_pb_module + ) + if to_pb_errors is not None: + errors.append( + Error( + None, + "Failed to generate one or more to-protobuf conversion", + underlying=errors, + ) + ) + else: + assert to_pb_blocks is not None + blocks.extend(to_pb_blocks) + + blocks.append( + Stripped("# endregion To Protocol Buffers"), + ) + + blocks.append(warning) + + out = io.StringIO() + for i, block in enumerate(blocks): + if i > 0: + out.write("\n\n\n") + + out.write(block) + + out.write("\n") + + return out.getvalue(), None + + +def execute( + context: run.Context, + stdout: TextIO, + stderr: TextIO, +) -> int: + """ + Execute the generation with the given parameters. + + Return the error code, or 0 if no errors. + """ + code, errors = _generate( + symbol_table=context.symbol_table, spec_impls=context.spec_impls + ) + if errors is not None: + run.write_error_report( + message=f"Failed to generate the Python-Protobuf library " + f"based on {context.model_path}", + errors=[context.lineno_columner.error_message(error) for error in errors], + stderr=stderr, + ) + return 1 + + assert code is not None + + pth = context.output_dir / "pbization.py" + try: + pth.write_text(code, encoding="utf-8") + except Exception as exception: + run.write_error_report( + message=f"Failed to write the Python-Protobuf library to {pth}", + errors=[str(exception)], + stderr=stderr, + ) + return 1 + + stdout.write(f"Code generated to: {context.output_dir}\n") + + return 0 diff --git a/aas_core_codegen/python_protobuf/naming.py b/aas_core_codegen/python_protobuf/naming.py new file mode 100644 index 00000000..39b0fe7f --- /dev/null +++ b/aas_core_codegen/python_protobuf/naming.py @@ -0,0 +1,85 @@ +"""Emulate naming logic for our types to Protocol Buffers in Python code.""" + +from aas_core_codegen.common import Identifier +from aas_core_codegen import naming + + +def enum_name(identifier: Identifier) -> Identifier: + """ + Generate a name for an enum based on its meta-model ``identifier``. + + >>> enum_name(Identifier("something")) + 'Something' + + >>> enum_name(Identifier("URL_to_something")) + 'UrlToSomething' + """ + return naming.capitalized_camel_case(identifier) + + +def enum_literal_constant_name( + enumeration_name: Identifier, literal_name: Identifier +) -> Identifier: + """ + Generate a constant identifier for an enum literal in the meta-model. + + >>> enum_literal_constant_name( + ... Identifier("that"), Identifier("literal") + ... ) + 'That_LITERAL' + + >>> enum_literal_constant_name( + ... Identifier("URL_to_something"), Identifier("ABC_shines") + ... ) + 'Urltosomething_ABC_SHINES' + + >>> enum_literal_constant_name( + ... Identifier("Modelling_kind"), Identifier("Template") + ... ) + 'Modellingkind_TEMPLATE' + """ + enum_name_joined = "".join(enumeration_name.split("_")) + + return Identifier(f"{enum_name_joined.capitalize()}_{literal_name.upper()}") + + +def property_name(identifier: Identifier) -> Identifier: + """ + Generate the name of the property in a Protocol Buffer generated class. + + >>> property_name(Identifier("something")) + 'something' + + >>> property_name(Identifier("something_to_URL")) + 'something_to_url' + + >>> property_name(Identifier("URL_to_something")) + 'url_to_something' + """ + return naming.lower_snake_case(identifier) + + +def class_name(identifier: Identifier) -> Identifier: + """ + Generate a name for a class based on its meta-model ``identifier``. + + >>> class_name(Identifier("something")) + 'Something' + + >>> class_name(Identifier("URL_to_something")) + 'UrlToSomething' + """ + return naming.capitalized_camel_case(identifier) + + +def choice_class_name(identifier: Identifier) -> Identifier: + """ + Generate a name for a choice (union) class based on its meta-model ``identifier``. + + >>> choice_class_name(Identifier("something")) + 'Something_choice' + + >>> choice_class_name(Identifier("URL_to_something")) + 'UrlToSomething_choice' + """ + return Identifier(naming.capitalized_camel_case(identifier) + "_choice") diff --git a/continuous_integration/precommit.py b/continuous_integration/precommit.py index b5fbcbb7..c6b01334 100644 --- a/continuous_integration/precommit.py +++ b/continuous_integration/precommit.py @@ -118,7 +118,7 @@ def main() -> int: # We exclude the snippets and the expected output from the formatting as # this causes too many unpredictable results and thus makes the generator # harder to debug. - exclude = ["test_data/python/test_main/aas_core_meta.v3"] + exclude = ["test_data/python", "test_data/python_protobuf"] if overwrite: exit_code = call_and_report( diff --git a/test_data/python_protobuf/test_main/aas_core_meta.v3/expected_output/pbization.py b/test_data/python_protobuf/test_main/aas_core_meta.v3/expected_output/pbization.py new file mode 100644 index 00000000..b18c32cb --- /dev/null +++ b/test_data/python_protobuf/test_main/aas_core_meta.v3/expected_output/pbization.py @@ -0,0 +1,10751 @@ +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. + + +"""Convert instances from and to Protocol Buffers.""" + + +from typing import Mapping, TypeVar + +import google.protobuf.message + +from aas_core3 import types +import aas_core3_protobuf.types_pb2 as types_pb + + +# region From Protocol Buffers + + +# fmt: off +_HAS_SEMANTICS_FROM_PB_CHOICE_MAP = { + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ), + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ), + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'capability': + lambda that: capability_from_pb( + that.capability + ), + 'entity': + lambda that: entity_from_pb( + that.entity + ), + 'extension': + lambda that: extension_from_pb( + that.extension + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'operation': + lambda that: operation_from_pb( + that.operation + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'qualifier': + lambda that: qualifier_from_pb( + that.qualifier + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ), + 'specific_asset_id': + lambda that: specific_asset_id_from_pb( + that.specific_asset_id + ), + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ), + 'submodel_element_collection': + lambda that: submodel_element_collection_from_pb( + that.submodel_element_collection + ), + 'submodel_element_list': + lambda that: submodel_element_list_from_pb( + that.submodel_element_list + ) +} +# fmt: on + + +def has_semantics_from_pb_choice( + that: types_pb.HasSemantics_choice +) -> types.HasSemantics: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_semantics_from_pb_choice + + some_bytes = b'... serialized types_pb.HasSemantics_choice ...' + has_semantics_choice_pb = types_pb.HasSemantics_choice() + has_semantics_choice_pb.FromString( + some_bytes + ) + + has_semantics = has_semantics_from_pb_choice( + has_semantics_choice_pb + ) + # Do something with the has_semantics... + """ + get_concrete_instance_from_pb = ( + _HAS_SEMANTICS_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.HasSemantics) + return result + + +def extension_from_pb( + that: types_pb.Extension +) -> types.Extension: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import extension_from_pb + + some_bytes = b'... serialized types_pb.Extension ...' + extension_pb = types_pb.Extension() + extension_pb.FromString( + some_bytes + ) + + extension = extension_from_pb( + extension_pb + ) + # Do something with the extension... + + """ + return types.Extension( + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + name=that.name, + value_type=( + data_type_def_xsd_from_pb( + that.value_type + ) + if that.HasField('value_type') + else None + ), + value=( + that.value + if that.HasField('value') + else None + ), + refers_to=( + list(map( + reference_from_pb, + that.refers_to + )) + if len(that.refers_to) > 0 + else None + ) + ) + + +# fmt: off +_HAS_EXTENSIONS_FROM_PB_CHOICE_MAP = { + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ), + 'asset_administration_shell': + lambda that: asset_administration_shell_from_pb( + that.asset_administration_shell + ), + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ), + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'capability': + lambda that: capability_from_pb( + that.capability + ), + 'concept_description': + lambda that: concept_description_from_pb( + that.concept_description + ), + 'entity': + lambda that: entity_from_pb( + that.entity + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'operation': + lambda that: operation_from_pb( + that.operation + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ), + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ), + 'submodel_element_collection': + lambda that: submodel_element_collection_from_pb( + that.submodel_element_collection + ), + 'submodel_element_list': + lambda that: submodel_element_list_from_pb( + that.submodel_element_list + ) +} +# fmt: on + + +def has_extensions_from_pb_choice( + that: types_pb.HasExtensions_choice +) -> types.HasExtensions: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_extensions_from_pb_choice + + some_bytes = b'... serialized types_pb.HasExtensions_choice ...' + has_extensions_choice_pb = types_pb.HasExtensions_choice() + has_extensions_choice_pb.FromString( + some_bytes + ) + + has_extensions = has_extensions_from_pb_choice( + has_extensions_choice_pb + ) + # Do something with the has_extensions... + """ + get_concrete_instance_from_pb = ( + _HAS_EXTENSIONS_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.HasExtensions) + return result + + +# fmt: off +_REFERABLE_FROM_PB_CHOICE_MAP = { + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ), + 'asset_administration_shell': + lambda that: asset_administration_shell_from_pb( + that.asset_administration_shell + ), + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ), + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'capability': + lambda that: capability_from_pb( + that.capability + ), + 'concept_description': + lambda that: concept_description_from_pb( + that.concept_description + ), + 'entity': + lambda that: entity_from_pb( + that.entity + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'operation': + lambda that: operation_from_pb( + that.operation + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ), + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ), + 'submodel_element_collection': + lambda that: submodel_element_collection_from_pb( + that.submodel_element_collection + ), + 'submodel_element_list': + lambda that: submodel_element_list_from_pb( + that.submodel_element_list + ) +} +# fmt: on + + +def referable_from_pb_choice( + that: types_pb.Referable_choice +) -> types.Referable: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import referable_from_pb_choice + + some_bytes = b'... serialized types_pb.Referable_choice ...' + referable_choice_pb = types_pb.Referable_choice() + referable_choice_pb.FromString( + some_bytes + ) + + referable = referable_from_pb_choice( + referable_choice_pb + ) + # Do something with the referable... + """ + get_concrete_instance_from_pb = ( + _REFERABLE_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.Referable) + return result + + +# fmt: off +_IDENTIFIABLE_FROM_PB_CHOICE_MAP = { + 'asset_administration_shell': + lambda that: asset_administration_shell_from_pb( + that.asset_administration_shell + ), + 'concept_description': + lambda that: concept_description_from_pb( + that.concept_description + ), + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ) +} +# fmt: on + + +def identifiable_from_pb_choice( + that: types_pb.Identifiable_choice +) -> types.Identifiable: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import identifiable_from_pb_choice + + some_bytes = b'... serialized types_pb.Identifiable_choice ...' + identifiable_choice_pb = types_pb.Identifiable_choice() + identifiable_choice_pb.FromString( + some_bytes + ) + + identifiable = identifiable_from_pb_choice( + identifiable_choice_pb + ) + # Do something with the identifiable... + """ + get_concrete_instance_from_pb = ( + _IDENTIFIABLE_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.Identifiable) + return result + + +# fmt: off +_MODELLING_KIND_FROM_PB_MAP = { + types_pb.ModellingKind.Modellingkind_TEMPLATE: + types.ModellingKind.TEMPLATE, + types_pb.ModellingKind.Modellingkind_INSTANCE: + types.ModellingKind.INSTANCE +} # type: Mapping[types_pb.ModellingKind, types.ModellingKind] +# fmt: on + + +def modelling_kind_from_pb( + that: types_pb.ModellingKind +) -> types.ModellingKind: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import modelling_kind_from_pb + >>> modelling_kind_from_pb( + ... types_pb.ModellingKind.Modellingkind_TEMPLATE + ... ) + + """ + return _MODELLING_KIND_FROM_PB_MAP[that] + + +# fmt: off +_HAS_KIND_FROM_PB_CHOICE_MAP = { + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ) +} +# fmt: on + + +def has_kind_from_pb_choice( + that: types_pb.HasKind_choice +) -> types.HasKind: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_kind_from_pb_choice + + some_bytes = b'... serialized types_pb.HasKind_choice ...' + has_kind_choice_pb = types_pb.HasKind_choice() + has_kind_choice_pb.FromString( + some_bytes + ) + + has_kind = has_kind_from_pb_choice( + has_kind_choice_pb + ) + # Do something with the has_kind... + """ + get_concrete_instance_from_pb = ( + _HAS_KIND_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.HasKind) + return result + + +# fmt: off +_HAS_DATA_SPECIFICATION_FROM_PB_CHOICE_MAP = { + 'administrative_information': + lambda that: administrative_information_from_pb( + that.administrative_information + ), + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ), + 'asset_administration_shell': + lambda that: asset_administration_shell_from_pb( + that.asset_administration_shell + ), + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ), + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'capability': + lambda that: capability_from_pb( + that.capability + ), + 'concept_description': + lambda that: concept_description_from_pb( + that.concept_description + ), + 'entity': + lambda that: entity_from_pb( + that.entity + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'operation': + lambda that: operation_from_pb( + that.operation + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ), + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ), + 'submodel_element_collection': + lambda that: submodel_element_collection_from_pb( + that.submodel_element_collection + ), + 'submodel_element_list': + lambda that: submodel_element_list_from_pb( + that.submodel_element_list + ) +} +# fmt: on + + +def has_data_specification_from_pb_choice( + that: types_pb.HasDataSpecification_choice +) -> types.HasDataSpecification: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_data_specification_from_pb_choice + + some_bytes = b'... serialized types_pb.HasDataSpecification_choice ...' + has_data_specification_choice_pb = types_pb.HasDataSpecification_choice() + has_data_specification_choice_pb.FromString( + some_bytes + ) + + has_data_specification = has_data_specification_from_pb_choice( + has_data_specification_choice_pb + ) + # Do something with the has_data_specification... + """ + get_concrete_instance_from_pb = ( + _HAS_DATA_SPECIFICATION_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.HasDataSpecification) + return result + + +def administrative_information_from_pb( + that: types_pb.AdministrativeInformation +) -> types.AdministrativeInformation: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import administrative_information_from_pb + + some_bytes = b'... serialized types_pb.AdministrativeInformation ...' + administrative_information_pb = types_pb.AdministrativeInformation() + administrative_information_pb.FromString( + some_bytes + ) + + administrative_information = administrative_information_from_pb( + administrative_information_pb + ) + # Do something with the administrative_information... + + """ + return types.AdministrativeInformation( + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + version=( + that.version + if that.HasField('version') + else None + ), + revision=( + that.revision + if that.HasField('revision') + else None + ), + creator=( + reference_from_pb( + that.creator + ) + if that.HasField('creator') + else None + ), + template_id=( + that.template_id + if that.HasField('template_id') + else None + ) + ) + + +# fmt: off +_QUALIFIABLE_FROM_PB_CHOICE_MAP = { + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ), + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ), + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'capability': + lambda that: capability_from_pb( + that.capability + ), + 'entity': + lambda that: entity_from_pb( + that.entity + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'operation': + lambda that: operation_from_pb( + that.operation + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ), + 'submodel': + lambda that: submodel_from_pb( + that.submodel + ), + 'submodel_element_collection': + lambda that: submodel_element_collection_from_pb( + that.submodel_element_collection + ), + 'submodel_element_list': + lambda that: submodel_element_list_from_pb( + that.submodel_element_list + ) +} +# fmt: on + + +def qualifiable_from_pb_choice( + that: types_pb.Qualifiable_choice +) -> types.Qualifiable: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import qualifiable_from_pb_choice + + some_bytes = b'... serialized types_pb.Qualifiable_choice ...' + qualifiable_choice_pb = types_pb.Qualifiable_choice() + qualifiable_choice_pb.FromString( + some_bytes + ) + + qualifiable = qualifiable_from_pb_choice( + qualifiable_choice_pb + ) + # Do something with the qualifiable... + """ + get_concrete_instance_from_pb = ( + _QUALIFIABLE_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.Qualifiable) + return result + + +# fmt: off +_QUALIFIER_KIND_FROM_PB_MAP = { + types_pb.QualifierKind.Qualifierkind_VALUE_QUALIFIER: + types.QualifierKind.VALUE_QUALIFIER, + types_pb.QualifierKind.Qualifierkind_CONCEPT_QUALIFIER: + types.QualifierKind.CONCEPT_QUALIFIER, + types_pb.QualifierKind.Qualifierkind_TEMPLATE_QUALIFIER: + types.QualifierKind.TEMPLATE_QUALIFIER +} # type: Mapping[types_pb.QualifierKind, types.QualifierKind] +# fmt: on + + +def qualifier_kind_from_pb( + that: types_pb.QualifierKind +) -> types.QualifierKind: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import qualifier_kind_from_pb + >>> qualifier_kind_from_pb( + ... types_pb.QualifierKind.Qualifierkind_VALUE_QUALIFIER + ... ) + + """ + return _QUALIFIER_KIND_FROM_PB_MAP[that] + + +def qualifier_from_pb( + that: types_pb.Qualifier +) -> types.Qualifier: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import qualifier_from_pb + + some_bytes = b'... serialized types_pb.Qualifier ...' + qualifier_pb = types_pb.Qualifier() + qualifier_pb.FromString( + some_bytes + ) + + qualifier = qualifier_from_pb( + qualifier_pb + ) + # Do something with the qualifier... + + """ + return types.Qualifier( + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + kind=( + qualifier_kind_from_pb( + that.kind + ) + if that.HasField('kind') + else None + ), + type=that.type, + value_type=data_type_def_xsd_from_pb( + that.value_type + ), + value=( + that.value + if that.HasField('value') + else None + ), + value_id=( + reference_from_pb( + that.value_id + ) + if that.HasField('value_id') + else None + ) + ) + + +def asset_administration_shell_from_pb( + that: types_pb.AssetAdministrationShell +) -> types.AssetAdministrationShell: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import asset_administration_shell_from_pb + + some_bytes = b'... serialized types_pb.AssetAdministrationShell ...' + asset_administration_shell_pb = types_pb.AssetAdministrationShell() + asset_administration_shell_pb.FromString( + some_bytes + ) + + asset_administration_shell = asset_administration_shell_from_pb( + asset_administration_shell_pb + ) + # Do something with the asset_administration_shell... + + """ + return types.AssetAdministrationShell( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + administration=( + administrative_information_from_pb( + that.administration + ) + if that.HasField('administration') + else None + ), + id=that.id, + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + derived_from=( + reference_from_pb( + that.derived_from + ) + if that.HasField('derived_from') + else None + ), + asset_information=asset_information_from_pb( + that.asset_information + ), + submodels=( + list(map( + reference_from_pb, + that.submodels + )) + if len(that.submodels) > 0 + else None + ) + ) + + +def asset_information_from_pb( + that: types_pb.AssetInformation +) -> types.AssetInformation: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import asset_information_from_pb + + some_bytes = b'... serialized types_pb.AssetInformation ...' + asset_information_pb = types_pb.AssetInformation() + asset_information_pb.FromString( + some_bytes + ) + + asset_information = asset_information_from_pb( + asset_information_pb + ) + # Do something with the asset_information... + + """ + return types.AssetInformation( + asset_kind=asset_kind_from_pb( + that.asset_kind + ), + global_asset_id=( + that.global_asset_id + if that.HasField('global_asset_id') + else None + ), + specific_asset_ids=( + list(map( + specific_asset_id_from_pb, + that.specific_asset_ids + )) + if len(that.specific_asset_ids) > 0 + else None + ), + asset_type=( + that.asset_type + if that.HasField('asset_type') + else None + ), + default_thumbnail=( + resource_from_pb( + that.default_thumbnail + ) + if that.HasField('default_thumbnail') + else None + ) + ) + + +def resource_from_pb( + that: types_pb.Resource +) -> types.Resource: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import resource_from_pb + + some_bytes = b'... serialized types_pb.Resource ...' + resource_pb = types_pb.Resource() + resource_pb.FromString( + some_bytes + ) + + resource = resource_from_pb( + resource_pb + ) + # Do something with the resource... + + """ + return types.Resource( + path=that.path, + content_type=( + that.content_type + if that.HasField('content_type') + else None + ) + ) + + +# fmt: off +_ASSET_KIND_FROM_PB_MAP = { + types_pb.AssetKind.Assetkind_TYPE: + types.AssetKind.TYPE, + types_pb.AssetKind.Assetkind_INSTANCE: + types.AssetKind.INSTANCE, + types_pb.AssetKind.Assetkind_NOT_APPLICABLE: + types.AssetKind.NOT_APPLICABLE +} # type: Mapping[types_pb.AssetKind, types.AssetKind] +# fmt: on + + +def asset_kind_from_pb( + that: types_pb.AssetKind +) -> types.AssetKind: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import asset_kind_from_pb + >>> asset_kind_from_pb( + ... types_pb.AssetKind.Assetkind_TYPE + ... ) + + """ + return _ASSET_KIND_FROM_PB_MAP[that] + + +def specific_asset_id_from_pb( + that: types_pb.SpecificAssetId +) -> types.SpecificAssetID: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import specific_asset_id_from_pb + + some_bytes = b'... serialized types_pb.SpecificAssetId ...' + specific_asset_id_pb = types_pb.SpecificAssetId() + specific_asset_id_pb.FromString( + some_bytes + ) + + specific_asset_id = specific_asset_id_from_pb( + specific_asset_id_pb + ) + # Do something with the specific_asset_id... + + """ + return types.SpecificAssetID( + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + name=that.name, + value=that.value, + external_subject_id=( + reference_from_pb( + that.external_subject_id + ) + if that.HasField('external_subject_id') + else None + ) + ) + + +def submodel_from_pb( + that: types_pb.Submodel +) -> types.Submodel: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_from_pb + + some_bytes = b'... serialized types_pb.Submodel ...' + submodel_pb = types_pb.Submodel() + submodel_pb.FromString( + some_bytes + ) + + submodel = submodel_from_pb( + submodel_pb + ) + # Do something with the submodel... + + """ + return types.Submodel( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + administration=( + administrative_information_from_pb( + that.administration + ) + if that.HasField('administration') + else None + ), + id=that.id, + kind=( + modelling_kind_from_pb( + that.kind + ) + if that.HasField('kind') + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + submodel_elements=( + list(map( + submodel_element_from_pb_choice, + that.submodel_elements + )) + if len(that.submodel_elements) > 0 + else None + ) + ) + + +# fmt: off +_SUBMODEL_ELEMENT_FROM_PB_CHOICE_MAP = { + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ), + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ), + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'capability': + lambda that: capability_from_pb( + that.capability + ), + 'entity': + lambda that: entity_from_pb( + that.entity + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'operation': + lambda that: operation_from_pb( + that.operation + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ), + 'submodel_element_collection': + lambda that: submodel_element_collection_from_pb( + that.submodel_element_collection + ), + 'submodel_element_list': + lambda that: submodel_element_list_from_pb( + that.submodel_element_list + ) +} +# fmt: on + + +def submodel_element_from_pb_choice( + that: types_pb.SubmodelElement_choice +) -> types.SubmodelElement: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_element_from_pb_choice + + some_bytes = b'... serialized types_pb.SubmodelElement_choice ...' + submodel_element_choice_pb = types_pb.SubmodelElement_choice() + submodel_element_choice_pb.FromString( + some_bytes + ) + + submodel_element = submodel_element_from_pb_choice( + submodel_element_choice_pb + ) + # Do something with the submodel_element... + """ + get_concrete_instance_from_pb = ( + _SUBMODEL_ELEMENT_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.SubmodelElement) + return result + + +# fmt: off +_RELATIONSHIP_ELEMENT_FROM_PB_CHOICE_MAP = { + 'relationship_element': + lambda that: relationship_element_from_pb( + that.relationship_element + ), + 'annotated_relationship_element': + lambda that: annotated_relationship_element_from_pb( + that.annotated_relationship_element + ) +} +# fmt: on + + +def relationship_element_from_pb_choice( + that: types_pb.RelationshipElement_choice +) -> types.RelationshipElement: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import relationship_element_from_pb_choice + + some_bytes = b'... serialized types_pb.RelationshipElement_choice ...' + relationship_element_choice_pb = types_pb.RelationshipElement_choice() + relationship_element_choice_pb.FromString( + some_bytes + ) + + relationship_element = relationship_element_from_pb_choice( + relationship_element_choice_pb + ) + # Do something with the relationship_element... + """ + get_concrete_instance_from_pb = ( + _RELATIONSHIP_ELEMENT_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.RelationshipElement) + return result + + +def relationship_element_from_pb( + that: types_pb.RelationshipElement +) -> types.RelationshipElement: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import relationship_element_from_pb + + some_bytes = b'... serialized types_pb.RelationshipElement ...' + relationship_element_pb = types_pb.RelationshipElement() + relationship_element_pb.FromString( + some_bytes + ) + + relationship_element = relationship_element_from_pb( + relationship_element_pb + ) + # Do something with the relationship_element... + + """ + return types.RelationshipElement( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + first=reference_from_pb( + that.first + ), + second=reference_from_pb( + that.second + ) + ) + + +# fmt: off +_AAS_SUBMODEL_ELEMENTS_FROM_PB_MAP = { + types_pb.AasSubmodelElements.Aassubmodelelements_ANNOTATED_RELATIONSHIP_ELEMENT: + types.AASSubmodelElements.ANNOTATED_RELATIONSHIP_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_BASIC_EVENT_ELEMENT: + types.AASSubmodelElements.BASIC_EVENT_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_BLOB: + types.AASSubmodelElements.BLOB, + types_pb.AasSubmodelElements.Aassubmodelelements_CAPABILITY: + types.AASSubmodelElements.CAPABILITY, + types_pb.AasSubmodelElements.Aassubmodelelements_DATA_ELEMENT: + types.AASSubmodelElements.DATA_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_ENTITY: + types.AASSubmodelElements.ENTITY, + types_pb.AasSubmodelElements.Aassubmodelelements_EVENT_ELEMENT: + types.AASSubmodelElements.EVENT_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_FILE: + types.AASSubmodelElements.FILE, + types_pb.AasSubmodelElements.Aassubmodelelements_MULTI_LANGUAGE_PROPERTY: + types.AASSubmodelElements.MULTI_LANGUAGE_PROPERTY, + types_pb.AasSubmodelElements.Aassubmodelelements_OPERATION: + types.AASSubmodelElements.OPERATION, + types_pb.AasSubmodelElements.Aassubmodelelements_PROPERTY: + types.AASSubmodelElements.PROPERTY, + types_pb.AasSubmodelElements.Aassubmodelelements_RANGE: + types.AASSubmodelElements.RANGE, + types_pb.AasSubmodelElements.Aassubmodelelements_REFERENCE_ELEMENT: + types.AASSubmodelElements.REFERENCE_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_RELATIONSHIP_ELEMENT: + types.AASSubmodelElements.RELATIONSHIP_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_SUBMODEL_ELEMENT: + types.AASSubmodelElements.SUBMODEL_ELEMENT, + types_pb.AasSubmodelElements.Aassubmodelelements_SUBMODEL_ELEMENT_LIST: + types.AASSubmodelElements.SUBMODEL_ELEMENT_LIST, + types_pb.AasSubmodelElements.Aassubmodelelements_SUBMODEL_ELEMENT_COLLECTION: + types.AASSubmodelElements.SUBMODEL_ELEMENT_COLLECTION +} # type: Mapping[types_pb.AasSubmodelElements, types.AASSubmodelElements] +# fmt: on + + +def aas_submodel_elements_from_pb( + that: types_pb.AasSubmodelElements +) -> types.AASSubmodelElements: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import aas_submodel_elements_from_pb + >>> aas_submodel_elements_from_pb( + ... types_pb.AasSubmodelElements.Aassubmodelelements_ANNOTATED_RELATIONSHIP_ELEMENT + ... ) + + """ + return _AAS_SUBMODEL_ELEMENTS_FROM_PB_MAP[that] + + +def submodel_element_list_from_pb( + that: types_pb.SubmodelElementList +) -> types.SubmodelElementList: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_element_list_from_pb + + some_bytes = b'... serialized types_pb.SubmodelElementList ...' + submodel_element_list_pb = types_pb.SubmodelElementList() + submodel_element_list_pb.FromString( + some_bytes + ) + + submodel_element_list = submodel_element_list_from_pb( + submodel_element_list_pb + ) + # Do something with the submodel_element_list... + + """ + return types.SubmodelElementList( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + order_relevant=( + that.order_relevant + if that.HasField('order_relevant') + else None + ), + semantic_id_list_element=( + reference_from_pb( + that.semantic_id_list_element + ) + if that.HasField('semantic_id_list_element') + else None + ), + type_value_list_element=aas_submodel_elements_from_pb( + that.type_value_list_element + ), + value_type_list_element=( + data_type_def_xsd_from_pb( + that.value_type_list_element + ) + if that.HasField('value_type_list_element') + else None + ), + value=( + list(map( + submodel_element_from_pb_choice, + that.value + )) + if len(that.value) > 0 + else None + ) + ) + + +def submodel_element_collection_from_pb( + that: types_pb.SubmodelElementCollection +) -> types.SubmodelElementCollection: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_element_collection_from_pb + + some_bytes = b'... serialized types_pb.SubmodelElementCollection ...' + submodel_element_collection_pb = types_pb.SubmodelElementCollection() + submodel_element_collection_pb.FromString( + some_bytes + ) + + submodel_element_collection = submodel_element_collection_from_pb( + submodel_element_collection_pb + ) + # Do something with the submodel_element_collection... + + """ + return types.SubmodelElementCollection( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value=( + list(map( + submodel_element_from_pb_choice, + that.value + )) + if len(that.value) > 0 + else None + ) + ) + + +# fmt: off +_DATA_ELEMENT_FROM_PB_CHOICE_MAP = { + 'blob': + lambda that: blob_from_pb( + that.blob + ), + 'file': + lambda that: file_from_pb( + that.file + ), + 'multi_language_property': + lambda that: multi_language_property_from_pb( + that.multi_language_property + ), + 'property': + lambda that: property_from_pb( + that.property + ), + 'range': + lambda that: range_from_pb( + that.range + ), + 'reference_element': + lambda that: reference_element_from_pb( + that.reference_element + ) +} +# fmt: on + + +def data_element_from_pb_choice( + that: types_pb.DataElement_choice +) -> types.DataElement: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import data_element_from_pb_choice + + some_bytes = b'... serialized types_pb.DataElement_choice ...' + data_element_choice_pb = types_pb.DataElement_choice() + data_element_choice_pb.FromString( + some_bytes + ) + + data_element = data_element_from_pb_choice( + data_element_choice_pb + ) + # Do something with the data_element... + """ + get_concrete_instance_from_pb = ( + _DATA_ELEMENT_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.DataElement) + return result + + +def property_from_pb( + that: types_pb.Property +) -> types.Property: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import property_from_pb + + some_bytes = b'... serialized types_pb.Property ...' + property_pb = types_pb.Property() + property_pb.FromString( + some_bytes + ) + + property = property_from_pb( + property_pb + ) + # Do something with the property... + + """ + return types.Property( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value_type=data_type_def_xsd_from_pb( + that.value_type + ), + value=( + that.value + if that.HasField('value') + else None + ), + value_id=( + reference_from_pb( + that.value_id + ) + if that.HasField('value_id') + else None + ) + ) + + +def multi_language_property_from_pb( + that: types_pb.MultiLanguageProperty +) -> types.MultiLanguageProperty: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import multi_language_property_from_pb + + some_bytes = b'... serialized types_pb.MultiLanguageProperty ...' + multi_language_property_pb = types_pb.MultiLanguageProperty() + multi_language_property_pb.FromString( + some_bytes + ) + + multi_language_property = multi_language_property_from_pb( + multi_language_property_pb + ) + # Do something with the multi_language_property... + + """ + return types.MultiLanguageProperty( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value=( + list(map( + lang_string_text_type_from_pb, + that.value + )) + if len(that.value) > 0 + else None + ), + value_id=( + reference_from_pb( + that.value_id + ) + if that.HasField('value_id') + else None + ) + ) + + +def range_from_pb( + that: types_pb.Range +) -> types.Range: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import range_from_pb + + some_bytes = b'... serialized types_pb.Range ...' + range_pb = types_pb.Range() + range_pb.FromString( + some_bytes + ) + + range = range_from_pb( + range_pb + ) + # Do something with the range... + + """ + return types.Range( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value_type=data_type_def_xsd_from_pb( + that.value_type + ), + min=( + that.min + if that.HasField('min') + else None + ), + max=( + that.max + if that.HasField('max') + else None + ) + ) + + +def reference_element_from_pb( + that: types_pb.ReferenceElement +) -> types.ReferenceElement: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import reference_element_from_pb + + some_bytes = b'... serialized types_pb.ReferenceElement ...' + reference_element_pb = types_pb.ReferenceElement() + reference_element_pb.FromString( + some_bytes + ) + + reference_element = reference_element_from_pb( + reference_element_pb + ) + # Do something with the reference_element... + + """ + return types.ReferenceElement( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value=( + reference_from_pb( + that.value + ) + if that.HasField('value') + else None + ) + ) + + +def blob_from_pb( + that: types_pb.Blob +) -> types.Blob: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import blob_from_pb + + some_bytes = b'... serialized types_pb.Blob ...' + blob_pb = types_pb.Blob() + blob_pb.FromString( + some_bytes + ) + + blob = blob_from_pb( + blob_pb + ) + # Do something with the blob... + + """ + return types.Blob( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value=( + bytearray(that.value) + if that.HasField('value') + else None + ), + content_type=that.content_type + ) + + +def file_from_pb( + that: types_pb.File +) -> types.File: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import file_from_pb + + some_bytes = b'... serialized types_pb.File ...' + file_pb = types_pb.File() + file_pb.FromString( + some_bytes + ) + + file = file_from_pb( + file_pb + ) + # Do something with the file... + + """ + return types.File( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + value=( + that.value + if that.HasField('value') + else None + ), + content_type=that.content_type + ) + + +def annotated_relationship_element_from_pb( + that: types_pb.AnnotatedRelationshipElement +) -> types.AnnotatedRelationshipElement: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import annotated_relationship_element_from_pb + + some_bytes = b'... serialized types_pb.AnnotatedRelationshipElement ...' + annotated_relationship_element_pb = types_pb.AnnotatedRelationshipElement() + annotated_relationship_element_pb.FromString( + some_bytes + ) + + annotated_relationship_element = annotated_relationship_element_from_pb( + annotated_relationship_element_pb + ) + # Do something with the annotated_relationship_element... + + """ + return types.AnnotatedRelationshipElement( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + first=reference_from_pb( + that.first + ), + second=reference_from_pb( + that.second + ), + annotations=( + list(map( + data_element_from_pb_choice, + that.annotations + )) + if len(that.annotations) > 0 + else None + ) + ) + + +def entity_from_pb( + that: types_pb.Entity +) -> types.Entity: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import entity_from_pb + + some_bytes = b'... serialized types_pb.Entity ...' + entity_pb = types_pb.Entity() + entity_pb.FromString( + some_bytes + ) + + entity = entity_from_pb( + entity_pb + ) + # Do something with the entity... + + """ + return types.Entity( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + statements=( + list(map( + submodel_element_from_pb_choice, + that.statements + )) + if len(that.statements) > 0 + else None + ), + entity_type=entity_type_from_pb( + that.entity_type + ), + global_asset_id=( + that.global_asset_id + if that.HasField('global_asset_id') + else None + ), + specific_asset_ids=( + list(map( + specific_asset_id_from_pb, + that.specific_asset_ids + )) + if len(that.specific_asset_ids) > 0 + else None + ) + ) + + +# fmt: off +_ENTITY_TYPE_FROM_PB_MAP = { + types_pb.EntityType.Entitytype_CO_MANAGED_ENTITY: + types.EntityType.CO_MANAGED_ENTITY, + types_pb.EntityType.Entitytype_SELF_MANAGED_ENTITY: + types.EntityType.SELF_MANAGED_ENTITY +} # type: Mapping[types_pb.EntityType, types.EntityType] +# fmt: on + + +def entity_type_from_pb( + that: types_pb.EntityType +) -> types.EntityType: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import entity_type_from_pb + >>> entity_type_from_pb( + ... types_pb.EntityType.Entitytype_CO_MANAGED_ENTITY + ... ) + + """ + return _ENTITY_TYPE_FROM_PB_MAP[that] + + +# fmt: off +_DIRECTION_FROM_PB_MAP = { + types_pb.Direction.Direction_INPUT: + types.Direction.INPUT, + types_pb.Direction.Direction_OUTPUT: + types.Direction.OUTPUT +} # type: Mapping[types_pb.Direction, types.Direction] +# fmt: on + + +def direction_from_pb( + that: types_pb.Direction +) -> types.Direction: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import direction_from_pb + >>> direction_from_pb( + ... types_pb.Direction.Direction_INPUT + ... ) + + """ + return _DIRECTION_FROM_PB_MAP[that] + + +# fmt: off +_STATE_OF_EVENT_FROM_PB_MAP = { + types_pb.StateOfEvent.Stateofevent_ON: + types.StateOfEvent.ON, + types_pb.StateOfEvent.Stateofevent_OFF: + types.StateOfEvent.OFF +} # type: Mapping[types_pb.StateOfEvent, types.StateOfEvent] +# fmt: on + + +def state_of_event_from_pb( + that: types_pb.StateOfEvent +) -> types.StateOfEvent: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import state_of_event_from_pb + >>> state_of_event_from_pb( + ... types_pb.StateOfEvent.Stateofevent_ON + ... ) + + """ + return _STATE_OF_EVENT_FROM_PB_MAP[that] + + +def event_payload_from_pb( + that: types_pb.EventPayload +) -> types.EventPayload: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import event_payload_from_pb + + some_bytes = b'... serialized types_pb.EventPayload ...' + event_payload_pb = types_pb.EventPayload() + event_payload_pb.FromString( + some_bytes + ) + + event_payload = event_payload_from_pb( + event_payload_pb + ) + # Do something with the event_payload... + + """ + return types.EventPayload( + source=reference_from_pb( + that.source + ), + source_semantic_id=( + reference_from_pb( + that.source_semantic_id + ) + if that.HasField('source_semantic_id') + else None + ), + observable_reference=reference_from_pb( + that.observable_reference + ), + observable_semantic_id=( + reference_from_pb( + that.observable_semantic_id + ) + if that.HasField('observable_semantic_id') + else None + ), + topic=( + that.topic + if that.HasField('topic') + else None + ), + subject_id=( + reference_from_pb( + that.subject_id + ) + if that.HasField('subject_id') + else None + ), + time_stamp=that.time_stamp, + payload=( + bytearray(that.payload) + if that.HasField('payload') + else None + ) + ) + + +# fmt: off +_EVENT_ELEMENT_FROM_PB_CHOICE_MAP = { + 'basic_event_element': + lambda that: basic_event_element_from_pb( + that.basic_event_element + ) +} +# fmt: on + + +def event_element_from_pb_choice( + that: types_pb.EventElement_choice +) -> types.EventElement: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import event_element_from_pb_choice + + some_bytes = b'... serialized types_pb.EventElement_choice ...' + event_element_choice_pb = types_pb.EventElement_choice() + event_element_choice_pb.FromString( + some_bytes + ) + + event_element = event_element_from_pb_choice( + event_element_choice_pb + ) + # Do something with the event_element... + """ + get_concrete_instance_from_pb = ( + _EVENT_ELEMENT_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.EventElement) + return result + + +def basic_event_element_from_pb( + that: types_pb.BasicEventElement +) -> types.BasicEventElement: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import basic_event_element_from_pb + + some_bytes = b'... serialized types_pb.BasicEventElement ...' + basic_event_element_pb = types_pb.BasicEventElement() + basic_event_element_pb.FromString( + some_bytes + ) + + basic_event_element = basic_event_element_from_pb( + basic_event_element_pb + ) + # Do something with the basic_event_element... + + """ + return types.BasicEventElement( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + observed=reference_from_pb( + that.observed + ), + direction=direction_from_pb( + that.direction + ), + state=state_of_event_from_pb( + that.state + ), + message_topic=( + that.message_topic + if that.HasField('message_topic') + else None + ), + message_broker=( + reference_from_pb( + that.message_broker + ) + if that.HasField('message_broker') + else None + ), + last_update=( + that.last_update + if that.HasField('last_update') + else None + ), + min_interval=( + that.min_interval + if that.HasField('min_interval') + else None + ), + max_interval=( + that.max_interval + if that.HasField('max_interval') + else None + ) + ) + + +def operation_from_pb( + that: types_pb.Operation +) -> types.Operation: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import operation_from_pb + + some_bytes = b'... serialized types_pb.Operation ...' + operation_pb = types_pb.Operation() + operation_pb.FromString( + some_bytes + ) + + operation = operation_from_pb( + operation_pb + ) + # Do something with the operation... + + """ + return types.Operation( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + input_variables=( + list(map( + operation_variable_from_pb, + that.input_variables + )) + if len(that.input_variables) > 0 + else None + ), + output_variables=( + list(map( + operation_variable_from_pb, + that.output_variables + )) + if len(that.output_variables) > 0 + else None + ), + inoutput_variables=( + list(map( + operation_variable_from_pb, + that.inoutput_variables + )) + if len(that.inoutput_variables) > 0 + else None + ) + ) + + +def operation_variable_from_pb( + that: types_pb.OperationVariable +) -> types.OperationVariable: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import operation_variable_from_pb + + some_bytes = b'... serialized types_pb.OperationVariable ...' + operation_variable_pb = types_pb.OperationVariable() + operation_variable_pb.FromString( + some_bytes + ) + + operation_variable = operation_variable_from_pb( + operation_variable_pb + ) + # Do something with the operation_variable... + + """ + return types.OperationVariable( + value=submodel_element_from_pb_choice( + that.value + ) + ) + + +def capability_from_pb( + that: types_pb.Capability +) -> types.Capability: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import capability_from_pb + + some_bytes = b'... serialized types_pb.Capability ...' + capability_pb = types_pb.Capability() + capability_pb.FromString( + some_bytes + ) + + capability = capability_from_pb( + capability_pb + ) + # Do something with the capability... + + """ + return types.Capability( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + semantic_id=( + reference_from_pb( + that.semantic_id + ) + if that.HasField('semantic_id') + else None + ), + supplemental_semantic_ids=( + list(map( + reference_from_pb, + that.supplemental_semantic_ids + )) + if len(that.supplemental_semantic_ids) > 0 + else None + ), + qualifiers=( + list(map( + qualifier_from_pb, + that.qualifiers + )) + if len(that.qualifiers) > 0 + else None + ), + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ) + ) + + +def concept_description_from_pb( + that: types_pb.ConceptDescription +) -> types.ConceptDescription: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import concept_description_from_pb + + some_bytes = b'... serialized types_pb.ConceptDescription ...' + concept_description_pb = types_pb.ConceptDescription() + concept_description_pb.FromString( + some_bytes + ) + + concept_description = concept_description_from_pb( + concept_description_pb + ) + # Do something with the concept_description... + + """ + return types.ConceptDescription( + extensions=( + list(map( + extension_from_pb, + that.extensions + )) + if len(that.extensions) > 0 + else None + ), + category=( + that.category + if that.HasField('category') + else None + ), + id_short=( + that.id_short + if that.HasField('id_short') + else None + ), + display_name=( + list(map( + lang_string_name_type_from_pb, + that.display_name + )) + if len(that.display_name) > 0 + else None + ), + description=( + list(map( + lang_string_text_type_from_pb, + that.description + )) + if len(that.description) > 0 + else None + ), + administration=( + administrative_information_from_pb( + that.administration + ) + if that.HasField('administration') + else None + ), + id=that.id, + embedded_data_specifications=( + list(map( + embedded_data_specification_from_pb, + that.embedded_data_specifications + )) + if len(that.embedded_data_specifications) > 0 + else None + ), + is_case_of=( + list(map( + reference_from_pb, + that.is_case_of + )) + if len(that.is_case_of) > 0 + else None + ) + ) + + +# fmt: off +_REFERENCE_TYPES_FROM_PB_MAP = { + types_pb.ReferenceTypes.Referencetypes_EXTERNAL_REFERENCE: + types.ReferenceTypes.EXTERNAL_REFERENCE, + types_pb.ReferenceTypes.Referencetypes_MODEL_REFERENCE: + types.ReferenceTypes.MODEL_REFERENCE +} # type: Mapping[types_pb.ReferenceTypes, types.ReferenceTypes] +# fmt: on + + +def reference_types_from_pb( + that: types_pb.ReferenceTypes +) -> types.ReferenceTypes: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import reference_types_from_pb + >>> reference_types_from_pb( + ... types_pb.ReferenceTypes.Referencetypes_EXTERNAL_REFERENCE + ... ) + + """ + return _REFERENCE_TYPES_FROM_PB_MAP[that] + + +def reference_from_pb( + that: types_pb.Reference +) -> types.Reference: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import reference_from_pb + + some_bytes = b'... serialized types_pb.Reference ...' + reference_pb = types_pb.Reference() + reference_pb.FromString( + some_bytes + ) + + reference = reference_from_pb( + reference_pb + ) + # Do something with the reference... + + """ + return types.Reference( + type=reference_types_from_pb( + that.type + ), + referred_semantic_id=( + reference_from_pb( + that.referred_semantic_id + ) + if that.HasField('referred_semantic_id') + else None + ), + keys=list(map( + key_from_pb, + that.keys + )) + ) + + +def key_from_pb( + that: types_pb.Key +) -> types.Key: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import key_from_pb + + some_bytes = b'... serialized types_pb.Key ...' + key_pb = types_pb.Key() + key_pb.FromString( + some_bytes + ) + + key = key_from_pb( + key_pb + ) + # Do something with the key... + + """ + return types.Key( + type=key_types_from_pb( + that.type + ), + value=that.value + ) + + +# fmt: off +_KEY_TYPES_FROM_PB_MAP = { + types_pb.KeyTypes.Keytypes_ANNOTATED_RELATIONSHIP_ELEMENT: + types.KeyTypes.ANNOTATED_RELATIONSHIP_ELEMENT, + types_pb.KeyTypes.Keytypes_ASSET_ADMINISTRATION_SHELL: + types.KeyTypes.ASSET_ADMINISTRATION_SHELL, + types_pb.KeyTypes.Keytypes_BASIC_EVENT_ELEMENT: + types.KeyTypes.BASIC_EVENT_ELEMENT, + types_pb.KeyTypes.Keytypes_BLOB: + types.KeyTypes.BLOB, + types_pb.KeyTypes.Keytypes_CAPABILITY: + types.KeyTypes.CAPABILITY, + types_pb.KeyTypes.Keytypes_CONCEPT_DESCRIPTION: + types.KeyTypes.CONCEPT_DESCRIPTION, + types_pb.KeyTypes.Keytypes_DATA_ELEMENT: + types.KeyTypes.DATA_ELEMENT, + types_pb.KeyTypes.Keytypes_ENTITY: + types.KeyTypes.ENTITY, + types_pb.KeyTypes.Keytypes_EVENT_ELEMENT: + types.KeyTypes.EVENT_ELEMENT, + types_pb.KeyTypes.Keytypes_FILE: + types.KeyTypes.FILE, + types_pb.KeyTypes.Keytypes_FRAGMENT_REFERENCE: + types.KeyTypes.FRAGMENT_REFERENCE, + types_pb.KeyTypes.Keytypes_GLOBAL_REFERENCE: + types.KeyTypes.GLOBAL_REFERENCE, + types_pb.KeyTypes.Keytypes_IDENTIFIABLE: + types.KeyTypes.IDENTIFIABLE, + types_pb.KeyTypes.Keytypes_MULTI_LANGUAGE_PROPERTY: + types.KeyTypes.MULTI_LANGUAGE_PROPERTY, + types_pb.KeyTypes.Keytypes_OPERATION: + types.KeyTypes.OPERATION, + types_pb.KeyTypes.Keytypes_PROPERTY: + types.KeyTypes.PROPERTY, + types_pb.KeyTypes.Keytypes_RANGE: + types.KeyTypes.RANGE, + types_pb.KeyTypes.Keytypes_REFERABLE: + types.KeyTypes.REFERABLE, + types_pb.KeyTypes.Keytypes_REFERENCE_ELEMENT: + types.KeyTypes.REFERENCE_ELEMENT, + types_pb.KeyTypes.Keytypes_RELATIONSHIP_ELEMENT: + types.KeyTypes.RELATIONSHIP_ELEMENT, + types_pb.KeyTypes.Keytypes_SUBMODEL: + types.KeyTypes.SUBMODEL, + types_pb.KeyTypes.Keytypes_SUBMODEL_ELEMENT: + types.KeyTypes.SUBMODEL_ELEMENT, + types_pb.KeyTypes.Keytypes_SUBMODEL_ELEMENT_COLLECTION: + types.KeyTypes.SUBMODEL_ELEMENT_COLLECTION, + types_pb.KeyTypes.Keytypes_SUBMODEL_ELEMENT_LIST: + types.KeyTypes.SUBMODEL_ELEMENT_LIST +} # type: Mapping[types_pb.KeyTypes, types.KeyTypes] +# fmt: on + + +def key_types_from_pb( + that: types_pb.KeyTypes +) -> types.KeyTypes: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import key_types_from_pb + >>> key_types_from_pb( + ... types_pb.KeyTypes.Keytypes_ANNOTATED_RELATIONSHIP_ELEMENT + ... ) + + """ + return _KEY_TYPES_FROM_PB_MAP[that] + + +# fmt: off +_DATA_TYPE_DEF_XSD_FROM_PB_MAP = { + types_pb.DataTypeDefXsd.Datatypedefxsd_ANY_URI: + types.DataTypeDefXSD.ANY_URI, + types_pb.DataTypeDefXsd.Datatypedefxsd_BASE_64_BINARY: + types.DataTypeDefXSD.BASE_64_BINARY, + types_pb.DataTypeDefXsd.Datatypedefxsd_BOOLEAN: + types.DataTypeDefXSD.BOOLEAN, + types_pb.DataTypeDefXsd.Datatypedefxsd_BYTE: + types.DataTypeDefXSD.BYTE, + types_pb.DataTypeDefXsd.Datatypedefxsd_DATE: + types.DataTypeDefXSD.DATE, + types_pb.DataTypeDefXsd.Datatypedefxsd_DATE_TIME: + types.DataTypeDefXSD.DATE_TIME, + types_pb.DataTypeDefXsd.Datatypedefxsd_DECIMAL: + types.DataTypeDefXSD.DECIMAL, + types_pb.DataTypeDefXsd.Datatypedefxsd_DOUBLE: + types.DataTypeDefXSD.DOUBLE, + types_pb.DataTypeDefXsd.Datatypedefxsd_DURATION: + types.DataTypeDefXSD.DURATION, + types_pb.DataTypeDefXsd.Datatypedefxsd_FLOAT: + types.DataTypeDefXSD.FLOAT, + types_pb.DataTypeDefXsd.Datatypedefxsd_G_DAY: + types.DataTypeDefXSD.G_DAY, + types_pb.DataTypeDefXsd.Datatypedefxsd_G_MONTH: + types.DataTypeDefXSD.G_MONTH, + types_pb.DataTypeDefXsd.Datatypedefxsd_G_MONTH_DAY: + types.DataTypeDefXSD.G_MONTH_DAY, + types_pb.DataTypeDefXsd.Datatypedefxsd_G_YEAR: + types.DataTypeDefXSD.G_YEAR, + types_pb.DataTypeDefXsd.Datatypedefxsd_G_YEAR_MONTH: + types.DataTypeDefXSD.G_YEAR_MONTH, + types_pb.DataTypeDefXsd.Datatypedefxsd_HEX_BINARY: + types.DataTypeDefXSD.HEX_BINARY, + types_pb.DataTypeDefXsd.Datatypedefxsd_INT: + types.DataTypeDefXSD.INT, + types_pb.DataTypeDefXsd.Datatypedefxsd_INTEGER: + types.DataTypeDefXSD.INTEGER, + types_pb.DataTypeDefXsd.Datatypedefxsd_LONG: + types.DataTypeDefXSD.LONG, + types_pb.DataTypeDefXsd.Datatypedefxsd_NEGATIVE_INTEGER: + types.DataTypeDefXSD.NEGATIVE_INTEGER, + types_pb.DataTypeDefXsd.Datatypedefxsd_NON_NEGATIVE_INTEGER: + types.DataTypeDefXSD.NON_NEGATIVE_INTEGER, + types_pb.DataTypeDefXsd.Datatypedefxsd_NON_POSITIVE_INTEGER: + types.DataTypeDefXSD.NON_POSITIVE_INTEGER, + types_pb.DataTypeDefXsd.Datatypedefxsd_POSITIVE_INTEGER: + types.DataTypeDefXSD.POSITIVE_INTEGER, + types_pb.DataTypeDefXsd.Datatypedefxsd_SHORT: + types.DataTypeDefXSD.SHORT, + types_pb.DataTypeDefXsd.Datatypedefxsd_STRING: + types.DataTypeDefXSD.STRING, + types_pb.DataTypeDefXsd.Datatypedefxsd_TIME: + types.DataTypeDefXSD.TIME, + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_BYTE: + types.DataTypeDefXSD.UNSIGNED_BYTE, + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_INT: + types.DataTypeDefXSD.UNSIGNED_INT, + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_LONG: + types.DataTypeDefXSD.UNSIGNED_LONG, + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_SHORT: + types.DataTypeDefXSD.UNSIGNED_SHORT +} # type: Mapping[types_pb.DataTypeDefXsd, types.DataTypeDefXSD] +# fmt: on + + +def data_type_def_xsd_from_pb( + that: types_pb.DataTypeDefXsd +) -> types.DataTypeDefXSD: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import data_type_def_xsd_from_pb + >>> data_type_def_xsd_from_pb( + ... types_pb.DataTypeDefXsd.Datatypedefxsd_ANY_URI + ... ) + + """ + return _DATA_TYPE_DEF_XSD_FROM_PB_MAP[that] + + +# fmt: off +_ABSTRACT_LANG_STRING_FROM_PB_CHOICE_MAP = { + 'lang_string_definition_type_iec_61360': + lambda that: lang_string_definition_type_iec_61360_from_pb( + that.lang_string_definition_type_iec_61360 + ), + 'lang_string_name_type': + lambda that: lang_string_name_type_from_pb( + that.lang_string_name_type + ), + 'lang_string_preferred_name_type_iec_61360': + lambda that: lang_string_preferred_name_type_iec_61360_from_pb( + that.lang_string_preferred_name_type_iec_61360 + ), + 'lang_string_short_name_type_iec_61360': + lambda that: lang_string_short_name_type_iec_61360_from_pb( + that.lang_string_short_name_type_iec_61360 + ), + 'lang_string_text_type': + lambda that: lang_string_text_type_from_pb( + that.lang_string_text_type + ) +} +# fmt: on + + +def abstract_lang_string_from_pb_choice( + that: types_pb.AbstractLangString_choice +) -> types.AbstractLangString: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import abstract_lang_string_from_pb_choice + + some_bytes = b'... serialized types_pb.AbstractLangString_choice ...' + abstract_lang_string_choice_pb = types_pb.AbstractLangString_choice() + abstract_lang_string_choice_pb.FromString( + some_bytes + ) + + abstract_lang_string = abstract_lang_string_from_pb_choice( + abstract_lang_string_choice_pb + ) + # Do something with the abstract_lang_string... + """ + get_concrete_instance_from_pb = ( + _ABSTRACT_LANG_STRING_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.AbstractLangString) + return result + + +def lang_string_name_type_from_pb( + that: types_pb.LangStringNameType +) -> types.LangStringNameType: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_name_type_from_pb + + some_bytes = b'... serialized types_pb.LangStringNameType ...' + lang_string_name_type_pb = types_pb.LangStringNameType() + lang_string_name_type_pb.FromString( + some_bytes + ) + + lang_string_name_type = lang_string_name_type_from_pb( + lang_string_name_type_pb + ) + # Do something with the lang_string_name_type... + + """ + return types.LangStringNameType( + language=that.language, + text=that.text + ) + + +def lang_string_text_type_from_pb( + that: types_pb.LangStringTextType +) -> types.LangStringTextType: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_text_type_from_pb + + some_bytes = b'... serialized types_pb.LangStringTextType ...' + lang_string_text_type_pb = types_pb.LangStringTextType() + lang_string_text_type_pb.FromString( + some_bytes + ) + + lang_string_text_type = lang_string_text_type_from_pb( + lang_string_text_type_pb + ) + # Do something with the lang_string_text_type... + + """ + return types.LangStringTextType( + language=that.language, + text=that.text + ) + + +def environment_from_pb( + that: types_pb.Environment +) -> types.Environment: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import environment_from_pb + + some_bytes = b'... serialized types_pb.Environment ...' + environment_pb = types_pb.Environment() + environment_pb.FromString( + some_bytes + ) + + environment = environment_from_pb( + environment_pb + ) + # Do something with the environment... + + """ + return types.Environment( + asset_administration_shells=( + list(map( + asset_administration_shell_from_pb, + that.asset_administration_shells + )) + if len(that.asset_administration_shells) > 0 + else None + ), + submodels=( + list(map( + submodel_from_pb, + that.submodels + )) + if len(that.submodels) > 0 + else None + ), + concept_descriptions=( + list(map( + concept_description_from_pb, + that.concept_descriptions + )) + if len(that.concept_descriptions) > 0 + else None + ) + ) + + +# fmt: off +_DATA_SPECIFICATION_CONTENT_FROM_PB_CHOICE_MAP = { + 'data_specification_iec_61360': + lambda that: data_specification_iec_61360_from_pb( + that.data_specification_iec_61360 + ) +} +# fmt: on + + +def data_specification_content_from_pb_choice( + that: types_pb.DataSpecificationContent_choice +) -> types.DataSpecificationContent: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import data_specification_content_from_pb_choice + + some_bytes = b'... serialized types_pb.DataSpecificationContent_choice ...' + data_specification_content_choice_pb = types_pb.DataSpecificationContent_choice() + data_specification_content_choice_pb.FromString( + some_bytes + ) + + data_specification_content = data_specification_content_from_pb_choice( + data_specification_content_choice_pb + ) + # Do something with the data_specification_content... + """ + get_concrete_instance_from_pb = ( + _DATA_SPECIFICATION_CONTENT_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.DataSpecificationContent) + return result + + +def embedded_data_specification_from_pb( + that: types_pb.EmbeddedDataSpecification +) -> types.EmbeddedDataSpecification: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import embedded_data_specification_from_pb + + some_bytes = b'... serialized types_pb.EmbeddedDataSpecification ...' + embedded_data_specification_pb = types_pb.EmbeddedDataSpecification() + embedded_data_specification_pb.FromString( + some_bytes + ) + + embedded_data_specification = embedded_data_specification_from_pb( + embedded_data_specification_pb + ) + # Do something with the embedded_data_specification... + + """ + return types.EmbeddedDataSpecification( + data_specification_content=data_specification_content_from_pb_choice( + that.data_specification_content + ), + data_specification=reference_from_pb( + that.data_specification + ) + ) + + +# fmt: off +_DATA_TYPE_IEC_61360_FROM_PB_MAP = { + types_pb.DataTypeIec61360.Datatypeiec61360_DATE: + types.DataTypeIEC61360.DATE, + types_pb.DataTypeIec61360.Datatypeiec61360_STRING: + types.DataTypeIEC61360.STRING, + types_pb.DataTypeIec61360.Datatypeiec61360_STRING_TRANSLATABLE: + types.DataTypeIEC61360.STRING_TRANSLATABLE, + types_pb.DataTypeIec61360.Datatypeiec61360_INTEGER_MEASURE: + types.DataTypeIEC61360.INTEGER_MEASURE, + types_pb.DataTypeIec61360.Datatypeiec61360_INTEGER_COUNT: + types.DataTypeIEC61360.INTEGER_COUNT, + types_pb.DataTypeIec61360.Datatypeiec61360_INTEGER_CURRENCY: + types.DataTypeIEC61360.INTEGER_CURRENCY, + types_pb.DataTypeIec61360.Datatypeiec61360_REAL_MEASURE: + types.DataTypeIEC61360.REAL_MEASURE, + types_pb.DataTypeIec61360.Datatypeiec61360_REAL_COUNT: + types.DataTypeIEC61360.REAL_COUNT, + types_pb.DataTypeIec61360.Datatypeiec61360_REAL_CURRENCY: + types.DataTypeIEC61360.REAL_CURRENCY, + types_pb.DataTypeIec61360.Datatypeiec61360_BOOLEAN: + types.DataTypeIEC61360.BOOLEAN, + types_pb.DataTypeIec61360.Datatypeiec61360_IRI: + types.DataTypeIEC61360.IRI, + types_pb.DataTypeIec61360.Datatypeiec61360_IRDI: + types.DataTypeIEC61360.IRDI, + types_pb.DataTypeIec61360.Datatypeiec61360_RATIONAL: + types.DataTypeIEC61360.RATIONAL, + types_pb.DataTypeIec61360.Datatypeiec61360_RATIONAL_MEASURE: + types.DataTypeIEC61360.RATIONAL_MEASURE, + types_pb.DataTypeIec61360.Datatypeiec61360_TIME: + types.DataTypeIEC61360.TIME, + types_pb.DataTypeIec61360.Datatypeiec61360_TIMESTAMP: + types.DataTypeIEC61360.TIMESTAMP, + types_pb.DataTypeIec61360.Datatypeiec61360_FILE: + types.DataTypeIEC61360.FILE, + types_pb.DataTypeIec61360.Datatypeiec61360_HTML: + types.DataTypeIEC61360.HTML, + types_pb.DataTypeIec61360.Datatypeiec61360_BLOB: + types.DataTypeIEC61360.BLOB +} # type: Mapping[types_pb.DataTypeIec61360, types.DataTypeIEC61360] +# fmt: on + + +def data_type_iec_61360_from_pb( + that: types_pb.DataTypeIec61360 +) -> types.DataTypeIEC61360: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import data_type_iec_61360_from_pb + >>> data_type_iec_61360_from_pb( + ... types_pb.DataTypeIec61360.Datatypeiec61360_DATE + ... ) + + """ + return _DATA_TYPE_IEC_61360_FROM_PB_MAP[that] + + +def level_type_from_pb( + that: types_pb.LevelType +) -> types.LevelType: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import level_type_from_pb + + some_bytes = b'... serialized types_pb.LevelType ...' + level_type_pb = types_pb.LevelType() + level_type_pb.FromString( + some_bytes + ) + + level_type = level_type_from_pb( + level_type_pb + ) + # Do something with the level_type... + + """ + return types.LevelType( + min=that.min, + nom=that.nom, + typ=that.typ, + max=that.max + ) + + +def value_reference_pair_from_pb( + that: types_pb.ValueReferencePair +) -> types.ValueReferencePair: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import value_reference_pair_from_pb + + some_bytes = b'... serialized types_pb.ValueReferencePair ...' + value_reference_pair_pb = types_pb.ValueReferencePair() + value_reference_pair_pb.FromString( + some_bytes + ) + + value_reference_pair = value_reference_pair_from_pb( + value_reference_pair_pb + ) + # Do something with the value_reference_pair... + + """ + return types.ValueReferencePair( + value=that.value, + value_id=reference_from_pb( + that.value_id + ) + ) + + +def value_list_from_pb( + that: types_pb.ValueList +) -> types.ValueList: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import value_list_from_pb + + some_bytes = b'... serialized types_pb.ValueList ...' + value_list_pb = types_pb.ValueList() + value_list_pb.FromString( + some_bytes + ) + + value_list = value_list_from_pb( + value_list_pb + ) + # Do something with the value_list... + + """ + return types.ValueList( + value_reference_pairs=list(map( + value_reference_pair_from_pb, + that.value_reference_pairs + )) + ) + + +def lang_string_preferred_name_type_iec_61360_from_pb( + that: types_pb.LangStringPreferredNameTypeIec61360 +) -> types.LangStringPreferredNameTypeIEC61360: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_preferred_name_type_iec_61360_from_pb + + some_bytes = b'... serialized types_pb.LangStringPreferredNameTypeIec61360 ...' + lang_string_preferred_name_type_iec_61360_pb = types_pb.LangStringPreferredNameTypeIec61360() + lang_string_preferred_name_type_iec_61360_pb.FromString( + some_bytes + ) + + lang_string_preferred_name_type_iec_61360 = lang_string_preferred_name_type_iec_61360_from_pb( + lang_string_preferred_name_type_iec_61360_pb + ) + # Do something with the lang_string_preferred_name_type_iec_61360... + + """ + return types.LangStringPreferredNameTypeIEC61360( + language=that.language, + text=that.text + ) + + +def lang_string_short_name_type_iec_61360_from_pb( + that: types_pb.LangStringShortNameTypeIec61360 +) -> types.LangStringShortNameTypeIEC61360: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_short_name_type_iec_61360_from_pb + + some_bytes = b'... serialized types_pb.LangStringShortNameTypeIec61360 ...' + lang_string_short_name_type_iec_61360_pb = types_pb.LangStringShortNameTypeIec61360() + lang_string_short_name_type_iec_61360_pb.FromString( + some_bytes + ) + + lang_string_short_name_type_iec_61360 = lang_string_short_name_type_iec_61360_from_pb( + lang_string_short_name_type_iec_61360_pb + ) + # Do something with the lang_string_short_name_type_iec_61360... + + """ + return types.LangStringShortNameTypeIEC61360( + language=that.language, + text=that.text + ) + + +def lang_string_definition_type_iec_61360_from_pb( + that: types_pb.LangStringDefinitionTypeIec61360 +) -> types.LangStringDefinitionTypeIEC61360: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_definition_type_iec_61360_from_pb + + some_bytes = b'... serialized types_pb.LangStringDefinitionTypeIec61360 ...' + lang_string_definition_type_iec_61360_pb = types_pb.LangStringDefinitionTypeIec61360() + lang_string_definition_type_iec_61360_pb.FromString( + some_bytes + ) + + lang_string_definition_type_iec_61360 = lang_string_definition_type_iec_61360_from_pb( + lang_string_definition_type_iec_61360_pb + ) + # Do something with the lang_string_definition_type_iec_61360... + + """ + return types.LangStringDefinitionTypeIEC61360( + language=that.language, + text=that.text + ) + + +def data_specification_iec_61360_from_pb( + that: types_pb.DataSpecificationIec61360 +) -> types.DataSpecificationIEC61360: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import data_specification_iec_61360_from_pb + + some_bytes = b'... serialized types_pb.DataSpecificationIec61360 ...' + data_specification_iec_61360_pb = types_pb.DataSpecificationIec61360() + data_specification_iec_61360_pb.FromString( + some_bytes + ) + + data_specification_iec_61360 = data_specification_iec_61360_from_pb( + data_specification_iec_61360_pb + ) + # Do something with the data_specification_iec_61360... + + """ + return types.DataSpecificationIEC61360( + preferred_name=list(map( + lang_string_preferred_name_type_iec_61360_from_pb, + that.preferred_name + )), + short_name=( + list(map( + lang_string_short_name_type_iec_61360_from_pb, + that.short_name + )) + if len(that.short_name) > 0 + else None + ), + unit=( + that.unit + if that.HasField('unit') + else None + ), + unit_id=( + reference_from_pb( + that.unit_id + ) + if that.HasField('unit_id') + else None + ), + source_of_definition=( + that.source_of_definition + if that.HasField('source_of_definition') + else None + ), + symbol=( + that.symbol + if that.HasField('symbol') + else None + ), + data_type=( + data_type_iec_61360_from_pb( + that.data_type + ) + if that.HasField('data_type') + else None + ), + definition=( + list(map( + lang_string_definition_type_iec_61360_from_pb, + that.definition + )) + if len(that.definition) > 0 + else None + ), + value_format=( + that.value_format + if that.HasField('value_format') + else None + ), + value_list=( + value_list_from_pb( + that.value_list + ) + if that.HasField('value_list') + else None + ), + value=( + that.value + if that.HasField('value') + else None + ), + level_type=( + level_type_from_pb( + that.level_type + ) + if that.HasField('level_type') + else None + ) + ) + + +# fmt: off +_FROM_PB_MAP = { + types_pb.HasSemantics_choice: + has_semantics_from_pb_choice, + types_pb.Extension: + extension_from_pb, + types_pb.HasExtensions_choice: + has_extensions_from_pb_choice, + types_pb.Referable_choice: + referable_from_pb_choice, + types_pb.Identifiable_choice: + identifiable_from_pb_choice, + types_pb.HasKind_choice: + has_kind_from_pb_choice, + types_pb.HasDataSpecification_choice: + has_data_specification_from_pb_choice, + types_pb.AdministrativeInformation: + administrative_information_from_pb, + types_pb.Qualifiable_choice: + qualifiable_from_pb_choice, + types_pb.Qualifier: + qualifier_from_pb, + types_pb.AssetAdministrationShell: + asset_administration_shell_from_pb, + types_pb.AssetInformation: + asset_information_from_pb, + types_pb.Resource: + resource_from_pb, + types_pb.SpecificAssetId: + specific_asset_id_from_pb, + types_pb.Submodel: + submodel_from_pb, + types_pb.SubmodelElement_choice: + submodel_element_from_pb_choice, + types_pb.RelationshipElement_choice: + relationship_element_from_pb_choice, + types_pb.SubmodelElementList: + submodel_element_list_from_pb, + types_pb.SubmodelElementCollection: + submodel_element_collection_from_pb, + types_pb.DataElement_choice: + data_element_from_pb_choice, + types_pb.Property: + property_from_pb, + types_pb.MultiLanguageProperty: + multi_language_property_from_pb, + types_pb.Range: + range_from_pb, + types_pb.ReferenceElement: + reference_element_from_pb, + types_pb.Blob: + blob_from_pb, + types_pb.File: + file_from_pb, + types_pb.AnnotatedRelationshipElement: + annotated_relationship_element_from_pb, + types_pb.Entity: + entity_from_pb, + types_pb.EventPayload: + event_payload_from_pb, + types_pb.EventElement_choice: + event_element_from_pb_choice, + types_pb.BasicEventElement: + basic_event_element_from_pb, + types_pb.Operation: + operation_from_pb, + types_pb.OperationVariable: + operation_variable_from_pb, + types_pb.Capability: + capability_from_pb, + types_pb.ConceptDescription: + concept_description_from_pb, + types_pb.Reference: + reference_from_pb, + types_pb.Key: + key_from_pb, + types_pb.AbstractLangString_choice: + abstract_lang_string_from_pb_choice, + types_pb.LangStringNameType: + lang_string_name_type_from_pb, + types_pb.LangStringTextType: + lang_string_text_type_from_pb, + types_pb.Environment: + environment_from_pb, + types_pb.DataSpecificationContent_choice: + data_specification_content_from_pb_choice, + types_pb.EmbeddedDataSpecification: + embedded_data_specification_from_pb, + types_pb.LevelType: + level_type_from_pb, + types_pb.ValueReferencePair: + value_reference_pair_from_pb, + types_pb.ValueList: + value_list_from_pb, + types_pb.LangStringPreferredNameTypeIec61360: + lang_string_preferred_name_type_iec_61360_from_pb, + types_pb.LangStringShortNameTypeIec61360: + lang_string_short_name_type_iec_61360_from_pb, + types_pb.LangStringDefinitionTypeIec61360: + lang_string_definition_type_iec_61360_from_pb, + types_pb.DataSpecificationIec61360: + data_specification_iec_61360_from_pb +} +# fmt: on + + +def from_pb( + that: google.protobuf.message.Message +) -> types.Class: + """ + Parse ``that`` Protocol Buffer into a model instance. + + The concrete parsing is determined based on the runtime type of ``that`` + Protocol Buffer. It is assumed that ``that`` is an instance of a message + coming from the Protocol Buffer definitions corresponding to the meta-model. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import from_pb + + some_bytes = b'... serialized types_pb.Extension ...' + instance_pb = types_pb.Extension() + instance_pb.FromString( + some_bytes + ) + + instance = from_pb( + instance_pb + ) + # Do something with the instance... + """ + from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + + if from_pb_func is None: + raise ValueError( + f"We do not know how to parse the protocol buffer " + f"of type {that.__class__} into a model instance." + ) + + result = from_pb_func(that) # type: ignore + assert isinstance(result, types.Class) + return result + + +# endregion From Protocol Buffers + + +# region To Protocol Buffers + + +T = TypeVar("T") + + +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): + """ + Visit instances in context with double-dispatch. + + This class is meant to be inherited from. If you do not override a method, + it will raise an exception. This is a partial visitor, meaning that some + visits are unexpected by design. + """ + # pylint: disable=missing-docstring + + def visit_extension_with_context( + self, + that: types.Extension, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_administrative_information_with_context( + self, + that: types.AdministrativeInformation, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_qualifier_with_context( + self, + that: types.Qualifier, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_asset_administration_shell_with_context( + self, + that: types.AssetAdministrationShell, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_asset_information_with_context( + self, + that: types.AssetInformation, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_resource_with_context( + self, + that: types.Resource, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_specific_asset_id_with_context( + self, + that: types.SpecificAssetID, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_property_with_context( + self, + that: types.Property, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_range_with_context( + self, + that: types.Range, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_blob_with_context( + self, + that: types.Blob, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_file_with_context( + self, + that: types.File, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_entity_with_context( + self, + that: types.Entity, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_event_payload_with_context( + self, + that: types.EventPayload, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_operation_with_context( + self, + that: types.Operation, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_operation_variable_with_context( + self, + that: types.OperationVariable, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_capability_with_context( + self, + that: types.Capability, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_concept_description_with_context( + self, + that: types.ConceptDescription, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_reference_with_context( + self, + that: types.Reference, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_key_with_context( + self, + that: types.Key, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_lang_string_name_type_with_context( + self, + that: types.LangStringNameType, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_lang_string_text_type_with_context( + self, + that: types.LangStringTextType, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_environment_with_context( + self, + that: types.Environment, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_embedded_data_specification_with_context( + self, + that: types.EmbeddedDataSpecification, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_level_type_with_context( + self, + that: types.LevelType, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_value_reference_pair_with_context( + self, + that: types.ValueReferencePair, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_value_list_with_context( + self, + that: types.ValueList, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_lang_string_preferred_name_type_iec_61360_with_context( + self, + that: types.LangStringPreferredNameTypeIEC61360, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_lang_string_short_name_type_iec_61360_with_context( + self, + that: types.LangStringShortNameTypeIEC61360, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_lang_string_definition_type_iec_61360_with_context( + self, + that: types.LangStringDefinitionTypeIEC61360, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_data_specification_iec_61360_with_context( + self, + that: types.DataSpecificationIEC61360, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + +class _HasSemanticsToPbChoice( + _PartialVisitorWithContext[ + types_pb.HasSemantics_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_capability_with_context( + self, + that: types.Capability, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.capability`` + according to ``that`` instance. + """ + capability_to_pb( + that, + context.capability + ) + + def visit_entity_with_context( + self, + that: types.Entity, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.entity`` + according to ``that`` instance. + """ + entity_to_pb( + that, + context.entity + ) + + def visit_extension_with_context( + self, + that: types.Extension, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.extension`` + according to ``that`` instance. + """ + extension_to_pb( + that, + context.extension + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_operation_with_context( + self, + that: types.Operation, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.operation`` + according to ``that`` instance. + """ + operation_to_pb( + that, + context.operation + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_qualifier_with_context( + self, + that: types.Qualifier, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.qualifier`` + according to ``that`` instance. + """ + qualifier_to_pb( + that, + context.qualifier + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + def visit_specific_asset_id_with_context( + self, + that: types.SpecificAssetID, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.specific_asset_id`` + according to ``that`` instance. + """ + specific_asset_id_to_pb( + that, + context.specific_asset_id + ) + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_collection`` + according to ``that`` instance. + """ + submodel_element_collection_to_pb( + that, + context.submodel_element_collection + ) + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: types_pb.HasSemantics_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_list`` + according to ``that`` instance. + """ + submodel_element_list_to_pb( + that, + context.submodel_element_list + ) + + +_HAS_SEMANTICS_TO_PB_CHOICE = _HasSemanticsToPbChoice() + + +def has_semantics_to_pb_choice( + that: types.HasSemantics, + target: types_pb.HasSemantics_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_semantics_to_pb_choice + + has_semantics = types.RelationshipElement( + ... # some constructor arguments + ) + + has_semantics_choice_pb = types_pb.HasSemantics_choice() + has_semantics_to_pb_choice( + has_semantics, + has_semantics_choice_pb + ) + + some_bytes = has_semantics_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _HAS_SEMANTICS_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def extension_to_pb( + that: types.Extension, + target: types_pb.Extension +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import extension_to_pb + + extension = types.Extension( + ... # some constructor arguments + ) + + extension_pb = types_pb.Extension() + extension_to_pb( + extension, + extension_pb + ) + + some_bytes = extension_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + target.name = that.name + + if that.value_type is not None: + target.value_type = data_type_def_xsd_to_pb( + that.value_type + ) + + if that.value is not None: + target.value = that.value + + if that.refers_to is not None: + for refers_to_item in that.refers_to: + refers_to_item_pb = target.refers_to.add() + reference_to_pb( + refers_to_item, + refers_to_item_pb) + + +class _HasExtensionsToPbChoice( + _PartialVisitorWithContext[ + types_pb.HasExtensions_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + def visit_asset_administration_shell_with_context( + self, + that: types.AssetAdministrationShell, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.asset_administration_shell`` + according to ``that`` instance. + """ + asset_administration_shell_to_pb( + that, + context.asset_administration_shell + ) + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_capability_with_context( + self, + that: types.Capability, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.capability`` + according to ``that`` instance. + """ + capability_to_pb( + that, + context.capability + ) + + def visit_concept_description_with_context( + self, + that: types.ConceptDescription, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.concept_description`` + according to ``that`` instance. + """ + concept_description_to_pb( + that, + context.concept_description + ) + + def visit_entity_with_context( + self, + that: types.Entity, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.entity`` + according to ``that`` instance. + """ + entity_to_pb( + that, + context.entity + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_operation_with_context( + self, + that: types.Operation, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.operation`` + according to ``that`` instance. + """ + operation_to_pb( + that, + context.operation + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_collection`` + according to ``that`` instance. + """ + submodel_element_collection_to_pb( + that, + context.submodel_element_collection + ) + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: types_pb.HasExtensions_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_list`` + according to ``that`` instance. + """ + submodel_element_list_to_pb( + that, + context.submodel_element_list + ) + + +_HAS_EXTENSIONS_TO_PB_CHOICE = _HasExtensionsToPbChoice() + + +def has_extensions_to_pb_choice( + that: types.HasExtensions, + target: types_pb.HasExtensions_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_extensions_to_pb_choice + + has_extensions = types.RelationshipElement( + ... # some constructor arguments + ) + + has_extensions_choice_pb = types_pb.HasExtensions_choice() + has_extensions_to_pb_choice( + has_extensions, + has_extensions_choice_pb + ) + + some_bytes = has_extensions_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _HAS_EXTENSIONS_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +class _ReferableToPbChoice( + _PartialVisitorWithContext[ + types_pb.Referable_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + def visit_asset_administration_shell_with_context( + self, + that: types.AssetAdministrationShell, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.asset_administration_shell`` + according to ``that`` instance. + """ + asset_administration_shell_to_pb( + that, + context.asset_administration_shell + ) + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_capability_with_context( + self, + that: types.Capability, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.capability`` + according to ``that`` instance. + """ + capability_to_pb( + that, + context.capability + ) + + def visit_concept_description_with_context( + self, + that: types.ConceptDescription, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.concept_description`` + according to ``that`` instance. + """ + concept_description_to_pb( + that, + context.concept_description + ) + + def visit_entity_with_context( + self, + that: types.Entity, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.entity`` + according to ``that`` instance. + """ + entity_to_pb( + that, + context.entity + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_operation_with_context( + self, + that: types.Operation, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.operation`` + according to ``that`` instance. + """ + operation_to_pb( + that, + context.operation + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_collection`` + according to ``that`` instance. + """ + submodel_element_collection_to_pb( + that, + context.submodel_element_collection + ) + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: types_pb.Referable_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_list`` + according to ``that`` instance. + """ + submodel_element_list_to_pb( + that, + context.submodel_element_list + ) + + +_REFERABLE_TO_PB_CHOICE = _ReferableToPbChoice() + + +def referable_to_pb_choice( + that: types.Referable, + target: types_pb.Referable_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import referable_to_pb_choice + + referable = types.RelationshipElement( + ... # some constructor arguments + ) + + referable_choice_pb = types_pb.Referable_choice() + referable_to_pb_choice( + referable, + referable_choice_pb + ) + + some_bytes = referable_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _REFERABLE_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +class _IdentifiableToPbChoice( + _PartialVisitorWithContext[ + types_pb.Identifiable_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_asset_administration_shell_with_context( + self, + that: types.AssetAdministrationShell, + context: types_pb.Identifiable_choice + ) -> None: + """ + Set the fields of ``context.asset_administration_shell`` + according to ``that`` instance. + """ + asset_administration_shell_to_pb( + that, + context.asset_administration_shell + ) + + def visit_concept_description_with_context( + self, + that: types.ConceptDescription, + context: types_pb.Identifiable_choice + ) -> None: + """ + Set the fields of ``context.concept_description`` + according to ``that`` instance. + """ + concept_description_to_pb( + that, + context.concept_description + ) + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.Identifiable_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + +_IDENTIFIABLE_TO_PB_CHOICE = _IdentifiableToPbChoice() + + +def identifiable_to_pb_choice( + that: types.Identifiable, + target: types_pb.Identifiable_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import identifiable_to_pb_choice + + identifiable = types.AssetAdministrationShell( + ... # some constructor arguments + ) + + identifiable_choice_pb = types_pb.Identifiable_choice() + identifiable_to_pb_choice( + identifiable, + identifiable_choice_pb + ) + + some_bytes = identifiable_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _IDENTIFIABLE_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +# fmt: off +_MODELLING_KIND_TO_PB_MAP = { + types.ModellingKind.TEMPLATE: + types_pb.ModellingKind.Modellingkind_TEMPLATE, + types.ModellingKind.INSTANCE: + types_pb.ModellingKind.Modellingkind_INSTANCE +} # type: Mapping[types.ModellingKind, types_pb.ModellingKind] +# fmt: on + + +def modelling_kind_to_pb( + that: types.ModellingKind +) -> types_pb.ModellingKind: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import modelling_kind_to_pb + >>> import aas_core3.types as types + >>> modelling_kind_to_pb( + ... types.ModellingKind.TEMPLATE + ... ) + 1 + """ + return _MODELLING_KIND_TO_PB_MAP[that] + + +class _HasKindToPbChoice( + _PartialVisitorWithContext[ + types_pb.HasKind_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.HasKind_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + +_HAS_KIND_TO_PB_CHOICE = _HasKindToPbChoice() + + +def has_kind_to_pb_choice( + that: types.HasKind, + target: types_pb.HasKind_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_kind_to_pb_choice + + has_kind = types.Submodel( + ... # some constructor arguments + ) + + has_kind_choice_pb = types_pb.HasKind_choice() + has_kind_to_pb_choice( + has_kind, + has_kind_choice_pb + ) + + some_bytes = has_kind_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _HAS_KIND_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +class _HasDataSpecificationToPbChoice( + _PartialVisitorWithContext[ + types_pb.HasDataSpecification_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_administrative_information_with_context( + self, + that: types.AdministrativeInformation, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.administrative_information`` + according to ``that`` instance. + """ + administrative_information_to_pb( + that, + context.administrative_information + ) + + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + def visit_asset_administration_shell_with_context( + self, + that: types.AssetAdministrationShell, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.asset_administration_shell`` + according to ``that`` instance. + """ + asset_administration_shell_to_pb( + that, + context.asset_administration_shell + ) + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_capability_with_context( + self, + that: types.Capability, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.capability`` + according to ``that`` instance. + """ + capability_to_pb( + that, + context.capability + ) + + def visit_concept_description_with_context( + self, + that: types.ConceptDescription, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.concept_description`` + according to ``that`` instance. + """ + concept_description_to_pb( + that, + context.concept_description + ) + + def visit_entity_with_context( + self, + that: types.Entity, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.entity`` + according to ``that`` instance. + """ + entity_to_pb( + that, + context.entity + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_operation_with_context( + self, + that: types.Operation, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.operation`` + according to ``that`` instance. + """ + operation_to_pb( + that, + context.operation + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_collection`` + according to ``that`` instance. + """ + submodel_element_collection_to_pb( + that, + context.submodel_element_collection + ) + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: types_pb.HasDataSpecification_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_list`` + according to ``that`` instance. + """ + submodel_element_list_to_pb( + that, + context.submodel_element_list + ) + + +_HAS_DATA_SPECIFICATION_TO_PB_CHOICE = _HasDataSpecificationToPbChoice() + + +def has_data_specification_to_pb_choice( + that: types.HasDataSpecification, + target: types_pb.HasDataSpecification_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import has_data_specification_to_pb_choice + + has_data_specification = types.AdministrativeInformation( + ... # some constructor arguments + ) + + has_data_specification_choice_pb = types_pb.HasDataSpecification_choice() + has_data_specification_to_pb_choice( + has_data_specification, + has_data_specification_choice_pb + ) + + some_bytes = has_data_specification_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _HAS_DATA_SPECIFICATION_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def administrative_information_to_pb( + that: types.AdministrativeInformation, + target: types_pb.AdministrativeInformation +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import administrative_information_to_pb + + administrative_information = types.AdministrativeInformation( + ... # some constructor arguments + ) + + administrative_information_pb = types_pb.AdministrativeInformation() + administrative_information_to_pb( + administrative_information, + administrative_information_pb + ) + + some_bytes = administrative_information_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.version is not None: + target.version = that.version + + if that.revision is not None: + target.revision = that.revision + + if that.creator is not None: + # We clear so that the field is set even if all the properties are None. + target.creator.Clear() + + reference_to_pb( + that.creator, + target.creator + ) + + if that.template_id is not None: + target.template_id = that.template_id + + +class _QualifiableToPbChoice( + _PartialVisitorWithContext[ + types_pb.Qualifiable_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_capability_with_context( + self, + that: types.Capability, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.capability`` + according to ``that`` instance. + """ + capability_to_pb( + that, + context.capability + ) + + def visit_entity_with_context( + self, + that: types.Entity, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.entity`` + according to ``that`` instance. + """ + entity_to_pb( + that, + context.entity + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_operation_with_context( + self, + that: types.Operation, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.operation`` + according to ``that`` instance. + """ + operation_to_pb( + that, + context.operation + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + def visit_submodel_with_context( + self, + that: types.Submodel, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.submodel`` + according to ``that`` instance. + """ + submodel_to_pb( + that, + context.submodel + ) + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_collection`` + according to ``that`` instance. + """ + submodel_element_collection_to_pb( + that, + context.submodel_element_collection + ) + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: types_pb.Qualifiable_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_list`` + according to ``that`` instance. + """ + submodel_element_list_to_pb( + that, + context.submodel_element_list + ) + + +_QUALIFIABLE_TO_PB_CHOICE = _QualifiableToPbChoice() + + +def qualifiable_to_pb_choice( + that: types.Qualifiable, + target: types_pb.Qualifiable_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import qualifiable_to_pb_choice + + qualifiable = types.RelationshipElement( + ... # some constructor arguments + ) + + qualifiable_choice_pb = types_pb.Qualifiable_choice() + qualifiable_to_pb_choice( + qualifiable, + qualifiable_choice_pb + ) + + some_bytes = qualifiable_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _QUALIFIABLE_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +# fmt: off +_QUALIFIER_KIND_TO_PB_MAP = { + types.QualifierKind.VALUE_QUALIFIER: + types_pb.QualifierKind.Qualifierkind_VALUE_QUALIFIER, + types.QualifierKind.CONCEPT_QUALIFIER: + types_pb.QualifierKind.Qualifierkind_CONCEPT_QUALIFIER, + types.QualifierKind.TEMPLATE_QUALIFIER: + types_pb.QualifierKind.Qualifierkind_TEMPLATE_QUALIFIER +} # type: Mapping[types.QualifierKind, types_pb.QualifierKind] +# fmt: on + + +def qualifier_kind_to_pb( + that: types.QualifierKind +) -> types_pb.QualifierKind: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import qualifier_kind_to_pb + >>> import aas_core3.types as types + >>> qualifier_kind_to_pb( + ... types.QualifierKind.VALUE_QUALIFIER + ... ) + 1 + """ + return _QUALIFIER_KIND_TO_PB_MAP[that] + + +def qualifier_to_pb( + that: types.Qualifier, + target: types_pb.Qualifier +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import qualifier_to_pb + + qualifier = types.Qualifier( + ... # some constructor arguments + ) + + qualifier_pb = types_pb.Qualifier() + qualifier_to_pb( + qualifier, + qualifier_pb + ) + + some_bytes = qualifier_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.kind is not None: + target.kind = qualifier_kind_to_pb( + that.kind + ) + + target.type = that.type + + target.value_type = data_type_def_xsd_to_pb( + that.value_type + ) + + if that.value is not None: + target.value = that.value + + if that.value_id is not None: + # We clear so that the field is set even if all the properties are None. + target.value_id.Clear() + + reference_to_pb( + that.value_id, + target.value_id + ) + + +def asset_administration_shell_to_pb( + that: types.AssetAdministrationShell, + target: types_pb.AssetAdministrationShell +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import asset_administration_shell_to_pb + + asset_administration_shell = types.AssetAdministrationShell( + ... # some constructor arguments + ) + + asset_administration_shell_pb = types_pb.AssetAdministrationShell() + asset_administration_shell_to_pb( + asset_administration_shell, + asset_administration_shell_pb + ) + + some_bytes = asset_administration_shell_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.administration is not None: + # We clear so that the field is set even if all the properties are None. + target.administration.Clear() + + administrative_information_to_pb( + that.administration, + target.administration + ) + + target.id = that.id + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.derived_from is not None: + # We clear so that the field is set even if all the properties are None. + target.derived_from.Clear() + + reference_to_pb( + that.derived_from, + target.derived_from + ) + + # We clear so that the field is set even if all the properties are None. + target.asset_information.Clear() + + asset_information_to_pb( + that.asset_information, + target.asset_information + ) + + if that.submodels is not None: + for submodels_item in that.submodels: + submodels_item_pb = target.submodels.add() + reference_to_pb( + submodels_item, + submodels_item_pb) + + +def asset_information_to_pb( + that: types.AssetInformation, + target: types_pb.AssetInformation +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import asset_information_to_pb + + asset_information = types.AssetInformation( + ... # some constructor arguments + ) + + asset_information_pb = types_pb.AssetInformation() + asset_information_to_pb( + asset_information, + asset_information_pb + ) + + some_bytes = asset_information_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.asset_kind = asset_kind_to_pb( + that.asset_kind + ) + + if that.global_asset_id is not None: + target.global_asset_id = that.global_asset_id + + if that.specific_asset_ids is not None: + for specific_asset_ids_item in that.specific_asset_ids: + specific_asset_ids_item_pb = target.specific_asset_ids.add() + specific_asset_id_to_pb( + specific_asset_ids_item, + specific_asset_ids_item_pb) + + if that.asset_type is not None: + target.asset_type = that.asset_type + + if that.default_thumbnail is not None: + # We clear so that the field is set even if all the properties are None. + target.default_thumbnail.Clear() + + resource_to_pb( + that.default_thumbnail, + target.default_thumbnail + ) + + +def resource_to_pb( + that: types.Resource, + target: types_pb.Resource +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import resource_to_pb + + resource = types.Resource( + ... # some constructor arguments + ) + + resource_pb = types_pb.Resource() + resource_to_pb( + resource, + resource_pb + ) + + some_bytes = resource_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.path = that.path + + if that.content_type is not None: + target.content_type = that.content_type + + +# fmt: off +_ASSET_KIND_TO_PB_MAP = { + types.AssetKind.TYPE: + types_pb.AssetKind.Assetkind_TYPE, + types.AssetKind.INSTANCE: + types_pb.AssetKind.Assetkind_INSTANCE, + types.AssetKind.NOT_APPLICABLE: + types_pb.AssetKind.Assetkind_NOT_APPLICABLE +} # type: Mapping[types.AssetKind, types_pb.AssetKind] +# fmt: on + + +def asset_kind_to_pb( + that: types.AssetKind +) -> types_pb.AssetKind: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import asset_kind_to_pb + >>> import aas_core3.types as types + >>> asset_kind_to_pb( + ... types.AssetKind.TYPE + ... ) + 1 + """ + return _ASSET_KIND_TO_PB_MAP[that] + + +def specific_asset_id_to_pb( + that: types.SpecificAssetID, + target: types_pb.SpecificAssetId +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import specific_asset_id_to_pb + + specific_asset_id = types.SpecificAssetID( + ... # some constructor arguments + ) + + specific_asset_id_pb = types_pb.SpecificAssetId() + specific_asset_id_to_pb( + specific_asset_id, + specific_asset_id_pb + ) + + some_bytes = specific_asset_id_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + target.name = that.name + + target.value = that.value + + if that.external_subject_id is not None: + # We clear so that the field is set even if all the properties are None. + target.external_subject_id.Clear() + + reference_to_pb( + that.external_subject_id, + target.external_subject_id + ) + + +def submodel_to_pb( + that: types.Submodel, + target: types_pb.Submodel +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_to_pb + + submodel = types.Submodel( + ... # some constructor arguments + ) + + submodel_pb = types_pb.Submodel() + submodel_to_pb( + submodel, + submodel_pb + ) + + some_bytes = submodel_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.administration is not None: + # We clear so that the field is set even if all the properties are None. + target.administration.Clear() + + administrative_information_to_pb( + that.administration, + target.administration + ) + + target.id = that.id + + if that.kind is not None: + target.kind = modelling_kind_to_pb( + that.kind + ) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.submodel_elements is not None: + for submodel_elements_item in that.submodel_elements: + submodel_elements_item_pb = target.submodel_elements.add() + submodel_element_to_pb_choice( + submodel_elements_item, + submodel_elements_item_pb) + + +class _SubmodelElementToPbChoice( + _PartialVisitorWithContext[ + types_pb.SubmodelElement_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_capability_with_context( + self, + that: types.Capability, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.capability`` + according to ``that`` instance. + """ + capability_to_pb( + that, + context.capability + ) + + def visit_entity_with_context( + self, + that: types.Entity, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.entity`` + according to ``that`` instance. + """ + entity_to_pb( + that, + context.entity + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_operation_with_context( + self, + that: types.Operation, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.operation`` + according to ``that`` instance. + """ + operation_to_pb( + that, + context.operation + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + def visit_submodel_element_collection_with_context( + self, + that: types.SubmodelElementCollection, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_collection`` + according to ``that`` instance. + """ + submodel_element_collection_to_pb( + that, + context.submodel_element_collection + ) + + def visit_submodel_element_list_with_context( + self, + that: types.SubmodelElementList, + context: types_pb.SubmodelElement_choice + ) -> None: + """ + Set the fields of ``context.submodel_element_list`` + according to ``that`` instance. + """ + submodel_element_list_to_pb( + that, + context.submodel_element_list + ) + + +_SUBMODEL_ELEMENT_TO_PB_CHOICE = _SubmodelElementToPbChoice() + + +def submodel_element_to_pb_choice( + that: types.SubmodelElement, + target: types_pb.SubmodelElement_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_element_to_pb_choice + + submodel_element = types.RelationshipElement( + ... # some constructor arguments + ) + + submodel_element_choice_pb = types_pb.SubmodelElement_choice() + submodel_element_to_pb_choice( + submodel_element, + submodel_element_choice_pb + ) + + some_bytes = submodel_element_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _SUBMODEL_ELEMENT_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +class _RelationshipElementToPbChoice( + _PartialVisitorWithContext[ + types_pb.RelationshipElement_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_relationship_element_with_context( + self, + that: types.RelationshipElement, + context: types_pb.RelationshipElement_choice + ) -> None: + """ + Set the fields of ``context.relationship_element`` + according to ``that`` instance. + """ + relationship_element_to_pb( + that, + context.relationship_element + ) + + def visit_annotated_relationship_element_with_context( + self, + that: types.AnnotatedRelationshipElement, + context: types_pb.RelationshipElement_choice + ) -> None: + """ + Set the fields of ``context.annotated_relationship_element`` + according to ``that`` instance. + """ + annotated_relationship_element_to_pb( + that, + context.annotated_relationship_element + ) + + +_RELATIONSHIP_ELEMENT_TO_PB_CHOICE = _RelationshipElementToPbChoice() + + +def relationship_element_to_pb_choice( + that: types.RelationshipElement, + target: types_pb.RelationshipElement_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import relationship_element_to_pb_choice + + relationship_element = types.AnnotatedRelationshipElement( + ... # some constructor arguments + ) + + relationship_element_choice_pb = types_pb.RelationshipElement_choice() + relationship_element_to_pb_choice( + relationship_element, + relationship_element_choice_pb + ) + + some_bytes = relationship_element_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _RELATIONSHIP_ELEMENT_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def relationship_element_to_pb( + that: types.RelationshipElement, + target: types_pb.RelationshipElement +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import relationship_element_to_pb + + relationship_element = types.RelationshipElement( + ... # some constructor arguments + ) + + relationship_element_pb = types_pb.RelationshipElement() + relationship_element_to_pb( + relationship_element, + relationship_element_pb + ) + + some_bytes = relationship_element_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + # We clear so that the field is set even if all the properties are None. + target.first.Clear() + + reference_to_pb( + that.first, + target.first + ) + + # We clear so that the field is set even if all the properties are None. + target.second.Clear() + + reference_to_pb( + that.second, + target.second + ) + + +# fmt: off +_AAS_SUBMODEL_ELEMENTS_TO_PB_MAP = { + types.AASSubmodelElements.ANNOTATED_RELATIONSHIP_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_ANNOTATED_RELATIONSHIP_ELEMENT, + types.AASSubmodelElements.BASIC_EVENT_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_BASIC_EVENT_ELEMENT, + types.AASSubmodelElements.BLOB: + types_pb.AasSubmodelElements.Aassubmodelelements_BLOB, + types.AASSubmodelElements.CAPABILITY: + types_pb.AasSubmodelElements.Aassubmodelelements_CAPABILITY, + types.AASSubmodelElements.DATA_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_DATA_ELEMENT, + types.AASSubmodelElements.ENTITY: + types_pb.AasSubmodelElements.Aassubmodelelements_ENTITY, + types.AASSubmodelElements.EVENT_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_EVENT_ELEMENT, + types.AASSubmodelElements.FILE: + types_pb.AasSubmodelElements.Aassubmodelelements_FILE, + types.AASSubmodelElements.MULTI_LANGUAGE_PROPERTY: + types_pb.AasSubmodelElements.Aassubmodelelements_MULTI_LANGUAGE_PROPERTY, + types.AASSubmodelElements.OPERATION: + types_pb.AasSubmodelElements.Aassubmodelelements_OPERATION, + types.AASSubmodelElements.PROPERTY: + types_pb.AasSubmodelElements.Aassubmodelelements_PROPERTY, + types.AASSubmodelElements.RANGE: + types_pb.AasSubmodelElements.Aassubmodelelements_RANGE, + types.AASSubmodelElements.REFERENCE_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_REFERENCE_ELEMENT, + types.AASSubmodelElements.RELATIONSHIP_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_RELATIONSHIP_ELEMENT, + types.AASSubmodelElements.SUBMODEL_ELEMENT: + types_pb.AasSubmodelElements.Aassubmodelelements_SUBMODEL_ELEMENT, + types.AASSubmodelElements.SUBMODEL_ELEMENT_LIST: + types_pb.AasSubmodelElements.Aassubmodelelements_SUBMODEL_ELEMENT_LIST, + types.AASSubmodelElements.SUBMODEL_ELEMENT_COLLECTION: + types_pb.AasSubmodelElements.Aassubmodelelements_SUBMODEL_ELEMENT_COLLECTION +} # type: Mapping[types.AASSubmodelElements, types_pb.AasSubmodelElements] +# fmt: on + + +def aas_submodel_elements_to_pb( + that: types.AASSubmodelElements +) -> types_pb.AasSubmodelElements: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import aas_submodel_elements_to_pb + >>> import aas_core3.types as types + >>> aas_submodel_elements_to_pb( + ... types.AASSubmodelElements.ANNOTATED_RELATIONSHIP_ELEMENT + ... ) + 1 + """ + return _AAS_SUBMODEL_ELEMENTS_TO_PB_MAP[that] + + +def submodel_element_list_to_pb( + that: types.SubmodelElementList, + target: types_pb.SubmodelElementList +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_element_list_to_pb + + submodel_element_list = types.SubmodelElementList( + ... # some constructor arguments + ) + + submodel_element_list_pb = types_pb.SubmodelElementList() + submodel_element_list_to_pb( + submodel_element_list, + submodel_element_list_pb + ) + + some_bytes = submodel_element_list_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.order_relevant is not None: + target.order_relevant = that.order_relevant + + if that.semantic_id_list_element is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id_list_element.Clear() + + reference_to_pb( + that.semantic_id_list_element, + target.semantic_id_list_element + ) + + target.type_value_list_element = aas_submodel_elements_to_pb( + that.type_value_list_element + ) + + if that.value_type_list_element is not None: + target.value_type_list_element = data_type_def_xsd_to_pb( + that.value_type_list_element + ) + + if that.value is not None: + for value_item in that.value: + value_item_pb = target.value.add() + submodel_element_to_pb_choice( + value_item, + value_item_pb) + + +def submodel_element_collection_to_pb( + that: types.SubmodelElementCollection, + target: types_pb.SubmodelElementCollection +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import submodel_element_collection_to_pb + + submodel_element_collection = types.SubmodelElementCollection( + ... # some constructor arguments + ) + + submodel_element_collection_pb = types_pb.SubmodelElementCollection() + submodel_element_collection_to_pb( + submodel_element_collection, + submodel_element_collection_pb + ) + + some_bytes = submodel_element_collection_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.value is not None: + for value_item in that.value: + value_item_pb = target.value.add() + submodel_element_to_pb_choice( + value_item, + value_item_pb) + + +class _DataElementToPbChoice( + _PartialVisitorWithContext[ + types_pb.DataElement_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_blob_with_context( + self, + that: types.Blob, + context: types_pb.DataElement_choice + ) -> None: + """ + Set the fields of ``context.blob`` + according to ``that`` instance. + """ + blob_to_pb( + that, + context.blob + ) + + def visit_file_with_context( + self, + that: types.File, + context: types_pb.DataElement_choice + ) -> None: + """ + Set the fields of ``context.file`` + according to ``that`` instance. + """ + file_to_pb( + that, + context.file + ) + + def visit_multi_language_property_with_context( + self, + that: types.MultiLanguageProperty, + context: types_pb.DataElement_choice + ) -> None: + """ + Set the fields of ``context.multi_language_property`` + according to ``that`` instance. + """ + multi_language_property_to_pb( + that, + context.multi_language_property + ) + + def visit_property_with_context( + self, + that: types.Property, + context: types_pb.DataElement_choice + ) -> None: + """ + Set the fields of ``context.property`` + according to ``that`` instance. + """ + property_to_pb( + that, + context.property + ) + + def visit_range_with_context( + self, + that: types.Range, + context: types_pb.DataElement_choice + ) -> None: + """ + Set the fields of ``context.range`` + according to ``that`` instance. + """ + range_to_pb( + that, + context.range + ) + + def visit_reference_element_with_context( + self, + that: types.ReferenceElement, + context: types_pb.DataElement_choice + ) -> None: + """ + Set the fields of ``context.reference_element`` + according to ``that`` instance. + """ + reference_element_to_pb( + that, + context.reference_element + ) + + +_DATA_ELEMENT_TO_PB_CHOICE = _DataElementToPbChoice() + + +def data_element_to_pb_choice( + that: types.DataElement, + target: types_pb.DataElement_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import data_element_to_pb_choice + + data_element = types.Blob( + ... # some constructor arguments + ) + + data_element_choice_pb = types_pb.DataElement_choice() + data_element_to_pb_choice( + data_element, + data_element_choice_pb + ) + + some_bytes = data_element_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _DATA_ELEMENT_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def property_to_pb( + that: types.Property, + target: types_pb.Property +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import property_to_pb + + property = types.Property( + ... # some constructor arguments + ) + + property_pb = types_pb.Property() + property_to_pb( + property, + property_pb + ) + + some_bytes = property_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + target.value_type = data_type_def_xsd_to_pb( + that.value_type + ) + + if that.value is not None: + target.value = that.value + + if that.value_id is not None: + # We clear so that the field is set even if all the properties are None. + target.value_id.Clear() + + reference_to_pb( + that.value_id, + target.value_id + ) + + +def multi_language_property_to_pb( + that: types.MultiLanguageProperty, + target: types_pb.MultiLanguageProperty +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import multi_language_property_to_pb + + multi_language_property = types.MultiLanguageProperty( + ... # some constructor arguments + ) + + multi_language_property_pb = types_pb.MultiLanguageProperty() + multi_language_property_to_pb( + multi_language_property, + multi_language_property_pb + ) + + some_bytes = multi_language_property_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.value is not None: + for value_item in that.value: + value_item_pb = target.value.add() + lang_string_text_type_to_pb( + value_item, + value_item_pb) + + if that.value_id is not None: + # We clear so that the field is set even if all the properties are None. + target.value_id.Clear() + + reference_to_pb( + that.value_id, + target.value_id + ) + + +def range_to_pb( + that: types.Range, + target: types_pb.Range +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import range_to_pb + + range = types.Range( + ... # some constructor arguments + ) + + range_pb = types_pb.Range() + range_to_pb( + range, + range_pb + ) + + some_bytes = range_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + target.value_type = data_type_def_xsd_to_pb( + that.value_type + ) + + if that.min is not None: + target.min = that.min + + if that.max is not None: + target.max = that.max + + +def reference_element_to_pb( + that: types.ReferenceElement, + target: types_pb.ReferenceElement +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import reference_element_to_pb + + reference_element = types.ReferenceElement( + ... # some constructor arguments + ) + + reference_element_pb = types_pb.ReferenceElement() + reference_element_to_pb( + reference_element, + reference_element_pb + ) + + some_bytes = reference_element_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.value is not None: + # We clear so that the field is set even if all the properties are None. + target.value.Clear() + + reference_to_pb( + that.value, + target.value + ) + + +def blob_to_pb( + that: types.Blob, + target: types_pb.Blob +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import blob_to_pb + + blob = types.Blob( + ... # some constructor arguments + ) + + blob_pb = types_pb.Blob() + blob_to_pb( + blob, + blob_pb + ) + + some_bytes = blob_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.value is not None: + target.value = bytes(that.value) + + target.content_type = that.content_type + + +def file_to_pb( + that: types.File, + target: types_pb.File +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import file_to_pb + + file = types.File( + ... # some constructor arguments + ) + + file_pb = types_pb.File() + file_to_pb( + file, + file_pb + ) + + some_bytes = file_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.value is not None: + target.value = that.value + + target.content_type = that.content_type + + +def annotated_relationship_element_to_pb( + that: types.AnnotatedRelationshipElement, + target: types_pb.AnnotatedRelationshipElement +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import annotated_relationship_element_to_pb + + annotated_relationship_element = types.AnnotatedRelationshipElement( + ... # some constructor arguments + ) + + annotated_relationship_element_pb = types_pb.AnnotatedRelationshipElement() + annotated_relationship_element_to_pb( + annotated_relationship_element, + annotated_relationship_element_pb + ) + + some_bytes = annotated_relationship_element_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + # We clear so that the field is set even if all the properties are None. + target.first.Clear() + + reference_to_pb( + that.first, + target.first + ) + + # We clear so that the field is set even if all the properties are None. + target.second.Clear() + + reference_to_pb( + that.second, + target.second + ) + + if that.annotations is not None: + for annotations_item in that.annotations: + annotations_item_pb = target.annotations.add() + data_element_to_pb_choice( + annotations_item, + annotations_item_pb) + + +def entity_to_pb( + that: types.Entity, + target: types_pb.Entity +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import entity_to_pb + + entity = types.Entity( + ... # some constructor arguments + ) + + entity_pb = types_pb.Entity() + entity_to_pb( + entity, + entity_pb + ) + + some_bytes = entity_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.statements is not None: + for statements_item in that.statements: + statements_item_pb = target.statements.add() + submodel_element_to_pb_choice( + statements_item, + statements_item_pb) + + target.entity_type = entity_type_to_pb( + that.entity_type + ) + + if that.global_asset_id is not None: + target.global_asset_id = that.global_asset_id + + if that.specific_asset_ids is not None: + for specific_asset_ids_item in that.specific_asset_ids: + specific_asset_ids_item_pb = target.specific_asset_ids.add() + specific_asset_id_to_pb( + specific_asset_ids_item, + specific_asset_ids_item_pb) + + +# fmt: off +_ENTITY_TYPE_TO_PB_MAP = { + types.EntityType.CO_MANAGED_ENTITY: + types_pb.EntityType.Entitytype_CO_MANAGED_ENTITY, + types.EntityType.SELF_MANAGED_ENTITY: + types_pb.EntityType.Entitytype_SELF_MANAGED_ENTITY +} # type: Mapping[types.EntityType, types_pb.EntityType] +# fmt: on + + +def entity_type_to_pb( + that: types.EntityType +) -> types_pb.EntityType: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import entity_type_to_pb + >>> import aas_core3.types as types + >>> entity_type_to_pb( + ... types.EntityType.CO_MANAGED_ENTITY + ... ) + 1 + """ + return _ENTITY_TYPE_TO_PB_MAP[that] + + +# fmt: off +_DIRECTION_TO_PB_MAP = { + types.Direction.INPUT: + types_pb.Direction.Direction_INPUT, + types.Direction.OUTPUT: + types_pb.Direction.Direction_OUTPUT +} # type: Mapping[types.Direction, types_pb.Direction] +# fmt: on + + +def direction_to_pb( + that: types.Direction +) -> types_pb.Direction: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import direction_to_pb + >>> import aas_core3.types as types + >>> direction_to_pb( + ... types.Direction.INPUT + ... ) + 1 + """ + return _DIRECTION_TO_PB_MAP[that] + + +# fmt: off +_STATE_OF_EVENT_TO_PB_MAP = { + types.StateOfEvent.ON: + types_pb.StateOfEvent.Stateofevent_ON, + types.StateOfEvent.OFF: + types_pb.StateOfEvent.Stateofevent_OFF +} # type: Mapping[types.StateOfEvent, types_pb.StateOfEvent] +# fmt: on + + +def state_of_event_to_pb( + that: types.StateOfEvent +) -> types_pb.StateOfEvent: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import state_of_event_to_pb + >>> import aas_core3.types as types + >>> state_of_event_to_pb( + ... types.StateOfEvent.ON + ... ) + 1 + """ + return _STATE_OF_EVENT_TO_PB_MAP[that] + + +def event_payload_to_pb( + that: types.EventPayload, + target: types_pb.EventPayload +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import event_payload_to_pb + + event_payload = types.EventPayload( + ... # some constructor arguments + ) + + event_payload_pb = types_pb.EventPayload() + event_payload_to_pb( + event_payload, + event_payload_pb + ) + + some_bytes = event_payload_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + # We clear so that the field is set even if all the properties are None. + target.source.Clear() + + reference_to_pb( + that.source, + target.source + ) + + if that.source_semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.source_semantic_id.Clear() + + reference_to_pb( + that.source_semantic_id, + target.source_semantic_id + ) + + # We clear so that the field is set even if all the properties are None. + target.observable_reference.Clear() + + reference_to_pb( + that.observable_reference, + target.observable_reference + ) + + if that.observable_semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.observable_semantic_id.Clear() + + reference_to_pb( + that.observable_semantic_id, + target.observable_semantic_id + ) + + if that.topic is not None: + target.topic = that.topic + + if that.subject_id is not None: + # We clear so that the field is set even if all the properties are None. + target.subject_id.Clear() + + reference_to_pb( + that.subject_id, + target.subject_id + ) + + target.time_stamp = that.time_stamp + + if that.payload is not None: + target.payload = bytes(that.payload) + + +class _EventElementToPbChoice( + _PartialVisitorWithContext[ + types_pb.EventElement_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_basic_event_element_with_context( + self, + that: types.BasicEventElement, + context: types_pb.EventElement_choice + ) -> None: + """ + Set the fields of ``context.basic_event_element`` + according to ``that`` instance. + """ + basic_event_element_to_pb( + that, + context.basic_event_element + ) + + +_EVENT_ELEMENT_TO_PB_CHOICE = _EventElementToPbChoice() + + +def event_element_to_pb_choice( + that: types.EventElement, + target: types_pb.EventElement_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import event_element_to_pb_choice + + event_element = types.BasicEventElement( + ... # some constructor arguments + ) + + event_element_choice_pb = types_pb.EventElement_choice() + event_element_to_pb_choice( + event_element, + event_element_choice_pb + ) + + some_bytes = event_element_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _EVENT_ELEMENT_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def basic_event_element_to_pb( + that: types.BasicEventElement, + target: types_pb.BasicEventElement +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import basic_event_element_to_pb + + basic_event_element = types.BasicEventElement( + ... # some constructor arguments + ) + + basic_event_element_pb = types_pb.BasicEventElement() + basic_event_element_to_pb( + basic_event_element, + basic_event_element_pb + ) + + some_bytes = basic_event_element_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + # We clear so that the field is set even if all the properties are None. + target.observed.Clear() + + reference_to_pb( + that.observed, + target.observed + ) + + target.direction = direction_to_pb( + that.direction + ) + + target.state = state_of_event_to_pb( + that.state + ) + + if that.message_topic is not None: + target.message_topic = that.message_topic + + if that.message_broker is not None: + # We clear so that the field is set even if all the properties are None. + target.message_broker.Clear() + + reference_to_pb( + that.message_broker, + target.message_broker + ) + + if that.last_update is not None: + target.last_update = that.last_update + + if that.min_interval is not None: + target.min_interval = that.min_interval + + if that.max_interval is not None: + target.max_interval = that.max_interval + + +def operation_to_pb( + that: types.Operation, + target: types_pb.Operation +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import operation_to_pb + + operation = types.Operation( + ... # some constructor arguments + ) + + operation_pb = types_pb.Operation() + operation_to_pb( + operation, + operation_pb + ) + + some_bytes = operation_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.input_variables is not None: + for input_variables_item in that.input_variables: + input_variables_item_pb = target.input_variables.add() + operation_variable_to_pb( + input_variables_item, + input_variables_item_pb) + + if that.output_variables is not None: + for output_variables_item in that.output_variables: + output_variables_item_pb = target.output_variables.add() + operation_variable_to_pb( + output_variables_item, + output_variables_item_pb) + + if that.inoutput_variables is not None: + for inoutput_variables_item in that.inoutput_variables: + inoutput_variables_item_pb = target.inoutput_variables.add() + operation_variable_to_pb( + inoutput_variables_item, + inoutput_variables_item_pb) + + +def operation_variable_to_pb( + that: types.OperationVariable, + target: types_pb.OperationVariable +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import operation_variable_to_pb + + operation_variable = types.OperationVariable( + ... # some constructor arguments + ) + + operation_variable_pb = types_pb.OperationVariable() + operation_variable_to_pb( + operation_variable, + operation_variable_pb + ) + + some_bytes = operation_variable_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + submodel_element_to_pb_choice( + that.value, + target.value + ) + + +def capability_to_pb( + that: types.Capability, + target: types_pb.Capability +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import capability_to_pb + + capability = types.Capability( + ... # some constructor arguments + ) + + capability_pb = types_pb.Capability() + capability_to_pb( + capability, + capability_pb + ) + + some_bytes = capability_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.semantic_id.Clear() + + reference_to_pb( + that.semantic_id, + target.semantic_id + ) + + if that.supplemental_semantic_ids is not None: + for supplemental_semantic_ids_item in that.supplemental_semantic_ids: + supplemental_semantic_ids_item_pb = target.supplemental_semantic_ids.add() + reference_to_pb( + supplemental_semantic_ids_item, + supplemental_semantic_ids_item_pb) + + if that.qualifiers is not None: + for qualifiers_item in that.qualifiers: + qualifiers_item_pb = target.qualifiers.add() + qualifier_to_pb( + qualifiers_item, + qualifiers_item_pb) + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + +def concept_description_to_pb( + that: types.ConceptDescription, + target: types_pb.ConceptDescription +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import concept_description_to_pb + + concept_description = types.ConceptDescription( + ... # some constructor arguments + ) + + concept_description_pb = types_pb.ConceptDescription() + concept_description_to_pb( + concept_description, + concept_description_pb + ) + + some_bytes = concept_description_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.extensions is not None: + for extensions_item in that.extensions: + extensions_item_pb = target.extensions.add() + extension_to_pb( + extensions_item, + extensions_item_pb) + + if that.category is not None: + target.category = that.category + + if that.id_short is not None: + target.id_short = that.id_short + + if that.display_name is not None: + for display_name_item in that.display_name: + display_name_item_pb = target.display_name.add() + lang_string_name_type_to_pb( + display_name_item, + display_name_item_pb) + + if that.description is not None: + for description_item in that.description: + description_item_pb = target.description.add() + lang_string_text_type_to_pb( + description_item, + description_item_pb) + + if that.administration is not None: + # We clear so that the field is set even if all the properties are None. + target.administration.Clear() + + administrative_information_to_pb( + that.administration, + target.administration + ) + + target.id = that.id + + if that.embedded_data_specifications is not None: + for embedded_data_specifications_item in that.embedded_data_specifications: + embedded_data_specifications_item_pb = target.embedded_data_specifications.add() + embedded_data_specification_to_pb( + embedded_data_specifications_item, + embedded_data_specifications_item_pb) + + if that.is_case_of is not None: + for is_case_of_item in that.is_case_of: + is_case_of_item_pb = target.is_case_of.add() + reference_to_pb( + is_case_of_item, + is_case_of_item_pb) + + +# fmt: off +_REFERENCE_TYPES_TO_PB_MAP = { + types.ReferenceTypes.EXTERNAL_REFERENCE: + types_pb.ReferenceTypes.Referencetypes_EXTERNAL_REFERENCE, + types.ReferenceTypes.MODEL_REFERENCE: + types_pb.ReferenceTypes.Referencetypes_MODEL_REFERENCE +} # type: Mapping[types.ReferenceTypes, types_pb.ReferenceTypes] +# fmt: on + + +def reference_types_to_pb( + that: types.ReferenceTypes +) -> types_pb.ReferenceTypes: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import reference_types_to_pb + >>> import aas_core3.types as types + >>> reference_types_to_pb( + ... types.ReferenceTypes.EXTERNAL_REFERENCE + ... ) + 1 + """ + return _REFERENCE_TYPES_TO_PB_MAP[that] + + +def reference_to_pb( + that: types.Reference, + target: types_pb.Reference +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import reference_to_pb + + reference = types.Reference( + ... # some constructor arguments + ) + + reference_pb = types_pb.Reference() + reference_to_pb( + reference, + reference_pb + ) + + some_bytes = reference_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.type = reference_types_to_pb( + that.type + ) + + if that.referred_semantic_id is not None: + # We clear so that the field is set even if all the properties are None. + target.referred_semantic_id.Clear() + + reference_to_pb( + that.referred_semantic_id, + target.referred_semantic_id + ) + + for keys_item in that.keys: + keys_item_pb = target.keys.add() + key_to_pb( + keys_item, + keys_item_pb) + + +def key_to_pb( + that: types.Key, + target: types_pb.Key +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import key_to_pb + + key = types.Key( + ... # some constructor arguments + ) + + key_pb = types_pb.Key() + key_to_pb( + key, + key_pb + ) + + some_bytes = key_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.type = key_types_to_pb( + that.type + ) + + target.value = that.value + + +# fmt: off +_KEY_TYPES_TO_PB_MAP = { + types.KeyTypes.ANNOTATED_RELATIONSHIP_ELEMENT: + types_pb.KeyTypes.Keytypes_ANNOTATED_RELATIONSHIP_ELEMENT, + types.KeyTypes.ASSET_ADMINISTRATION_SHELL: + types_pb.KeyTypes.Keytypes_ASSET_ADMINISTRATION_SHELL, + types.KeyTypes.BASIC_EVENT_ELEMENT: + types_pb.KeyTypes.Keytypes_BASIC_EVENT_ELEMENT, + types.KeyTypes.BLOB: + types_pb.KeyTypes.Keytypes_BLOB, + types.KeyTypes.CAPABILITY: + types_pb.KeyTypes.Keytypes_CAPABILITY, + types.KeyTypes.CONCEPT_DESCRIPTION: + types_pb.KeyTypes.Keytypes_CONCEPT_DESCRIPTION, + types.KeyTypes.DATA_ELEMENT: + types_pb.KeyTypes.Keytypes_DATA_ELEMENT, + types.KeyTypes.ENTITY: + types_pb.KeyTypes.Keytypes_ENTITY, + types.KeyTypes.EVENT_ELEMENT: + types_pb.KeyTypes.Keytypes_EVENT_ELEMENT, + types.KeyTypes.FILE: + types_pb.KeyTypes.Keytypes_FILE, + types.KeyTypes.FRAGMENT_REFERENCE: + types_pb.KeyTypes.Keytypes_FRAGMENT_REFERENCE, + types.KeyTypes.GLOBAL_REFERENCE: + types_pb.KeyTypes.Keytypes_GLOBAL_REFERENCE, + types.KeyTypes.IDENTIFIABLE: + types_pb.KeyTypes.Keytypes_IDENTIFIABLE, + types.KeyTypes.MULTI_LANGUAGE_PROPERTY: + types_pb.KeyTypes.Keytypes_MULTI_LANGUAGE_PROPERTY, + types.KeyTypes.OPERATION: + types_pb.KeyTypes.Keytypes_OPERATION, + types.KeyTypes.PROPERTY: + types_pb.KeyTypes.Keytypes_PROPERTY, + types.KeyTypes.RANGE: + types_pb.KeyTypes.Keytypes_RANGE, + types.KeyTypes.REFERABLE: + types_pb.KeyTypes.Keytypes_REFERABLE, + types.KeyTypes.REFERENCE_ELEMENT: + types_pb.KeyTypes.Keytypes_REFERENCE_ELEMENT, + types.KeyTypes.RELATIONSHIP_ELEMENT: + types_pb.KeyTypes.Keytypes_RELATIONSHIP_ELEMENT, + types.KeyTypes.SUBMODEL: + types_pb.KeyTypes.Keytypes_SUBMODEL, + types.KeyTypes.SUBMODEL_ELEMENT: + types_pb.KeyTypes.Keytypes_SUBMODEL_ELEMENT, + types.KeyTypes.SUBMODEL_ELEMENT_COLLECTION: + types_pb.KeyTypes.Keytypes_SUBMODEL_ELEMENT_COLLECTION, + types.KeyTypes.SUBMODEL_ELEMENT_LIST: + types_pb.KeyTypes.Keytypes_SUBMODEL_ELEMENT_LIST +} # type: Mapping[types.KeyTypes, types_pb.KeyTypes] +# fmt: on + + +def key_types_to_pb( + that: types.KeyTypes +) -> types_pb.KeyTypes: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import key_types_to_pb + >>> import aas_core3.types as types + >>> key_types_to_pb( + ... types.KeyTypes.ANNOTATED_RELATIONSHIP_ELEMENT + ... ) + 1 + """ + return _KEY_TYPES_TO_PB_MAP[that] + + +# fmt: off +_DATA_TYPE_DEF_XSD_TO_PB_MAP = { + types.DataTypeDefXSD.ANY_URI: + types_pb.DataTypeDefXsd.Datatypedefxsd_ANY_URI, + types.DataTypeDefXSD.BASE_64_BINARY: + types_pb.DataTypeDefXsd.Datatypedefxsd_BASE_64_BINARY, + types.DataTypeDefXSD.BOOLEAN: + types_pb.DataTypeDefXsd.Datatypedefxsd_BOOLEAN, + types.DataTypeDefXSD.BYTE: + types_pb.DataTypeDefXsd.Datatypedefxsd_BYTE, + types.DataTypeDefXSD.DATE: + types_pb.DataTypeDefXsd.Datatypedefxsd_DATE, + types.DataTypeDefXSD.DATE_TIME: + types_pb.DataTypeDefXsd.Datatypedefxsd_DATE_TIME, + types.DataTypeDefXSD.DECIMAL: + types_pb.DataTypeDefXsd.Datatypedefxsd_DECIMAL, + types.DataTypeDefXSD.DOUBLE: + types_pb.DataTypeDefXsd.Datatypedefxsd_DOUBLE, + types.DataTypeDefXSD.DURATION: + types_pb.DataTypeDefXsd.Datatypedefxsd_DURATION, + types.DataTypeDefXSD.FLOAT: + types_pb.DataTypeDefXsd.Datatypedefxsd_FLOAT, + types.DataTypeDefXSD.G_DAY: + types_pb.DataTypeDefXsd.Datatypedefxsd_G_DAY, + types.DataTypeDefXSD.G_MONTH: + types_pb.DataTypeDefXsd.Datatypedefxsd_G_MONTH, + types.DataTypeDefXSD.G_MONTH_DAY: + types_pb.DataTypeDefXsd.Datatypedefxsd_G_MONTH_DAY, + types.DataTypeDefXSD.G_YEAR: + types_pb.DataTypeDefXsd.Datatypedefxsd_G_YEAR, + types.DataTypeDefXSD.G_YEAR_MONTH: + types_pb.DataTypeDefXsd.Datatypedefxsd_G_YEAR_MONTH, + types.DataTypeDefXSD.HEX_BINARY: + types_pb.DataTypeDefXsd.Datatypedefxsd_HEX_BINARY, + types.DataTypeDefXSD.INT: + types_pb.DataTypeDefXsd.Datatypedefxsd_INT, + types.DataTypeDefXSD.INTEGER: + types_pb.DataTypeDefXsd.Datatypedefxsd_INTEGER, + types.DataTypeDefXSD.LONG: + types_pb.DataTypeDefXsd.Datatypedefxsd_LONG, + types.DataTypeDefXSD.NEGATIVE_INTEGER: + types_pb.DataTypeDefXsd.Datatypedefxsd_NEGATIVE_INTEGER, + types.DataTypeDefXSD.NON_NEGATIVE_INTEGER: + types_pb.DataTypeDefXsd.Datatypedefxsd_NON_NEGATIVE_INTEGER, + types.DataTypeDefXSD.NON_POSITIVE_INTEGER: + types_pb.DataTypeDefXsd.Datatypedefxsd_NON_POSITIVE_INTEGER, + types.DataTypeDefXSD.POSITIVE_INTEGER: + types_pb.DataTypeDefXsd.Datatypedefxsd_POSITIVE_INTEGER, + types.DataTypeDefXSD.SHORT: + types_pb.DataTypeDefXsd.Datatypedefxsd_SHORT, + types.DataTypeDefXSD.STRING: + types_pb.DataTypeDefXsd.Datatypedefxsd_STRING, + types.DataTypeDefXSD.TIME: + types_pb.DataTypeDefXsd.Datatypedefxsd_TIME, + types.DataTypeDefXSD.UNSIGNED_BYTE: + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_BYTE, + types.DataTypeDefXSD.UNSIGNED_INT: + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_INT, + types.DataTypeDefXSD.UNSIGNED_LONG: + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_LONG, + types.DataTypeDefXSD.UNSIGNED_SHORT: + types_pb.DataTypeDefXsd.Datatypedefxsd_UNSIGNED_SHORT +} # type: Mapping[types.DataTypeDefXSD, types_pb.DataTypeDefXsd] +# fmt: on + + +def data_type_def_xsd_to_pb( + that: types.DataTypeDefXSD +) -> types_pb.DataTypeDefXsd: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import data_type_def_xsd_to_pb + >>> import aas_core3.types as types + >>> data_type_def_xsd_to_pb( + ... types.DataTypeDefXSD.ANY_URI + ... ) + 1 + """ + return _DATA_TYPE_DEF_XSD_TO_PB_MAP[that] + + +class _AbstractLangStringToPbChoice( + _PartialVisitorWithContext[ + types_pb.AbstractLangString_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_lang_string_definition_type_iec_61360_with_context( + self, + that: types.LangStringDefinitionTypeIEC61360, + context: types_pb.AbstractLangString_choice + ) -> None: + """ + Set the fields of ``context.lang_string_definition_type_iec_61360`` + according to ``that`` instance. + """ + lang_string_definition_type_iec_61360_to_pb( + that, + context.lang_string_definition_type_iec_61360 + ) + + def visit_lang_string_name_type_with_context( + self, + that: types.LangStringNameType, + context: types_pb.AbstractLangString_choice + ) -> None: + """ + Set the fields of ``context.lang_string_name_type`` + according to ``that`` instance. + """ + lang_string_name_type_to_pb( + that, + context.lang_string_name_type + ) + + def visit_lang_string_preferred_name_type_iec_61360_with_context( + self, + that: types.LangStringPreferredNameTypeIEC61360, + context: types_pb.AbstractLangString_choice + ) -> None: + """ + Set the fields of ``context.lang_string_preferred_name_type_iec_61360`` + according to ``that`` instance. + """ + lang_string_preferred_name_type_iec_61360_to_pb( + that, + context.lang_string_preferred_name_type_iec_61360 + ) + + def visit_lang_string_short_name_type_iec_61360_with_context( + self, + that: types.LangStringShortNameTypeIEC61360, + context: types_pb.AbstractLangString_choice + ) -> None: + """ + Set the fields of ``context.lang_string_short_name_type_iec_61360`` + according to ``that`` instance. + """ + lang_string_short_name_type_iec_61360_to_pb( + that, + context.lang_string_short_name_type_iec_61360 + ) + + def visit_lang_string_text_type_with_context( + self, + that: types.LangStringTextType, + context: types_pb.AbstractLangString_choice + ) -> None: + """ + Set the fields of ``context.lang_string_text_type`` + according to ``that`` instance. + """ + lang_string_text_type_to_pb( + that, + context.lang_string_text_type + ) + + +_ABSTRACT_LANG_STRING_TO_PB_CHOICE = _AbstractLangStringToPbChoice() + + +def abstract_lang_string_to_pb_choice( + that: types.AbstractLangString, + target: types_pb.AbstractLangString_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import abstract_lang_string_to_pb_choice + + abstract_lang_string = types.LangStringDefinitionTypeIEC61360( + ... # some constructor arguments + ) + + abstract_lang_string_choice_pb = types_pb.AbstractLangString_choice() + abstract_lang_string_to_pb_choice( + abstract_lang_string, + abstract_lang_string_choice_pb + ) + + some_bytes = abstract_lang_string_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _ABSTRACT_LANG_STRING_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def lang_string_name_type_to_pb( + that: types.LangStringNameType, + target: types_pb.LangStringNameType +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_name_type_to_pb + + lang_string_name_type = types.LangStringNameType( + ... # some constructor arguments + ) + + lang_string_name_type_pb = types_pb.LangStringNameType() + lang_string_name_type_to_pb( + lang_string_name_type, + lang_string_name_type_pb + ) + + some_bytes = lang_string_name_type_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.language = that.language + + target.text = that.text + + +def lang_string_text_type_to_pb( + that: types.LangStringTextType, + target: types_pb.LangStringTextType +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_text_type_to_pb + + lang_string_text_type = types.LangStringTextType( + ... # some constructor arguments + ) + + lang_string_text_type_pb = types_pb.LangStringTextType() + lang_string_text_type_to_pb( + lang_string_text_type, + lang_string_text_type_pb + ) + + some_bytes = lang_string_text_type_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.language = that.language + + target.text = that.text + + +def environment_to_pb( + that: types.Environment, + target: types_pb.Environment +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import environment_to_pb + + environment = types.Environment( + ... # some constructor arguments + ) + + environment_pb = types_pb.Environment() + environment_to_pb( + environment, + environment_pb + ) + + some_bytes = environment_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + if that.asset_administration_shells is not None: + for asset_administration_shells_item in that.asset_administration_shells: + asset_administration_shells_item_pb = target.asset_administration_shells.add() + asset_administration_shell_to_pb( + asset_administration_shells_item, + asset_administration_shells_item_pb) + + if that.submodels is not None: + for submodels_item in that.submodels: + submodels_item_pb = target.submodels.add() + submodel_to_pb( + submodels_item, + submodels_item_pb) + + if that.concept_descriptions is not None: + for concept_descriptions_item in that.concept_descriptions: + concept_descriptions_item_pb = target.concept_descriptions.add() + concept_description_to_pb( + concept_descriptions_item, + concept_descriptions_item_pb) + + +class _DataSpecificationContentToPbChoice( + _PartialVisitorWithContext[ + types_pb.DataSpecificationContent_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_data_specification_iec_61360_with_context( + self, + that: types.DataSpecificationIEC61360, + context: types_pb.DataSpecificationContent_choice + ) -> None: + """ + Set the fields of ``context.data_specification_iec_61360`` + according to ``that`` instance. + """ + data_specification_iec_61360_to_pb( + that, + context.data_specification_iec_61360 + ) + + +_DATA_SPECIFICATION_CONTENT_TO_PB_CHOICE = _DataSpecificationContentToPbChoice() + + +def data_specification_content_to_pb_choice( + that: types.DataSpecificationContent, + target: types_pb.DataSpecificationContent_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import data_specification_content_to_pb_choice + + data_specification_content = types.DataSpecificationIEC61360( + ... # some constructor arguments + ) + + data_specification_content_choice_pb = types_pb.DataSpecificationContent_choice() + data_specification_content_to_pb_choice( + data_specification_content, + data_specification_content_choice_pb + ) + + some_bytes = data_specification_content_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _DATA_SPECIFICATION_CONTENT_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def embedded_data_specification_to_pb( + that: types.EmbeddedDataSpecification, + target: types_pb.EmbeddedDataSpecification +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import embedded_data_specification_to_pb + + embedded_data_specification = types.EmbeddedDataSpecification( + ... # some constructor arguments + ) + + embedded_data_specification_pb = types_pb.EmbeddedDataSpecification() + embedded_data_specification_to_pb( + embedded_data_specification, + embedded_data_specification_pb + ) + + some_bytes = embedded_data_specification_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + data_specification_content_to_pb_choice( + that.data_specification_content, + target.data_specification_content + ) + + # We clear so that the field is set even if all the properties are None. + target.data_specification.Clear() + + reference_to_pb( + that.data_specification, + target.data_specification + ) + + +# fmt: off +_DATA_TYPE_IEC_61360_TO_PB_MAP = { + types.DataTypeIEC61360.DATE: + types_pb.DataTypeIec61360.Datatypeiec61360_DATE, + types.DataTypeIEC61360.STRING: + types_pb.DataTypeIec61360.Datatypeiec61360_STRING, + types.DataTypeIEC61360.STRING_TRANSLATABLE: + types_pb.DataTypeIec61360.Datatypeiec61360_STRING_TRANSLATABLE, + types.DataTypeIEC61360.INTEGER_MEASURE: + types_pb.DataTypeIec61360.Datatypeiec61360_INTEGER_MEASURE, + types.DataTypeIEC61360.INTEGER_COUNT: + types_pb.DataTypeIec61360.Datatypeiec61360_INTEGER_COUNT, + types.DataTypeIEC61360.INTEGER_CURRENCY: + types_pb.DataTypeIec61360.Datatypeiec61360_INTEGER_CURRENCY, + types.DataTypeIEC61360.REAL_MEASURE: + types_pb.DataTypeIec61360.Datatypeiec61360_REAL_MEASURE, + types.DataTypeIEC61360.REAL_COUNT: + types_pb.DataTypeIec61360.Datatypeiec61360_REAL_COUNT, + types.DataTypeIEC61360.REAL_CURRENCY: + types_pb.DataTypeIec61360.Datatypeiec61360_REAL_CURRENCY, + types.DataTypeIEC61360.BOOLEAN: + types_pb.DataTypeIec61360.Datatypeiec61360_BOOLEAN, + types.DataTypeIEC61360.IRI: + types_pb.DataTypeIec61360.Datatypeiec61360_IRI, + types.DataTypeIEC61360.IRDI: + types_pb.DataTypeIec61360.Datatypeiec61360_IRDI, + types.DataTypeIEC61360.RATIONAL: + types_pb.DataTypeIec61360.Datatypeiec61360_RATIONAL, + types.DataTypeIEC61360.RATIONAL_MEASURE: + types_pb.DataTypeIec61360.Datatypeiec61360_RATIONAL_MEASURE, + types.DataTypeIEC61360.TIME: + types_pb.DataTypeIec61360.Datatypeiec61360_TIME, + types.DataTypeIEC61360.TIMESTAMP: + types_pb.DataTypeIec61360.Datatypeiec61360_TIMESTAMP, + types.DataTypeIEC61360.FILE: + types_pb.DataTypeIec61360.Datatypeiec61360_FILE, + types.DataTypeIEC61360.HTML: + types_pb.DataTypeIec61360.Datatypeiec61360_HTML, + types.DataTypeIEC61360.BLOB: + types_pb.DataTypeIec61360.Datatypeiec61360_BLOB +} # type: Mapping[types.DataTypeIEC61360, types_pb.DataTypeIec61360] +# fmt: on + + +def data_type_iec_61360_to_pb( + that: types.DataTypeIEC61360 +) -> types_pb.DataTypeIec61360: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import data_type_iec_61360_to_pb + >>> import aas_core3.types as types + >>> data_type_iec_61360_to_pb( + ... types.DataTypeIEC61360.DATE + ... ) + 1 + """ + return _DATA_TYPE_IEC_61360_TO_PB_MAP[that] + + +def level_type_to_pb( + that: types.LevelType, + target: types_pb.LevelType +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import level_type_to_pb + + level_type = types.LevelType( + ... # some constructor arguments + ) + + level_type_pb = types_pb.LevelType() + level_type_to_pb( + level_type, + level_type_pb + ) + + some_bytes = level_type_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.min = that.min + + target.nom = that.nom + + target.typ = that.typ + + target.max = that.max + + +def value_reference_pair_to_pb( + that: types.ValueReferencePair, + target: types_pb.ValueReferencePair +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import value_reference_pair_to_pb + + value_reference_pair = types.ValueReferencePair( + ... # some constructor arguments + ) + + value_reference_pair_pb = types_pb.ValueReferencePair() + value_reference_pair_to_pb( + value_reference_pair, + value_reference_pair_pb + ) + + some_bytes = value_reference_pair_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.value = that.value + + # We clear so that the field is set even if all the properties are None. + target.value_id.Clear() + + reference_to_pb( + that.value_id, + target.value_id + ) + + +def value_list_to_pb( + that: types.ValueList, + target: types_pb.ValueList +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import value_list_to_pb + + value_list = types.ValueList( + ... # some constructor arguments + ) + + value_list_pb = types_pb.ValueList() + value_list_to_pb( + value_list, + value_list_pb + ) + + some_bytes = value_list_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + for value_reference_pairs_item in that.value_reference_pairs: + value_reference_pairs_item_pb = target.value_reference_pairs.add() + value_reference_pair_to_pb( + value_reference_pairs_item, + value_reference_pairs_item_pb) + + +def lang_string_preferred_name_type_iec_61360_to_pb( + that: types.LangStringPreferredNameTypeIEC61360, + target: types_pb.LangStringPreferredNameTypeIec61360 +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_preferred_name_type_iec_61360_to_pb + + lang_string_preferred_name_type_iec_61360 = types.LangStringPreferredNameTypeIEC61360( + ... # some constructor arguments + ) + + lang_string_preferred_name_type_iec_61360_pb = types_pb.LangStringPreferredNameTypeIec61360() + lang_string_preferred_name_type_iec_61360_to_pb( + lang_string_preferred_name_type_iec_61360, + lang_string_preferred_name_type_iec_61360_pb + ) + + some_bytes = lang_string_preferred_name_type_iec_61360_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.language = that.language + + target.text = that.text + + +def lang_string_short_name_type_iec_61360_to_pb( + that: types.LangStringShortNameTypeIEC61360, + target: types_pb.LangStringShortNameTypeIec61360 +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_short_name_type_iec_61360_to_pb + + lang_string_short_name_type_iec_61360 = types.LangStringShortNameTypeIEC61360( + ... # some constructor arguments + ) + + lang_string_short_name_type_iec_61360_pb = types_pb.LangStringShortNameTypeIec61360() + lang_string_short_name_type_iec_61360_to_pb( + lang_string_short_name_type_iec_61360, + lang_string_short_name_type_iec_61360_pb + ) + + some_bytes = lang_string_short_name_type_iec_61360_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.language = that.language + + target.text = that.text + + +def lang_string_definition_type_iec_61360_to_pb( + that: types.LangStringDefinitionTypeIEC61360, + target: types_pb.LangStringDefinitionTypeIec61360 +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import lang_string_definition_type_iec_61360_to_pb + + lang_string_definition_type_iec_61360 = types.LangStringDefinitionTypeIEC61360( + ... # some constructor arguments + ) + + lang_string_definition_type_iec_61360_pb = types_pb.LangStringDefinitionTypeIec61360() + lang_string_definition_type_iec_61360_to_pb( + lang_string_definition_type_iec_61360, + lang_string_definition_type_iec_61360_pb + ) + + some_bytes = lang_string_definition_type_iec_61360_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.language = that.language + + target.text = that.text + + +def data_specification_iec_61360_to_pb( + that: types.DataSpecificationIEC61360, + target: types_pb.DataSpecificationIec61360 +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import data_specification_iec_61360_to_pb + + data_specification_iec_61360 = types.DataSpecificationIEC61360( + ... # some constructor arguments + ) + + data_specification_iec_61360_pb = types_pb.DataSpecificationIec61360() + data_specification_iec_61360_to_pb( + data_specification_iec_61360, + data_specification_iec_61360_pb + ) + + some_bytes = data_specification_iec_61360_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + for preferred_name_item in that.preferred_name: + preferred_name_item_pb = target.preferred_name.add() + lang_string_preferred_name_type_iec_61360_to_pb( + preferred_name_item, + preferred_name_item_pb) + + if that.short_name is not None: + for short_name_item in that.short_name: + short_name_item_pb = target.short_name.add() + lang_string_short_name_type_iec_61360_to_pb( + short_name_item, + short_name_item_pb) + + if that.unit is not None: + target.unit = that.unit + + if that.unit_id is not None: + # We clear so that the field is set even if all the properties are None. + target.unit_id.Clear() + + reference_to_pb( + that.unit_id, + target.unit_id + ) + + if that.source_of_definition is not None: + target.source_of_definition = that.source_of_definition + + if that.symbol is not None: + target.symbol = that.symbol + + if that.data_type is not None: + target.data_type = data_type_iec_61360_to_pb( + that.data_type + ) + + if that.definition is not None: + for definition_item in that.definition: + definition_item_pb = target.definition.add() + lang_string_definition_type_iec_61360_to_pb( + definition_item, + definition_item_pb) + + if that.value_format is not None: + target.value_format = that.value_format + + if that.value_list is not None: + # We clear so that the field is set even if all the properties are None. + target.value_list.Clear() + + value_list_to_pb( + that.value_list, + target.value_list + ) + + if that.value is not None: + target.value = that.value + + if that.level_type is not None: + # We clear so that the field is set even if all the properties are None. + target.level_type.Clear() + + level_type_to_pb( + that.level_type, + target.level_type + ) + + +class _ToPbTransformer( + types.AbstractTransformer[google.protobuf.message.Message] +): + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer + class. We made this decision with the compactness of messages in mind. + """ + def transform_extension( + self, + that: types.Extension + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Extension`. + """ + result = types_pb.Extension() + extension_to_pb(that, result) + return result + + def transform_administrative_information( + self, + that: types.AdministrativeInformation + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.AdministrativeInformation`. + """ + result = types_pb.AdministrativeInformation() + administrative_information_to_pb(that, result) + return result + + def transform_qualifier( + self, + that: types.Qualifier + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Qualifier`. + """ + result = types_pb.Qualifier() + qualifier_to_pb(that, result) + return result + + def transform_asset_administration_shell( + self, + that: types.AssetAdministrationShell + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.AssetAdministrationShell`. + """ + result = types_pb.AssetAdministrationShell() + asset_administration_shell_to_pb(that, result) + return result + + def transform_asset_information( + self, + that: types.AssetInformation + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.AssetInformation`. + """ + result = types_pb.AssetInformation() + asset_information_to_pb(that, result) + return result + + def transform_resource( + self, + that: types.Resource + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Resource`. + """ + result = types_pb.Resource() + resource_to_pb(that, result) + return result + + def transform_specific_asset_id( + self, + that: types.SpecificAssetID + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.SpecificAssetId`. + """ + result = types_pb.SpecificAssetId() + specific_asset_id_to_pb(that, result) + return result + + def transform_submodel( + self, + that: types.Submodel + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Submodel`. + """ + result = types_pb.Submodel() + submodel_to_pb(that, result) + return result + + def transform_relationship_element( + self, + that: types.RelationshipElement + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.RelationshipElement_choice`. + """ + result = types_pb.RelationshipElement_choice() + relationship_element_to_pb_choice(that, result) + return result + + def transform_submodel_element_list( + self, + that: types.SubmodelElementList + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.SubmodelElementList`. + """ + result = types_pb.SubmodelElementList() + submodel_element_list_to_pb(that, result) + return result + + def transform_submodel_element_collection( + self, + that: types.SubmodelElementCollection + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.SubmodelElementCollection`. + """ + result = types_pb.SubmodelElementCollection() + submodel_element_collection_to_pb(that, result) + return result + + def transform_property( + self, + that: types.Property + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Property`. + """ + result = types_pb.Property() + property_to_pb(that, result) + return result + + def transform_multi_language_property( + self, + that: types.MultiLanguageProperty + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.MultiLanguageProperty`. + """ + result = types_pb.MultiLanguageProperty() + multi_language_property_to_pb(that, result) + return result + + def transform_range( + self, + that: types.Range + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Range`. + """ + result = types_pb.Range() + range_to_pb(that, result) + return result + + def transform_reference_element( + self, + that: types.ReferenceElement + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.ReferenceElement`. + """ + result = types_pb.ReferenceElement() + reference_element_to_pb(that, result) + return result + + def transform_blob( + self, + that: types.Blob + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Blob`. + """ + result = types_pb.Blob() + blob_to_pb(that, result) + return result + + def transform_file( + self, + that: types.File + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.File`. + """ + result = types_pb.File() + file_to_pb(that, result) + return result + + def transform_annotated_relationship_element( + self, + that: types.AnnotatedRelationshipElement + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.AnnotatedRelationshipElement`. + """ + result = types_pb.AnnotatedRelationshipElement() + annotated_relationship_element_to_pb(that, result) + return result + + def transform_entity( + self, + that: types.Entity + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Entity`. + """ + result = types_pb.Entity() + entity_to_pb(that, result) + return result + + def transform_event_payload( + self, + that: types.EventPayload + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.EventPayload`. + """ + result = types_pb.EventPayload() + event_payload_to_pb(that, result) + return result + + def transform_basic_event_element( + self, + that: types.BasicEventElement + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.BasicEventElement`. + """ + result = types_pb.BasicEventElement() + basic_event_element_to_pb(that, result) + return result + + def transform_operation( + self, + that: types.Operation + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Operation`. + """ + result = types_pb.Operation() + operation_to_pb(that, result) + return result + + def transform_operation_variable( + self, + that: types.OperationVariable + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.OperationVariable`. + """ + result = types_pb.OperationVariable() + operation_variable_to_pb(that, result) + return result + + def transform_capability( + self, + that: types.Capability + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Capability`. + """ + result = types_pb.Capability() + capability_to_pb(that, result) + return result + + def transform_concept_description( + self, + that: types.ConceptDescription + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.ConceptDescription`. + """ + result = types_pb.ConceptDescription() + concept_description_to_pb(that, result) + return result + + def transform_reference( + self, + that: types.Reference + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Reference`. + """ + result = types_pb.Reference() + reference_to_pb(that, result) + return result + + def transform_key( + self, + that: types.Key + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Key`. + """ + result = types_pb.Key() + key_to_pb(that, result) + return result + + def transform_lang_string_name_type( + self, + that: types.LangStringNameType + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.LangStringNameType`. + """ + result = types_pb.LangStringNameType() + lang_string_name_type_to_pb(that, result) + return result + + def transform_lang_string_text_type( + self, + that: types.LangStringTextType + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.LangStringTextType`. + """ + result = types_pb.LangStringTextType() + lang_string_text_type_to_pb(that, result) + return result + + def transform_environment( + self, + that: types.Environment + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Environment`. + """ + result = types_pb.Environment() + environment_to_pb(that, result) + return result + + def transform_embedded_data_specification( + self, + that: types.EmbeddedDataSpecification + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.EmbeddedDataSpecification`. + """ + result = types_pb.EmbeddedDataSpecification() + embedded_data_specification_to_pb(that, result) + return result + + def transform_level_type( + self, + that: types.LevelType + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.LevelType`. + """ + result = types_pb.LevelType() + level_type_to_pb(that, result) + return result + + def transform_value_reference_pair( + self, + that: types.ValueReferencePair + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.ValueReferencePair`. + """ + result = types_pb.ValueReferencePair() + value_reference_pair_to_pb(that, result) + return result + + def transform_value_list( + self, + that: types.ValueList + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.ValueList`. + """ + result = types_pb.ValueList() + value_list_to_pb(that, result) + return result + + def transform_lang_string_preferred_name_type_iec_61360( + self, + that: types.LangStringPreferredNameTypeIEC61360 + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.LangStringPreferredNameTypeIec61360`. + """ + result = types_pb.LangStringPreferredNameTypeIec61360() + lang_string_preferred_name_type_iec_61360_to_pb(that, result) + return result + + def transform_lang_string_short_name_type_iec_61360( + self, + that: types.LangStringShortNameTypeIEC61360 + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.LangStringShortNameTypeIec61360`. + """ + result = types_pb.LangStringShortNameTypeIec61360() + lang_string_short_name_type_iec_61360_to_pb(that, result) + return result + + def transform_lang_string_definition_type_iec_61360( + self, + that: types.LangStringDefinitionTypeIEC61360 + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.LangStringDefinitionTypeIec61360`. + """ + result = types_pb.LangStringDefinitionTypeIec61360() + lang_string_definition_type_iec_61360_to_pb(that, result) + return result + + def transform_data_specification_iec_61360( + self, + that: types.DataSpecificationIEC61360 + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.DataSpecificationIec61360`. + """ + result = types_pb.DataSpecificationIec61360() + data_specification_iec_61360_to_pb(that, result) + return result + + +_TO_PB_TRANSFORMER = _ToPbTransformer() + + +def to_pb( + that: types.Class, +) -> google.protobuf.message.Message: + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer message type instead of the choice (union) type. + We made this decision with the compactness of messages in mind as choice types + would occupy a tiny bit more space. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + from aas_core3_protobuf.pbization import to_pb + + instance = types.Extension( + ... # some constructor arguments + ) + + instance_pb = to_pb( + instance + ) + + some_bytes = instance_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + return _TO_PB_TRANSFORMER.transform(that) + + +# endregion To Protocol Buffers + + +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. diff --git a/test_data/python_protobuf/test_main/aas_core_meta.v3/expected_output/stdout.txt b/test_data/python_protobuf/test_main/aas_core_meta.v3/expected_output/stdout.txt new file mode 100644 index 00000000..2d755fc5 --- /dev/null +++ b/test_data/python_protobuf/test_main/aas_core_meta.v3/expected_output/stdout.txt @@ -0,0 +1 @@ +Code generated to: diff --git a/test_data/python_protobuf/test_main/aas_core_meta.v3/input/snippets/base_qualified_module_name.txt b/test_data/python_protobuf/test_main/aas_core_meta.v3/input/snippets/base_qualified_module_name.txt new file mode 100644 index 00000000..74a993a9 --- /dev/null +++ b/test_data/python_protobuf/test_main/aas_core_meta.v3/input/snippets/base_qualified_module_name.txt @@ -0,0 +1 @@ +aas_core3 \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/aas_core_meta.v3/input/snippets/qualified_module_name_for_protobuf_library.txt b/test_data/python_protobuf/test_main/aas_core_meta.v3/input/snippets/qualified_module_name_for_protobuf_library.txt new file mode 100644 index 00000000..3ae856f7 --- /dev/null +++ b/test_data/python_protobuf/test_main/aas_core_meta.v3/input/snippets/qualified_module_name_for_protobuf_library.txt @@ -0,0 +1 @@ +aas_core3_protobuf \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/abstract_and_concrete_classes/expected_output/pbization.py b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/expected_output/pbization.py new file mode 100644 index 00000000..3391d0b1 --- /dev/null +++ b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/expected_output/pbization.py @@ -0,0 +1,532 @@ +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. + + +"""Convert instances from and to Protocol Buffers.""" + + +from typing import Mapping, TypeVar + +import google.protobuf.message + +from aas_core3 import types +import aas_core3_protobuf.types_pb2 as types_pb + + +# region From Protocol Buffers + + +# fmt: off +_SOMETHING_ABSTRACT_FROM_PB_CHOICE_MAP = { + 'another_concrete': + lambda that: another_concrete_from_pb( + that.another_concrete + ), + 'something_concrete': + lambda that: something_concrete_from_pb( + that.something_concrete + ) +} +# fmt: on + + +def something_abstract_from_pb_choice( + that: types_pb.SomethingAbstract_choice +) -> types.SomethingAbstract: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_abstract_from_pb_choice + + some_bytes = b'... serialized types_pb.SomethingAbstract_choice ...' + something_abstract_choice_pb = types_pb.SomethingAbstract_choice() + something_abstract_choice_pb.FromString( + some_bytes + ) + + something_abstract = something_abstract_from_pb_choice( + something_abstract_choice_pb + ) + # Do something with the something_abstract... + """ + get_concrete_instance_from_pb = ( + _SOMETHING_ABSTRACT_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.SomethingAbstract) + return result + + +def something_concrete_from_pb( + that: types_pb.SomethingConcrete +) -> types.SomethingConcrete: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_concrete_from_pb + + some_bytes = b'... serialized types_pb.SomethingConcrete ...' + something_concrete_pb = types_pb.SomethingConcrete() + something_concrete_pb.FromString( + some_bytes + ) + + something_concrete = something_concrete_from_pb( + something_concrete_pb + ) + # Do something with the something_concrete... + + """ + return types.SomethingConcrete( + some_str=that.some_str, + something_str=that.something_str + ) + + +def another_concrete_from_pb( + that: types_pb.AnotherConcrete +) -> types.AnotherConcrete: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import another_concrete_from_pb + + some_bytes = b'... serialized types_pb.AnotherConcrete ...' + another_concrete_pb = types_pb.AnotherConcrete() + another_concrete_pb.FromString( + some_bytes + ) + + another_concrete = another_concrete_from_pb( + another_concrete_pb + ) + # Do something with the another_concrete... + + """ + return types.AnotherConcrete( + some_str=that.some_str, + another_str=that.another_str + ) + + +def container_from_pb( + that: types_pb.Container +) -> types.Container: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import container_from_pb + + some_bytes = b'... serialized types_pb.Container ...' + container_pb = types_pb.Container() + container_pb.FromString( + some_bytes + ) + + container = container_from_pb( + container_pb + ) + # Do something with the container... + + """ + return types.Container( + something_abstract=something_abstract_from_pb_choice( + that.something_abstract + ) + ) + + +# fmt: off +_FROM_PB_MAP = { + types_pb.SomethingAbstract_choice: + something_abstract_from_pb_choice, + types_pb.SomethingConcrete: + something_concrete_from_pb, + types_pb.AnotherConcrete: + another_concrete_from_pb, + types_pb.Container: + container_from_pb +} +# fmt: on + + +def from_pb( + that: google.protobuf.message.Message +) -> types.Class: + """ + Parse ``that`` Protocol Buffer into a model instance. + + The concrete parsing is determined based on the runtime type of ``that`` + Protocol Buffer. It is assumed that ``that`` is an instance of a message + coming from the Protocol Buffer definitions corresponding to the meta-model. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import from_pb + + some_bytes = b'... serialized types_pb.SomethingConcrete ...' + instance_pb = types_pb.SomethingConcrete() + instance_pb.FromString( + some_bytes + ) + + instance = from_pb( + instance_pb + ) + # Do something with the instance... + """ + from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + + if from_pb_func is None: + raise ValueError( + f"We do not know how to parse the protocol buffer " + f"of type {that.__class__} into a model instance." + ) + + result = from_pb_func(that) # type: ignore + assert isinstance(result, types.Class) + return result + + +# endregion From Protocol Buffers + + +# region To Protocol Buffers + + +T = TypeVar("T") + + +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): + """ + Visit instances in context with double-dispatch. + + This class is meant to be inherited from. If you do not override a method, + it will raise an exception. This is a partial visitor, meaning that some + visits are unexpected by design. + """ + # pylint: disable=missing-docstring + + def visit_something_concrete_with_context( + self, + that: types.SomethingConcrete, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_another_concrete_with_context( + self, + that: types.AnotherConcrete, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_container_with_context( + self, + that: types.Container, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + +class _SomethingAbstractToPbChoice( + _PartialVisitorWithContext[ + types_pb.SomethingAbstract_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_another_concrete_with_context( + self, + that: types.AnotherConcrete, + context: types_pb.SomethingAbstract_choice + ) -> None: + """ + Set the fields of ``context.another_concrete`` + according to ``that`` instance. + """ + another_concrete_to_pb( + that, + context.another_concrete + ) + + def visit_something_concrete_with_context( + self, + that: types.SomethingConcrete, + context: types_pb.SomethingAbstract_choice + ) -> None: + """ + Set the fields of ``context.something_concrete`` + according to ``that`` instance. + """ + something_concrete_to_pb( + that, + context.something_concrete + ) + + +_SOMETHING_ABSTRACT_TO_PB_CHOICE = _SomethingAbstractToPbChoice() + + +def something_abstract_to_pb_choice( + that: types.SomethingAbstract, + target: types_pb.SomethingAbstract_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_abstract_to_pb_choice + + something_abstract = types.AnotherConcrete( + ... # some constructor arguments + ) + + something_abstract_choice_pb = types_pb.SomethingAbstract_choice() + something_abstract_to_pb_choice( + something_abstract, + something_abstract_choice_pb + ) + + some_bytes = something_abstract_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _SOMETHING_ABSTRACT_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def something_concrete_to_pb( + that: types.SomethingConcrete, + target: types_pb.SomethingConcrete +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_concrete_to_pb + + something_concrete = types.SomethingConcrete( + ... # some constructor arguments + ) + + something_concrete_pb = types_pb.SomethingConcrete() + something_concrete_to_pb( + something_concrete, + something_concrete_pb + ) + + some_bytes = something_concrete_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.some_str = that.some_str + + target.something_str = that.something_str + + +def another_concrete_to_pb( + that: types.AnotherConcrete, + target: types_pb.AnotherConcrete +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import another_concrete_to_pb + + another_concrete = types.AnotherConcrete( + ... # some constructor arguments + ) + + another_concrete_pb = types_pb.AnotherConcrete() + another_concrete_to_pb( + another_concrete, + another_concrete_pb + ) + + some_bytes = another_concrete_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.some_str = that.some_str + + target.another_str = that.another_str + + +def container_to_pb( + that: types.Container, + target: types_pb.Container +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import container_to_pb + + container = types.Container( + ... # some constructor arguments + ) + + container_pb = types_pb.Container() + container_to_pb( + container, + container_pb + ) + + some_bytes = container_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + something_abstract_to_pb_choice( + that.something_abstract, + target.something_abstract + ) + + +class _ToPbTransformer( + types.AbstractTransformer[google.protobuf.message.Message] +): + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer + class. We made this decision with the compactness of messages in mind. + """ + def transform_something_concrete( + self, + that: types.SomethingConcrete + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.SomethingConcrete`. + """ + result = types_pb.SomethingConcrete() + something_concrete_to_pb(that, result) + return result + + def transform_another_concrete( + self, + that: types.AnotherConcrete + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.AnotherConcrete`. + """ + result = types_pb.AnotherConcrete() + another_concrete_to_pb(that, result) + return result + + def transform_container( + self, + that: types.Container + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Container`. + """ + result = types_pb.Container() + container_to_pb(that, result) + return result + + +_TO_PB_TRANSFORMER = _ToPbTransformer() + + +def to_pb( + that: types.Class, +) -> google.protobuf.message.Message: + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer message type instead of the choice (union) type. + We made this decision with the compactness of messages in mind as choice types + would occupy a tiny bit more space. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + from aas_core3_protobuf.pbization import to_pb + + instance = types.SomethingConcrete( + ... # some constructor arguments + ) + + instance_pb = to_pb( + instance + ) + + some_bytes = instance_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + return _TO_PB_TRANSFORMER.transform(that) + + +# endregion To Protocol Buffers + + +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. diff --git a/test_data/python_protobuf/test_main/abstract_and_concrete_classes/expected_output/stdout.txt b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/expected_output/stdout.txt new file mode 100644 index 00000000..2d755fc5 --- /dev/null +++ b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/expected_output/stdout.txt @@ -0,0 +1 @@ +Code generated to: diff --git a/test_data/python_protobuf/test_main/abstract_and_concrete_classes/input/snippets/base_qualified_module_name.txt b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/input/snippets/base_qualified_module_name.txt new file mode 100644 index 00000000..74a993a9 --- /dev/null +++ b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/input/snippets/base_qualified_module_name.txt @@ -0,0 +1 @@ +aas_core3 \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/abstract_and_concrete_classes/input/snippets/qualified_module_name_for_protobuf_library.txt b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/input/snippets/qualified_module_name_for_protobuf_library.txt new file mode 100644 index 00000000..3ae856f7 --- /dev/null +++ b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/input/snippets/qualified_module_name_for_protobuf_library.txt @@ -0,0 +1 @@ +aas_core3_protobuf \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/abstract_and_concrete_classes/meta_model.py b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/meta_model.py new file mode 100644 index 00000000..2bec28db --- /dev/null +++ b/test_data/python_protobuf/test_main/abstract_and_concrete_classes/meta_model.py @@ -0,0 +1,37 @@ +@abstract +@serialization(with_model_type=True) +class Something_abstract: + some_str: str + + def __init__(self, some_str: str) -> None: + self.some_str = some_str + + +class Something_concrete(Something_abstract): + something_str: str + + def __init__(self, some_str: str, something_str: str) -> None: + Something_abstract.__init__(self, some_str) + + self.something_str = something_str + + +class Another_concrete(Something_abstract): + another_str: str + + def __init__(self, some_str: str, another_str: str) -> None: + Something_abstract.__init__(self, some_str) + + self.another_str = another_str + + +class Container: + something_abstract: Something_abstract + + def __init__(self, something_abstract: Something_abstract) -> None: + self.something_abstract = something_abstract + + +__version__ = "V198.4" + +__xml_namespace__ = "https://dummy/198/4" diff --git a/test_data/python_protobuf/test_main/concrete_class_with_descendant/expected_output/pbization.py b/test_data/python_protobuf/test_main/concrete_class_with_descendant/expected_output/pbization.py new file mode 100644 index 00000000..706a636a --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_descendant/expected_output/pbization.py @@ -0,0 +1,527 @@ +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. + + +"""Convert instances from and to Protocol Buffers.""" + + +from typing import Mapping, TypeVar + +import google.protobuf.message + +from aas_core3 import types +import aas_core3_protobuf.types_pb2 as types_pb + + +# region From Protocol Buffers + + +# fmt: off +_SOMETHING_FROM_PB_CHOICE_MAP = { + 'something': + lambda that: something_from_pb( + that.something + ), + 'something_more_concrete': + lambda that: something_more_concrete_from_pb( + that.something_more_concrete + ) +} +# fmt: on + + +def something_from_pb_choice( + that: types_pb.Something_choice +) -> types.Something: + """ + Parse ``that`` Protocol Buffer based on its runtime ``WhichOneof``. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_from_pb_choice + + some_bytes = b'... serialized types_pb.Something_choice ...' + something_choice_pb = types_pb.Something_choice() + something_choice_pb.FromString( + some_bytes + ) + + something = something_from_pb_choice( + something_choice_pb + ) + # Do something with the something... + """ + get_concrete_instance_from_pb = ( + _SOMETHING_FROM_PB_CHOICE_MAP[ + that.WhichOneof("value") + ] + ) + + result = get_concrete_instance_from_pb(that) # type: ignore + + assert isinstance(result, types.Something) + return result + + +def something_from_pb( + that: types_pb.Something +) -> types.Something: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_from_pb + + some_bytes = b'... serialized types_pb.Something ...' + something_pb = types_pb.Something() + something_pb.FromString( + some_bytes + ) + + something = something_from_pb( + something_pb + ) + # Do something with the something... + + """ + return types.Something( + some_str=that.some_str + ) + + +def something_more_concrete_from_pb( + that: types_pb.SomethingMoreConcrete +) -> types.SomethingMoreConcrete: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_more_concrete_from_pb + + some_bytes = b'... serialized types_pb.SomethingMoreConcrete ...' + something_more_concrete_pb = types_pb.SomethingMoreConcrete() + something_more_concrete_pb.FromString( + some_bytes + ) + + something_more_concrete = something_more_concrete_from_pb( + something_more_concrete_pb + ) + # Do something with the something_more_concrete... + + """ + return types.SomethingMoreConcrete( + some_str=that.some_str, + some_more_concrete_str=that.some_more_concrete_str + ) + + +def container_from_pb( + that: types_pb.Container +) -> types.Container: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import container_from_pb + + some_bytes = b'... serialized types_pb.Container ...' + container_pb = types_pb.Container() + container_pb.FromString( + some_bytes + ) + + container = container_from_pb( + container_pb + ) + # Do something with the container... + + """ + return types.Container( + something=something_from_pb_choice( + that.something + ) + ) + + +# fmt: off +_FROM_PB_MAP = { + types_pb.Something_choice: + something_from_pb_choice, + types_pb.SomethingMoreConcrete: + something_more_concrete_from_pb, + types_pb.Container: + container_from_pb +} +# fmt: on + + +def from_pb( + that: google.protobuf.message.Message +) -> types.Class: + """ + Parse ``that`` Protocol Buffer into a model instance. + + The concrete parsing is determined based on the runtime type of ``that`` + Protocol Buffer. It is assumed that ``that`` is an instance of a message + coming from the Protocol Buffer definitions corresponding to the meta-model. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import from_pb + + some_bytes = b'... serialized types_pb.Something ...' + instance_pb = types_pb.Something() + instance_pb.FromString( + some_bytes + ) + + instance = from_pb( + instance_pb + ) + # Do something with the instance... + """ + from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + + if from_pb_func is None: + raise ValueError( + f"We do not know how to parse the protocol buffer " + f"of type {that.__class__} into a model instance." + ) + + result = from_pb_func(that) # type: ignore + assert isinstance(result, types.Class) + return result + + +# endregion From Protocol Buffers + + +# region To Protocol Buffers + + +T = TypeVar("T") + + +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): + """ + Visit instances in context with double-dispatch. + + This class is meant to be inherited from. If you do not override a method, + it will raise an exception. This is a partial visitor, meaning that some + visits are unexpected by design. + """ + # pylint: disable=missing-docstring + + def visit_something_with_context( + self, + that: types.Something, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_something_more_concrete_with_context( + self, + that: types.SomethingMoreConcrete, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_container_with_context( + self, + that: types.Container, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + +class _SomethingToPbChoice( + _PartialVisitorWithContext[ + types_pb.Something_choice + ] +): + """Set the fields of the corresponding one-of value.""" + def visit_something_with_context( + self, + that: types.Something, + context: types_pb.Something_choice + ) -> None: + """ + Set the fields of ``context.something`` + according to ``that`` instance. + """ + something_to_pb( + that, + context.something + ) + + def visit_something_more_concrete_with_context( + self, + that: types.SomethingMoreConcrete, + context: types_pb.Something_choice + ) -> None: + """ + Set the fields of ``context.something_more_concrete`` + according to ``that`` instance. + """ + something_more_concrete_to_pb( + that, + context.something_more_concrete + ) + + +_SOMETHING_TO_PB_CHOICE = _SomethingToPbChoice() + + +def something_to_pb_choice( + that: types.Something, + target: types_pb.Something_choice +) -> None: + """ + Set the chosen value in ``target`` based on ``that`` instance. + + The chosen value in ``target`` is determined based on the runtime type of ``that`` + instance. All the fields of the value are recursively set according to ``that`` + instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_to_pb_choice + + something = types.SomethingMoreConcrete( + ... # some constructor arguments + ) + + something_choice_pb = types_pb.Something_choice() + something_to_pb_choice( + something, + something_choice_pb + ) + + some_bytes = something_choice_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + + """ + _SOMETHING_TO_PB_CHOICE.visit_with_context( + that, + target + ) + + +def something_to_pb( + that: types.Something, + target: types_pb.Something +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_to_pb + + something = types.Something( + ... # some constructor arguments + ) + + something_pb = types_pb.Something() + something_to_pb( + something, + something_pb + ) + + some_bytes = something_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.some_str = that.some_str + + +def something_more_concrete_to_pb( + that: types.SomethingMoreConcrete, + target: types_pb.SomethingMoreConcrete +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_more_concrete_to_pb + + something_more_concrete = types.SomethingMoreConcrete( + ... # some constructor arguments + ) + + something_more_concrete_pb = types_pb.SomethingMoreConcrete() + something_more_concrete_to_pb( + something_more_concrete, + something_more_concrete_pb + ) + + some_bytes = something_more_concrete_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.some_str = that.some_str + + target.some_more_concrete_str = that.some_more_concrete_str + + +def container_to_pb( + that: types.Container, + target: types_pb.Container +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import container_to_pb + + container = types.Container( + ... # some constructor arguments + ) + + container_pb = types_pb.Container() + container_to_pb( + container, + container_pb + ) + + some_bytes = container_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + something_to_pb_choice( + that.something, + target.something + ) + + +class _ToPbTransformer( + types.AbstractTransformer[google.protobuf.message.Message] +): + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer + class. We made this decision with the compactness of messages in mind. + """ + def transform_something( + self, + that: types.Something + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Something_choice`. + """ + result = types_pb.Something_choice() + something_to_pb_choice(that, result) + return result + + def transform_something_more_concrete( + self, + that: types.SomethingMoreConcrete + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.SomethingMoreConcrete`. + """ + result = types_pb.SomethingMoreConcrete() + something_more_concrete_to_pb(that, result) + return result + + def transform_container( + self, + that: types.Container + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Container`. + """ + result = types_pb.Container() + container_to_pb(that, result) + return result + + +_TO_PB_TRANSFORMER = _ToPbTransformer() + + +def to_pb( + that: types.Class, +) -> google.protobuf.message.Message: + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer message type instead of the choice (union) type. + We made this decision with the compactness of messages in mind as choice types + would occupy a tiny bit more space. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + from aas_core3_protobuf.pbization import to_pb + + instance = types.Something( + ... # some constructor arguments + ) + + instance_pb = to_pb( + instance + ) + + some_bytes = instance_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + return _TO_PB_TRANSFORMER.transform(that) + + +# endregion To Protocol Buffers + + +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. diff --git a/test_data/python_protobuf/test_main/concrete_class_with_descendant/expected_output/stdout.txt b/test_data/python_protobuf/test_main/concrete_class_with_descendant/expected_output/stdout.txt new file mode 100644 index 00000000..2d755fc5 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_descendant/expected_output/stdout.txt @@ -0,0 +1 @@ +Code generated to: diff --git a/test_data/python_protobuf/test_main/concrete_class_with_descendant/input/snippets/base_qualified_module_name.txt b/test_data/python_protobuf/test_main/concrete_class_with_descendant/input/snippets/base_qualified_module_name.txt new file mode 100644 index 00000000..74a993a9 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_descendant/input/snippets/base_qualified_module_name.txt @@ -0,0 +1 @@ +aas_core3 \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_descendant/input/snippets/qualified_module_name_for_protobuf_library.txt b/test_data/python_protobuf/test_main/concrete_class_with_descendant/input/snippets/qualified_module_name_for_protobuf_library.txt new file mode 100644 index 00000000..3ae856f7 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_descendant/input/snippets/qualified_module_name_for_protobuf_library.txt @@ -0,0 +1 @@ +aas_core3_protobuf \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_descendant/meta_model.py b/test_data/python_protobuf/test_main/concrete_class_with_descendant/meta_model.py new file mode 100644 index 00000000..c2da45e7 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_descendant/meta_model.py @@ -0,0 +1,27 @@ +@serialization(with_model_type=True) +class Something: + some_str: str + + def __init__(self, some_str: str) -> None: + self.some_str = some_str + + +class Something_more_concrete(Something): + some_more_concrete_str: str + + def __init__(self, some_str: str, some_more_concrete_str: str) -> None: + Something.__init__(self, some_str) + + self.some_more_concrete_str = some_more_concrete_str + + +class Container: + something: Something + + def __init__(self, something: Something) -> None: + self.something = something + + +__version__ = "V198.4" + +__xml_namespace__ = "https://dummy/198/4" diff --git a/test_data/python_protobuf/test_main/concrete_class_with_enum/expected_output/pbization.py b/test_data/python_protobuf/test_main/concrete_class_with_enum/expected_output/pbization.py new file mode 100644 index 00000000..26cf548e --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_enum/expected_output/pbization.py @@ -0,0 +1,290 @@ +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. + + +"""Convert instances from and to Protocol Buffers.""" + + +from typing import Mapping, TypeVar + +import google.protobuf.message + +from aas_core3 import types +import aas_core3_protobuf.types_pb2 as types_pb + + +# region From Protocol Buffers + + +# fmt: off +_MODELLING_KIND_FROM_PB_MAP = { + types_pb.ModellingKind.Modellingkind_TEMPLATE: + types.ModellingKind.TEMPLATE, + types_pb.ModellingKind.Modellingkind_INSTANCE: + types.ModellingKind.INSTANCE +} # type: Mapping[types_pb.ModellingKind, types.ModellingKind] +# fmt: on + + +def modelling_kind_from_pb( + that: types_pb.ModellingKind +) -> types.ModellingKind: + """ + Parse ``that`` enum back from its Protocol Buffer representation. + + >>> import aas_core3_protobuf.types_pb2 as types_pb + >>> from aas_core3_protobuf.pbization import modelling_kind_from_pb + >>> modelling_kind_from_pb( + ... types_pb.ModellingKind.Modellingkind_TEMPLATE + ... ) + + """ + return _MODELLING_KIND_FROM_PB_MAP[that] + + +def something_from_pb( + that: types_pb.Something +) -> types.Something: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_from_pb + + some_bytes = b'... serialized types_pb.Something ...' + something_pb = types_pb.Something() + something_pb.FromString( + some_bytes + ) + + something = something_from_pb( + something_pb + ) + # Do something with the something... + + """ + return types.Something( + modelling_kind=modelling_kind_from_pb( + that.modelling_kind + ), + optional_modelling_kind=( + modelling_kind_from_pb( + that.optional_modelling_kind + ) + if that.HasField('optional_modelling_kind') + else None + ) + ) + + +# fmt: off +_FROM_PB_MAP = { + types_pb.Something: + something_from_pb +} +# fmt: on + + +def from_pb( + that: google.protobuf.message.Message +) -> types.Class: + """ + Parse ``that`` Protocol Buffer into a model instance. + + The concrete parsing is determined based on the runtime type of ``that`` + Protocol Buffer. It is assumed that ``that`` is an instance of a message + coming from the Protocol Buffer definitions corresponding to the meta-model. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import from_pb + + some_bytes = b'... serialized types_pb.Something ...' + instance_pb = types_pb.Something() + instance_pb.FromString( + some_bytes + ) + + instance = from_pb( + instance_pb + ) + # Do something with the instance... + """ + from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + + if from_pb_func is None: + raise ValueError( + f"We do not know how to parse the protocol buffer " + f"of type {that.__class__} into a model instance." + ) + + result = from_pb_func(that) # type: ignore + assert isinstance(result, types.Class) + return result + + +# endregion From Protocol Buffers + + +# region To Protocol Buffers + + +T = TypeVar("T") + + +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): + """ + Visit instances in context with double-dispatch. + + This class is meant to be inherited from. If you do not override a method, + it will raise an exception. This is a partial visitor, meaning that some + visits are unexpected by design. + """ + # pylint: disable=missing-docstring + + def visit_something_with_context( + self, + that: types.Something, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + +# fmt: off +_MODELLING_KIND_TO_PB_MAP = { + types.ModellingKind.TEMPLATE: + types_pb.ModellingKind.Modellingkind_TEMPLATE, + types.ModellingKind.INSTANCE: + types_pb.ModellingKind.Modellingkind_INSTANCE +} # type: Mapping[types.ModellingKind, types_pb.ModellingKind] +# fmt: on + + +def modelling_kind_to_pb( + that: types.ModellingKind +) -> types_pb.ModellingKind: + """ + Convert ``that`` enum to its Protocol Buffer representation. + + >>> from aas_core3_protobuf.pbization import modelling_kind_to_pb + >>> import aas_core3.types as types + >>> modelling_kind_to_pb( + ... types.ModellingKind.TEMPLATE + ... ) + 1 + """ + return _MODELLING_KIND_TO_PB_MAP[that] + + +def something_to_pb( + that: types.Something, + target: types_pb.Something +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_to_pb + + something = types.Something( + ... # some constructor arguments + ) + + something_pb = types_pb.Something() + something_to_pb( + something, + something_pb + ) + + some_bytes = something_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.modelling_kind = modelling_kind_to_pb( + that.modelling_kind + ) + + if that.optional_modelling_kind is not None: + target.optional_modelling_kind = modelling_kind_to_pb( + that.optional_modelling_kind + ) + + +class _ToPbTransformer( + types.AbstractTransformer[google.protobuf.message.Message] +): + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer + class. We made this decision with the compactness of messages in mind. + """ + def transform_something( + self, + that: types.Something + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Something`. + """ + result = types_pb.Something() + something_to_pb(that, result) + return result + + +_TO_PB_TRANSFORMER = _ToPbTransformer() + + +def to_pb( + that: types.Class, +) -> google.protobuf.message.Message: + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer message type instead of the choice (union) type. + We made this decision with the compactness of messages in mind as choice types + would occupy a tiny bit more space. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + from aas_core3_protobuf.pbization import to_pb + + instance = types.Something( + ... # some constructor arguments + ) + + instance_pb = to_pb( + instance + ) + + some_bytes = instance_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + return _TO_PB_TRANSFORMER.transform(that) + + +# endregion To Protocol Buffers + + +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. diff --git a/test_data/python_protobuf/test_main/concrete_class_with_enum/expected_output/stdout.txt b/test_data/python_protobuf/test_main/concrete_class_with_enum/expected_output/stdout.txt new file mode 100644 index 00000000..2d755fc5 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_enum/expected_output/stdout.txt @@ -0,0 +1 @@ +Code generated to: diff --git a/test_data/python_protobuf/test_main/concrete_class_with_enum/input/snippets/base_qualified_module_name.txt b/test_data/python_protobuf/test_main/concrete_class_with_enum/input/snippets/base_qualified_module_name.txt new file mode 100644 index 00000000..74a993a9 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_enum/input/snippets/base_qualified_module_name.txt @@ -0,0 +1 @@ +aas_core3 \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_enum/input/snippets/qualified_module_name_for_protobuf_library.txt b/test_data/python_protobuf/test_main/concrete_class_with_enum/input/snippets/qualified_module_name_for_protobuf_library.txt new file mode 100644 index 00000000..3ae856f7 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_enum/input/snippets/qualified_module_name_for_protobuf_library.txt @@ -0,0 +1 @@ +aas_core3_protobuf \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_enum/meta_model.py b/test_data/python_protobuf/test_main/concrete_class_with_enum/meta_model.py new file mode 100644 index 00000000..196aebbc --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_enum/meta_model.py @@ -0,0 +1,21 @@ +class Modelling_kind(Enum): + Template = "Template" + Instance = "Instance" + + +class Something: + modelling_kind: Modelling_kind + optional_modelling_kind: Optional[Modelling_kind] + + def __init__( + self, + modelling_kind: Modelling_kind, + optional_modelling_kind: Optional[Modelling_kind], + ) -> None: + self.modelling_kind = modelling_kind + self.optional_modelling_kind = optional_modelling_kind + + +__version__ = "V198.4" + +__xml_namespace__ = "https://dummy/198/4" diff --git a/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/expected_output/pbization.py b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/expected_output/pbization.py new file mode 100644 index 00000000..31c4b456 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/expected_output/pbization.py @@ -0,0 +1,328 @@ +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. + + +"""Convert instances from and to Protocol Buffers.""" + + +from typing import Mapping, TypeVar + +import google.protobuf.message + +from aas_core3 import types +import aas_core3_protobuf.types_pb2 as types_pb + + +# region From Protocol Buffers + + +def item_from_pb( + that: types_pb.Item +) -> types.Item: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import item_from_pb + + some_bytes = b'... serialized types_pb.Item ...' + item_pb = types_pb.Item() + item_pb.FromString( + some_bytes + ) + + item = item_from_pb( + item_pb + ) + # Do something with the item... + + """ + return types.Item( + some_str=that.some_str + ) + + +def something_from_pb( + that: types_pb.Something +) -> types.Something: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_from_pb + + some_bytes = b'... serialized types_pb.Something ...' + something_pb = types_pb.Something() + something_pb.FromString( + some_bytes + ) + + something = something_from_pb( + something_pb + ) + # Do something with the something... + + """ + return types.Something( + items=list(map( + item_from_pb, + that.items + )), + optional_items=( + list(map( + item_from_pb, + that.optional_items + )) + if len(that.optional_items) > 0 + else None + ) + ) + + +# fmt: off +_FROM_PB_MAP = { + types_pb.Item: + item_from_pb, + types_pb.Something: + something_from_pb +} +# fmt: on + + +def from_pb( + that: google.protobuf.message.Message +) -> types.Class: + """ + Parse ``that`` Protocol Buffer into a model instance. + + The concrete parsing is determined based on the runtime type of ``that`` + Protocol Buffer. It is assumed that ``that`` is an instance of a message + coming from the Protocol Buffer definitions corresponding to the meta-model. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import from_pb + + some_bytes = b'... serialized types_pb.Item ...' + instance_pb = types_pb.Item() + instance_pb.FromString( + some_bytes + ) + + instance = from_pb( + instance_pb + ) + # Do something with the instance... + """ + from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + + if from_pb_func is None: + raise ValueError( + f"We do not know how to parse the protocol buffer " + f"of type {that.__class__} into a model instance." + ) + + result = from_pb_func(that) # type: ignore + assert isinstance(result, types.Class) + return result + + +# endregion From Protocol Buffers + + +# region To Protocol Buffers + + +T = TypeVar("T") + + +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): + """ + Visit instances in context with double-dispatch. + + This class is meant to be inherited from. If you do not override a method, + it will raise an exception. This is a partial visitor, meaning that some + visits are unexpected by design. + """ + # pylint: disable=missing-docstring + + def visit_item_with_context( + self, + that: types.Item, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + def visit_something_with_context( + self, + that: types.Something, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + +def item_to_pb( + that: types.Item, + target: types_pb.Item +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import item_to_pb + + item = types.Item( + ... # some constructor arguments + ) + + item_pb = types_pb.Item() + item_to_pb( + item, + item_pb + ) + + some_bytes = item_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.some_str = that.some_str + + +def something_to_pb( + that: types.Something, + target: types_pb.Something +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_to_pb + + something = types.Something( + ... # some constructor arguments + ) + + something_pb = types_pb.Something() + something_to_pb( + something, + something_pb + ) + + some_bytes = something_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + for items_item in that.items: + items_item_pb = target.items.add() + item_to_pb( + items_item, + items_item_pb) + + if that.optional_items is not None: + for optional_items_item in that.optional_items: + optional_items_item_pb = target.optional_items.add() + item_to_pb( + optional_items_item, + optional_items_item_pb) + + +class _ToPbTransformer( + types.AbstractTransformer[google.protobuf.message.Message] +): + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer + class. We made this decision with the compactness of messages in mind. + """ + def transform_item( + self, + that: types.Item + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Item`. + """ + result = types_pb.Item() + item_to_pb(that, result) + return result + + def transform_something( + self, + that: types.Something + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Something`. + """ + result = types_pb.Something() + something_to_pb(that, result) + return result + + +_TO_PB_TRANSFORMER = _ToPbTransformer() + + +def to_pb( + that: types.Class, +) -> google.protobuf.message.Message: + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer message type instead of the choice (union) type. + We made this decision with the compactness of messages in mind as choice types + would occupy a tiny bit more space. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + from aas_core3_protobuf.pbization import to_pb + + instance = types.Item( + ... # some constructor arguments + ) + + instance_pb = to_pb( + instance + ) + + some_bytes = instance_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + return _TO_PB_TRANSFORMER.transform(that) + + +# endregion To Protocol Buffers + + +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. diff --git a/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/expected_output/stdout.txt b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/expected_output/stdout.txt new file mode 100644 index 00000000..2d755fc5 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/expected_output/stdout.txt @@ -0,0 +1 @@ +Code generated to: diff --git a/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/input/snippets/base_qualified_module_name.txt b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/input/snippets/base_qualified_module_name.txt new file mode 100644 index 00000000..74a993a9 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/input/snippets/base_qualified_module_name.txt @@ -0,0 +1 @@ +aas_core3 \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/input/snippets/qualified_module_name_for_protobuf_library.txt b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/input/snippets/qualified_module_name_for_protobuf_library.txt new file mode 100644 index 00000000..3ae856f7 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/input/snippets/qualified_module_name_for_protobuf_library.txt @@ -0,0 +1 @@ +aas_core3_protobuf \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/meta_model.py b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/meta_model.py new file mode 100644 index 00000000..747452d9 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_list_of_instances/meta_model.py @@ -0,0 +1,19 @@ +class Item: + some_str: str + + def __init__(self, some_str: str) -> None: + self.some_str = some_str + + +class Something: + items: List[Item] + optional_items: Optional[List[Item]] + + def __init__(self, items: List[Item], optional_items: Optional[List[Item]]) -> None: + self.items = items + self.optional_items = optional_items + + +__version__ = "V198.4" + +__xml_namespace__ = "https://dummy/198/4" diff --git a/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/expected_output/pbization.py b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/expected_output/pbization.py new file mode 100644 index 00000000..080d6875 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/expected_output/pbization.py @@ -0,0 +1,274 @@ +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. + + +"""Convert instances from and to Protocol Buffers.""" + + +from typing import Mapping, TypeVar + +import google.protobuf.message + +from aas_core3 import types +import aas_core3_protobuf.types_pb2 as types_pb + + +# region From Protocol Buffers + + +def something_from_pb( + that: types_pb.Something +) -> types.Something: + """ + Parse ``that`` Protocol Buffer to an instance of a concrete class. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_from_pb + + some_bytes = b'... serialized types_pb.Something ...' + something_pb = types_pb.Something() + something_pb.FromString( + some_bytes + ) + + something = something_from_pb( + something_pb + ) + # Do something with the something... + + """ + return types.Something( + some_bool=that.some_bool, + some_int=that.some_int, + some_float=that.some_float, + some_str=that.some_str, + some_byte_array=bytearray(that.some_byte_array), + some_optional_bool=( + that.some_optional_bool + if that.HasField('some_optional_bool') + else None + ), + some_optional_int=( + that.some_optional_int + if that.HasField('some_optional_int') + else None + ), + some_optional_float=( + that.some_optional_float + if that.HasField('some_optional_float') + else None + ), + some_optional_str=( + that.some_optional_str + if that.HasField('some_optional_str') + else None + ), + some_optional_byte_array=( + bytearray(that.some_optional_byte_array) + if that.HasField('some_optional_byte_array') + else None + ) + ) + + +# fmt: off +_FROM_PB_MAP = { + types_pb.Something: + something_from_pb +} +# fmt: on + + +def from_pb( + that: google.protobuf.message.Message +) -> types.Class: + """ + Parse ``that`` Protocol Buffer into a model instance. + + The concrete parsing is determined based on the runtime type of ``that`` + Protocol Buffer. It is assumed that ``that`` is an instance of a message + coming from the Protocol Buffer definitions corresponding to the meta-model. + + Example usage: + + .. code-block:: + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import from_pb + + some_bytes = b'... serialized types_pb.Something ...' + instance_pb = types_pb.Something() + instance_pb.FromString( + some_bytes + ) + + instance = from_pb( + instance_pb + ) + # Do something with the instance... + """ + from_pb_func = _FROM_PB_MAP.get(that.__class__, None) + + if from_pb_func is None: + raise ValueError( + f"We do not know how to parse the protocol buffer " + f"of type {that.__class__} into a model instance." + ) + + result = from_pb_func(that) # type: ignore + assert isinstance(result, types.Class) + return result + + +# endregion From Protocol Buffers + + +# region To Protocol Buffers + + +T = TypeVar("T") + + +class _PartialVisitorWithContext(types.AbstractVisitorWithContext[T]): + """ + Visit instances in context with double-dispatch. + + This class is meant to be inherited from. If you do not override a method, + it will raise an exception. This is a partial visitor, meaning that some + visits are unexpected by design. + """ + # pylint: disable=missing-docstring + + def visit_something_with_context( + self, + that: types.Something, + context: T + ) -> None: + raise AssertionError(f"Unexpected visitation of {that.__class__}") + + +def something_to_pb( + that: types.Something, + target: types_pb.Something +) -> None: + """ + Set fields in ``target`` based on ``that`` instance. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + import aas_core3_protobuf.types_pb2 as types_pb + from aas_core3_protobuf.pbization import something_to_pb + + something = types.Something( + ... # some constructor arguments + ) + + something_pb = types_pb.Something() + something_to_pb( + something, + something_pb + ) + + some_bytes = something_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + target.some_bool = that.some_bool + + target.some_int = that.some_int + + target.some_float = that.some_float + + target.some_str = that.some_str + + target.some_byte_array = bytes(that.some_byte_array) + + if that.some_optional_bool is not None: + target.some_optional_bool = that.some_optional_bool + + if that.some_optional_int is not None: + target.some_optional_int = that.some_optional_int + + if that.some_optional_float is not None: + target.some_optional_float = that.some_optional_float + + if that.some_optional_str is not None: + target.some_optional_str = that.some_optional_str + + if that.some_optional_byte_array is not None: + target.some_optional_byte_array = bytes(that.some_optional_byte_array) + + +class _ToPbTransformer( + types.AbstractTransformer[google.protobuf.message.Message] +): + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer instead of the choice (union) Protocol Buffer + class. We made this decision with the compactness of messages in mind. + """ + def transform_something( + self, + that: types.Something + ) -> google.protobuf.message.Message: + """ + Convert ``that`` instance + to a :py:class:`types_pb.Something`. + """ + result = types_pb.Something() + something_to_pb(that, result) + return result + + +_TO_PB_TRANSFORMER = _ToPbTransformer() + + +def to_pb( + that: types.Class, +) -> google.protobuf.message.Message: + """ + Dispatch to-pb conversion to the concrete functions. + + The classes with descendants (i.e., subtypes) are always going to be converted + to their concrete Protocol Buffer message type instead of the choice (union) type. + We made this decision with the compactness of messages in mind as choice types + would occupy a tiny bit more space. + + Example usage: + + .. code-block:: + + import aas_core3.types as types + + from aas_core3_protobuf.pbization import to_pb + + instance = types.Something( + ... # some constructor arguments + ) + + instance_pb = to_pb( + instance + ) + + some_bytes = instance_pb.SerializeToString() + # Do something with some_bytes. For example, transfer them + # over the wire. + """ + return _TO_PB_TRANSFORMER.transform(that) + + +# endregion To Protocol Buffers + + +# Automatically generated with python_protobuf/main.py. +# Do NOT edit or append. diff --git a/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/expected_output/stdout.txt b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/expected_output/stdout.txt new file mode 100644 index 00000000..2d755fc5 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/expected_output/stdout.txt @@ -0,0 +1 @@ +Code generated to: diff --git a/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/input/snippets/base_qualified_module_name.txt b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/input/snippets/base_qualified_module_name.txt new file mode 100644 index 00000000..74a993a9 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/input/snippets/base_qualified_module_name.txt @@ -0,0 +1 @@ +aas_core3 \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/input/snippets/qualified_module_name_for_protobuf_library.txt b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/input/snippets/qualified_module_name_for_protobuf_library.txt new file mode 100644 index 00000000..3ae856f7 --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/input/snippets/qualified_module_name_for_protobuf_library.txt @@ -0,0 +1 @@ +aas_core3_protobuf \ No newline at end of file diff --git a/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/meta_model.py b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/meta_model.py new file mode 100644 index 00000000..ff0207fc --- /dev/null +++ b/test_data/python_protobuf/test_main/concrete_class_with_primitive_attributes/meta_model.py @@ -0,0 +1,41 @@ +class Something: + some_bool: bool + some_int: int + some_float: float + some_str: str + some_byte_array: bytearray + + some_optional_bool: Optional[bool] + some_optional_int: Optional[int] + some_optional_float: Optional[float] + some_optional_str: Optional[str] + some_optional_byte_array: Optional[bytearray] + + def __init__( + self, + some_bool: bool, + some_int: int, + some_float: float, + some_str: str, + some_byte_array: bytearray, + some_optional_bool: Optional[bool], + some_optional_int: Optional[int], + some_optional_float: Optional[float], + some_optional_str: Optional[str], + some_optional_byte_array: Optional[bytearray], + ) -> None: + self.some_bool = some_bool + self.some_int = some_int + self.some_float = some_float + self.some_str = some_str + self.some_byte_array = some_byte_array + self.some_optional_bool = some_optional_bool + self.some_optional_int = some_optional_int + self.some_optional_float = some_optional_float + self.some_optional_str = some_optional_str + self.some_optional_byte_array = some_optional_byte_array + + +__version__ = "V198.4" + +__xml_namespace__ = "https://dummy/198/4" diff --git a/tests/python_protobuf/__init__.py b/tests/python_protobuf/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/python_protobuf/test_main.py b/tests/python_protobuf/test_main.py new file mode 100644 index 00000000..8a3d3e62 --- /dev/null +++ b/tests/python_protobuf/test_main.py @@ -0,0 +1,120 @@ +# pylint: disable=missing-docstring + +import contextlib +import io +import os +import pathlib +import tempfile +import unittest + +import aas_core_meta.v3 + +import aas_core_codegen.main +import aas_core_codegen.python_protobuf.main +import tests.common + + +class Test_against_recorded(unittest.TestCase): + _REPO_DIR = pathlib.Path(os.path.realpath(__file__)).parent.parent.parent + PARENT_CASE_DIR = _REPO_DIR / "test_data" / "python_protobuf" / "test_main" + + def test_against_meta_models(self) -> None: + assert ( + Test_against_recorded.PARENT_CASE_DIR.exists() + and Test_against_recorded.PARENT_CASE_DIR.is_dir() + ), f"{Test_against_recorded.PARENT_CASE_DIR=}" + + # fmt: off + test_cases = ( + tests.common.find_meta_models_in_parent_directory_of_test_cases_and_modules( + parent_case_dir=Test_against_recorded.PARENT_CASE_DIR, + aas_core_meta_modules=[ + aas_core_meta.v3 + ] + ) + ) + # fmt: on + + for test_case in test_cases: + snippets_dir = test_case.case_dir / "input/snippets" + assert snippets_dir.exists() and snippets_dir.is_dir(), snippets_dir + + expected_output_dir = test_case.case_dir / "expected_output" + + with contextlib.ExitStack() as exit_stack: + if tests.common.RERECORD: + output_dir = expected_output_dir + expected_output_dir.mkdir(exist_ok=True, parents=True) + else: + assert ( + expected_output_dir.exists() and expected_output_dir.is_dir() + ), expected_output_dir + + # pylint: disable=consider-using-with + tmp_dir = tempfile.TemporaryDirectory() + exit_stack.push(tmp_dir) + output_dir = pathlib.Path(tmp_dir.name) + + params = aas_core_codegen.main.Parameters( + model_path=test_case.model_path, + target=aas_core_codegen.main.Target.PYTHON_PROTOBUF, + snippets_dir=snippets_dir, + output_dir=output_dir, + ) + + stdout = io.StringIO() + stderr = io.StringIO() + + return_code = aas_core_codegen.main.execute( + params=params, stdout=stdout, stderr=stderr + ) + + if stderr.getvalue() != "": + raise AssertionError( + f"Expected no stderr on valid models, but got:\n" + f"{stderr.getvalue()}" + ) + + self.assertEqual( + 0, return_code, "Expected 0 return code on valid models" + ) + + stdout_pth = expected_output_dir / "stdout.txt" + normalized_stdout = stdout.getvalue().replace( + str(output_dir), "" + ) + + if tests.common.RERECORD: + stdout_pth.write_text(normalized_stdout, encoding="utf-8") + else: + self.assertEqual( + normalized_stdout, + stdout_pth.read_text(encoding="utf-8"), + stdout_pth, + ) + + for relevant_rel_pth in [ + pathlib.Path("pbization.py"), + ]: + expected_pth = expected_output_dir / relevant_rel_pth + output_pth = output_dir / relevant_rel_pth + + if not output_pth.exists(): + raise FileNotFoundError( + f"The output file is missing: {output_pth}" + ) + + if tests.common.RERECORD: + expected_pth.write_text( + output_pth.read_text(encoding="utf-8"), encoding="utf-8" + ) + else: + self.assertEqual( + expected_pth.read_text(encoding="utf-8"), + output_pth.read_text(encoding="utf-8"), + f"The files {expected_pth} and {output_pth} do not match.", + ) + + +if __name__ == "__main__": + unittest.main()