refactor: a shorter name for converting leaf values to string
Change-Id: I175e86967bc7480f49623f381f7a3723f421bf89
diff --git a/src/firewall/Firewall.cpp b/src/firewall/Firewall.cpp
index f95ed39..345747d 100644
--- a/src/firewall/Firewall.cpp
+++ b/src/firewall/Firewall.cpp
@@ -67,17 +67,17 @@
if (nodeSchemaPath == nodepaths::ace_comment) {
// We will use the ACE name as a comment inside the rule. However, the comment must be at the end, so we
// save it for later.
- comment = velia::utils::getValueAsString(node);
+ comment = velia::utils::asString(node);
} else if (nodeSchemaPath == nodepaths::ipv4_matches) {
// Here we save the ip we're matching against.
- match = " ip saddr "s + velia::utils::getValueAsString(node);
+ match = " ip saddr "s + velia::utils::asString(node);
} else if (nodeSchemaPath == nodepaths::ipv6_matches) {
// Here we save the ip we're matching against.
- match = " ip6 saddr "s + velia::utils::getValueAsString(node);
+ match = " ip6 saddr "s + velia::utils::asString(node);
} else if (nodeSchemaPath == nodepaths::action) {
// Action is the last statement we get, so this is where we create the actual rule.
ss << "add rule inet filter acls" << match;
- auto action = velia::utils::getValueAsString(node);
+ auto action = velia::utils::asString(node);
if (action == "ietf-access-control-list:accept") {
ss << " accept";
} else if (action == "ietf-access-control-list:drop") {
diff --git a/src/system/Authentication.cpp b/src/system/Authentication.cpp
index 460e53c..1d8861e 100644
--- a/src/system/Authentication.cpp
+++ b/src/system/Authentication.cpp
@@ -281,8 +281,8 @@
sysrepo::RpcActionCb changePasswordCb = [this, changePassword] (auto, auto, auto, auto input, auto, auto, auto output) {
auto userNode = utils::getUniqueSubtree(input, authentication_container + "/users").value();
- auto name = utils::getValueAsString(utils::getUniqueSubtree(userNode, "name").value());
- auto password = utils::getValueAsString(utils::getUniqueSubtree(userNode, "change-password/password-cleartext").value());
+ auto name = utils::asString(utils::getUniqueSubtree(userNode, "name").value());
+ auto password = utils::asString(utils::getUniqueSubtree(userNode, "change-password/password-cleartext").value());
m_log->debug("Changing password for {}", name);
try {
changePassword(name, password, m_etc_shadow);
@@ -300,8 +300,8 @@
sysrepo::RpcActionCb addKeyCb = [this] (auto, auto, auto, auto input, auto, auto, auto output) {
auto userNode = utils::getUniqueSubtree(input, authentication_container + "/users").value();
- auto name = utils::getValueAsString(utils::getUniqueSubtree(userNode, "name").value());
- auto key = utils::getValueAsString(utils::getUniqueSubtree(userNode, "add-authorized-key/key").value());
+ auto name = utils::asString(utils::getUniqueSubtree(userNode, "name").value());
+ auto key = utils::asString(utils::getUniqueSubtree(userNode, "add-authorized-key/key").value());
m_log->debug("Adding key for {}", name);
try {
addKey(name, key);
@@ -318,8 +318,8 @@
sysrepo::RpcActionCb removeKeyCb = [this] (auto, auto, auto, auto input, auto, auto, auto output) {
auto userNode = utils::getUniqueSubtree(input, authentication_container + "/users").value();
- auto name = utils::getValueAsString(utils::getUniqueSubtree(userNode, "name").value());
- auto key = std::stol(utils::getValueAsString(utils::getUniqueSubtree(userNode, "authorized-keys/index").value()));
+ auto name = utils::asString(utils::getUniqueSubtree(userNode, "name").value());
+ auto key = std::stol(utils::asString(utils::getUniqueSubtree(userNode, "authorized-keys/index").value()));
m_log->debug("Removing key for {}", name);
try {
removeKey(name, key);
diff --git a/src/system/Firmware.cpp b/src/system/Firmware.cpp
index 4b5956c..b1808e4 100644
--- a/src/system/Firmware.cpp
+++ b/src/system/Firmware.cpp
@@ -82,7 +82,7 @@
auto lock = updateSlotStatus();
try {
- auto source = utils::getValueAsString(*input.findPath("url"));
+ auto source = utils::asString(*input.findPath("url"));
m_rauc->install(source);
} catch (sdbus::Error& e) {
m_log->warn("RAUC install error: '{}'", e.what());
@@ -95,7 +95,7 @@
m_srSubscribeRPC = m_srSessionRPC.onRPCAction(CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install", cbRPC);
::sysrepo::RpcActionCb markSlotAs = [this](auto, auto, auto path, auto input, auto, auto, auto) {
- auto bootName = utils::getValueAsString(*input.parent()->findPath("name"));
+ auto bootName = utils::asString(*input.parent()->findPath("name"));
std::string action;
if (path == CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot/set-active-after-reboot") {
action = "active";
diff --git a/src/system/IETFInterfacesConfig.cpp b/src/system/IETFInterfacesConfig.cpp
index ab42481..91136a9 100644
--- a/src/system/IETFInterfacesConfig.cpp
+++ b/src/system/IETFInterfacesConfig.cpp
@@ -52,7 +52,7 @@
const auto xpath = "ietf-ip:" + proto + "/enabled";
if (auto node = velia::utils::getUniqueSubtree(linkEntry, xpath)) {
- return velia::utils::getValueAsString(node.value()) == "true"s;
+ return velia::utils::asString(node.value()) == "true"s;
}
return false;
@@ -93,10 +93,10 @@
for (const auto& linkEntry : linkEntries) {
std::map<std::string, std::vector<std::string>> configValues;
- auto linkName = utils::getValueAsString(utils::getUniqueSubtree(linkEntry, "name").value());
+ auto linkName = utils::asString(utils::getUniqueSubtree(linkEntry, "name").value());
if (auto node = utils::getUniqueSubtree(linkEntry, "description")) {
- configValues["Network"].push_back("Description="s + utils::getValueAsString(node.value()));
+ configValues["Network"].push_back("Description="s + utils::asString(node.value()));
}
// if addresses present, generate them...
@@ -110,8 +110,8 @@
const auto addresses = linkEntry.findXPath(IPAddressListXPath);
for (const auto& ipEntry : addresses) {
- auto ipAddress = utils::getValueAsString(utils::getUniqueSubtree(ipEntry, "ip").value());
- auto prefixLen = utils::getValueAsString(utils::getUniqueSubtree(ipEntry, "prefix-length").value());
+ auto ipAddress = utils::asString(utils::getUniqueSubtree(ipEntry, "ip").value());
+ auto prefixLen = utils::asString(utils::getUniqueSubtree(ipEntry, "prefix-length").value());
spdlog::get("system")->trace("Link {}: address {}/{} configured", linkName, ipAddress, prefixLen);
configValues["Network"].push_back("Address="s + ipAddress + "/" + prefixLen);
@@ -123,7 +123,7 @@
bool isSlave = false;
if (auto node = utils::getUniqueSubtree(linkEntry, "czechlight-network:bridge")) {
- configValues["Network"].push_back("Bridge="s + utils::getValueAsString(node.value()));
+ configValues["Network"].push_back("Bridge="s + utils::asString(node.value()));
isSlave = true;
}
@@ -132,13 +132,13 @@
}
// network autoconfiguration
- if (auto node = utils::getUniqueSubtree(linkEntry, "ietf-ip:ipv6/ietf-ip:autoconf/ietf-ip:create-global-addresses"); protocolEnabled(linkEntry, "ipv6") && utils::getValueAsString(node.value()) == "true"s) {
+ if (auto node = utils::getUniqueSubtree(linkEntry, "ietf-ip:ipv6/ietf-ip:autoconf/ietf-ip:create-global-addresses"); protocolEnabled(linkEntry, "ipv6") && utils::asString(node.value()) == "true"s) {
configValues["Network"].push_back("IPv6AcceptRA=true");
} else {
configValues["Network"].push_back("IPv6AcceptRA=false");
}
- if (auto node = utils::getUniqueSubtree(linkEntry, "ietf-ip:ipv4/czechlight-network:dhcp-client"); protocolEnabled(linkEntry, "ipv4") && utils::getValueAsString(node.value()) == "true"s) {
+ if (auto node = utils::getUniqueSubtree(linkEntry, "ietf-ip:ipv4/czechlight-network:dhcp-client"); protocolEnabled(linkEntry, "ipv4") && utils::asString(node.value()) == "true"s) {
configValues["Network"].push_back("DHCP=ipv4");
} else {
configValues["Network"].push_back("DHCP=no");
diff --git a/src/system/JournalUpload.cpp b/src/system/JournalUpload.cpp
index e3c20e9..50239ff 100644
--- a/src/system/JournalUpload.cpp
+++ b/src/system/JournalUpload.cpp
@@ -29,7 +29,7 @@
std::string url;
auto hostNode = data->findPath(UPLOAD_URL_CONTAINER + "/host"s);
- auto hostValue = velia::utils::getValueAsString(*hostNode);
+ auto hostValue = velia::utils::asString(*hostNode);
auto isIpv6 = hostNode->asTerm().valueType().internalPluginId().find("ipv6") != std::string::npos;
url += data->findPath(UPLOAD_URL_CONTAINER + "/protocol"s)->asTerm().valueStr();
diff --git a/src/system/LED.cpp b/src/system/LED.cpp
index 35ac43d..18ec7b2 100644
--- a/src/system/LED.cpp
+++ b/src/system/LED.cpp
@@ -54,7 +54,7 @@
m_srSubscribe = m_srSession.onRPCAction(
CZECHLIGHT_SYSTEM_LEDS_MODULE_PREFIX + "uid",
[this, uidMaxBrightness, triggerFile, brightnessFile](auto session, auto, auto, auto input, auto, auto, auto) {
- std::string val = utils::getValueAsString(utils::getUniqueSubtree(input, CZECHLIGHT_SYSTEM_LEDS_MODULE_PREFIX + "uid/state").value());
+ std::string val = utils::asString(utils::getUniqueSubtree(input, CZECHLIGHT_SYSTEM_LEDS_MODULE_PREFIX + "uid/state").value());
try {
if (val == "on") {
diff --git a/src/utils/libyang.cpp b/src/utils/libyang.cpp
index 79a5e86..800cfe0 100644
--- a/src/utils/libyang.cpp
+++ b/src/utils/libyang.cpp
@@ -5,7 +5,7 @@
namespace velia::utils {
-std::string getValueAsString(const libyang::DataNode& node)
+std::string asString(const libyang::DataNode& node)
{
if (node.schema().nodeType() != libyang::NodeType::Leaf) {
throw std::logic_error("retrieveString: invalid node");
diff --git a/src/utils/libyang.h b/src/utils/libyang.h
index e91e82b..b337e5c 100644
--- a/src/utils/libyang.h
+++ b/src/utils/libyang.h
@@ -22,7 +22,7 @@
* @param node A libyang data node. Mustn't be nullptr. Must be a leaf.
*
*/
-std::string getValueAsString(const libyang::DataNode& node);
+std::string asString(const libyang::DataNode& node);
/** @brief Gets exactly one node based on `path` starting from `start`. Uses findXPath, so it works even with lists with
* missing predicates.
diff --git a/tests/sysrepo_ietf-hardware.cpp b/tests/sysrepo_ietf-hardware.cpp
index 003f593..050d5c2 100644
--- a/tests/sysrepo_ietf-hardware.cpp
+++ b/tests/sysrepo_ietf-hardware.cpp
@@ -46,7 +46,7 @@
auto directLeafNodeQuery = [&](const std::string& xpath) {
auto val = client.getData(xpath);
REQUIRE(val);
- return velia::utils::getValueAsString(*val->findPath(xpath));
+ return velia::utils::asString(*val->findPath(xpath));
};
auto sysfsTempCpu = std::make_shared<FakeHWMon>();