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