diff options
author | Ed Tanous <edtanous@google.com> | 2022-03-15 20:44:42 +0300 |
---|---|---|
committer | Ed Tanous <edtanous@google.com> | 2022-05-13 01:08:18 +0300 |
commit | 1476687deb1697d865b20458a0097c9ab5fd44e2 (patch) | |
tree | 6964ba82c382d03522f7a413a61ae1164b8e242e /include | |
parent | 1656b296313d75b172dcdbe5f37ff1d1b54800dc (diff) | |
download | bmcweb-1476687deb1697d865b20458a0097c9ab5fd44e2.tar.xz |
Remove brace initialization of json objects
Brace initialization of json objects, while quite interesting from an
academic sense, are very difficult for people to grok, and lead to
inconsistencies. This patchset aims to remove a majority of them in
lieu of operator[]. Interestingly, this saves about 1% of the binary
size of bmcweb.
This also has an added benefit that as a design pattern, we're never
constructing a new object, then moving it into place, we're always
adding to the existing object, which in the future _could_ make things
like OEM schemas or properties easier, as there's no case where we're
completely replacing the response object.
Tested:
Ran redfish service validator. No new failures.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Iae409b0a40ddd3ae6112cb2d52c6f6ab388595fe
Diffstat (limited to 'include')
-rw-r--r-- | include/dbus_monitor.hpp | 13 | ||||
-rw-r--r-- | include/ibm/management_console_rest.hpp | 12 | ||||
-rw-r--r-- | include/image_upload.hpp | 15 | ||||
-rw-r--r-- | include/login_routes.hpp | 22 | ||||
-rw-r--r-- | include/openbmc_dbus_rest.hpp | 112 | ||||
-rw-r--r-- | include/persistent_data.hpp | 81 |
6 files changed, 141 insertions, 114 deletions
diff --git a/include/dbus_monitor.hpp b/include/dbus_monitor.hpp index 764d157985..977f3a3a3a 100644 --- a/include/dbus_monitor.hpp +++ b/include/dbus_monitor.hpp @@ -45,8 +45,9 @@ inline int onPropertyUpdate(sd_bus_message* m, void* userdata, return 0; } sdbusplus::message::message message(m); - nlohmann::json j{{"event", message.get_member()}, - {"path", message.get_path()}}; + nlohmann::json json; + json["event"] = message.get_member(); + json["path"] = message.get_path(); if (strcmp(message.get_member(), "PropertiesChanged") == 0) { nlohmann::json data; @@ -63,8 +64,8 @@ inline int onPropertyUpdate(sd_bus_message* m, void* userdata, } // data is type sa{sv}as and is an array[3] of string, object, array - j["interface"] = data[0]; - j["properties"] = data[1]; + json["interface"] = data[0]; + json["properties"] = data[1]; } else if (strcmp(message.get_member(), "InterfacesAdded") == 0) { @@ -88,7 +89,7 @@ inline int onPropertyUpdate(sd_bus_message* m, void* userdata, auto it = thisSession->second.interfaces.find(entry.key()); if (it != thisSession->second.interfaces.end()) { - j["interfaces"][entry.key()] = entry.value(); + json["interfaces"][entry.key()] = entry.value(); } } } @@ -100,7 +101,7 @@ inline int onPropertyUpdate(sd_bus_message* m, void* userdata, } connection->sendText( - j.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); + json.dump(2, ' ', true, nlohmann::json::error_handler_t::replace)); return 0; } diff --git a/include/ibm/management_console_rest.hpp b/include/ibm/management_console_rest.hpp index 8e307464ca..0fbd2e22c1 100644 --- a/include/ibm/management_console_rest.hpp +++ b/include/ibm/management_console_rest.hpp @@ -690,12 +690,12 @@ inline void requestRoutes(App& app) asyncResp->res.jsonValue["@odata.id"] = "/ibm/v1/"; asyncResp->res.jsonValue["Id"] = "IBM Rest RootService"; asyncResp->res.jsonValue["Name"] = "IBM Service Root"; - asyncResp->res.jsonValue["ConfigFiles"] = { - {"@odata.id", "/ibm/v1/Host/ConfigFiles"}}; - asyncResp->res.jsonValue["LockService"] = { - {"@odata.id", "/ibm/v1/HMC/LockService"}}; - asyncResp->res.jsonValue["BroadcastService"] = { - {"@odata.id", "/ibm/v1/HMC/BroadcastService"}}; + asyncResp->res.jsonValue["ConfigFiles"]["@odata.id"] = + "/ibm/v1/Host/ConfigFiles"; + asyncResp->res.jsonValue["LockService"]["@odata.id"] = + "/ibm/v1/HMC/LockService"; + asyncResp->res.jsonValue["BroadcastService"]["@odata.id"] = + "/ibm/v1/HMC/BroadcastService"; }); BMCWEB_ROUTE(app, "/ibm/v1/Host/ConfigFiles") diff --git a/include/image_upload.hpp b/include/image_upload.hpp index 330ea9c379..aefb27e3a8 100644 --- a/include/image_upload.hpp +++ b/include/image_upload.hpp @@ -51,12 +51,10 @@ inline void } asyncResp->res.result(boost::beast::http::status::bad_request); - asyncResp->res.jsonValue = { - {"data", - {{"description", - "Version already exists or failed to be extracted"}}}, - {"message", "400 Bad Request"}, - {"status", "error"}}; + asyncResp->res.jsonValue["data"]["description"] = + "Version already exists or failed to be extracted"; + asyncResp->res.jsonValue["message"] = "400 Bad Request"; + asyncResp->res.jsonValue["status"] = "error"; }; std::function<void(sdbusplus::message::message&)> callback = @@ -80,8 +78,9 @@ inline void leaf = path.str; } - asyncResp->res.jsonValue = { - {"data", leaf}, {"message", "200 OK"}, {"status", "ok"}}; + asyncResp->res.jsonValue["data"] = leaf; + asyncResp->res.jsonValue["message"] = "200 OK"; + asyncResp->res.jsonValue["status"] = "ok"; BMCWEB_LOG_DEBUG << "ending response"; fwUpdateMatcher = nullptr; } diff --git a/include/login_routes.hpp b/include/login_routes.hpp index abcdaee3dc..a4fa9b7ad1 100644 --- a/include/login_routes.hpp +++ b/include/login_routes.hpp @@ -199,11 +199,11 @@ inline void requestRoutes(App& app) // structure, and doesn't actually look at the status // code. // TODO(ed).... Fix that upstream - asyncResp->res.jsonValue = { - {"data", - "User '" + std::string(username) + "' logged in"}, - {"message", "200 OK"}, - {"status", "ok"}}; + + asyncResp->res.jsonValue["data"] = + "User '" + std::string(username) + "' logged in"; + asyncResp->res.jsonValue["message"] = "200 OK"; + asyncResp->res.jsonValue["status"] = "ok"; // Hack alert. Boost beast by default doesn't let you // declare multiple headers of the same name, and in @@ -226,8 +226,8 @@ inline void requestRoutes(App& app) else { // if content type is json, assume json token - asyncResp->res.jsonValue = { - {"token", session->sessionToken}}; + asyncResp->res.jsonValue["token"] = + session->sessionToken; } } } @@ -245,10 +245,10 @@ inline void requestRoutes(App& app) const auto& session = req.session; if (session != nullptr) { - asyncResp->res.jsonValue = { - {"data", "User '" + session->username + "' logged out"}, - {"message", "200 OK"}, - {"status", "ok"}}; + asyncResp->res.jsonValue["data"] = + "User '" + session->username + "' logged out"; + asyncResp->res.jsonValue["message"] = "200 OK"; + asyncResp->res.jsonValue["status"] = "ok"; persistent_data::SessionStore::getInstance().removeSession( session); diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp index 4f4bd71ff6..75e31f19f9 100644 --- a/include/openbmc_dbus_rest.hpp +++ b/include/openbmc_dbus_rest.hpp @@ -57,9 +57,9 @@ inline void setErrorResponse(crow::Response& res, const std::string_view msg) { res.result(result); - res.jsonValue = {{"data", {{"description", desc}}}, - {"message", msg}, - {"status", "error"}}; + res.jsonValue["data"]["description"] = desc; + res.jsonValue["message"] = msg; + res.jsonValue["status"] = "error"; } inline void @@ -69,9 +69,9 @@ inline void { if (transaction->res.jsonValue.is_null()) { - transaction->res.jsonValue = {{"status", "ok"}, - {"bus_name", processName}, - {"objects", nlohmann::json::array()}}; + transaction->res.jsonValue["status"] = "ok"; + transaction->res.jsonValue["bus_name"] = processName; + transaction->res.jsonValue["objects"] = nlohmann::json::array(); } crow::connections::systemBus->async_method_call( @@ -87,8 +87,10 @@ inline void << "\n"; return; } - transaction->res.jsonValue["objects"].push_back( - {{"path", objectPath}}); + nlohmann::json::object_t object; + object["path"] = objectPath; + + transaction->res.jsonValue["objects"].push_back(std::move(object)); tinyxml2::XMLDocument doc; @@ -468,9 +470,9 @@ struct InProgressActionData } else { - res.jsonValue = {{"status", "ok"}, - {"message", "200 OK"}, - {"data", methodResponse}}; + res.jsonValue["status"] = "ok"; + res.jsonValue["message"] = "200 OK"; + res.jsonValue["data"] = methodResponse; } } } @@ -1626,9 +1628,9 @@ inline void handleList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, } else { - asyncResp->res.jsonValue = {{"status", "ok"}, - {"message", "200 OK"}, - {"data", objectPaths}}; + asyncResp->res.jsonValue["status"] = "ok"; + asyncResp->res.jsonValue["message"] = "200 OK"; + asyncResp->res.jsonValue["data"] = objectPaths; } }, "xyz.openbmc_project.ObjectMapper", @@ -1642,9 +1644,9 @@ inline void handleEnumerate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, { BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath; - asyncResp->res.jsonValue = {{"message", "200 OK"}, - {"status", "ok"}, - {"data", nlohmann::json::object()}}; + asyncResp->res.jsonValue["message"] = "200 OK"; + asyncResp->res.jsonValue["status"] = "ok"; + asyncResp->res.jsonValue["data"] = nlohmann::json::object(); crow::connections::systemBus->async_method_call( [objectPath, asyncResp]( @@ -1773,10 +1775,11 @@ inline void handleGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, } else { - asyncResp->res.jsonValue = { - {"status", "ok"}, - {"message", "200 OK"}, - {"data", *response}}; + asyncResp->res.jsonValue["status"] = "ok"; + asyncResp->res.jsonValue["message"] = + "200 OK"; + asyncResp->res.jsonValue["data"] = + *response; } } }); @@ -2000,11 +2003,17 @@ inline void handlePut(const crow::Request& req, else { transaction->asyncResp - ->res.jsonValue = { - {"status", "ok"}, - {"message", - "200 OK"}, - {"data", nullptr}}; + ->res.jsonValue + ["status"] = + "ok"; + transaction->asyncResp + ->res.jsonValue + ["message"] = + "200 OK"; + transaction->asyncResp + ->res + .jsonValue["data"] = + nullptr; } }); } @@ -2178,16 +2187,17 @@ inline void BMCWEB_LOG_ERROR << "XML document failed to parse " << processName << " " << objectPath << "\n"; - asyncResp->res.jsonValue = {{"status", "XML parse error"}}; + asyncResp->res.jsonValue["status"] = "XML parse error"; asyncResp->res.result( boost::beast::http::status::internal_server_error); return; } BMCWEB_LOG_DEBUG << introspectXml; - asyncResp->res.jsonValue = {{"status", "ok"}, - {"bus_name", processName}, - {"object_path", objectPath}}; + asyncResp->res.jsonValue["status"] = "ok"; + asyncResp->res.jsonValue["bus_name"] = processName; + asyncResp->res.jsonValue["object_path"] = objectPath; + nlohmann::json& interfacesArray = asyncResp->res.jsonValue["interfaces"]; interfacesArray = nlohmann::json::array(); @@ -2199,7 +2209,9 @@ inline void const char* ifaceName = interface->Attribute("name"); if (ifaceName != nullptr) { - interfacesArray.push_back({{"name", ifaceName}}); + nlohmann::json::object_t interface; + interface["name"] = ifaceName; + interfacesArray.push_back(std::move(interface)); } interface = interface->NextSiblingElement("interface"); @@ -2235,10 +2247,11 @@ inline void boost::beast::http::status::internal_server_error); return; } - asyncResp->res.jsonValue = {{"status", "ok"}, - {"bus_name", processName}, - {"interface", interfaceName}, - {"object_path", objectPath}}; + + asyncResp->res.jsonValue["status"] = "ok"; + asyncResp->res.jsonValue["bus_name"] = processName; + asyncResp->res.jsonValue["interface"] = interfaceName; + asyncResp->res.jsonValue["object_path"] = objectPath; nlohmann::json& methodsArray = asyncResp->res.jsonValue["methods"]; @@ -2313,9 +2326,13 @@ inline void uri += interfaceName; uri += "/"; uri += name; - methodsArray.push_back({{"name", name}, - {"uri", std::move(uri)}, - {"args", argsArray}}); + + nlohmann::json::object_t object; + object["name"] = name; + object["uri"] = std::move(uri); + object["args"] = argsArray; + + methodsArray.push_back(std::move(object)); } methods = methods->NextSiblingElement("method"); } @@ -2343,8 +2360,10 @@ inline void const char* name = signals->Attribute("name"); if (name != nullptr) { - signalsArray.push_back( - {{"name", name}, {"args", argsArray}}); + nlohmann::json::object_t object; + object["name"] = name; + object["args"] = argsArray; + signalsArray.push_back(std::move(object)); } signals = signals->NextSiblingElement("signal"); @@ -2424,8 +2443,11 @@ inline void requestRoutes(App& app) .methods(boost::beast::http::verb::get)( [](const crow::Request&, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { - asyncResp->res.jsonValue = {{"buses", {{{"name", "system"}}}}, - {"status", "ok"}}; + nlohmann::json::array_t buses; + nlohmann::json& bus = buses.emplace_back(); + bus["name"] = "system"; + asyncResp->res.jsonValue["busses"] = std::move(buses); + asyncResp->res.jsonValue["status"] = "ok"; }); BMCWEB_ROUTE(app, "/bus/system/") @@ -2445,11 +2467,13 @@ inline void requestRoutes(App& app) else { std::sort(names.begin(), names.end()); - asyncResp->res.jsonValue = {{"status", "ok"}}; + asyncResp->res.jsonValue["status"] = "ok"; auto& objectsSub = asyncResp->res.jsonValue["objects"]; for (auto& name : names) { - objectsSub.push_back({{"name", name}}); + nlohmann::json::object_t object; + object["name"] = name; + objectsSub.push_back(std::move(object)); } } }; diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp index 6d0f7c1a1d..1ca925eb23 100644 --- a/include/persistent_data.hpp +++ b/include/persistent_data.hpp @@ -207,25 +207,25 @@ class ConfigFile const auto& c = SessionStore::getInstance().getAuthMethodsConfig(); const auto& eventServiceConfig = EventServiceStore::getInstance().getEventServiceConfig(); - nlohmann::json data{ - {"auth_config", - {{"XToken", c.xtoken}, - {"Cookie", c.cookie}, - {"SessionToken", c.sessionToken}, - {"BasicAuth", c.basic}, - {"TLS", c.tls}} + nlohmann::json::object_t data; + nlohmann::json& authConfig = data["auth_config"]; - }, - {"eventservice_config", - {{"ServiceEnabled", eventServiceConfig.enabled}, - {"DeliveryRetryAttempts", eventServiceConfig.retryAttempts}, - {"DeliveryRetryIntervalSeconds", - eventServiceConfig.retryTimeoutInterval}} + authConfig["XToken"] = c.xtoken; + authConfig["Cookie"] = c.cookie; + authConfig["SessionToken"] = c.sessionToken; + authConfig["BasicAuth"] = c.basic; + authConfig["TLS"] = c.tls; - }, - {"system_uuid", systemUuid}, - {"revision", jsonRevision}, - {"timeout", SessionStore::getInstance().getTimeoutInSeconds()}}; + nlohmann::json& eventserviceConfig = data["eventservice_config"]; + eventserviceConfig["ServiceEnabled"] = eventServiceConfig.enabled; + eventserviceConfig["DeliveryRetryAttempts"] = + eventServiceConfig.retryAttempts; + eventserviceConfig["DeliveryRetryIntervalSeconds"] = + eventServiceConfig.retryTimeoutInterval; + + data["system_uuid"] = systemUuid; + data["revision"] = jsonRevision; + data["timeout"] = SessionStore::getInstance().getTimeoutInSeconds(); nlohmann::json& sessions = data["sessions"]; sessions = nlohmann::json::array(); @@ -234,16 +234,16 @@ class ConfigFile if (p.second->persistence != persistent_data::PersistenceType::SINGLE_REQUEST) { - sessions.push_back({ - {"unique_id", p.second->uniqueId}, - {"session_token", p.second->sessionToken}, - {"username", p.second->username}, - {"csrf_token", p.second->csrfToken}, - {"client_ip", p.second->clientIp}, + nlohmann::json::object_t session; + session["unique_id"] = p.second->uniqueId; + session["session_token"] = p.second->sessionToken; + session["username"] = p.second->username; + session["csrf_token"] = p.second->csrfToken; + session["client_ip"] = p.second->clientIp; #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE - {"client_id", p.second->clientId}, + session["client_id"] = p.second->clientId; #endif - }); + sessions.push_back(std::move(session)); } } nlohmann::json& subscriptions = data["subscriptions"]; @@ -270,20 +270,23 @@ class ConfigFile headers[std::move(name)] = header.value(); } - subscriptions.push_back({ - {"Id", subValue->id}, - {"Context", subValue->customText}, - {"DeliveryRetryPolicy", subValue->retryPolicy}, - {"Destination", subValue->destinationUrl}, - {"EventFormatType", subValue->eventFormatType}, - {"HttpHeaders", std::move(headers)}, - {"MessageIds", subValue->registryMsgIds}, - {"Protocol", subValue->protocol}, - {"RegistryPrefixes", subValue->registryPrefixes}, - {"ResourceTypes", subValue->resourceTypes}, - {"SubscriptionType", subValue->subscriptionType}, - {"MetricReportDefinitions", subValue->metricReportDefinitions}, - }); + nlohmann::json::object_t subscription; + + subscription["Id"] = subValue->id; + subscription["Context"] = subValue->customText; + subscription["DeliveryRetryPolicy"] = subValue->retryPolicy; + subscription["Destination"] = subValue->destinationUrl; + subscription["EventFormatType"] = subValue->eventFormatType; + subscription["HttpHeaders"] = std::move(headers); + subscription["MessageIds"] = subValue->registryMsgIds; + subscription["Protocol"] = subValue->protocol; + subscription["RegistryPrefixes"] = subValue->registryPrefixes; + subscription["ResourceTypes"] = subValue->resourceTypes; + subscription["SubscriptionType"] = subValue->subscriptionType; + subscription["MetricReportDefinitions"] = + subValue->metricReportDefinitions; + + subscriptions.push_back(std::move(subscription)); } persistentFile << data; } |