Skip to content

Commit 1b4df5f

Browse files
committed
entity: moved name to _name to prevent nameclash with "name" property #29
1 parent bbd67ab commit 1b4df5f

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

objectbox/model/entity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class _Entity(object):
3333
def __init__(self, user_type, uid: int = 0):
3434
self.user_type = user_type
3535
self.iduid = IdUid(0, uid)
36-
self.name = user_type.__name__
36+
self._name = user_type.__name__
3737
self.last_property_iduid = IdUid(0, 0)
3838

3939
self.properties: List[Property] = list() # List[Property]
@@ -64,7 +64,7 @@ def __call__(self, **properties):
6464
object_ = self.user_type()
6565
for prop_name, prop_val in properties.items():
6666
if not hasattr(object_, prop_name):
67-
raise Exception(f"Entity {self.name} has no property \"{prop_name}\"")
67+
raise Exception(f"Entity {self._name} has no property \"{prop_name}\"")
6868
setattr(object_, prop_name, prop_val)
6969
return object_
7070

@@ -119,7 +119,7 @@ def get_property(self, name: str):
119119
for prop in self.properties:
120120
if prop.name == name:
121121
return prop
122-
raise Exception(f"Property \"{name}\" not found in Entity: \"{self.name}\"")
122+
raise Exception(f"Property \"{name}\" not found in Entity: \"{self._name}\"")
123123

124124
def get_property_id(self, prop: Union[int, str, Property]) -> int:
125125
""" A convenient way to get the property ID regardless having its ID, name or Property. """

objectbox/model/idsync.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _save_model_json(self):
8686
for entity in self.model.entities:
8787
entity_json = {
8888
"id": str(entity.iduid),
89-
"name": entity.name,
89+
"name": entity._name,
9090
"lastPropertyId": str(entity.last_property_iduid),
9191
"properties": []
9292
}
@@ -178,7 +178,7 @@ def _validate_matching_prop(self, entity: _Entity, prop: Property, prop_json: Di
178178
elif prop.index is not None and "indexId" not in prop_json:
179179
raise ValueError("property has index, but index not found in JSON")
180180
except ValueError as error:
181-
raise ValueError(f"Property {entity.name}.{prop.name} mismatches property found in JSON file: {error}")
181+
raise ValueError(f"Property {entity._name}.{prop.name} mismatches property found in JSON file: {error}")
182182

183183
def _sync_index(self, entity: _Entity, prop: Property, prop_json: Optional[Dict[str, Any]]) -> bool:
184184
assert prop.index is not None
@@ -251,9 +251,9 @@ def _sync_entity(self, entity: _Entity) -> bool:
251251
# User provided a UID not matching any entity, make sure it's not assigned elsewhere
252252
self._validate_uid_unassigned(entity.uid)
253253
else:
254-
write_json = entity.name != entity_json["name"] # If renaming we shall update the JSON
254+
write_json = entity._name != entity_json["name"] # If renaming we shall update the JSON
255255
else:
256-
entity_json = self._find_entity_json_by_name(entity.name)
256+
entity_json = self._find_entity_json_by_name(entity._name)
257257

258258
# Write JSON if the number of properties differs (to handle removed property)
259259
if entity_json is not None:

objectbox/model/model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def entity(self, entity: _Entity):
4040
raise Exception(f"The given type is not an Entity: {type(entity)}. "
4141
f"Ensure to have an @Entity annotation on the class.")
4242
for other_entity in self.entities: # Linear search (we should't have many entities)
43-
if entity.name == other_entity.name:
44-
raise Exception(f"Duplicate entity: \"{entity.name}\"")
43+
if entity._name == other_entity._name:
44+
raise Exception(f"Duplicate entity: \"{entity._name}\"")
4545
self.entities.append(entity)
4646

4747
def validate_ids_assigned(self):
@@ -51,16 +51,16 @@ def validate_ids_assigned(self):
5151
for entity in self.entities:
5252
has_properties = len(entity.properties) > 0
5353
if not entity.iduid.is_assigned():
54-
raise ValueError(f"Entity \"{entity.name}\" ID/UID not assigned")
54+
raise ValueError(f"Entity \"{entity._name}\" ID/UID not assigned")
5555
for prop in entity.properties:
5656
if not prop.iduid.is_assigned():
57-
raise ValueError(f"Property \"{entity.name}.{prop.name}\" ID/UID not assigned")
57+
raise ValueError(f"Property \"{entity._name}.{prop.name}\" ID/UID not assigned")
5858
if prop.index is not None:
5959
has_indices = True
6060
if not prop.index.iduid.is_assigned():
61-
raise ValueError(f"Property index \"{entity.name}.{prop.name}\" ID/UID not assigned")
61+
raise ValueError(f"Property index \"{entity._name}.{prop.name}\" ID/UID not assigned")
6262
if has_properties and not entity.last_property_iduid.is_assigned():
63-
raise ValueError(f"Entity \"{entity.name}\" last property ID/UID not assigned")
63+
raise ValueError(f"Entity \"{entity._name}\" last property ID/UID not assigned")
6464
if has_entities and not self.last_entity_iduid.is_assigned():
6565
raise ValueError("Last entity ID/UID not assigned")
6666
if has_indices and not self.last_index_iduid.is_assigned():
@@ -96,7 +96,7 @@ def _create_property(self, prop: Property):
9696
self._create_index(prop.index)
9797

9898
def _create_entity(self, entity: _Entity):
99-
obx_model_entity(self._c_model, c_str(entity.name), entity.id, entity.uid)
99+
obx_model_entity(self._c_model, c_str(entity._name), entity.id, entity.uid)
100100
for prop in entity.properties:
101101
self._create_property(prop)
102102
obx_model_entity_last_property_id(self._c_model, entity.last_property_iduid.id, entity.last_property_iduid.uid)

0 commit comments

Comments
 (0)