json utility: Update sort algorithms
Modified sort utility to be able to sort on a specified key.
New utility function sortJsonArrayByKey() added.
Note:
- Function odataObjectCmp() renamed to objectKeyCmp()
- New function odataObjectCmp() created which calls objectKeyCmp() with
@odata.id key specified.
- Comments for odataObjectCmp() didn't match behavior for object
without key. These objects are sorted as less than objects with the
key.
- sortJSONResponse() modified to use the new sortJsonArrayByKey().
Tested:
- Added new unit tests. These tests are in addition to the existing
tests. So they focus on testing comparing by different keys.
The existing tests already cover the different permutations of the
basic comparisons.
- Redfish Service validator passes
Change-Id: I949b7cb868c59a8eeda3798e6a82a1572bbc5792
Signed-off-by: Ed Tanous <etanous@nvidia.com>
Signed-off-by: Janet Adkins <janeta@us.ibm.com>
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index a02eb60..f344220 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -709,30 +709,34 @@
for (const std::string& sensorGroup : sensorHeaders)
{
nlohmann::json::iterator entry = response.find(sensorGroup);
- if (entry != response.end())
+ if (entry == response.end())
{
- std::sort(entry->begin(), entry->end(),
- [](const nlohmann::json& c1, const nlohmann::json& c2) {
- return c1["Name"] < c2["Name"];
- });
+ continue;
+ }
+ nlohmann::json::array_t* arr =
+ entry->get_ptr<nlohmann::json::array_t*>();
+ if (arr == nullptr)
+ {
+ continue;
+ }
+ json_util::sortJsonArrayByKey(*arr, "Name");
- // add the index counts to the end of each entry
- size_t count = 0;
- for (nlohmann::json& sensorJson : *entry)
+ // add the index counts to the end of each entry
+ size_t count = 0;
+ for (nlohmann::json& sensorJson : *entry)
+ {
+ nlohmann::json::iterator odata = sensorJson.find("@odata.id");
+ if (odata == sensorJson.end())
{
- nlohmann::json::iterator odata = sensorJson.find("@odata.id");
- if (odata == sensorJson.end())
- {
- continue;
- }
- std::string* value = odata->get_ptr<std::string*>();
- if (value != nullptr)
- {
- *value += "/" + std::to_string(count);
- sensorJson["MemberId"] = std::to_string(count);
- count++;
- sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
- }
+ continue;
+ }
+ std::string* value = odata->get_ptr<std::string*>();
+ if (value != nullptr)
+ {
+ *value += "/" + std::to_string(count);
+ sensorJson["MemberId"] = std::to_string(count);
+ count++;
+ sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
}
}
}