Skip to content

Commit

Permalink
REMOVE strings2numbers in valueBson() and compoundValueBson()
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed Aug 6, 2024
1 parent 147dadc commit ce12776
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/lib/mongoBackend/MongoCommonSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ 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);
}
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);
Expand Down Expand Up @@ -214,15 +214,15 @@ 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);
}
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);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/mongoBackend/MongoCommonUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 8 additions & 30 deletions src/lib/mongoBackend/compoundValueBson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,18 @@
*
* 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<orion::CompoundValueNode*>& children, orion::BSONArrayBuilder& b, bool strings2numbers, bool encode)
void compoundValueBson(const std::vector<orion::CompoundValueNode*>& children, orion::BSONArrayBuilder& b, bool encode)
{
for (unsigned int ix = 0; ix < children.size(); ++ix)
{
orion::CompoundValueNode* child = children[ix];

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)
{
Expand All @@ -79,14 +68,14 @@ void compoundValueBson(const std::vector<orion::CompoundValueNode*>& 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)
Expand All @@ -106,13 +95,10 @@ void compoundValueBson(const std::vector<orion::CompoundValueNode*>& 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<orion::CompoundValueNode*>& children, orion::BSONObjBuilder& b, bool strings2numbers, bool encode)
void compoundValueBson(const std::vector<orion::CompoundValueNode*>& children, orion::BSONObjBuilder& b, bool encode)
{
for (unsigned int ix = 0; ix < children.size(); ++ix)
{
Expand All @@ -121,15 +107,7 @@ void compoundValueBson(const std::vector<orion::CompoundValueNode*>& 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)
{
Expand All @@ -147,14 +125,14 @@ void compoundValueBson(const std::vector<orion::CompoundValueNode*>& 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)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/mongoBackend/compoundValueBson.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
*
* compoundValueBson (for objects) -
*/
extern void compoundValueBson(const std::vector<orion::CompoundValueNode*>& children, orion::BSONObjBuilder& b, bool strings2numbers = false, bool encode = true);
extern void compoundValueBson(const std::vector<orion::CompoundValueNode*>& children, orion::BSONObjBuilder& b, bool encode = true);



/* ****************************************************************************
*
* compoundValueBson (for arrays) -
*/
extern void compoundValueBson(const std::vector<orion::CompoundValueNode*>& children, orion::BSONArrayBuilder& b, bool strings2numbers = false, bool encode = true);
extern void compoundValueBson(const std::vector<orion::CompoundValueNode*>& children, orion::BSONArrayBuilder& b, bool encode = true);

#endif // SRC_LIB_MONGOBACKEND_COMPOUNDVALUEBSON_H_
4 changes: 2 additions & 2 deletions src/lib/mongoBackend/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
16 changes: 7 additions & 9 deletions src/lib/ngsi/ContextAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -290,15 +288,15 @@ 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)
{
// 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;
Expand All @@ -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());
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/lib/ngsi/ContextAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit ce12776

Please sign in to comment.