diff --git a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/_get_type.py b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/_get_type.py new file mode 100644 index 000000000..c216738bb --- /dev/null +++ b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/_get_type.py @@ -0,0 +1,36 @@ +from typing import Any + +from google.protobuf.descriptor_pb2 import FieldDescriptorProto +from google.protobuf.type_pb2 import Field + +_TYPE_DEFULAT_MAPPING = { + Field.TYPE_FLOAT: float(), + Field.TYPE_DOUBLE: float(), + Field.TYPE_INT32: int(), + Field.TYPE_INT64: int(), + Field.TYPE_UINT32: int(), + Field.TYPE_UINT64: int(), + Field.TYPE_BOOL: bool(), + Field.TYPE_STRING: str(), + Field.TYPE_ENUM: int(), +} + +_TYPE_FIELD_MAPPING = { + Field.TYPE_FLOAT: FieldDescriptorProto.TYPE_FLOAT, + Field.TYPE_DOUBLE: FieldDescriptorProto.TYPE_DOUBLE, + Field.TYPE_INT32: FieldDescriptorProto.TYPE_INT32, + Field.TYPE_INT64: FieldDescriptorProto.TYPE_INT64, + Field.TYPE_UINT32: FieldDescriptorProto.TYPE_UINT32, + Field.TYPE_UINT64: FieldDescriptorProto.TYPE_UINT64, + Field.TYPE_BOOL: FieldDescriptorProto.TYPE_BOOL, + Field.TYPE_STRING: FieldDescriptorProto.TYPE_STRING, + Field.TYPE_ENUM: FieldDescriptorProto.TYPE_ENUM, + Field.TYPE_MESSAGE: FieldDescriptorProto.TYPE_MESSAGE, +} + + +def get_type_default(type: Field.Kind.ValueType, repeated: bool) -> Any: + """Get the default value for the give type.""" + if repeated: + return list() + return _TYPE_DEFULAT_MAPPING.get(type) diff --git a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/decoder.py b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/decoder.py index d0b2e5c81..91959544d 100644 --- a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/decoder.py +++ b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/decoder.py @@ -58,7 +58,7 @@ def deserialize_parameters( def _deserialize_enum_parameter( - repeated: bool, field_value: Union[List[int], int], enum_type: Any + repeated: bool, field_value: Any, enum_type: Any ) -> Union[List[Any], Any]: """Convert all enums into the user defined enum type. diff --git a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/default_value.py b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/default_value.py deleted file mode 100644 index 8a54edf3a..000000000 --- a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/default_value.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import Any - -from google.protobuf import type_pb2 - -_TYPE_DEFULAT_MAPPING = { - type_pb2.Field.TYPE_FLOAT: float(), - type_pb2.Field.TYPE_DOUBLE: float(), - type_pb2.Field.TYPE_INT32: int(), - type_pb2.Field.TYPE_INT64: int(), - type_pb2.Field.TYPE_UINT32: int(), - type_pb2.Field.TYPE_UINT64: int(), - type_pb2.Field.TYPE_BOOL: bool(), - type_pb2.Field.TYPE_STRING: str(), - type_pb2.Field.TYPE_ENUM: int(), -} - - -def get_type_default(type: type_pb2.Field.Kind.ValueType, repeated: bool) -> Any: - """Get the default value for the give type.""" - if repeated: - return list() - return _TYPE_DEFULAT_MAPPING.get(type) diff --git a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/encoder.py b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/encoder.py index 16e246e9d..0fd12ead9 100644 --- a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/encoder.py +++ b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/encoder.py @@ -6,7 +6,7 @@ from google.protobuf import descriptor_pool, message_factory from google.protobuf.descriptor_pb2 import FieldDescriptorProto -from ni_measurement_plugin_sdk_service._internal.parameter.default_value import ( +from ni_measurement_plugin_sdk_service._internal.parameter._get_type import ( get_type_default, ) from ni_measurement_plugin_sdk_service._internal.parameter.metadata import ( diff --git a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/metadata.py b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/metadata.py index 652fa0504..c9db44531 100644 --- a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/metadata.py +++ b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/metadata.py @@ -10,7 +10,7 @@ ENUM_VALUES_KEY, TYPE_SPECIALIZATION_KEY, ) -from ni_measurement_plugin_sdk_service._internal.parameter.default_value import ( +from ni_measurement_plugin_sdk_service._internal.parameter._get_type import ( get_type_default, ) from ni_measurement_plugin_sdk_service.measurement.info import TypeSpecialization @@ -60,7 +60,7 @@ def initialize( field_name = underscore_display_name else: field_name = "".join( - char for char in underscore_display_name if char.isalnum() or char == "_" + char for char in underscore_display_name if char.isalnum() or char == "_" ) return ParameterMetadata( diff --git a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/serialization_descriptors.py b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/serialization_descriptors.py index ec7f10328..0a57fd1cf 100644 --- a/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/serialization_descriptors.py +++ b/packages/service/ni_measurement_plugin_sdk_service/_internal/parameter/serialization_descriptors.py @@ -4,9 +4,12 @@ from typing import List from google.protobuf import descriptor_pb2, descriptor_pool -from google.protobuf.descriptor_pb2 import FieldDescriptorProto, DescriptorProto +from google.protobuf.descriptor_pb2 import DescriptorProto, FieldDescriptorProto from ni_measurement_plugin_sdk_service._annotations import ENUM_VALUES_KEY +from ni_measurement_plugin_sdk_service._internal.parameter._get_type import ( + _TYPE_FIELD_MAPPING, +) from ni_measurement_plugin_sdk_service._internal.parameter.metadata import ( ParameterMetadata, ) @@ -20,7 +23,7 @@ def _create_enum_type_class( """Implement a enum class in 'file_descriptor'.""" enum_dict = loads(parameter_metadata.annotations[ENUM_VALUES_KEY]) enum_type_name = _get_enum_type(parameter_metadata).__name__ - + # if enum is a protobuf then enum_type_name is 1st letter of each enum name # e.g. {"NONE": 0, "RED": 1, "GREEN": 2} -> NRG if enum_type_name == "int" or enum_type_name == "NoneType": @@ -51,7 +54,7 @@ def _create_field( field_descriptor = message_proto.field.add() field_descriptor.number = index field_descriptor.name = metadata.field_name - field_descriptor.type = metadata.type + field_descriptor.type = _TYPE_FIELD_MAPPING[metadata.type] if metadata.repeated: field_descriptor.label = FieldDescriptorProto.LABEL_REPEATED @@ -92,17 +95,7 @@ def create_file_descriptor( input_metadata: List[ParameterMetadata], pool: descriptor_pool.DescriptorPool, ) -> None: - """Creates two message types in one file descriptor proto. - - Args: - service_class_name (str): Unique service name. - - output_metadata (List[ParameterMetadata]): Metadata of output parameters. - - input_metadata (List[ParameterMetadata]): Metadata of input parameters. - - pool (DescriptorPool): Descriptor pool holding file descriptors and enum classes. - """ + """Creates two message types in one file descriptor proto.""" try: pool.FindFileByName(service_name) except KeyError: diff --git a/packages/service/tests/unit/test_default_value.py b/packages/service/tests/unit/test_default_value.py index 42678a6ac..1be45f278 100644 --- a/packages/service/tests/unit/test_default_value.py +++ b/packages/service/tests/unit/test_default_value.py @@ -4,7 +4,7 @@ from google.protobuf import type_pb2 from ni_measurement_plugin_sdk_service._internal.parameter import ( - default_value, + _get_type, ) @@ -25,6 +25,6 @@ ], ) def test___get_default_value___returns_type_defaults(type, is_repeated, expected_default_value): - test_default_value = default_value.get_type_default(type, is_repeated) + test_default_value = _get_type.get_type_default(type, is_repeated) assert test_default_value == expected_default_value