From ce12776eb987e8a3e9104bf25d4b5e19fe4ea6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Tue, 6 Aug 2024 16:36:45 +0200 Subject: [PATCH] REMOVE strings2numbers in valueBson() and compoundValueBson() --- .../mongoBackend/MongoCommonSubscription.cpp | 8 ++-- src/lib/mongoBackend/MongoCommonUpdate.cpp | 8 ++-- src/lib/mongoBackend/compoundValueBson.cpp | 38 ++++--------------- src/lib/mongoBackend/compoundValueBson.h | 4 +- src/lib/mongoBackend/location.cpp | 4 +- src/lib/ngsi/ContextAttribute.cpp | 16 ++++---- src/lib/ngsi/ContextAttribute.h | 6 +-- 7 files changed, 29 insertions(+), 55 deletions(-) diff --git a/src/lib/mongoBackend/MongoCommonSubscription.cpp b/src/lib/mongoBackend/MongoCommonSubscription.cpp index b9b802dd1e..5751446d8e 100644 --- a/src/lib/mongoBackend/MongoCommonSubscription.cpp +++ b/src/lib/mongoBackend/MongoCommonSubscription.cpp @@ -142,7 +142,7 @@ static void setCustomHttpInfo(const HttpInfo& httpInfo, orion::BSONObjBuilder* b if (httpInfo.json->isObject()) { orion::BSONObjBuilder jsonBuilder; - compoundValueBson(httpInfo.json->childV, jsonBuilder, false); + compoundValueBson(httpInfo.json->childV, jsonBuilder); orion::BSONObj jsonBuilderObj = jsonBuilder.obj(); logStr = jsonBuilderObj.toString(); b->append(CSUB_JSON, jsonBuilderObj); @@ -150,7 +150,7 @@ static void setCustomHttpInfo(const HttpInfo& httpInfo, orion::BSONObjBuilder* b else // httpInfo.json->isVector(); { orion::BSONArrayBuilder jsonBuilder; - compoundValueBson(httpInfo.json->childV, jsonBuilder, false); + compoundValueBson(httpInfo.json->childV, jsonBuilder); orion::BSONArray jsonBuilderArr = jsonBuilder.arr(); logStr = jsonBuilderArr.toString(); b->append(CSUB_JSON, jsonBuilderArr); @@ -214,7 +214,7 @@ static void setCustomMqttInfo(const ngsiv2::MqttInfo& mqttInfo, orion::BSONObjBu if (mqttInfo.json->isObject()) { orion::BSONObjBuilder jsonBuilder; - compoundValueBson(mqttInfo.json->childV, jsonBuilder, false); + compoundValueBson(mqttInfo.json->childV, jsonBuilder); orion::BSONObj jsonBuilderObj = jsonBuilder.obj(); logStr = jsonBuilderObj.toString(); b->append(CSUB_JSON, jsonBuilderObj); @@ -222,7 +222,7 @@ static void setCustomMqttInfo(const ngsiv2::MqttInfo& mqttInfo, orion::BSONObjBu else // httpInfo.json->isVector(); { orion::BSONArrayBuilder jsonBuilder; - compoundValueBson(mqttInfo.json->childV, jsonBuilder, false); + compoundValueBson(mqttInfo.json->childV, jsonBuilder); orion::BSONArray jsonBuilderArr = jsonBuilder.arr(); logStr = jsonBuilderArr.toString(); b->append(CSUB_JSON, jsonBuilderArr); diff --git a/src/lib/mongoBackend/MongoCommonUpdate.cpp b/src/lib/mongoBackend/MongoCommonUpdate.cpp index df04541147..28c35bb1a3 100644 --- a/src/lib/mongoBackend/MongoCommonUpdate.cpp +++ b/src/lib/mongoBackend/MongoCommonUpdate.cpp @@ -3204,13 +3204,13 @@ static bool calculateOperator(ContextElementResponse* cerP, const std::string& o else if (child0->valueType == orion::ValueTypeVector) { orion::BSONArrayBuilder ba; - compoundValueBson(child0->childV, ba, false, false); + compoundValueBson(child0->childV, ba, false); b->append(valueKey, ba.arr()); } else if (child0->valueType == orion::ValueTypeObject) { orion::BSONObjBuilder bo; - compoundValueBson(child0->childV, bo, false, false); + compoundValueBson(child0->childV, bo, false); b->append(valueKey, bo.obj()); } else if (child0->valueType == orion::ValueTypeNotGiven) @@ -3314,13 +3314,13 @@ static bool calculateSetOperator(ContextElementResponse* cerP, orion::BSONObjBui else if (child->valueType == orion::ValueTypeVector) { orion::BSONArrayBuilder ba; - compoundValueBson(child->childV, ba, false, false); + compoundValueBson(child->childV, ba, false); b->append(valueKey, ba.arr()); } else if (child->valueType == orion::ValueTypeObject) { orion::BSONObjBuilder bo; - compoundValueBson(child->childV, bo, false, false); + compoundValueBson(child->childV, bo, false); b->append(valueKey, bo.obj()); } else if (child->valueType == orion::ValueTypeNotGiven) diff --git a/src/lib/mongoBackend/compoundValueBson.cpp b/src/lib/mongoBackend/compoundValueBson.cpp index 5f6bc9e920..6dca849eda 100644 --- a/src/lib/mongoBackend/compoundValueBson.cpp +++ b/src/lib/mongoBackend/compoundValueBson.cpp @@ -39,13 +39,10 @@ * * compoundValueBson (for arrays) - * -* strings2numbers is used only for the GEO_JSON generation logic to ensure NGSIv1 -* strings are converted to numbers (strings are not allowed in GeoJSON) -* * encode set to true only in the calculateOperator functions, eg. to avoid that * "$each" get replaced by ">each" (see https://github.com/telefonicaid/fiware-orion/issues/744#issuecomment-996752938) */ -void compoundValueBson(const std::vector& children, orion::BSONArrayBuilder& b, bool strings2numbers, bool encode) +void compoundValueBson(const std::vector& children, orion::BSONArrayBuilder& b, bool encode) { for (unsigned int ix = 0; ix < children.size(); ++ix) { @@ -53,15 +50,7 @@ void compoundValueBson(const std::vector& children, o if (child->valueType == orion::ValueTypeString) { - if ((strings2numbers) && (str2double(child->stringValue.c_str(), &child->numberValue))) - { - b.append(child->numberValue); - } - else - { - // Fails in str2double() means that values is not an actual number, so we do nothing and leave it as it is - b.append(child->stringValue); - } + b.append(child->stringValue); } else if (child->valueType == orion::ValueTypeNumber) { @@ -79,14 +68,14 @@ void compoundValueBson(const std::vector& children, o { orion::BSONArrayBuilder ba; - compoundValueBson(child->childV, ba, strings2numbers, encode); + compoundValueBson(child->childV, ba, encode); b.append(ba.arr()); } else if (child->valueType == orion::ValueTypeObject) { orion::BSONObjBuilder bo; - compoundValueBson(child->childV, bo, strings2numbers, encode); + compoundValueBson(child->childV, bo, encode); b.append(bo.obj()); } else if (child->valueType == orion::ValueTypeNotGiven) @@ -106,13 +95,10 @@ void compoundValueBson(const std::vector& children, o * * compoundValueBson (for objects) - * -* strings2numbers is used only for the GEO_JSON generation logic to ensure NGSIv1 -* strings are converted to numbers (strings are not allowed in GeoJSON) -* * encode set to true only in the calculateOperator functions, eg. to avoid that * "$each" get replaced by ">each" (see https://github.com/telefonicaid/fiware-orion/issues/744#issuecomment-996752938) */ -void compoundValueBson(const std::vector& children, orion::BSONObjBuilder& b, bool strings2numbers, bool encode) +void compoundValueBson(const std::vector& children, orion::BSONObjBuilder& b, bool encode) { for (unsigned int ix = 0; ix < children.size(); ++ix) { @@ -121,15 +107,7 @@ void compoundValueBson(const std::vector& children, o if (child->valueType == orion::ValueTypeString) { - if ((strings2numbers) && (str2double(child->stringValue.c_str(), &child->numberValue))) - { - b.append(effectiveName, child->numberValue); - } - else - { - // Fails in str2double() means that values is not an actual number, so we do nothing and leave it as it is - b.append(effectiveName, child->stringValue); - } + b.append(effectiveName, child->stringValue); } else if (child->valueType == orion::ValueTypeNumber) { @@ -147,14 +125,14 @@ void compoundValueBson(const std::vector& children, o { orion::BSONArrayBuilder ba; - compoundValueBson(child->childV, ba, strings2numbers, encode); + compoundValueBson(child->childV, ba, encode); b.append(effectiveName, ba.arr()); } else if (child->valueType == orion::ValueTypeObject) { orion::BSONObjBuilder bo; - compoundValueBson(child->childV, bo, strings2numbers, encode); + compoundValueBson(child->childV, bo, encode); b.append(effectiveName, bo.obj()); } else if (child->valueType == orion::ValueTypeNotGiven) diff --git a/src/lib/mongoBackend/compoundValueBson.h b/src/lib/mongoBackend/compoundValueBson.h index aeaa29902a..aa514c7bfb 100644 --- a/src/lib/mongoBackend/compoundValueBson.h +++ b/src/lib/mongoBackend/compoundValueBson.h @@ -37,7 +37,7 @@ * * compoundValueBson (for objects) - */ -extern void compoundValueBson(const std::vector& children, orion::BSONObjBuilder& b, bool strings2numbers = false, bool encode = true); +extern void compoundValueBson(const std::vector& children, orion::BSONObjBuilder& b, bool encode = true); @@ -45,6 +45,6 @@ extern void compoundValueBson(const std::vector& chil * * compoundValueBson (for arrays) - */ -extern void compoundValueBson(const std::vector& children, orion::BSONArrayBuilder& b, bool strings2numbers = false, bool encode = true); +extern void compoundValueBson(const std::vector& children, orion::BSONArrayBuilder& b, bool encode = true); #endif // SRC_LIB_MONGOBACKEND_COMPOUNDVALUEBSON_H_ diff --git a/src/lib/mongoBackend/location.cpp b/src/lib/mongoBackend/location.cpp index 85caa77a47..e07b011604 100644 --- a/src/lib/mongoBackend/location.cpp +++ b/src/lib/mongoBackend/location.cpp @@ -374,9 +374,9 @@ static bool getGeoJson } else { - // Autocast doesn't make sense in this context, strings2numbers enabled in the case of NGSIv1 + // Autocast doesn't make sense in this context // FIXME P7: boolean return value should be managed? - caP->valueBson(std::string(ENT_ATTRS_VALUE), &bo, "", true, false); + caP->valueBson(std::string(ENT_ATTRS_VALUE), &bo, "", true); geoJson->appendElements(getObjectFieldF(bo.obj(), ENT_ATTRS_VALUE)); } diff --git a/src/lib/ngsi/ContextAttribute.cpp b/src/lib/ngsi/ContextAttribute.cpp index e38d400931..47befa2d77 100644 --- a/src/lib/ngsi/ContextAttribute.cpp +++ b/src/lib/ngsi/ContextAttribute.cpp @@ -149,8 +149,7 @@ bool ContextAttribute::calculateOperator ( const std::string& valueKey, orion::CompoundValueNode* upOp, - orion::BSONObjBuilder* bsonAttr, - bool strings2numbers + orion::BSONObjBuilder* bsonAttr ) const { std::string op = upOp->name; @@ -208,7 +207,7 @@ bool ContextAttribute::calculateOperator case orion::ValueTypeVector: case orion::ValueTypeObject: - compoundValueBson(compoundValueP->childV, ba2, strings2numbers); + compoundValueBson(compoundValueP->childV, ba2); ba.append(ba2.arr()); break; @@ -238,7 +237,7 @@ bool ContextAttribute::calculateOperator break; case orion::ValueTypeObject: - compoundValueBson(upOp->childV, bo, strings2numbers); + compoundValueBson(upOp->childV, bo); bsonAttr->append(valueKey, bo.obj()); break; @@ -277,8 +276,7 @@ bool ContextAttribute::valueBson const std::string& valueKey, orion::BSONObjBuilder* bsonAttr, const std::string& attrType, - bool autocast, - bool strings2numbers + bool autocast ) const { if (compoundValueP == NULL) @@ -290,7 +288,7 @@ bool ContextAttribute::valueBson if (compoundValueP->valueType == orion::ValueTypeVector) { orion::BSONArrayBuilder b; - compoundValueBson(compoundValueP->childV, b, strings2numbers); + compoundValueBson(compoundValueP->childV, b); bsonAttr->append(valueKey, b.arr()); } else if (compoundValueP->valueType == orion::ValueTypeObject) @@ -298,7 +296,7 @@ bool ContextAttribute::valueBson // Special processing of update operators if ((compoundValueP->childV.size() > 0) && (isUpdateOperator(compoundValueP->childV[0]->name))) { - if (!calculateOperator(valueKey, compoundValueP->childV[0], bsonAttr, strings2numbers)) + if (!calculateOperator(valueKey, compoundValueP->childV[0], bsonAttr)) { // in this case we return without generating any BSON return false; @@ -307,7 +305,7 @@ bool ContextAttribute::valueBson else { orion::BSONObjBuilder b; - compoundValueBson(compoundValueP->childV, b, strings2numbers); + compoundValueBson(compoundValueP->childV, b); bsonAttr->append(valueKey, b.obj()); } } diff --git a/src/lib/ngsi/ContextAttribute.h b/src/lib/ngsi/ContextAttribute.h index 3b98844fc1..b9842b06bf 100644 --- a/src/lib/ngsi/ContextAttribute.h +++ b/src/lib/ngsi/ContextAttribute.h @@ -129,8 +129,7 @@ typedef struct ContextAttribute bool valueBson(const std::string& valueKey, orion::BSONObjBuilder* bsonAttr, const std::string& attrType, - bool autocast, - bool strings2numbers = false) const; + bool autocast) const; /* Helper method to be use in some places wher '%s' is needed */ std::string getValue(void) const; @@ -152,8 +151,7 @@ typedef struct ContextAttribute bool calculateOperator(const std::string& valueKey, orion::CompoundValueNode* upOp, - orion::BSONObjBuilder* bsonAttr, - bool strings2numbers) const; + orion::BSONObjBuilder* bsonAttr) const; } ContextAttribute;