EntityManager: Make MapperGetSubTreeResponse type comprehensible

From there, rework a bunch of uses of the unaliased types to use the new
aliased types and fix up some of the variable names to improve
readability.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I498e0d93091cae43e59907ee1908afc97ac601ee
diff --git a/include/EntityManager.hpp b/include/EntityManager.hpp
index eb8a86d..2b4565b 100644
--- a/include/EntityManager.hpp
+++ b/include/EntityManager.hpp
@@ -30,16 +30,8 @@
 #include <optional>
 #include <string>
 
-// paths - > interfaces -> properties
-using MapperGetSubTreeResponse = boost::container::flat_map<
-    std::string,
-    boost::container::flat_map<
-        std::string,
-        boost::container::flat_map<std::string, DBusValueVariant>>>;
-
-// vector of tuple<map<propertyName, variant>, D-Bus path>>
-using FoundDeviceT = std::vector<std::tuple<
-    boost::container::flat_map<std::string, DBusValueVariant>, std::string>>;
+// The string is the path to the object hosting the interface
+using FoundDeviceT = std::vector<std::tuple<DBusInterface, std::string>>;
 
 struct CmpStr
 {
diff --git a/include/Utils.hpp b/include/Utils.hpp
index fa155f9..13c199c 100644
--- a/include/Utils.hpp
+++ b/include/Utils.hpp
@@ -35,6 +35,10 @@
 using DBusValueVariant =
     std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
                  int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
+using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
+using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
+using MapperGetSubTreeResponse =
+    boost::container::flat_map<std::string, DBusObject>;
 
 enum class TemplateOperation
 {
@@ -137,18 +141,13 @@
 }
 
 std::optional<std::string> templateCharReplace(
-    nlohmann::json::iterator& keyPair,
-    const boost::container::flat_map<
-        std::string, boost::container::flat_map<std::string, DBusValueVariant>>&
-        allInterfaces,
-    const size_t foundDeviceIdx,
+    nlohmann::json::iterator& keyPair, const DBusObject& object,
+    const size_t index,
     const std::optional<std::string>& replaceStr = std::nullopt);
 
 std::optional<std::string> templateCharReplace(
-    nlohmann::json::iterator& keyPair,
-    const boost::container::flat_map<std::string, DBusValueVariant>&
-        foundDevice,
-    const size_t foundDeviceIdx,
+    nlohmann::json::iterator& keyPair, const DBusInterface& interface,
+    const size_t index,
     const std::optional<std::string>& replaceStr = std::nullopt);
 
 inline bool deviceHasLogging(const nlohmann::json& json)
diff --git a/src/PerformScan.cpp b/src/PerformScan.cpp
index 5d05403..0998f22 100644
--- a/src/PerformScan.cpp
+++ b/src/PerformScan.cpp
@@ -51,10 +51,8 @@
     }
 
     systemBus->async_method_call(
-        [call, scan, probeVector, retries](
-            boost::system::error_code& errc,
-            const boost::container::flat_map<std::string, DBusValueVariant>&
-                resp) {
+        [call, scan, probeVector, retries](boost::system::error_code& errc,
+                                           const DBusInterface& resp) {
             if (errc)
             {
                 std::cerr << "error calling getall on  " << std::get<0>(call)
@@ -210,9 +208,8 @@
     }
 }
 
-std::string getRecordName(
-    const boost::container::flat_map<std::string, DBusValueVariant>& probe,
-    const std::string& probeName)
+std::string getRecordName(const DBusInterface& probe,
+                          const std::string& probeName)
 {
     if (probe.empty())
     {
@@ -379,14 +376,12 @@
                 std::optional<std::string> replaceStr;
 
                 MapperGetSubTreeResponse::mapped_type emptyInterfaces;
-                boost::container::flat_map<std::string, DBusValueVariant>
-                    emptyProps;
-                emptyInterfaces.emplace(std::string{}, emptyProps);
+                DBusInterface emptyInterface;
+                emptyInterfaces.emplace(std::string{}, emptyInterface);
 
                 for (auto& foundDeviceAndPath : foundDevices)
                 {
-                    const boost::container::flat_map<
-                        std::string, DBusValueVariant>& foundDevice =
+                    const DBusInterface& foundDevice =
                         std::get<0>(foundDeviceAndPath);
                     const std::string& path = std::get<1>(foundDeviceAndPath);
 
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 48c04fa..83e5654 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -187,17 +187,14 @@
 // Replaces the template character like the other version of this function,
 // but checks all properties on all interfaces provided to do the substitution
 // with.
-std::optional<std::string> templateCharReplace(
-    nlohmann::json::iterator& keyPair,
-    const boost::container::flat_map<
-        std::string, boost::container::flat_map<std::string, DBusValueVariant>>&
-        allInterfaces,
-    const size_t foundDeviceIdx, const std::optional<std::string>& replaceStr)
+std::optional<std::string>
+    templateCharReplace(nlohmann::json::iterator& keyPair,
+                        const DBusObject& object, const size_t index,
+                        const std::optional<std::string>& replaceStr)
 {
-    for (const auto& [interface, properties] : allInterfaces)
+    for (const auto& [_, interface] : object)
     {
-        auto ret = templateCharReplace(keyPair, properties, foundDeviceIdx,
-                                       replaceStr);
+        auto ret = templateCharReplace(keyPair, interface, index, replaceStr);
         if (ret)
         {
             return ret;
@@ -209,11 +206,10 @@
 // finds the template character (currently set to $) and replaces the value with
 // the field found in a dbus object i.e. $ADDRESS would get populated with the
 // ADDRESS field from a object on dbus
-std::optional<std::string> templateCharReplace(
-    nlohmann::json::iterator& keyPair,
-    const boost::container::flat_map<std::string, DBusValueVariant>&
-        foundDevice,
-    const size_t foundDeviceIdx, const std::optional<std::string>& replaceStr)
+std::optional<std::string>
+    templateCharReplace(nlohmann::json::iterator& keyPair,
+                        const DBusInterface& interface, const size_t index,
+                        const std::optional<std::string>& replaceStr)
 {
     std::optional<std::string> ret = std::nullopt;
 
@@ -223,8 +219,7 @@
         for (auto nextLayer = keyPair.value().begin();
              nextLayer != keyPair.value().end(); nextLayer++)
         {
-            templateCharReplace(nextLayer, foundDevice, foundDeviceIdx,
-                                replaceStr);
+            templateCharReplace(nextLayer, interface, index, replaceStr);
         }
         return ret;
     }
@@ -236,16 +231,15 @@
     }
 
     boost::replace_all(*strPtr, std::string(templateChar) + "index",
-                       std::to_string(foundDeviceIdx));
+                       std::to_string(index));
     if (replaceStr)
     {
-        boost::replace_all(*strPtr, *replaceStr,
-                           std::to_string(foundDeviceIdx));
+        boost::replace_all(*strPtr, *replaceStr, std::to_string(index));
     }
 
-    for (auto& foundDevicePair : foundDevice)
+    for (auto& propertyPair : interface)
     {
-        std::string templateName = templateChar + foundDevicePair.first;
+        std::string templateName = templateChar + propertyPair.first;
         boost::iterator_range<std::string::const_iterator> find =
             boost::ifind_first(*strPtr, templateName);
         if (find)
@@ -259,17 +253,17 @@
             if (!start && find.end() == strPtr->end())
             {
                 std::visit([&](auto&& val) { keyPair.value() = val; },
-                           foundDevicePair.second);
+                           propertyPair.second);
                 return ret;
             }
             if (nextItemIdx > strPtr->size() ||
                 std::find(mathChars.begin(), mathChars.end(),
                           strPtr->at(nextItemIdx)) == mathChars.end())
             {
-                std::string val = std::visit(VariantToStringVisitor(),
-                                             foundDevicePair.second);
-                boost::ireplace_all(*strPtr,
-                                    templateChar + foundDevicePair.first, val);
+                std::string val =
+                    std::visit(VariantToStringVisitor(), propertyPair.second);
+                boost::ireplace_all(*strPtr, templateChar + propertyPair.first,
+                                    val);
                 continue;
             }
 
@@ -298,8 +292,7 @@
             // we assume that the replacement is a number, because we can
             // only do math on numbers.. we might concatenate strings in the
             // future, but thats later
-            int number =
-                std::visit(VariantToIntVisitor(), foundDevicePair.second);
+            int number = std::visit(VariantToIntVisitor(), propertyPair.second);
 
             bool isOperator = true;
             TemplateOperation next = TemplateOperation::addition;
diff --git a/test/test_entity-manager.cpp b/test/test_entity-manager.cpp
index fbef838..b1eec84 100644
--- a/test/test_entity-manager.cpp
+++ b/test/test_entity-manager.cpp
@@ -1,6 +1,5 @@
 #include "Utils.hpp"
 
-#include <boost/container/flat_map.hpp>
 #include <nlohmann/json.hpp>
 
 #include <string>
@@ -14,7 +13,7 @@
 {
     nlohmann::json j = {{"foo", "$bus"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["BUS"] = 23;
 
     templateCharReplace(it, data, 0);
@@ -27,7 +26,7 @@
 {
     nlohmann::json j = {{"foo", "$TEST"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = std::string("Test");
 
     templateCharReplace(it, data, 0);
@@ -40,7 +39,7 @@
 {
     nlohmann::json j = {{"foo", "the $TEST"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = std::string("Test");
 
     templateCharReplace(it, data, 0);
@@ -53,7 +52,7 @@
 {
     nlohmann::json j = {{"foo", "the $TEST worked"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = std::string("Test");
 
     templateCharReplace(it, data, 0);
@@ -66,7 +65,7 @@
 {
     nlohmann::json j = {{"foo", "the Test $TEST"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 23;
 
     templateCharReplace(it, data, 0);
@@ -79,7 +78,7 @@
 {
     nlohmann::json j = {{"foo", "3 plus 1 equals $TEST + 1"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 3;
 
     templateCharReplace(it, data, 0);
@@ -92,7 +91,7 @@
 {
     nlohmann::json j = {{"foo", "3 minus 1 equals $TEST - 1 !"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 3;
 
     templateCharReplace(it, data, 0);
@@ -105,7 +104,7 @@
 {
     nlohmann::json j = {{"foo", "3 mod 2 equals $TEST % 2"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 3;
 
     templateCharReplace(it, data, 0);
@@ -118,7 +117,7 @@
 {
     nlohmann::json j = {{"foo", "3 * 2 equals $TEST * 2"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 3;
 
     templateCharReplace(it, data, 0);
@@ -131,7 +130,7 @@
 {
     nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 4;
 
     templateCharReplace(it, data, 0);
@@ -144,7 +143,7 @@
 {
     nlohmann::json j = {{"foo", "4 * 2 % 6 equals $TEST * 2 % 6"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 4;
 
     templateCharReplace(it, data, 0);
@@ -157,7 +156,7 @@
 {
     nlohmann::json j = {{"foo", "$FOO $BAR"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["FOO"] = std::string("foo");
     data["BAR"] = std::string("bar");
 
@@ -171,7 +170,7 @@
 {
     nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2 $BAR"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["TEST"] = 4;
     data["BAR"] = std::string("bar");
 
@@ -184,7 +183,7 @@
 {
     nlohmann::json j = {{"foo", "4 / 2 equals $ADDRESS / 2 $BAR"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["ADDRESS"] = 4;
     data["BAR"] = std::string("bar");
 
@@ -201,7 +200,7 @@
                         {"Name", "$bus sensor 0"},
                         {"Type", "SomeType"}};
 
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["BUS"] = 15;
 
     for (auto it = j.begin(); it != j.end(); it++)
@@ -219,7 +218,7 @@
 {
     nlohmann::json j = {{"foo", "twelve is $TEST"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
     data["test"] = 12;
 
     templateCharReplace(it, data, 0);
@@ -232,7 +231,7 @@
 {
     nlohmann::json j = {{"foo", "0x54"}};
     auto it = j.begin();
-    boost::container::flat_map<std::string, DBusValueVariant> data;
+    DBusInterface data;
 
     templateCharReplace(it, data, 0);