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 | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame^] | 8 | #include <experimental/iterator> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 9 | #include <sstream> |
Václav Kubernát | cb3af40 | 2020-02-12 16:49:17 +0100 | [diff] [blame] | 10 | #include "completion.hpp" |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 11 | #include "utils.hpp" |
| 12 | |
| 13 | std::string joinPaths(const std::string& prefix, const std::string& suffix) |
| 14 | { |
Václav Kubernát | a44bdf2 | 2020-01-24 12:15:31 +0100 | [diff] [blame] | 15 | // These two if statements are essential for the algorithm: |
| 16 | // The first one solves joining nothing and a relative path - the algorithm |
| 17 | // down below adds a leading slash, turning it into an absolute path. |
| 18 | // The second one would always add a trailing slash to the path. |
| 19 | if (prefix.empty()) { |
| 20 | return suffix; |
| 21 | } |
| 22 | |
| 23 | if (suffix.empty()) { |
| 24 | return prefix; |
| 25 | } |
| 26 | |
| 27 | // Otherwise, strip slashes where the join is going to happen. This will |
| 28 | // also change "/" to "", but the return statement takes care of that and |
| 29 | // inserts the slash again. |
| 30 | auto prefixWithoutTrailingSlash = !prefix.empty() && prefix.back() == '/' ? prefix.substr(0, prefix.length() - 1) : prefix; |
| 31 | auto suffixWithoutLeadingSlash = !suffix.empty() && suffix.front() == '/' ? suffix.substr(1) : suffix; |
| 32 | |
| 33 | // And join the result with a slash. |
| 34 | return prefixWithoutTrailingSlash + '/' + suffixWithoutLeadingSlash; |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 35 | } |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 36 | |
| 37 | std::string stripLastNodeFromPath(const std::string& path) |
| 38 | { |
| 39 | std::string res = path; |
| 40 | auto pos = res.find_last_of('/'); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 41 | if (pos == res.npos) { // path has no backslash - it's either empty, or is a relative path with one fragment |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 42 | res.clear(); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 43 | } else if (pos == 0) { // path has one backslash at the start - it's either "/" or "/one-path-fragment" |
| 44 | return "/"; |
| 45 | } else { |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 46 | res.erase(pos); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 47 | } |
Václav Kubernát | 60d6f29 | 2018-05-25 09:45:32 +0200 | [diff] [blame] | 48 | return res; |
| 49 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 50 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 51 | schemaPath_ pathWithoutLastNode(const schemaPath_& path) |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 52 | { |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 53 | 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] | 54 | } |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 55 | |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 56 | struct impl_leafDataTypeToString { |
| 57 | std::string operator()(const yang::String) |
| 58 | { |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 59 | return "a string"; |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 60 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 61 | std::string operator()(const yang::Decimal) |
| 62 | { |
| 63 | return "a decimal"; |
| 64 | } |
| 65 | std::string operator()(const yang::Bool) |
| 66 | { |
| 67 | return "a boolean"; |
| 68 | } |
| 69 | std::string operator()(const yang::Int8) |
| 70 | { |
| 71 | return "an 8-bit integer"; |
| 72 | } |
| 73 | std::string operator()(const yang::Uint8) |
| 74 | { |
| 75 | return "an 8-bit unsigned integer"; |
| 76 | } |
| 77 | std::string operator()(const yang::Int16) |
| 78 | { |
| 79 | return "a 16-bit integer"; |
| 80 | } |
| 81 | std::string operator()(const yang::Uint16) |
| 82 | { |
| 83 | return "a 16-bit unsigned integer"; |
| 84 | } |
| 85 | std::string operator()(const yang::Int32) |
| 86 | { |
| 87 | return "a 32-bit integer"; |
| 88 | } |
| 89 | std::string operator()(const yang::Uint32) |
| 90 | { |
| 91 | return "a 32-bit unsigned integer"; |
| 92 | } |
| 93 | std::string operator()(const yang::Int64) |
| 94 | { |
| 95 | return "a 64-bit integer"; |
| 96 | } |
| 97 | std::string operator()(const yang::Uint64) |
| 98 | { |
| 99 | return "a 64-bit unsigned integer"; |
| 100 | } |
| 101 | std::string operator()(const yang::Binary) |
| 102 | { |
| 103 | return "a base64-encoded binary value"; |
| 104 | } |
| 105 | std::string operator()(const yang::Enum&) |
| 106 | { |
| 107 | return "an enum"; |
| 108 | } |
| 109 | std::string operator()(const yang::IdentityRef&) |
| 110 | { |
| 111 | return "an identity"; |
| 112 | } |
| 113 | std::string operator()(const yang::LeafRef&) |
| 114 | { |
| 115 | return "a leafref"; |
| 116 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame^] | 117 | std::string operator()(const yang::Union& type) |
| 118 | { |
| 119 | std::ostringstream ss; |
| 120 | std::transform(type.m_unionTypes.begin(), type.m_unionTypes.end(), std::experimental::make_ostream_joiner(ss, ", "), [this](const auto& unionType) { |
| 121 | return std::visit(*this, unionType); |
| 122 | }); |
| 123 | return ss.str(); |
| 124 | } |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | std::string leafDataTypeToString(const yang::LeafDataType& type) |
| 128 | { |
| 129 | return std::visit(impl_leafDataTypeToString{}, type); |
Václav Kubernát | 0b0272f | 2018-06-13 14:13:08 +0200 | [diff] [blame] | 130 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 131 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 132 | std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 133 | { |
| 134 | if (!pair.first) { |
| 135 | return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second; |
| 136 | } else { |
| 137 | return pair.first.value() + ":" + pair.second; |
| 138 | } |
| 139 | } |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 140 | |
| 141 | std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair) |
| 142 | { |
| 143 | return fullNodeName(dataPathToSchemaPath(location), pair); |
| 144 | } |
Václav Kubernát | 4108e0d | 2018-10-29 13:32:22 +0100 | [diff] [blame] | 145 | |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 146 | struct leafDataToStringVisitor : boost::static_visitor<std::string> { |
| 147 | std::string operator()(const enum_& data) const |
| 148 | { |
| 149 | return data.m_value; |
| 150 | } |
| 151 | |
| 152 | std::string operator()(const binary_& data) const |
| 153 | { |
| 154 | return data.m_value; |
| 155 | } |
| 156 | |
| 157 | std::string operator()(const identityRef_& data) const |
| 158 | { |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 159 | return data.m_prefix.value().m_name + ":" + data.m_value; |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 162 | std::string operator()(const special_& data) const |
| 163 | { |
| 164 | return specialValueToString(data); |
| 165 | } |
| 166 | |
Jan Kundrát | c43acf7 | 2019-07-02 19:28:22 +0200 | [diff] [blame] | 167 | std::string operator()(const std::string& data) const |
| 168 | { |
| 169 | return data; |
| 170 | } |
| 171 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 172 | std::string operator()(const bool& data) const |
| 173 | { |
| 174 | if (data) |
| 175 | return "true"; |
| 176 | else |
| 177 | return "false"; |
| 178 | } |
| 179 | |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 180 | template <typename T> |
| 181 | std::string operator()(const T& data) const |
| 182 | { |
Jan Kundrát | c43acf7 | 2019-07-02 19:28:22 +0200 | [diff] [blame] | 183 | return std::to_string(data); |
Václav Kubernát | 9b72599 | 2019-05-29 16:39:47 +0200 | [diff] [blame] | 184 | } |
| 185 | }; |
| 186 | |
| 187 | std::string leafDataToString(const leaf_data_ value) |
| 188 | { |
| 189 | return boost::apply_visitor(leafDataToStringVisitor(), value); |
| 190 | } |