Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 1 | #include <fmt/format.h> |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 2 | #include <libyang/Tree_Data.hpp> |
| 3 | #include "utils/libyang.h" |
| 4 | |
Tomáš Pecka | fd5ebd3 | 2021-10-07 09:48:13 +0200 | [diff] [blame] | 5 | namespace velia::utils { |
| 6 | |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 7 | const char* getValueAsString(const libyang::S_Data_Node& node) |
| 8 | { |
| 9 | if (!node || node->schema()->nodetype() != LYS_LEAF) { |
| 10 | throw std::logic_error("retrieveString: invalid node"); |
| 11 | } |
| 12 | |
| 13 | return libyang::Data_Node_Leaf_List(node).value_str(); |
| 14 | } |
| 15 | |
Tomáš Pecka | fd90efb | 2021-10-07 10:40:44 +0200 | [diff] [blame] | 16 | std::optional<libyang::S_Data_Node> getUniqueSubtree(const libyang::S_Data_Node& start, const char* path) |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 17 | { |
| 18 | auto set = start->find_path(path); |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 19 | |
Tomáš Pecka | fd90efb | 2021-10-07 10:40:44 +0200 | [diff] [blame] | 20 | switch(set->number()) { |
| 21 | case 0: |
| 22 | return std::nullopt; |
| 23 | case 1: |
| 24 | return set->data().front(); |
| 25 | default: |
| 26 | throw std::runtime_error(fmt::format("getUniqueSubtree({}, {}): more than one match (got {})", start->path(), path, set->number())); |
| 27 | } |
Václav Kubernát | cbdc98a | 2021-01-28 13:24:37 +0100 | [diff] [blame] | 28 | } |
Tomáš Pecka | fd5ebd3 | 2021-10-07 09:48:13 +0200 | [diff] [blame] | 29 | } |