Skip to content

Commit

Permalink
entity: allow members init from constructor objectbox#24
Browse files Browse the repository at this point in the history
  • Loading branch information
loryruta committed Apr 10, 2024
1 parent 5df92a4 commit ff4c72a
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions objectbox/model/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ def __init__(self, cls, id: int, uid: int):
self.id_property = None
self.fill_properties()

def __call__(self, *args):
return self.cls(*args)
def __call__(self, **properties):
""" The constructor of the user Entity class. """
object_ = self.cls()
for prop_name, prop_val in properties.items():
if not hasattr(object_, prop_name):
raise Exception(f"Entity {self.name} has no property \"{prop_name}\"")
setattr(object_, prop_name, prop_val)
return object_

def fill_properties(self):
# TODO allow subclassing and support entities with __slots__ defined
Expand Down Expand Up @@ -235,8 +241,8 @@ def unmarshal(self, data: bytes):
return obj


def Entity(id: int = 0, uid: int = 0):
def Entity(id: int = 0, uid: int = 0) -> Callable[[Type], _Entity]:
""" Entity decorator that wraps _Entity to allow @Entity(id=, uid=); i.e. no class arguments. """
def wrapper(cls):
return _Entity(cls, id, uid)
def wrapper(class_):
return _Entity(class_, id, uid)
return wrapper

0 comments on commit ff4c72a

Please sign in to comment.