blob: 2734377bf7f2481d5d165dbd080e9b02330e0e50 [file] [log] [blame]
Václav Kubernát24df80e2018-06-06 15:18:03 +02001/*
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
9#include <experimental/iterator>
10#include "ast_path.hpp"
11#include "utils.hpp"
12
13container_::container_(const std::string& name)
14 : m_name(name)
15{
16}
17
18bool container_::operator==(const container_& b) const
19{
20 return this->m_name == b.m_name;
21}
22
23leaf_::leaf_(const std::string& name)
24 : m_name(name)
25{
26}
27
28bool leaf_::operator==(const leaf_& b) const
29{
30 return this->m_name == b.m_name;
31}
32
33listElement_::listElement_(const std::string& listName, const std::map<std::string, std::string>& keys)
34 : m_name(listName)
35 , m_keys(keys)
36{
37}
38
39bool listElement_::operator==(const listElement_& b) const
40{
41 return (this->m_name == b.m_name && this->m_keys == b.m_keys);
42}
43
44bool path_::operator==(const path_& b) const
45{
46 if (this->m_nodes.size() != b.m_nodes.size())
47 return false;
48 return this->m_nodes == b.m_nodes;
49}
50
51
52struct nodeToSchemaString : public boost::static_visitor<std::string> {
53 std::string operator()(const nodeup_&) const
54 {
55 return "..";
56 }
57 template <class T>
58 std::string operator()(const T& node) const
59 {
60 return node.m_name;
61 }
62};
63struct nodeToDataString : public boost::static_visitor<std::string> {
64 std::string operator()(const listElement_& node) const
65 {
66 std::ostringstream res;
67 res << node.m_name + "[";
68 std::transform(node.m_keys.begin(), node.m_keys.end(),
69 std::experimental::make_ostream_joiner(res, ' '),
70 [] (const auto& it) { return it.first + "=" + it.second; });
71 res << "]";
Václav Kubernátebca2552018-06-08 19:06:02 +020072 return res.str();
Václav Kubernát24df80e2018-06-06 15:18:03 +020073 }
74 std::string operator()(const nodeup_&) const
75 {
76 return "..";
77 }
78 template <class T>
79 std::string operator()(const T& node) const
80 {
81 return node.m_name;
82 }
83};
84
85std::string pathToDataString(const path_& path)
86{
87 std::string res;
88 for (const auto it : path.m_nodes)
89 res = joinPaths(res, boost::apply_visitor(nodeToDataString(), it));
90 return res;
91}
92
93std::string pathToSchemaString(const path_& path)
94{
95 std::string res;
96 for (const auto it : path.m_nodes)
97 res = joinPaths(res, boost::apply_visitor(nodeToSchemaString(), it));
98 return res;
99}