Skip to content

Commit

Permalink
Fixed sytleguide and type assignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
LazeringDeath committed Jul 18, 2024
1 parent 661482d commit 019cef9
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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":
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions packages/service/tests/unit/test_default_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from google.protobuf import type_pb2

from ni_measurement_plugin_sdk_service._internal.parameter import (
default_value,
_get_type,
)


Expand All @@ -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

0 comments on commit 019cef9

Please sign in to comment.