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 | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 9 | #include <iostream> |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 10 | #include "datastore_access.hpp" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 11 | #include "interpreter.hpp" |
| 12 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 13 | struct leafDataToString : boost::static_visitor<std::string> { |
| 14 | std::string operator()(const enum_& data) const |
| 15 | { |
| 16 | return data.m_value; |
| 17 | } |
| 18 | template <typename T> |
| 19 | std::string operator()(const T& data) const |
| 20 | { |
| 21 | std::stringstream stream; |
| 22 | stream << data; |
| 23 | return stream.str(); |
| 24 | } |
| 25 | }; |
| 26 | |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 27 | void Interpreter::operator()(const commit_&) const |
| 28 | { |
| 29 | m_datastore.commitChanges(); |
| 30 | } |
| 31 | |
Václav Kubernát | 6d79143 | 2018-10-25 16:00:35 +0200 | [diff] [blame] | 32 | void Interpreter::operator()(const discard_&) const |
| 33 | { |
| 34 | m_datastore.discardChanges(); |
| 35 | } |
| 36 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 37 | void Interpreter::operator()(const set_& set) const |
| 38 | { |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 39 | m_datastore.setLeaf(absolutePathFromCommand(set), set.m_data); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 42 | void Interpreter::operator()(const get_& get) const |
| 43 | { |
| 44 | auto items = m_datastore.getItems(absolutePathFromCommand(get)); |
| 45 | for (auto it : items) { |
| 46 | std::cout << it.first << " = " << boost::apply_visitor(leafDataToString(), it.second) << std::endl; |
| 47 | } |
| 48 | } |
| 49 | |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 50 | void Interpreter::operator()(const cd_& cd) const |
| 51 | { |
| 52 | m_parser.changeNode(cd.m_path); |
| 53 | } |
| 54 | |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 55 | void Interpreter::operator()(const create_& create) const |
| 56 | { |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 57 | m_datastore.createPresenceContainer(absolutePathFromCommand(create)); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void Interpreter::operator()(const delete_& delet) const |
| 61 | { |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 62 | m_datastore.deletePresenceContainer(absolutePathFromCommand(delet)); |
Václav Kubernát | b61336d | 2018-05-28 17:35:03 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 65 | void Interpreter::operator()(const ls_& ls) const |
| 66 | { |
| 67 | std::cout << "Possible nodes:" << std::endl; |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 68 | auto recursion{Recursion::NonRecursive}; |
| 69 | for (auto it : ls.m_options) { |
| 70 | if (it == LsOption::Recursive) |
| 71 | recursion = Recursion::Recursive; |
| 72 | } |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 73 | |
Václav Kubernát | e7d4aea | 2018-09-11 18:15:48 +0200 | [diff] [blame] | 74 | for (const auto& it : m_parser.availableNodes(ls.m_path, recursion)) |
Václav Kubernát | 11afac7 | 2018-07-18 14:59:53 +0200 | [diff] [blame] | 75 | std::cout << it << std::endl; |
| 76 | } |
| 77 | |
Václav Kubernát | 054cc99 | 2019-02-21 14:23:52 +0100 | [diff] [blame] | 78 | struct commandLongHelpVisitor : boost::static_visitor<const char*> { |
| 79 | template <typename T> |
| 80 | auto constexpr operator()(boost::type<T>) const |
| 81 | { |
| 82 | return T::longHelp; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | struct commandShortHelpVisitor : boost::static_visitor<const char*> { |
| 87 | template <typename T> |
| 88 | auto constexpr operator()(boost::type<T>) const |
| 89 | { |
| 90 | return T::shortHelp; |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | void Interpreter::operator()(const help_& help) const |
| 95 | { |
| 96 | if (help.m_cmd) |
| 97 | std::cout << boost::apply_visitor(commandLongHelpVisitor(), help.m_cmd.get()) << std::endl; |
| 98 | else |
| 99 | boost::mpl::for_each<CommandTypes, boost::type<boost::mpl::_>>([](auto cmd) { |
| 100 | std::cout << commandShortHelpVisitor()(cmd) << std::endl; |
| 101 | }); |
| 102 | } |
| 103 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 104 | template <typename T> |
| 105 | std::string Interpreter::absolutePathFromCommand(const T& command) const |
| 106 | { |
Václav Kubernát | 37171a1 | 2018-08-31 17:01:48 +0200 | [diff] [blame] | 107 | if (command.m_path.m_scope == Scope::Absolute) |
| 108 | return "/" + pathToDataString(command.m_path); |
| 109 | else |
| 110 | return joinPaths(m_parser.currentNode(), pathToDataString(command.m_path)); |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 113 | struct pathToStringVisitor : boost::static_visitor<std::string> { |
| 114 | std::string operator()(const schemaPath_& path) const |
| 115 | { |
| 116 | return pathToSchemaString(path); |
| 117 | } |
| 118 | std::string operator()(const dataPath_& path) const |
| 119 | { |
| 120 | return pathToDataString(path); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | struct getPathScopeVisitor : boost::static_visitor<Scope> { |
| 125 | template <typename T> |
| 126 | Scope operator()(const T& path) const |
| 127 | { |
| 128 | return path.m_scope; |
| 129 | } |
| 130 | }; |
| 131 | |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 132 | std::string Interpreter::absolutePathFromCommand(const get_& get) const |
| 133 | { |
| 134 | if (!get.m_path) { |
| 135 | return m_parser.currentNode(); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | const auto path = *get.m_path; |
| 139 | std::string pathString = boost::apply_visitor(pathToStringVisitor(), path); |
| 140 | auto pathScope{boost::apply_visitor(getPathScopeVisitor(), path)}; |
| 141 | |
| 142 | if (pathScope == Scope::Absolute) { |
| 143 | return "/" + pathString; |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 144 | } else { |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 145 | return joinPaths(m_parser.currentNode(), pathString); |
Václav Kubernát | b6ff0b6 | 2018-08-30 16:14:53 +0200 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 149 | Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore) |
Václav Kubernát | 812ee28 | 2018-08-30 17:10:03 +0200 | [diff] [blame] | 150 | : m_parser(parser) |
| 151 | , m_datastore(datastore) |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 152 | { |
| 153 | } |