Skip to content

Commit 850fe49

Browse files
authored
Merge pull request #13 from iron3oxide/update-typeid-from-uuid-fix
fix: Improve type prefix and suffix extraction
2 parents 1860b0c + b8884c0 commit 850fe49

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

tests/test_typeid.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import uuid6
33

44
from typeid import TypeID
5-
from typeid.errors import InvalidTypeIDStringException
5+
from typeid.errors import SuffixValidationException
66

77

88
def test_default_suffix() -> None:
@@ -84,10 +84,20 @@ def test_construct_type_from_string_with_prefix_standalone() -> None:
8484
assert isinstance(typeid.suffix, str)
8585

8686

87+
def test_construct_type_from_string_with_multi_underscore_prefix() -> None:
88+
string = "double_prefix_00041061050r3gg28a1c60t3gf"
89+
90+
typeid = TypeID.from_string(string)
91+
92+
assert isinstance(typeid, TypeID)
93+
assert typeid.prefix == "double_prefix"
94+
assert isinstance(typeid.suffix, str)
95+
96+
8797
def test_construct_type_from_invalid_string() -> None:
8898
string = "invalid_string_to_typeid"
8999

90-
with pytest.raises(InvalidTypeIDStringException):
100+
with pytest.raises(SuffixValidationException):
91101
TypeID.from_string(string)
92102

93103

typeid/typeid.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID:
8080

8181

8282
def get_prefix_and_suffix(string: str) -> tuple:
83-
parts = string.split("_")
84-
prefix = None
83+
parts = string.rsplit("_", 1)
8584
if len(parts) == 1:
85+
prefix = None
8686
suffix = parts[0]
87-
elif len(parts) == 2 and parts[0] != "":
88-
suffix = parts[1]
89-
prefix = parts[0]
90-
else:
87+
elif len(parts) == 2 and parts[0] == "":
9188
raise InvalidTypeIDStringException(f"Invalid TypeID: {string}")
89+
else:
90+
prefix, suffix = parts
9291

9392
return prefix, suffix
9493

0 commit comments

Comments
 (0)