Boost::urls::format
Boost 1.82 dropped a lovely new toy, boost::urls::format, which is a lot
like our urlFromPieces method, but better in that it makes the resulting
uris more readable, and allows doing things like fragments in a single
line instead of multiple. We should prefer it in some cases.
Tested:
Redfish service validator passes.
Spot checks of URLs work as expected.
Unit tests pass.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Ia7b38f0a95771c862507e7d5b4aa68aa1c98403c
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 93e8196..59c954b 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -32,6 +32,7 @@
#include <boost/algorithm/string/replace.hpp>
#include <boost/range/algorithm/replace_copy_if.hpp>
#include <boost/system/error_code.hpp>
+#include <boost/url/format.hpp>
#include <sdbusplus/asio/property.hpp>
#include <sdbusplus/unpack_properties.hpp>
@@ -559,8 +560,8 @@
}
populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
- asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis", chassisIdStr, chassisSubNode);
+ asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
+ "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
// Get the list of all sensors for this Chassis element
std::string sensorPath = *chassisPath + "/all_sensors";
@@ -1120,9 +1121,10 @@
.jsonValue["Redundancy"];
nlohmann::json::object_t redundancy;
- boost::urls::url url = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis", sensorsAsyncResp->chassisId,
- sensorsAsyncResp->chassisSubNode);
+ boost::urls::url url =
+ boost::urls::format("/redfish/v1/Chassis/{}/{}",
+ sensorsAsyncResp->chassisId,
+ sensorsAsyncResp->chassisSubNode);
url.set_fragment(("/Redundancy"_json_pointer / jResp.size())
.to_string());
redundancy["@odata.id"] = std::move(url);
@@ -2194,8 +2196,8 @@
// Add new PowerSupply object to JSON array
powerSupplyArray.push_back({});
nlohmann::json& powerSupply = powerSupplyArray.back();
- boost::urls::url url = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis", chassisId, "Power");
+ boost::urls::url url = boost::urls::format("/redfish/v1/Chassis/{}/Power",
+ chassisId);
url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
powerSupply["@odata.id"] = std::move(url);
powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
@@ -2319,10 +2321,10 @@
sensorId += sensorName;
sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
- crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis",
- sensorsAsyncResp->chassisId,
- sensorsAsyncResp->chassisSubNode, sensorId);
+ boost::urls::format("/redfish/v1/Chassis/{}/{}/{}",
+ sensorsAsyncResp->chassisId,
+ sensorsAsyncResp->chassisSubNode,
+ sensorId);
sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
}
else
@@ -2379,8 +2381,8 @@
// PowerControl. Follows MemberId naming and
// naming in power.hpp.
nlohmann::json::object_t power;
- boost::urls::url url = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis",
+ boost::urls::url url = boost::urls::format(
+ "/redfish/v1/Chassis/{}/{}",
sensorsAsyncResp->chassisId,
sensorsAsyncResp->chassisSubNode);
url.set_fragment((""_json_pointer / fieldName / "0")
@@ -2411,8 +2413,8 @@
sensorId += sensorName;
nlohmann::json::object_t member;
- member["@odata.id"] = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis",
+ member["@odata.id"] = boost::urls::format(
+ "/redfish/v1/Chassis/{}/{}/{}",
sensorsAsyncResp->chassisId,
sensorsAsyncResp->chassisSubNode, sensorId);
tempArray.emplace_back(std::move(member));
@@ -2421,8 +2423,8 @@
else
{
nlohmann::json::object_t member;
- boost::urls::url url = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis",
+ boost::urls::url url = boost::urls::format(
+ "/redfish/v1/Chassis/{}/{}",
sensorsAsyncResp->chassisId,
sensorsAsyncResp->chassisSubNode);
url.set_fragment(
@@ -2793,8 +2795,8 @@
std::string id = type;
id += "_";
id += sensorName;
- member["@odata.id"] = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis", chassisId, chassisSubNode, id);
+ member["@odata.id"] = boost::urls::format(
+ "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
entriesArray.emplace_back(std::move(member));
}
@@ -2890,8 +2892,8 @@
return;
}
- asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
- "redfish", "v1", "Chassis", chassisId, "Sensors", sensorId);
+ asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
+ "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
BMCWEB_LOG_DEBUG << "Sensor doGet enter";