aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson/node/node_visitor.cpp
blob: 69a66cc4ed8a8a7177720cc03648474d040f0263 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "node_visitor.h"

#include <util/generic/algorithm.h>
#include <util/string/printf.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

namespace {

template <typename Fun>
void Iterate(const TNode::TMapType& nodeMap, bool sortByKey, Fun action)
{
    if (sortByKey) {
        TVector<TNode::TMapType::const_iterator> iterators;
        for (auto it = nodeMap.begin(); it != nodeMap.end(); ++it) {
            iterators.push_back(it);
        }
        SortBy(iterators, [](TNode::TMapType::const_iterator it) { return it->first; });
        for (const auto& it : iterators) {
            action(*it);
        }
    } else {
        ForEach(nodeMap.begin(), nodeMap.end(), action);
    }
}

} // namespace

////////////////////////////////////////////////////////////////////////////////

TNodeVisitor::TNodeVisitor(NYson::IYsonConsumer* consumer, bool sortMapKeys)
    : Consumer_(consumer)
    , SortMapKeys_(sortMapKeys)
{ }

void TNodeVisitor::Visit(const TNode& node)
{
    VisitAny(node);
}

void TNodeVisitor::VisitAny(const TNode& node)
{
    if (node.HasAttributes()) {
        Consumer_->OnBeginAttributes();
        Iterate(node.GetAttributes().AsMap(), SortMapKeys_, [&](const std::pair<TString, TNode>& item) {
            Consumer_->OnKeyedItem(item.first);
            if (item.second.IsUndefined()) {
                ythrow TNode::TTypeError() << "unable to visit attribute value of type "
                    << TNode::EType::Undefined << "; attribute name: `" << item.first << '\'' ;
            }
            VisitAny(item.second);
        });
        Consumer_->OnEndAttributes();
    }

    switch (node.GetType()) {
        case TNode::String:
            VisitString(node);
            break;
        case TNode::Int64:
            VisitInt64(node);
            break;
        case TNode::Uint64:
            VisitUint64(node);
            break;
        case TNode::Double:
            VisitDouble(node);
            break;
        case TNode::Bool:
            VisitBool(node);
            break;
        case TNode::List:
            VisitList(node.AsList());
            break;
        case TNode::Map:
            VisitMap(node.AsMap());
            break;
        case TNode::Null:
            VisitEntity();
            break;
        case TNode::Undefined:
            ythrow TNode::TTypeError() << "unable to visit TNode of type " << node.GetType();
        default:
            Y_FAIL("Unexpected type: %d", node.GetType());
    }
}

void TNodeVisitor::VisitString(const TNode& node)
{
    Consumer_->OnStringScalar(node.AsString());
}

void TNodeVisitor::VisitInt64(const TNode& node)
{
    Consumer_->OnInt64Scalar(node.AsInt64());
}

void TNodeVisitor::VisitUint64(const TNode& node)
{
    Consumer_->OnUint64Scalar(node.AsUint64());
}

void TNodeVisitor::VisitDouble(const TNode& node)
{
    Consumer_->OnDoubleScalar(node.AsDouble());
}

void TNodeVisitor::VisitBool(const TNode& node)
{
    Consumer_->OnBooleanScalar(node.AsBool());
}

void TNodeVisitor::VisitList(const TNode::TListType& nodeList) 
{
    Consumer_->OnBeginList();
    size_t index = 0;
    for (const auto& item : nodeList) {
        Consumer_->OnListItem();
        if (item.IsUndefined()) {
            ythrow TNode::TTypeError() << "unable to visit list node child of type "
                << TNode::EType::Undefined << "; list index: " << index;
        }
        VisitAny(item);
        ++index;
    }
    Consumer_->OnEndList();
}

void TNodeVisitor::VisitMap(const TNode::TMapType& nodeMap) 
{
    Consumer_->OnBeginMap();
    Iterate(nodeMap, SortMapKeys_, [&](const std::pair<TString, TNode>& item) {
        Consumer_->OnKeyedItem(item.first);
        if (item.second.IsUndefined()) {
            ythrow TNode::TTypeError() << "unable to visit map node child of type "
                << TNode::EType::Undefined << "; map key: `" << item.first << '\'' ;
        }
        VisitAny(item.second);
    });
    Consumer_->OnEndMap();
}

void TNodeVisitor::VisitEntity()
{
    Consumer_->OnEntity();
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT