aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/ytree/helpers-inl.h
blob: ea204ab82beba999c100f1794e7db34cb6a742b3 (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
#ifndef HELPERS_INL_H_
#error "Direct inclusion of this file is not allowed, include helpers.h"
// For the sake of sane code completion.
#include "helpers.h"
#endif

#include "attribute_consumer.h"
#include "serialize.h"
#include "convert.h"

namespace NYT::NYTree {

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

template <class T>
T IAttributeDictionary::Get(TStringBuf key) const
{
    auto yson = GetYson(key);
    try {
        return ConvertTo<T>(yson);
    } catch (const std::exception& ex) {
        THROW_ERROR_EXCEPTION("Error parsing attribute %Qv",
            key)
            << ex;
    }
}

template <class T>
T IAttributeDictionary::GetAndRemove(const TString& key)
{
    auto result = Get<T>(key);
    Remove(key);
    return result;
}

template <class T>
T IAttributeDictionary::Get(TStringBuf key, const T& defaultValue) const
{
    return Find<T>(key).value_or(defaultValue);
}

template <class T>
T IAttributeDictionary::GetAndRemove(const TString& key, const T& defaultValue)
{
    auto result = Find<T>(key);
    if (result) {
        Remove(key);
        return *result;
    } else {
        return defaultValue;
    }
}

template <class T>
typename TOptionalTraits<T>::TOptional IAttributeDictionary::Find(TStringBuf key) const
{
    auto yson = FindYson(key);
    if (!yson) {
        return typename TOptionalTraits<T>::TOptional();
    }
    try {
        return ConvertTo<T>(yson);
    } catch (const std::exception& ex) {
        THROW_ERROR_EXCEPTION("Error parsing attribute %Qv",
            key)
            << ex;
    }
}

template <class T>
typename TOptionalTraits<T>::TOptional IAttributeDictionary::FindAndRemove(const TString& key)
{
    auto result = Find<T>(key);
    if (result) {
        Remove(key);
    }
    return result;
}

template <class T>
void IAttributeDictionary::Set(const TString& key, const T& value)
{
    auto yson = ConvertToYsonString(value, NYson::EYsonFormat::Binary);
    SetYson(key, yson);
}

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

template <class T, class R>
IYPathServicePtr IYPathService::FromMethod(
    R (std::remove_cv_t<T>::*method) () const,
    const TWeakPtr<T>& weak)
{
    auto boundProducer = NYson::TYsonProducer(BIND_NO_PROPAGATE([=] (NYson::IYsonConsumer* consumer) {
        auto strong = weak.Lock();
        if (strong) {
            Serialize((strong.Get()->*method)(), consumer);
        } else {
            consumer->OnBeginAttributes();
            consumer->OnKeyedItem("object_destroyed");
            consumer->OnBooleanScalar(true);
            consumer->OnEndAttributes();
            consumer->OnEntity();
        }
    }));

    return FromProducer(std::move(boundProducer));
}

template <class T>
IYPathServicePtr IYPathService::FromMethod(
    void (std::remove_cv_t<T>::*producer) (NYson::IYsonConsumer*) const,
    const TWeakPtr<T>& weak)
{
    auto boundProducer = NYson::TYsonProducer(BIND_NO_PROPAGATE([=] (NYson::IYsonConsumer* consumer) {
        auto strong = weak.Lock();
        if (strong) {
            (strong.Get()->*producer)(consumer);
        } else {
            consumer->OnBeginAttributes();
            consumer->OnKeyedItem("object_destroyed");
            consumer->OnBooleanScalar(true);
            consumer->OnEndAttributes();
            consumer->OnEntity();
        }
    }));

    return FromProducer(std::move(boundProducer));
}

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

template <class TReq, class TRsp>
TIntrusivePtr<TTypedYPathServiceContext<TReq, TRsp>> DeserializeAsTypedOrThrow(
    const IYPathServiceContextPtr& context,
    const NRpc::THandlerInvocationOptions& options)
{
    auto typedContext = New<TTypedYPathServiceContext<TReq, TRsp>>(context, options);
    if (!typedContext->DeserializeRequest()) {
        THROW_ERROR_EXCEPTION("Error deserializing request");
    }

    return typedContext;
}

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

} // namespace NYT::NYTree