Skip to content
Snippets Groups Projects
Commit 46fc68a9 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- Added control check while converting an invalid entry in grpc_to_enum() method
parent 186f5c42
No related branches found
No related tags found
1 merge request!235Release TeraFlowSDN 3.0
......@@ -306,4 +306,4 @@ def device_select(db_engine : Engine, request : DeviceFilter) -> DeviceList:
obj_list : List[DeviceModel] = query.filter(DeviceModel.device_uuid.in_(device_uuids)).all()
return [obj.dump(**dump_params) for obj in obj_list]
devices = run_transaction(sessionmaker(bind=db_engine), callback)
return DeviceList(devices=devices)
\ No newline at end of file
return DeviceList(devices=devices)
......@@ -14,16 +14,21 @@
import re
from enum import Enum
from typing import Optional
from typing import Any, Optional
# Enumeration classes are redundant with gRPC classes, but gRPC does not provide a programmatical method to retrieve
# the values it expects from strings containing the desired value symbol or its integer value, so a kind of mapping is
# required. Besides, ORM Models expect Enum classes in EnumeratedFields; we create specific and conveniently defined
# Enum classes to serve both purposes.
def grpc_to_enum(grpc_enum_class, orm_enum_class : Enum, grpc_enum_value, grpc_enum_prefix : Optional[str] = None):
def grpc_to_enum(
grpc_enum_class, orm_enum_class : Enum, grpc_enum_value, grpc_enum_prefix : Optional[str] = None,
fail_if_not_found : bool = False
) -> Optional[Any]:
enum_name = grpc_enum_class.Name(grpc_enum_value)
_orig_enum_name = enum_name
_orig_grpc_enum_prefix = grpc_enum_prefix
if grpc_enum_prefix is None:
grpc_enum_prefix = orm_enum_class.__name__.upper()
#grpc_enum_prefix = re.sub(r'^ORM_(.+)$', r'\1', grpc_enum_prefix)
......@@ -35,4 +40,7 @@ def grpc_to_enum(grpc_enum_class, orm_enum_class : Enum, grpc_enum_value, grpc_e
enum_name = enum_name.replace(grpc_enum_prefix, '')
orm_enum_value = orm_enum_class._member_map_.get(enum_name)
if orm_enum_value is None and fail_if_not_found:
MSG = 'Unable to map gRPC Enum Value ({:s} / {:s}) to ORM Enum Value; grpc_enum_prefix={:s}'
raise Exception(MSG.format(str(grpc_enum_value), str(_orig_enum_name), str(_orig_grpc_enum_prefix)))
return orm_enum_value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment