Skip to content

Commit

Permalink
Merge pull request #13 from iron3oxide/update-typeid-from-uuid-fix
Browse files Browse the repository at this point in the history
fix: Improve type prefix and suffix extraction
  • Loading branch information
akhundMurad authored Jun 16, 2024
2 parents 1860b0c + b8884c0 commit 850fe49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 12 additions & 2 deletions tests/test_typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid6

from typeid import TypeID
from typeid.errors import InvalidTypeIDStringException
from typeid.errors import SuffixValidationException


def test_default_suffix() -> None:
Expand Down Expand Up @@ -84,10 +84,20 @@ def test_construct_type_from_string_with_prefix_standalone() -> None:
assert isinstance(typeid.suffix, str)


def test_construct_type_from_string_with_multi_underscore_prefix() -> None:
string = "double_prefix_00041061050r3gg28a1c60t3gf"

typeid = TypeID.from_string(string)

assert isinstance(typeid, TypeID)
assert typeid.prefix == "double_prefix"
assert isinstance(typeid.suffix, str)


def test_construct_type_from_invalid_string() -> None:
string = "invalid_string_to_typeid"

with pytest.raises(InvalidTypeIDStringException):
with pytest.raises(SuffixValidationException):
TypeID.from_string(string)


Expand Down
11 changes: 5 additions & 6 deletions typeid/typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID:


def get_prefix_and_suffix(string: str) -> tuple:
parts = string.split("_")
prefix = None
parts = string.rsplit("_", 1)
if len(parts) == 1:
prefix = None
suffix = parts[0]
elif len(parts) == 2 and parts[0] != "":
suffix = parts[1]
prefix = parts[0]
else:
elif len(parts) == 2 and parts[0] == "":
raise InvalidTypeIDStringException(f"Invalid TypeID: {string}")
else:
prefix, suffix = parts

return prefix, suffix

Expand Down

0 comments on commit 850fe49

Please sign in to comment.