Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLATFORM-1762 Create get_services_identifiers_by_tag_name method at container #20

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 43 additions & 29 deletions src/pypendency/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,15 @@ def set(self, identifier: str, service: object, tags: Optional[Set[Tag]] = None)
for tag in tags:
self.__add_service_to_tag_group(tag, identifier)

def has(self, identifier: str) -> bool:
jorgemo-fever marked this conversation as resolved.
Show resolved Hide resolved
return identifier in self._service_mapping

def get(self, identifier: str) -> Optional[object]:
if self.is_resolved() is False:
self.resolve()

return self._do_get(identifier)

def get_by_tag(self, tag: Tag) -> Set[object]:
if self.is_resolved() is False:
self.resolve()

if tag not in self._tags_mapping:
raise exceptions.TagNotFoundInContainer(tag.identifier)

return set([self._do_get(service) for service in self._tags_mapping[tag]])

def get_by_tag_name(self, tag_identifier: str, tag_value: Optional[object] = Tag.UNSET_VALUE) -> Set[object]:
if self.is_resolved() is False:
self.resolve()

services = set()
for tag, tagged_services in self._tags_mapping.items():
if tag.identifier == tag_identifier and (tag_value == Tag.UNSET_VALUE or tag.value == tag_value):
for tagged_service in tagged_services:
services.add(self._do_get(tagged_service))

return services

def get_service_tags(self, service_identifier: str) -> Set[Tag]:
if self.is_resolved() is False:
self.resolve()

return {tag for tag, services in self._tags_mapping.items() if service_identifier in services}

def _do_get(self, identifier: str) -> Optional[object]:
empty = object()

Expand Down Expand Up @@ -149,5 +125,43 @@ def __instance_from_fqn(self, fully_qualified_name: str, args: list, kwargs: dic
except TypeError as e:
raise exceptions.ServiceInstantiationFailed(fully_qualified_name) from e

def has(self, identifier: str) -> bool:
return identifier in self._service_mapping
def get_service_identifier(self, service: Union[object, Definition]) -> str:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this isn't new, it was moved, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has was moved, and get_service_identifier is the only new method i created

if self.is_resolved() is False:
self.resolve()

matching_identifiers: Set[str] = {k for k, v in self._service_mapping.items() if v == service}

if len(matching_identifiers) == 0:
raise exceptions.ServiceNotFoundInContainer(service.__str__())

if len(matching_identifiers) > 1:
raise exceptions.ServiceIsRegisteredWithMultipleIdentifiers(service)

return matching_identifiers.pop()

def get_by_tag(self, tag: Tag) -> Set[object]:
if self.is_resolved() is False:
self.resolve()

if tag not in self._tags_mapping:
raise exceptions.TagNotFoundInContainer(tag.identifier)

return set([self._do_get(service) for service in self._tags_mapping[tag]])

def get_by_tag_name(self, tag_identifier: str, tag_value: Optional[object] = Tag.UNSET_VALUE) -> Set[object]:
if self.is_resolved() is False:
self.resolve()

services = set()
for tag, tagged_services in self._tags_mapping.items():
if tag.identifier == tag_identifier and (tag_value == Tag.UNSET_VALUE or tag.value == tag_value):
for tagged_service in tagged_services:
services.add(self._do_get(tagged_service))

return services

def get_service_tags(self, service_identifier: str) -> Set[Tag]:
if self.is_resolved() is False:
self.resolve()

return {tag for tag, services in self._tags_mapping.items() if service_identifier in services}
11 changes: 11 additions & 0 deletions src/pypendency/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Union

from pypendency.definition import Definition
from pypendency.tag import Tag


Expand Down Expand Up @@ -34,6 +37,7 @@ def __init__(self, fully_qualified_name: str):
f"Container can't locate any class in {fully_qualified_name}"
)


class ServiceInstantiationFailed(Exception):
def __init__(self, service_fqn: str) -> None:
self.service_fqn = service_fqn
Expand All @@ -45,6 +49,13 @@ def __init__(self, tag_identifier: str) -> None:
self.tag_identifier = tag_identifier
super().__init__(f"The tag '{tag_identifier}' does not exist in the container")


class PypendencyCallbackException(Exception):
def __init__(self) -> None:
super().__init__(f"Exception on_resolved_callback")


class ServiceIsRegisteredWithMultipleIdentifiers(Exception):
def __init__(self, service: Union[object, Definition]) -> None:
self.service = service
super().__init__(f"The service {service} is registered with multiple identifiers")
25 changes: 25 additions & 0 deletions tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ def test_get_service_tags(self):
self.assertEqual(1, len(tags))
self.assertEqual(list(tags)[0], test_tag)

def test_get_service_identifier_raises_when_registered_multiple_times(self):
container = Container([])
test_service = object()
container.set("test_service", test_service)
container.set("another_test_service", test_service)

with self.assertRaises(exceptions.ServiceIsRegisteredWithMultipleIdentifiers):
container.get_service_identifier(test_service)

def test_get_service_identifier_raises_when_service_not_registered(self):
container = Container([])
test_service = object()

with self.assertRaises(exceptions.ServiceNotFoundInContainer):
container.get_service_identifier(test_service)

def test_get_service_identifier(self):
container = Container([])
test_service = object()
container.set("test_service", test_service)

identifier = container.get_service_identifier(test_service)

self.assertEqual("test_service", identifier)

def test_has(self):
container = Container([
Definition("example", "example.fqn"),
Expand Down