Make prefixes optional in listInstanceToString
For libyang, I need a version without prefixes. Side note: the call in
SysrepoAccess would also work without the prefixes, but I'm going to
leave them, so that the functionality doesn't change.
Change-Id: I0c309288ec39ec3f5e835050fb73a5238ebe8e33
diff --git a/src/netconf_access.cpp b/src/netconf_access.cpp
index 2a2fa96..15b79a8 100644
--- a/src/netconf_access.cpp
+++ b/src/netconf_access.cpp
@@ -134,7 +134,7 @@
if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
sourceNode->insert_attr(yangModule, "value", leafDataToString(relative.m_path.at(".")).c_str());
} else {
- sourceNode->insert_attr(yangModule, "key", instanceToString(node->node_module()->name(), relative.m_path).c_str());
+ sourceNode->insert_attr(yangModule, "key", instanceToString(relative.m_path, node->node_module()->name()).c_str());
}
}
doEditFromDataNode(sourceNode);
diff --git a/src/sysrepo_access.cpp b/src/sysrepo_access.cpp
index d8a64da..7b9cb7a 100644
--- a/src/sysrepo_access.cpp
+++ b/src/sysrepo_access.cpp
@@ -313,7 +313,7 @@
if (m_schema->nodeType(source) == yang::NodeTypes::LeafList) {
destPathStr = stripLeafListValueFromPath(source) + "[.='" + leafDataToString(relative.m_path.at(".")) + "']";
} else {
- destPathStr = stripLastListInstanceFromPath(source) + instanceToString(m_schema->dataNodeFromPath(source)->node_module()->name(), relative.m_path);
+ destPathStr = stripLastListInstanceFromPath(source) + instanceToString(relative.m_path, m_schema->dataNodeFromPath(source)->node_module()->name());
}
}
m_session->move_item(source.c_str(), toSrMoveOp(move), destPathStr.c_str());
diff --git a/src/utils.cpp b/src/utils.cpp
index cf789b7..e151417 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -243,12 +243,13 @@
return res;
}
-std::string instanceToString(const std::string& modName, const ListInstance& instance)
+std::string instanceToString(const ListInstance& instance, const std::optional<std::string>& modName)
{
std::string instanceStr;
+ auto modulePrefix = modName ? *modName + ":" : "";
for (const auto& [key, value] : instance) {
using namespace std::string_literals;
- instanceStr += "[" + modName + ":" + key + "=" + escapeListKeyString(leafDataToString(value)) + "]";
+ instanceStr += "[" + modulePrefix + key + "=" + escapeListKeyString(leafDataToString(value)) + "]";
}
return instanceStr;
}
diff --git a/src/utils.hpp b/src/utils.hpp
index 0196ff8..ec3f3c8 100644
--- a/src/utils.hpp
+++ b/src/utils.hpp
@@ -27,5 +27,5 @@
schemaPath_ anyPathToSchemaPath(const boost::variant<dataPath_, schemaPath_, module_>& path);
std::string stripLeafListValueFromPath(const std::string& path);
std::string stripLastListInstanceFromPath(const std::string& path);
-// The string includes module name prefixes.
-std::string instanceToString(const std::string& modName, const ListInstance& instance);
+// The second argument controls whether module prefixes should be added to the keys.
+std::string instanceToString(const ListInstance& instance, const std::optional<std::string>& modName = std::nullopt);