Skip to content

Commit

Permalink
컨테이너 인터페이스 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
qodot committed Sep 20, 2023
1 parent 8a85f40 commit 03bf1c9
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/appl/i_container.py
@@ -0,0 +1,39 @@
from __future__ import annotations

import abc
import inspect
from typing import Any, Type, TypeVar

T = TypeVar("T")


class IContainer(abc.ABC):
def __init__(self) -> None:
self.obj_map = {}

def register(self, obj: Any) -> None:
self.obj_map[type(obj)] = obj

def resolve(self, type_: Type[T]) -> T:
impl_type = type_
if inspect.isabstract(type_):
impl_types = type_.__subclasses__()
if len(impl_types) == 0:
raise NotRegisteredTypeError(f"type: {type_}")

impl_type = impl_types[0]

try:
obj = self.obj_map[impl_type]
except KeyError:
raise NotRegisteredTypeError(f"type: {type_}")

return obj

@abc.abstractmethod
def compose(self) -> None:
pass


class NotRegisteredTypeError(Exception):
pass

0 comments on commit 03bf1c9

Please sign in to comment.