Remove Schema::is* methods
These methods are not really used anywhere, and if they are, they can be
replaced with a call to Schema::nodeType. No reason to keep them. I had
to implement StaticSchema::nodeType(std::string) because of
presenceContainerPath_class, but that's ok.
Change-Id: Ia27f6aa1f1cd098f087767365cb6a706c255c206
diff --git a/src/ast_handlers.hpp b/src/ast_handlers.hpp
index b9d7a9d..b522957 100644
--- a/src/ast_handlers.hpp
+++ b/src/ast_handlers.hpp
@@ -157,19 +157,8 @@
return;
}
- try {
- boost::optional<std::string> module;
- if (ast.m_nodes.back().m_prefix)
- module = ast.m_nodes.back().m_prefix.value().m_name;
- container_ cont = std::get<container_>(ast.m_nodes.back().m_suffix);
- auto location = pathWithoutLastNode(parserContext.currentSchemaPath());
-
- if (!schema.isPresenceContainer(location, {module, cont.m_name})) {
- parserContext.m_errorMsg = "This container is not a presence container.";
- _pass(context) = false;
- }
- } catch (std::bad_variant_access&) {
- parserContext.m_errorMsg = "This is not a container.";
+ if (schema.nodeType(pathToSchemaString(parserContext.currentSchemaPath(), Prefixes::Always)) != yang::NodeTypes::PresenceContainer) {
+ parserContext.m_errorMsg = "This container is not a presence container.";
_pass(context) = false;
}
}
diff --git a/src/schema.cpp b/src/schema.cpp
index e092569..c390e31 100644
--- a/src/schema.cpp
+++ b/src/schema.cpp
@@ -9,49 +9,3 @@
#include "schema.hpp"
Schema::~Schema() = default;
-
-bool Schema::isList(const schemaPath_& location, const ModuleNodePair& node) const
-{
- try {
- return nodeType(location, node) == yang::NodeTypes::List;
- } catch (InvalidNodeException&) {
- return false;
- }
-}
-
-bool Schema::isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const
-{
- try {
- return nodeType(location, node) == yang::NodeTypes::PresenceContainer;
- } catch (InvalidNodeException&) {
- return false;
- }
-}
-
-bool Schema::isContainer(const schemaPath_& location, const ModuleNodePair& node) const
-{
- try {
- auto type = nodeType(location, node);
- return type == yang::NodeTypes::Container || type == yang::NodeTypes::PresenceContainer;
- } catch (InvalidNodeException&) {
- return false;
- }
-}
-
-bool Schema::isLeaf(const schemaPath_& location, const ModuleNodePair& node) const
-{
- try {
- return nodeType(location, node) == yang::NodeTypes::Leaf;
- } catch (InvalidNodeException&) {
- return false;
- }
-}
-
-bool Schema::isLeafList(const std::string& path) const
-{
- try {
- return nodeType(path) == yang::NodeTypes::LeafList;
- } catch (InvalidNodeException&) {
- return false;
- }
-}
diff --git a/src/schema.hpp b/src/schema.hpp
index 2daa73f..75c1f8f 100644
--- a/src/schema.hpp
+++ b/src/schema.hpp
@@ -54,10 +54,6 @@
public:
virtual ~Schema();
- bool isContainer(const schemaPath_& location, const ModuleNodePair& node) const;
- bool isLeaf(const schemaPath_& location, const ModuleNodePair& node) const;
- bool isList(const schemaPath_& location, const ModuleNodePair& node) const;
- bool isPresenceContainer(const schemaPath_& location, const ModuleNodePair& node) const;
bool isLeafList(const std::string& path) const;
virtual yang::NodeTypes nodeType(const std::string& path) const = 0;
virtual yang::NodeTypes nodeType(const schemaPath_& location, const ModuleNodePair& node) const = 0;
diff --git a/src/static_schema.cpp b/src/static_schema.cpp
index 989ef21..a7130b3 100644
--- a/src/static_schema.cpp
+++ b/src/static_schema.cpp
@@ -244,9 +244,12 @@
throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."};
}
-yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
+yang::NodeTypes StaticSchema::nodeType(const std::string& path) const
{
- throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
+ auto locationString = stripLastNodeFromPath(path);
+
+ auto node = fullNodeName(locationString, lastNodeOfSchemaPath(path));
+ return std::visit(impl_nodeType{}, children(locationString).at(node).m_nodeType);
}
std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
diff --git a/src/sysrepo_access.cpp b/src/sysrepo_access.cpp
index 3af5f79..3278766 100644
--- a/src/sysrepo_access.cpp
+++ b/src/sysrepo_access.cpp
@@ -196,7 +196,7 @@
return;
for (unsigned int i = 0; i < items->val_cnt(); i++) {
auto value = leafValueFromVal(items->val(i));
- if (m_schema->isLeafList(items->val(i)->xpath())) {
+ if (m_schema->nodeType(items->val(i)->xpath()) == yang::NodeTypes::LeafList) {
res.push_back({items->val(i)->xpath(), special_{SpecialValue::LeafList}});
std::string leafListPath = items->val(i)->xpath();
while (i < items->val_cnt() && leafListPath == items->val(i)->xpath()) {
diff --git a/tests/yang.cpp b/tests/yang.cpp
index e2ece6f..9419051 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -459,106 +459,10 @@
SECTION("positive")
{
- SECTION("isContainer")
- {
- SECTION("example-schema:a")
- {
- node.first = "example-schema";
- node.second = "a";
- }
-
- SECTION("example-schema:a/a2")
- {
- path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
- node.second = "a2";
- }
-
- SECTION("example-schema:ethernet")
- {
- node.first = "example-schema";
- node.second = "ethernet";
- }
-
- SECTION("example-schema:loopback")
- {
- node.first = "example-schema";
- node.second = "loopback";
- }
-
- REQUIRE(ys.isContainer(path, node));
- }
- SECTION("isLeaf")
- {
- SECTION("example-schema:leafString")
- {
- node.first = "example-schema";
- node.second = "leafString";
- }
-
- SECTION("example-schema:a/leafa")
- {
- path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
- node.first = "example-schema";
- node.second = "leafa";
- }
-
- SECTION("example-schema:carry")
- {
- node.first = "example-schema";
- node.second = "carry";
- }
-
- SECTION("example-schema:zero")
- {
- node.first = "example-schema";
- node.second = "zero";
- }
-
- SECTION("example-schema:direction")
- {
- node.first = "example-schema";
- node.second = "direction";
- }
-
- SECTION("example-schema:interrupt")
- {
- node.first = "example-schema";
- node.second = "interrupt";
- }
-
- REQUIRE(ys.isLeaf(path, node));
- }
SECTION("isModule")
{
REQUIRE(ys.isModule("example-schema"));
}
- SECTION("isList")
- {
- SECTION("example-schema:_list")
- {
- node.first = "example-schema";
- node.second = "_list";
- }
-
- SECTION("example-schema:twoKeyList")
- {
- node.first = "example-schema";
- node.second = "twoKeyList";
- }
-
- REQUIRE(ys.isList(path, node));
- }
- SECTION("isPresenceContainer")
- {
- SECTION("example-schema:a/a2/a3")
- {
- path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
- path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a2")));
- node.second = "a3";
- }
-
- REQUIRE(ys.isPresenceContainer(path, node));
- }
SECTION("listHasKey")
{
@@ -1231,29 +1135,7 @@
node.second = "nevim";
}
- REQUIRE(!ys.isPresenceContainer(path, node));
- REQUIRE(!ys.isList(path, node));
- REQUIRE(!ys.isLeaf(path, node));
- REQUIRE(!ys.isContainer(path, node));
- }
-
- SECTION("\"is\" methods return false for existing nodes for different nodetypes")
- {
- SECTION("example-schema:a")
- {
- node.first = "example-schema";
- node.second = "a";
- }
-
- SECTION("example-schema:a/a2")
- {
- path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
- node.second = "a2";
- }
-
- REQUIRE(!ys.isPresenceContainer(path, node));
- REQUIRE(!ys.isList(path, node));
- REQUIRE(!ys.isLeaf(path, node));
+ REQUIRE_THROWS(ys.nodeType(path, node));
}
SECTION("nodetype-specific methods called with different nodetypes")
@@ -1283,10 +1165,7 @@
node.second = "startAndStop";
}
- REQUIRE(!ys.isPresenceContainer(path, node));
- REQUIRE(!ys.isList(path, node));
- REQUIRE(!ys.isLeaf(path, node));
- REQUIRE(!ys.isContainer(path, node));
+ REQUIRE_THROWS(ys.nodeType(path, node));
}
SECTION("choice is not a node")
@@ -1297,10 +1176,7 @@
node.second = "interface";
}
- REQUIRE(!ys.isPresenceContainer(path, node));
- REQUIRE(!ys.isList(path, node));
- REQUIRE(!ys.isLeaf(path, node));
- REQUIRE(!ys.isContainer(path, node));
+ REQUIRE_THROWS(ys.nodeType(path, node));
}
SECTION("case is not a node")
@@ -1317,10 +1193,7 @@
node.second = "caseEthernet";
}
- REQUIRE(!ys.isPresenceContainer(path, node));
- REQUIRE(!ys.isList(path, node));
- REQUIRE(!ys.isLeaf(path, node));
- REQUIRE(!ys.isContainer(path, node));
+ REQUIRE_THROWS(ys.nodeType(path, node));
}
}
}