Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +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 | */ |
| 8 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 9 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 10 | #include <boost/mpl/for_each.hpp> |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 11 | #include <iostream> |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 12 | #include <sstream> |
Václav Kubernát | e066fe2 | 2022-04-06 00:32:26 +0200 | [diff] [blame^] | 13 | #include "UniqueResource.hpp" |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 14 | #include "datastore_access.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 15 | #include "interpreter.hpp" |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 16 | #include "utils.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 17 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 18 | struct pathToStringVisitor : boost::static_visitor<std::string> { |
| 19 | std::string operator()(const module_& path) const |
| 20 | { |
| 21 | using namespace std::string_literals; |
| 22 | return "/"s + boost::get<module_>(path).m_name + ":*"; |
| 23 | } |
| 24 | std::string operator()(const schemaPath_& path) const |
| 25 | { |
| 26 | return pathToSchemaString(path, Prefixes::WhenNeeded); |
| 27 | } |
| 28 | std::string operator()(const dataPath_& path) const |
| 29 | { |
| 30 | return pathToDataString(path, Prefixes::WhenNeeded); |
| 31 | } |
| 32 | }; |
| 33 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 34 | namespace { |
| 35 | void printTree(const DatastoreAccess::Tree tree) |
| 36 | { |
| 37 | for (auto it = tree.begin(); it != tree.end(); it++) { |
| 38 | auto [path, value] = *it; |
| 39 | if (value.type() == typeid(special_) && boost::get<special_>(value).m_value == SpecialValue::LeafList) { |
| 40 | auto leafListPrefix = path; |
| 41 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 42 | |
| 43 | while (it + 1 != tree.end() && boost::starts_with((it + 1)->first, leafListPrefix)) { |
| 44 | ++it; |
| 45 | std::cout << stripLeafListValueFromPath(it->first) << " = " << leafDataToString(it->second) << std::endl; |
| 46 | } |
| 47 | } else { |
| 48 | std::cout << path << " = " << leafDataToString(value) << std::endl; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 54 | template <typename PathType> |
| 55 | std::string pathToString(const PathType& path) |
| 56 | { |
| 57 | return boost::apply_visitor(pathToStringVisitor(), path); |
| 58 | } |
| 59 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 60 | void Interpreter::operator()(const commit_&) const |
| 61 | { |
| 62 | m_datastore.commitChanges(); |
| 63 | } |
| 64 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 65 | void Interpreter::operator()(const discard_&) const |
| 66 | { |
| 67 | m_datastore.discardChanges(); |
| 68 | } |
| 69 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 70 | void Interpreter::operator()(const set_& set) const |
| 71 | { |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 72 | auto data = set.m_data; |
| 73 | |
| 74 | // If the user didn't supply a module prefix for identityref, we need to add it ourselves |
| 75 | if (data.type() == typeid(identityRef_)) { |
| 76 | auto identityRef = boost::get<identityRef_>(data); |
| 77 | if (!identityRef.m_prefix) { |
| 78 | identityRef.m_prefix = set.m_path.m_nodes.front().m_prefix.value(); |
| 79 | data = identityRef; |
| 80 | } |
| 81 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 82 | m_datastore.setLeaf(pathToString(toCanonicalPath(set.m_path)), data); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 85 | void Interpreter::operator()(const get_& get) const |
| 86 | { |
Václav Kubernát | e066fe2 | 2022-04-06 00:32:26 +0200 | [diff] [blame^] | 87 | auto targetSwitcher = make_unique_resource([] {}, [this, oldTarget = m_datastore.target()] { |
| 88 | m_datastore.setTarget(oldTarget); |
| 89 | }); |
| 90 | |
| 91 | if (get.m_dsTarget) { |
| 92 | m_datastore.setTarget(*get.m_dsTarget); |
| 93 | } |
| 94 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 95 | auto items = m_datastore.getItems(pathToString(toCanonicalPath(get.m_path))); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 96 | printTree(items); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 99 | void Interpreter::operator()(const cd_& cd) const |
| 100 | { |
Václav Kubernát | 218ee7b | 2022-03-30 22:35:26 +0200 | [diff] [blame] | 101 | if (auto rpcInputPath = m_datastore.inputDatastorePath(); rpcInputPath && !pathToDataString(cd.m_path, Prefixes::WhenNeeded).starts_with(m_parser.currentNode())) { |
| 102 | throw std::runtime_error("Can't cd out of `prepare` context"); |
| 103 | } |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 104 | m_parser.changeNode(cd.m_path); |
| 105 | } |
| 106 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 107 | void Interpreter::operator()(const create_& create) const |
| 108 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 109 | m_datastore.createItem(pathToString(toCanonicalPath(create.m_path))); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void Interpreter::operator()(const delete_& delet) const |
| 113 | { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 114 | m_datastore.deleteItem(pathToString(toCanonicalPath(delet.m_path))); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 117 | void Interpreter::operator()(const ls_& ls) const |
| 118 | { |
| 119 | std::cout << "Possible nodes:" << std::endl; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 120 | auto recursion{Recursion::NonRecursive}; |
| 121 | for (auto it : ls.m_options) { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 122 | if (it == LsOption::Recursive) { |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 123 | recursion = Recursion::Recursive; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 124 | } |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 125 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 126 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 127 | auto toPrint = m_datastore.schema()->availableNodes(toCanonicalPath(ls.m_path), recursion); |
Václav Kubernát | 8208687 | 2020-04-29 01:09:50 +0200 | [diff] [blame] | 128 | |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 129 | for (const auto& it : toPrint) { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 130 | std::cout << (it.first ? *it.first + ":" : "") + it.second << std::endl; |
Václav Kubernát | 95b0887 | 2020-04-28 01:04:17 +0200 | [diff] [blame] | 131 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 134 | void Interpreter::operator()(const copy_& copy) const |
| 135 | { |
| 136 | m_datastore.copyConfig(copy.m_source, copy.m_destination); |
| 137 | } |
| 138 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 139 | std::string Interpreter::buildTypeInfo(const std::string& path) const |
| 140 | { |
| 141 | std::ostringstream ss; |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 142 | std::string typeDescription; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 143 | switch (m_datastore.schema()->nodeType(path)) { |
| 144 | case yang::NodeTypes::Container: |
| 145 | ss << "container"; |
| 146 | break; |
| 147 | case yang::NodeTypes::PresenceContainer: |
| 148 | ss << "presence container"; |
| 149 | break; |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 150 | case yang::NodeTypes::Leaf: { |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 151 | auto leafType = m_datastore.schema()->leafType(path); |
| 152 | auto typedefName = m_datastore.schema()->leafTypeName(path); |
| 153 | std::string baseTypeStr; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 154 | if (std::holds_alternative<yang::LeafRef>(leafType.m_type)) { |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 155 | ss << "-> "; |
| 156 | ss << m_datastore.schema()->leafrefPath(path) << " "; |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 157 | baseTypeStr = leafDataTypeToString(std::get<yang::LeafRef>(leafType.m_type).m_targetType->m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 158 | } else { |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 159 | baseTypeStr = leafDataTypeToString(leafType.m_type); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (typedefName) { |
| 163 | ss << *typedefName << " (" << baseTypeStr << ")"; |
| 164 | } else { |
| 165 | ss << baseTypeStr; |
| 166 | } |
| 167 | |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 168 | if (leafType.m_units) { |
| 169 | ss << " [" + *leafType.m_units + "]"; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 172 | if (leafType.m_description) { |
| 173 | typeDescription = "\nType description: " + *leafType.m_description; |
| 174 | } |
| 175 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 176 | if (m_datastore.schema()->leafIsKey(path)) { |
| 177 | ss << " (key)"; |
| 178 | } |
Václav Kubernát | b1a75c6 | 2020-04-21 15:20:16 +0200 | [diff] [blame] | 179 | |
| 180 | if (auto defaultValue = m_datastore.schema()->defaultValue(path)) { |
| 181 | ss << " default: " << leafDataToString(*defaultValue); |
| 182 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | case yang::NodeTypes::List: |
| 186 | ss << "list"; |
| 187 | break; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 188 | case yang::NodeTypes::Rpc: |
| 189 | ss << "RPC"; |
| 190 | break; |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 191 | case yang::NodeTypes::LeafList: |
Václav Kubernát | 160e5a2 | 2020-12-01 01:11:28 +0100 | [diff] [blame] | 192 | ss << "leaf-list"; |
| 193 | break; |
| 194 | case yang::NodeTypes::Action: |
| 195 | ss << "action"; |
| 196 | break; |
| 197 | case yang::NodeTypes::AnyXml: |
| 198 | throw std::logic_error("Sorry, anyxml isn't supported yet."); |
Václav Kubernát | aaafeae | 2020-05-05 15:41:45 +0200 | [diff] [blame] | 199 | case yang::NodeTypes::Notification: |
Václav Kubernát | 160e5a2 | 2020-12-01 01:11:28 +0100 | [diff] [blame] | 200 | throw std::logic_error("Sorry, notifications aren't supported yet."); |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 201 | } |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 202 | |
| 203 | if (!m_datastore.schema()->isConfig(path)) { |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 204 | ss << " (ro)\n"; |
Václav Kubernát | 0599e9f | 2020-04-21 09:51:33 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Václav Kubernát | a1c4c9e | 2020-04-22 00:37:52 +0200 | [diff] [blame] | 207 | auto status = m_datastore.schema()->status(path); |
| 208 | auto statusStr = status == yang::Status::Deprecated ? " (deprecated)" : |
| 209 | status == yang::Status::Obsolete ? " (obsolete)" : |
| 210 | ""; |
| 211 | |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 212 | ss << statusStr; |
| 213 | |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 214 | if (auto description = m_datastore.schema()->description(path)) { |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 215 | ss << std::endl << *description << std::endl; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 216 | } |
Václav Kubernát | 1ae24f4 | 2020-12-01 02:32:04 +0100 | [diff] [blame] | 217 | |
| 218 | ss << typeDescription; |
| 219 | return ss.str(); |
| 220 | } |
| 221 | |
| 222 | void Interpreter::operator()(const describe_& describe) const |
| 223 | { |
| 224 | auto fullPath = pathToString(toCanonicalPath(describe.m_path)); |
| 225 | |
| 226 | std::cout << pathToString(describe.m_path) << ": " << buildTypeInfo(fullPath) << std::endl; |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 229 | void Interpreter::operator()(const move_& move) const |
| 230 | { |
| 231 | m_datastore.moveItem(pathToDataString(move.m_source, Prefixes::WhenNeeded), move.m_destination); |
| 232 | } |
| 233 | |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 234 | void Interpreter::operator()(const dump_& dump) const |
| 235 | { |
| 236 | std::cout << m_datastore.dump(dump.m_format) << "\n"; |
| 237 | } |
| 238 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 239 | void Interpreter::operator()(const prepare_& prepare) const |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 240 | { |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 241 | m_datastore.initiate(pathToString(toCanonicalPath(prepare.m_path))); |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 242 | m_parser.changeNode(prepare.m_path); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 245 | void Interpreter::operator()(const exec_& exec) const |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 246 | { |
| 247 | m_parser.changeNode({Scope::Absolute, {}}); |
Václav Kubernát | d8408e0 | 2020-12-02 05:13:27 +0100 | [diff] [blame] | 248 | if (exec.m_path) { |
| 249 | m_datastore.initiate(pathToString(toCanonicalPath(*exec.m_path))); |
| 250 | } |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 251 | auto output = m_datastore.execute(); |
| 252 | std::cout << "RPC/action output:\n"; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 253 | printTree(output); |
| 254 | } |
| 255 | |
| 256 | void Interpreter::operator()(const cancel_&) const |
| 257 | { |
| 258 | m_parser.changeNode({Scope::Absolute, {}}); |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 259 | m_datastore.cancel(); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Václav Kubernát | 162165e | 2021-02-22 09:46:53 +0100 | [diff] [blame] | 262 | void Interpreter::operator()(const switch_& switch_cmd) const |
| 263 | { |
| 264 | m_datastore.setTarget(switch_cmd.m_target); |
| 265 | } |
| 266 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 267 | struct commandLongHelpVisitor : boost::static_visitor<const char*> { |
| 268 | template <typename T> |
| 269 | auto constexpr operator()(boost::type<T>) const |
| 270 | { |
| 271 | return T::longHelp; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | struct commandShortHelpVisitor : boost::static_visitor<const char*> { |
| 276 | template <typename T> |
| 277 | auto constexpr operator()(boost::type<T>) const |
| 278 | { |
| 279 | return T::shortHelp; |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | void Interpreter::operator()(const help_& help) const |
| 284 | { |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 285 | if (help.m_cmd) { |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 286 | std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl; |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 287 | } else { |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 288 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) { |
| 289 | std::cout << commandShortHelpVisitor()(cmd) << std::endl; |
| 290 | }); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 291 | } |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 294 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 295 | boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const boost::optional<PathType>& optPath) const |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 296 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 297 | if (!optPath) { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 298 | return m_parser.currentPath(); |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 299 | } |
| 300 | return toCanonicalPath(*optPath); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 303 | struct impl_toCanonicalPath { |
| 304 | const dataPath_& m_parserPath; |
| 305 | |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 306 | using ReturnType = boost::variant<dataPath_, schemaPath_, module_>; |
| 307 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 308 | impl_toCanonicalPath(const dataPath_& parserPath) |
| 309 | : m_parserPath(parserPath) |
| 310 | { |
| 311 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 312 | ReturnType operator()(const module_& path) const |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 313 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 314 | return path; |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 315 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 316 | ReturnType operator()(const schemaPath_& path) const |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 317 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 318 | return impl(path); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 319 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 320 | ReturnType operator()(const dataPath_& path) const |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 321 | { |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 322 | return impl(path); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 323 | } |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 324 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 325 | private: |
| 326 | template <typename PathType> |
Václav Kubernát | 59e4ee4 | 2020-07-08 17:32:45 +0200 | [diff] [blame] | 327 | [[nodiscard]] ReturnType impl(const PathType& suffix) const |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 328 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 329 | PathType res = [this] { |
| 330 | if constexpr (std::is_same<PathType, schemaPath_>()) { |
| 331 | return dataPathToSchemaPath(m_parserPath); |
| 332 | } else { |
| 333 | return m_parserPath; |
| 334 | } |
| 335 | }(); |
Václav Kubernát | d624799 | 2020-05-27 00:17:56 +0200 | [diff] [blame] | 336 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 337 | if (suffix.m_scope == Scope::Absolute) { |
Václav Kubernát | 59be0de | 2020-06-15 13:58:45 +0200 | [diff] [blame] | 338 | res = {Scope::Absolute, {}}; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 339 | } |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 340 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 341 | for (const auto& fragment : suffix.m_nodes) { |
Václav Kubernát | e781b90 | 2020-06-15 14:35:11 +0200 | [diff] [blame] | 342 | res.pushFragment(fragment); |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 343 | } |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 344 | |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 345 | return res; |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | template <typename PathType> |
Václav Kubernát | 4c3d22f | 2020-06-12 16:05:01 +0200 | [diff] [blame] | 350 | boost::variant<dataPath_, schemaPath_, module_> Interpreter::toCanonicalPath(const PathType& path) const |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 351 | { |
Václav Kubernát | 7e16769 | 2020-06-12 09:53:01 +0200 | [diff] [blame] | 352 | if constexpr (std::is_same<PathType, dataPath_>()) { |
| 353 | return impl_toCanonicalPath(m_parser.currentPath())(path); |
| 354 | } else { |
| 355 | return boost::apply_visitor(impl_toCanonicalPath(m_parser.currentPath()), path); |
| 356 | } |
Václav Kubernát | 9cfcd87 | 2020-02-18 12:34:02 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 359 | Interpreter::Interpreter(Parser& parser, ProxyDatastore& datastore) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 360 | : m_parser(parser) |
| 361 | , m_datastore(datastore) |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 362 | { |
| 363 | } |