diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index deb932f2..a0f96b08 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -21,6 +21,7 @@ - Removed comments on line no.462 and 467 in sql_translator.py (#659) - Added logs in src/wq/ql/notify.py (#656) - Added logs in src/wq/core/task.py (#662) +- Replaced entity with getter (#652) ### Bug fixes diff --git a/src/translators/sql_translator.py b/src/translators/sql_translator.py index 2b4f508e..fe66fc03 100644 --- a/src/translators/sql_translator.py +++ b/src/translators/sql_translator.py @@ -73,8 +73,6 @@ def current_timex() -> str: return datetime.utcnow().isoformat(timespec='milliseconds') -# TODO: use below getters everywhere rather than entity id and type strings! - def entity_id(entity: dict) -> Optional[str]: """ Safely get the NGSI ID of the given entity. @@ -216,7 +214,7 @@ def insert(self, entities, fiware_service=None, fiware_servicepath='/'): if len(service_paths) == 1: entities_by_type = {} for e in entities: - entities_by_type.setdefault(e['type'], []).append(e) + entities_by_type.setdefault(entity_type(e), []).append(e) res = None for et in entities_by_type.keys(): @@ -233,7 +231,7 @@ def insert(self, entities, fiware_service=None, fiware_servicepath='/'): for path in entities_by_service_path.keys(): entities_by_type = {} for e in entities_by_service_path[path]: - entities_by_type.setdefault(e['type'], []).append(e) + entities_by_type.setdefault(entity_type(e), []).append(e) res = None for et in entities_by_type.keys(): @@ -250,7 +248,7 @@ def insert(self, entities, fiware_service=None, fiware_servicepath='/'): return res def _insert_entities_of_type(self, - entity_type, + entityType, entities, fiware_service=None, fiware_servicepath='/'): @@ -258,9 +256,9 @@ def _insert_entities_of_type(self, # Also, an entity can't have an attribute with the same name # as that specified by ORIGINAL_ENTITY_COL_NAME. for e in entities: - if e[NGSI_TYPE] != entity_type: + if entity_type(e) != entityType: msg = "Entity {} is not of type {}." - raise ValueError(msg.format(e[NGSI_ID], entity_type)) + raise ValueError(msg.format(entity_id(e), entity_type)) if self.TIME_INDEX_NAME not in e: msg = "Translating entity without TIME_INDEX. " \ @@ -270,7 +268,7 @@ def _insert_entities_of_type(self, if ORIGINAL_ENTITY_COL in e: raise ValueError( - f"Entity {e[NGSI_ID]} has a reserved attribute name: " + + f"Entity {entity_id(e)} has a reserved attribute name: " + "'{ORIGINAL_ENTITY_COL_NAME}'") # Define column types @@ -293,7 +291,7 @@ def _insert_entities_of_type(self, } for e in entities: - entity_id = e.get('id') + entityId = entity_id(e) for attr in iter_entity_attrs(e): if attr == self.TIME_INDEX_NAME: continue @@ -330,10 +328,10 @@ def _insert_entities_of_type(self, col = self._ea2cn(attr) original_attrs[col] = (attr, attr_t) - table[col] = self._compute_type(entity_id, attr_t, e[attr]) + table[col] = self._compute_type(entityId, attr_t, e[attr]) # Create/Update metadata table for this type - table_name = self._et2tn(entity_type, fiware_service) + table_name = self._et2tn(entityType, fiware_service) modified = self._update_metadata_table(table_name, original_attrs) # Sort out data table. if modified and modified == original_attrs.keys(): @@ -468,9 +466,9 @@ def _preprocess_values(self, e, original_attrs, col_names, values = [] for cn in col_names: if cn == ENTITY_TYPE_COL: - values.append(e['type']) + values.append(entity_type(e)) elif cn == ENTITY_ID_COL: - values.append(e['id']) + values.append(entity_id(e)) elif cn == self.TIME_INDEX_NAME: values.append(e[self.TIME_INDEX_NAME]) elif cn == FIWARE_SERVICEPATH: