Skip to content

Commit 57b5857

Browse files
author
Ivan Dlugos
committed
make Entity an annotation (python decorator) and start building a model in C
1 parent e9cdb3e commit 57b5857

7 files changed

Lines changed: 51 additions & 17 deletions

File tree

objectbox/builder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ def __init__(self):
88

99
def model(self, model: Model) -> 'Builder':
1010
self.__model = model
11-
pass
11+
return self
12+
13+
def build(self) -> 'ObjectBox':
14+
return self

objectbox/model/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
__all__ = [
66
'Model',
77
'Entity',
8-
98
'Id'
109
]

objectbox/model/entity.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
1-
class Entity:
2-
pass
1+
# entity decorator
2+
class _Entity(object):
3+
def __init__(self, cls, id: int, uid: int):
4+
self.cls = cls
5+
self.name: str = cls.__name__
6+
self.id = id
7+
self.uid = uid
8+
9+
# currently, ID and UID are mandatory and are not fetched from the model.json
10+
if id <= 0:
11+
raise ValueError("invalid or no 'id; given in the @Entity annotation")
12+
13+
if uid <= 0:
14+
raise ValueError("invalid or no 'uid' given in the @Entity annotation")
15+
16+
def __call__(self):
17+
return self.cls()
18+
19+
20+
# wrap _Entity to allow @Entity(id=, uid=), i.e. no class argument
21+
def Entity(cls=None, id: int = 0, uid: int = 0):
22+
if cls:
23+
return _Entity(cls, id, uid)
24+
else:
25+
def wrapper(cls):
26+
return _Entity(cls, id, uid)
27+
28+
return wrapper

objectbox/model/model.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from objectbox.model import Entity
1+
from objectbox.model.entity import _Entity
2+
from objectbox.c import *
3+
24
from typing import List
35

46

@@ -20,13 +22,15 @@ class Model:
2022
retired_index_uids: List[int]
2123
retired_relation_uids: List[int]
2224

23-
__entities: List[type]
24-
2525
def __init__(self):
26-
self.__entities = list()
26+
self.__entities: List[type] = list()
27+
self.__model = obx_model_create()
28+
29+
def entity(self, entity: _Entity):
30+
if not isinstance(entity, _Entity):
31+
raise ValueError("Given type is not an Entity. Are you passing an instance instead of a type or did you "
32+
"forget the '@Entity' annotation?")
2733

28-
def add_entity(self, entity_type: type):
29-
if not issubclass(entity_type, Entity):
30-
raise ValueError("given type is not a subclass of 'Entity'")
34+
obx_model_entity(self.__model, entity.name.encode('utf-8'), entity.id, entity.uid)
3135

32-
self.__entities.append(entity_type)
36+
self.__entities.append(entity)

objectbox/model/properties.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class Property:
2-
__slots__ = ['is_id', 'py_type']
2+
__is_id: bool
3+
__py_type: type
34

45
def __init__(self, py_type: type):
5-
self.is_id = isinstance(self, Id)
6-
self.py_type = py_type
6+
self.__is_id = isinstance(self, Id)
7+
self.__py_type = py_type
78

89

910
class Id(Property):

tests/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from objectbox.model import *
22

33

4-
class TestEntity(Entity):
4+
@Entity(id=1, uid=1)
5+
class TestEntity:
56
id = Id()

tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def test_model():
66
model = objectbox.Model()
7-
model.add_entity(TestEntity)
7+
model.entity(TestEntity)
88

99
builder = objectbox.Builder()
1010
builder.model(model)

0 commit comments

Comments
 (0)