Skip to content

Commit b6ab93e

Browse files
N-giveNathan Givens
andauthored
IWF-684 allow None as internal channel prefix type (#93)
* IWF-684 allow `None` as internal channel prefix type --------- Co-authored-by: Nathan Givens <[email protected]>
1 parent 8a909d3 commit b6ab93e

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

iwf/communication_schema.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass, field
22
from enum import Enum
3-
from typing import List, Optional
3+
from typing import List, Optional, Union
44

55

66
class CommunicationMethodType(Enum):
@@ -22,15 +22,23 @@ def signal_channel_def(cls, name: str, value_type: type):
2222
)
2323

2424
@classmethod
25-
def internal_channel_def(cls, name: str, value_type: type):
25+
def internal_channel_def(cls, name: str, value_type: Union[type, None]):
2626
return CommunicationMethod(
27-
name, CommunicationMethodType.InternalChannel, value_type, False
27+
name,
28+
CommunicationMethodType.InternalChannel,
29+
value_type if value_type is not None else type(None),
30+
False,
2831
)
2932

3033
@classmethod
31-
def internal_channel_def_by_prefix(cls, name_prefix: str, value_type: type):
34+
def internal_channel_def_by_prefix(
35+
cls, name_prefix: str, value_type: Union[type, None]
36+
):
3237
return CommunicationMethod(
33-
name_prefix, CommunicationMethodType.InternalChannel, value_type, True
38+
name_prefix,
39+
CommunicationMethodType.InternalChannel,
40+
value_type if value_type is not None else type(None),
41+
True,
3442
)
3543

3644

iwf/tests/test_internal_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_workflow_states(self) -> StateSchema:
120120
def get_communication_schema(self) -> CommunicationSchema:
121121
return CommunicationSchema.create(
122122
CommunicationMethod.internal_channel_def(test_channel_name1, int),
123-
CommunicationMethod.internal_channel_def(test_channel_name2, type(None)),
123+
CommunicationMethod.internal_channel_def(test_channel_name2, None),
124124
CommunicationMethod.internal_channel_def(test_channel_name3, int),
125125
CommunicationMethod.internal_channel_def(test_channel_name4, str),
126126
CommunicationMethod.internal_channel_def_by_prefix(

iwf/tests/test_internal_channel_with_no_prefix_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_workflow_states(self) -> StateSchema:
9494

9595
def get_communication_schema(self) -> CommunicationSchema:
9696
return CommunicationSchema.create(
97-
CommunicationMethod.internal_channel_def(internal_channel_name, type(None)),
97+
CommunicationMethod.internal_channel_def(internal_channel_name, None),
9898
# Defining a standard channel (non-prefix) to make sure messages to the channel with a suffix added will not be accepted
9999
CommunicationMethod.internal_channel_def(test_non_prefix_channel_name, str),
100100
)

iwf/type_store.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ def __init__(self, class_type: Type):
2323
self._prefix_to_type_store = dict()
2424

2525
def is_valid_name_or_prefix(self, name: str) -> bool:
26-
t = self._do_get_type(name)
27-
return t is not None
26+
return self._validate_name(name)
2827

2928
def get_type(self, name: str) -> type:
30-
t = self._do_get_type(name)
29+
is_registered = self._validate_name(name)
3130

31+
if not is_registered:
32+
raise NotRegisteredError(f"{self._class_type} not registered: {name}")
33+
34+
t = self._do_get_type(name)
3235
if t is None:
3336
raise NotRegisteredError(f"{self._class_type} not registered: {name}")
3437

@@ -41,18 +44,26 @@ def add_internal_channel_def(self, obj: CommunicationMethod):
4144
)
4245
self._do_add_to_store(obj.is_prefix, obj.name, obj.value_type)
4346

44-
def _do_get_type(self, name: str) -> Optional[type]:
47+
def _validate_name(self, name: str) -> bool:
4548
if name in self._name_to_type_store:
46-
return self._name_to_type_store[name]
49+
return True
50+
51+
for prefix in self._prefix_to_type_store.keys():
52+
if name.startswith(prefix):
53+
return True
4754

48-
prefixes = self._prefix_to_type_store.keys()
55+
return False
4956

50-
first = next((prefix for prefix in prefixes if name.startswith(prefix)), None)
57+
def _do_get_type(self, name: str) -> Optional[type]:
58+
if name in self._name_to_type_store:
59+
t = self._name_to_type_store[name]
60+
return t if t is not None else type(None)
5161

52-
if first is None:
53-
return None
62+
for prefix, t in self._prefix_to_type_store.items():
63+
if name.startswith(prefix):
64+
return t if t is not None else type(None)
5465

55-
return self._prefix_to_type_store.get(first, None)
66+
return None
5667

5768
def _do_add_to_store(self, is_prefix: bool, name: str, t: Optional[type]):
5869
if is_prefix:

0 commit comments

Comments
 (0)