aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/ytree/node.cpp
blob: dbfcb31ac9a7423c8948c4ac821310585c0fa0be (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
#include "node.h"
#include "convert.h"
#include "node_detail.h"
#include "tree_visitor.h"

#include <yt/yt/core/yson/writer.h>

#include <library/cpp/yt/misc/cast.h>

namespace NYT::NYTree {

using namespace NYson;

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

INodePtr IMapNode::GetChildOrThrow(const TString& key) const
{
    auto child = FindChild(key);
    if (!child) {
        ThrowNoSuchChildKey(this, key);
    }
    return child;
}

TString IMapNode::GetChildKeyOrThrow(const IConstNodePtr& child)
{
    auto optionalKey = FindChildKey(child);
    if (!optionalKey) {
        THROW_ERROR_EXCEPTION("Node is not a child");
    }
    return *optionalKey;
}

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

INodePtr IListNode::GetChildOrThrow(int index) const
{
    auto child = FindChild(index);
    if (!child) {
        ThrowNoSuchChildIndex(this, index);
    }
    return child;
}

int IListNode::GetChildIndexOrThrow(const IConstNodePtr& child)
{
    auto optionalIndex = FindChildIndex(child);
    if (!optionalIndex) {
        THROW_ERROR_EXCEPTION("Node is not a child");
    }
    return *optionalIndex;
}

int IListNode::AdjustChildIndexOrThrow(int index) const
{
    auto adjustedIndex = NYPath::TryAdjustListIndex(index, GetChildCount());
    if (!adjustedIndex) {
        ThrowNoSuchChildIndex(this, index);
    }
    return *adjustedIndex;
}

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

void Serialize(const INode& value, IYsonConsumer* consumer)
{
    VisitTree(const_cast<INode*>(&value), consumer, /*stable*/ true, TAttributeFilter());
}

void Deserialize(INodePtr& value, const INodePtr& node)
{
    value = node;
}

#define DESERIALIZE_TYPED(type) \
    void Deserialize(I##type##NodePtr& value, const INodePtr& node) \
    { \
        value = node->As##type(); \
    } \
    \
    void Deserialize(I##type##NodePtr& value, NYson::TYsonPullParserCursor* cursor) \
    { \
        auto node = ExtractTo<INodePtr>(cursor); \
        value = node->As##type(); \
    }

DESERIALIZE_TYPED(String)
DESERIALIZE_TYPED(Int64)
DESERIALIZE_TYPED(Uint64)
DESERIALIZE_TYPED(Double)
DESERIALIZE_TYPED(Boolean)
DESERIALIZE_TYPED(Map)
DESERIALIZE_TYPED(List)
DESERIALIZE_TYPED(Entity)

#undef DESERIALIZE_TYPED

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

void Deserialize(INodePtr& value, NYson::TYsonPullParserCursor* cursor)
{
    auto builder = CreateBuilderFromFactory(GetEphemeralNodeFactory());
    builder->BeginTree();
    cursor->TransferComplexValue(builder.get());
    value = builder->EndTree();
}

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

} // namespace NYT::NYTree