Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 8 | #include <boost/algorithm/string/erase.hpp> |
| 9 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 10 | #include "utils.hpp" |
| 11 | |
| 12 | std::string joinPaths(const std::string& prefix, const std::string& suffix) |
| 13 | { |
Václav Kubernát | 50949e6 | 2018-08-30 12:50:08 +0200 | [diff] [blame] | 14 | if (prefix.empty() || suffix.empty() || prefix == "/") |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 15 | return prefix + suffix; |
| 16 | else |
| 17 | return prefix + '/' + suffix; |
| 18 | } |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 19 | |
| 20 | std::string stripLastNodeFromPath(const std::string& path) |
| 21 | { |
| 22 | std::string res = path; |
| 23 | auto pos = res.find_last_of('/'); |
| 24 | if (pos == res.npos) |
| 25 | res.clear(); |
| 26 | else |
| 27 | res.erase(pos); |
| 28 | return res; |
| 29 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 30 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 31 | schemaPath_ pathWithoutLastNode(const schemaPath_& path) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 32 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 33 | return schemaPath_{path.m_scope, decltype(schemaPath_::m_nodes)(path.m_nodes.begin(), path.m_nodes.end() - 1)}; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 34 | } |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 35 | |
| 36 | std::string leafDataTypeToString(yang::LeafDataTypes type) |
| 37 | { |
| 38 | switch (type) { |
| 39 | case yang::LeafDataTypes::String: |
| 40 | return "a string"; |
| 41 | case yang::LeafDataTypes::Decimal: |
| 42 | return "a decimal"; |
| 43 | case yang::LeafDataTypes::Bool: |
| 44 | return "a boolean"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 45 | case yang::LeafDataTypes::Int8: |
| 46 | return "an 8-bit integer"; |
| 47 | case yang::LeafDataTypes::Uint8: |
| 48 | return "an 8-bit unsigned integer"; |
| 49 | case yang::LeafDataTypes::Int16: |
| 50 | return "a 16-bit integer"; |
| 51 | case yang::LeafDataTypes::Uint16: |
| 52 | return "a 16-bit unsigned integer"; |
| 53 | case yang::LeafDataTypes::Int32: |
| 54 | return "a 32-bit integer"; |
| 55 | case yang::LeafDataTypes::Uint32: |
| 56 | return "a 32-bit unsigned integer"; |
| 57 | case yang::LeafDataTypes::Int64: |
| 58 | return "a 64-bit integer"; |
| 59 | case yang::LeafDataTypes::Uint64: |
| 60 | return "a 64-bit unsigned integer"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 61 | case yang::LeafDataTypes::Enum: |
| 62 | return "an enum"; |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 63 | case yang::LeafDataTypes::IdentityRef: |
| 64 | return "an identity"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 65 | default: |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 66 | throw std::runtime_error("leafDataTypeToString: unsupported leaf data type"); |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 67 | } |
| 68 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 69 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 70 | std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 71 | { |
| 72 | if (!pair.first) { |
| 73 | return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second; |
| 74 | } else { |
| 75 | return pair.first.value() + ":" + pair.second; |
| 76 | } |
| 77 | } |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 78 | |
| 79 | std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair) |
| 80 | { |
| 81 | return fullNodeName(dataPathToSchemaPath(location), pair); |
| 82 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 83 | |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 84 | std::set<std::string> filterByPrefix(const std::set<std::string>& set, const std::string_view prefix) |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 85 | { |
| 86 | std::set<std::string> filtered; |
| 87 | std::copy_if(set.begin(), set.end(), |
| 88 | std::inserter(filtered, filtered.end()), |
| 89 | [prefix] (auto it) { return boost::starts_with(it, prefix); }); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 90 | return filtered; |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 91 | } |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 92 | |
| 93 | struct leafDataToStringVisitor : boost::static_visitor<std::string> { |
| 94 | std::string operator()(const enum_& data) const |
| 95 | { |
| 96 | return data.m_value; |
| 97 | } |
| 98 | |
| 99 | std::string operator()(const binary_& data) const |
| 100 | { |
| 101 | return data.m_value; |
| 102 | } |
| 103 | |
| 104 | std::string operator()(const identityRef_& data) const |
| 105 | { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame^] | 106 | return data.m_prefix.value().m_name + ":" + data.m_value; |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Jan Kundrát | c43acf7 | 2019-07-02 19:28:22 +0200 | [diff] [blame] | 109 | std::string operator()(const std::string& data) const |
| 110 | { |
| 111 | return data; |
| 112 | } |
| 113 | |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 114 | template <typename T> |
| 115 | std::string operator()(const T& data) const |
| 116 | { |
Jan Kundrát | c43acf7 | 2019-07-02 19:28:22 +0200 | [diff] [blame] | 117 | return std::to_string(data); |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | |
| 121 | std::string leafDataToString(const leaf_data_ value) |
| 122 | { |
| 123 | return boost::apply_visitor(leafDataToStringVisitor(), value); |
| 124 | } |