entity-manager: probe: factor out probe logic
Move the probe matching logic to a function so unit tests can be written
for it.
Change-Id: Ie6d1ec82deb48437138c9065aea3a0eeacc9cffb
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/include/Utils.hpp b/include/Utils.hpp
index 90392fb..6e663e6 100644
--- a/include/Utils.hpp
+++ b/include/Utils.hpp
@@ -149,3 +149,9 @@
}
return true;
}
+
+/// \brief Match a Dbus property against a probe statement.
+/// \param probe the probe statement to match against.
+/// \param dbusValue the property value being matched to a probe.
+/// \return true if the dbusValue matched the probe otherwise false.
+bool matchProbe(const nlohmann::json& probe, const BasicVariantType& dbusValue);
diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp
index 62c5749..8b3dc4d 100644
--- a/src/EntityManager.cpp
+++ b/src/EntityManager.cpp
@@ -278,64 +278,7 @@
auto deviceValue = device.find(match.first);
if (deviceValue != device.end())
{
- switch (match.second.type())
- {
- case nlohmann::json::value_t::string:
- {
- std::regex search(match.second.get<std::string>());
- std::smatch regMatch;
-
- // convert value to string respresentation
- std::string probeValue = std::visit(
- VariantToStringVisitor(), deviceValue->second);
- if (!std::regex_search(probeValue, regMatch, search))
- {
- deviceMatches = false;
- break;
- }
- break;
- }
- case nlohmann::json::value_t::boolean:
- case nlohmann::json::value_t::number_unsigned:
- {
- unsigned int probeValue = std::visit(
- VariantToUnsignedIntVisitor(), deviceValue->second);
-
- if (probeValue != match.second.get<unsigned int>())
- {
- deviceMatches = false;
- }
- break;
- }
- case nlohmann::json::value_t::number_integer:
- {
- int probeValue = std::visit(VariantToIntVisitor(),
- deviceValue->second);
-
- if (probeValue != match.second.get<int>())
- {
- deviceMatches = false;
- }
- break;
- }
- case nlohmann::json::value_t::number_float:
- {
- float probeValue = std::visit(VariantToFloatVisitor(),
- deviceValue->second);
-
- if (probeValue != match.second.get<float>())
- {
- deviceMatches = false;
- }
- break;
- }
- default:
- {
- std::cerr << "unexpected dbus probe type "
- << match.second.type_name() << "\n";
- deviceMatches = false;
- }
- }
+ deviceMatches = matchProbe(match.second, deviceValue->second);
}
else
{
diff --git a/src/Utils.cpp b/src/Utils.cpp
index b872ee7..27f1c15 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -391,3 +391,66 @@
}
return ret;
}
+
+bool matchProbe(const nlohmann::json& probe, const BasicVariantType& dbusValue)
+{
+ bool deviceMatches = true;
+
+ switch (probe.type())
+ {
+ case nlohmann::json::value_t::string:
+ {
+ std::regex search(probe.get<std::string>());
+ std::smatch regMatch;
+
+ // convert value to string respresentation
+ std::string probeValue =
+ std::visit(VariantToStringVisitor(), dbusValue);
+ if (!std::regex_search(probeValue, regMatch, search))
+ {
+ deviceMatches = false;
+ break;
+ }
+ break;
+ }
+ case nlohmann::json::value_t::boolean:
+ case nlohmann::json::value_t::number_unsigned:
+ {
+ unsigned int probeValue =
+ std::visit(VariantToUnsignedIntVisitor(), dbusValue);
+
+ if (probeValue != probe.get<unsigned int>())
+ {
+ deviceMatches = false;
+ }
+ break;
+ }
+ case nlohmann::json::value_t::number_integer:
+ {
+ int probeValue = std::visit(VariantToIntVisitor(), dbusValue);
+
+ if (probeValue != probe.get<int>())
+ {
+ deviceMatches = false;
+ }
+ break;
+ }
+ case nlohmann::json::value_t::number_float:
+ {
+ float probeValue = std::visit(VariantToFloatVisitor(), dbusValue);
+
+ if (probeValue != probe.get<float>())
+ {
+ deviceMatches = false;
+ }
+ break;
+ }
+ default:
+ {
+ std::cerr << "unexpected dbus probe type " << probe.type_name()
+ << "\n";
+ deviceMatches = false;
+ }
+ }
+ return deviceMatches;
+}