aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/ytree/yson_serializable.h
blob: 74b7d97d7d19a2111ac4e710668863a2f10b9359 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#pragma once

#include "public.h"
#include "node.h"
#include "yson_serialize_common.h"

#include <yt/yt/core/misc/error.h>
#include <yt/yt/core/misc/mpl.h>
#include <yt/yt/core/misc/property.h>

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

#include <functional>
#include <optional>

namespace NYT::NYTree {

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

class TYsonSerializableLite
    : private TNonCopyable
{
public:
    typedef std::function<void()> TPostprocessor;
    typedef std::function<void()> TPreprocessor;

    struct IParameter
        : public TRefCounted
    {
        virtual void Load(
            NYTree::INodePtr node,
            const NYPath::TYPath& path,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) = 0;

        virtual void SafeLoad(
            NYTree::INodePtr node,
            const NYPath::TYPath& path,
            const std::function<void()>& validate,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) = 0;

        virtual void Load(
            NYson::TYsonPullParserCursor* cursor,
            const NYPath::TYPath& path,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) = 0;

        virtual void SafeLoad(
            NYson::TYsonPullParserCursor* cursor,
            const NYPath::TYPath& path,
            const std::function<void()>& validate,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) = 0;

        virtual void Postprocess(const NYPath::TYPath& path) const = 0;
        virtual void SetDefaults() = 0;
        virtual void Save(NYson::IYsonConsumer* consumer) const = 0;
        virtual bool CanOmitValue() const = 0;
        virtual const TString& GetKey() const = 0;
        virtual const std::vector<TString>& GetAliases() const = 0;
        virtual IMapNodePtr GetUnrecognizedRecursively() const = 0;
        virtual void SetKeepUnrecognizedRecursively() = 0;
    };

    typedef TIntrusivePtr<IParameter> IParameterPtr;

    template <class T>
    class TParameter
        : public IParameter
    {
    public:
        typedef std::function<void(const T&)> TPostprocessor;
        typedef typename TOptionalTraits<T>::TValue TValueType;

        TParameter(TString key, T& parameter);

        void Load(
            NYTree::INodePtr node,
            const NYPath::TYPath& path,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) override;

        void SafeLoad(
            NYTree::INodePtr node,
            const NYPath::TYPath& path,
            const std::function<void()>& validate,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) override;

        void Load(
            NYson::TYsonPullParserCursor* cursor,
            const NYPath::TYPath& path,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) override;

        void SafeLoad(
            NYson::TYsonPullParserCursor* cursor,
            const NYPath::TYPath& path,
            const std::function<void()>& validate,
            std::optional<EMergeStrategy> mergeStrategy = std::nullopt) override;

        void Postprocess(const NYPath::TYPath& path) const override;
        void SetDefaults() override;
        void Save(NYson::IYsonConsumer* consumer) const override;
        bool CanOmitValue() const override;
        const TString& GetKey() const override;
        const std::vector<TString>& GetAliases() const override;
        IMapNodePtr GetUnrecognizedRecursively() const override;
        void SetKeepUnrecognizedRecursively() override;

    public:
        TParameter& Optional();
        TParameter& Default(const T& defaultValue = T());
        TParameter& DontSerializeDefault();
        TParameter& CheckThat(TPostprocessor validator);
        TParameter& GreaterThan(TValueType value);
        TParameter& GreaterThanOrEqual(TValueType value);
        TParameter& LessThan(TValueType value);
        TParameter& LessThanOrEqual(TValueType value);
        TParameter& InRange(TValueType lowerBound, TValueType upperBound);
        TParameter& NonEmpty();
        TParameter& Alias(const TString& name);
        TParameter& MergeBy(EMergeStrategy strategy);

        template <class... TArgs>
        TParameter& DefaultNew(TArgs&&... args);

    private:
        TString Key;
        T& Parameter;
        std::optional<T> DefaultValue;
        bool SerializeDefault = true;
        std::vector<TPostprocessor> Postprocessors;
        std::vector<TString> Aliases;
        EMergeStrategy MergeStrategy;
        bool KeepUnrecognizedRecursively = false;
    };

public:
    TYsonSerializableLite();

    void Load(
        NYTree::INodePtr node,
        bool postprocess = true,
        bool setDefaults = true,
        const NYPath::TYPath& path = "");

    void Load(
        NYson::TYsonPullParserCursor* cursor,
        bool postprocess = true,
        bool setDefaults = true,
        const NYPath::TYPath& path = "");

    void Postprocess(const NYPath::TYPath& path = "") const;

    void SetDefaults();

    void Save(NYson::IYsonConsumer* consumer) const;

    IMapNodePtr GetUnrecognized() const;
    IMapNodePtr GetUnrecognizedRecursively() const;
    IMapNodePtr GetRecursiveUnrecognized() const;

    void SetUnrecognizedStrategy(EUnrecognizedStrategy strategy);

    THashSet<TString> GetRegisteredKeys() const;
    int GetParameterCount() const;

    void SaveParameter(const TString& key, NYson::IYsonConsumer* consumer) const;
    void LoadParameter(const TString& key, const NYTree::INodePtr& node, EMergeStrategy mergeStrategy) const;
    void ResetParameter(const TString& key) const;

    std::vector<TString> GetAllParameterAliases(const TString& key) const;

protected:
    template <class T>
    TParameter<T>& RegisterParameter(
        TString parameterName,
        T& value);

    void RegisterPreprocessor(const TPreprocessor& func);
    void RegisterPostprocessor(const TPostprocessor& func);

private:
    template <class T>
    friend class TParameter;

    THashMap<TString, IParameterPtr> Parameters;

    NYTree::IMapNodePtr Unrecognized;
    EUnrecognizedStrategy UnrecognizedStrategy = EUnrecognizedStrategy::Drop;

    std::vector<TPreprocessor> Preprocessors;
    std::vector<TPostprocessor> Postprocessors;

    IParameterPtr GetParameter(const TString& keyOrAlias) const;
};

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

class TYsonSerializable
    : public TRefCounted
    , public TYsonSerializableLite
{ };

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

template <class T>
TIntrusivePtr<T> CloneYsonSerializable(const TIntrusivePtr<T>& obj);
template <class T>
std::vector<TIntrusivePtr<T>> CloneYsonSerializables(const std::vector<TIntrusivePtr<T>>& objs);
template <class T>
THashMap<TString, TIntrusivePtr<T>> CloneYsonSerializables(const THashMap<TString, TIntrusivePtr<T>>& objs);

void Serialize(const TYsonSerializableLite& value, NYson::IYsonConsumer* consumer);
void Deserialize(TYsonSerializableLite& value, NYTree::INodePtr node);
void Deserialize(TYsonSerializableLite& value, NYson::TYsonPullParserCursor* cursor);

template <class T>
TIntrusivePtr<T> UpdateYsonSerializable(
    const TIntrusivePtr<T>& obj,
    const NYTree::INodePtr& patch);

template <class T>
TIntrusivePtr<T> UpdateYsonSerializable(
    const TIntrusivePtr<T>& obj,
    const NYson::TYsonString& patch);

template <class T>
bool ReconfigureYsonSerializable(
    const TIntrusivePtr<T>& config,
    const NYson::TYsonString& newConfigYson);

template <class T>
bool ReconfigureYsonSerializable(
    const TIntrusivePtr<T>& config,
    const TIntrusivePtr<T>& newConfig);

template <class T>
bool ReconfigureYsonSerializable(
    const TIntrusivePtr<T>& config,
    const NYTree::INodePtr& newConfigNode);

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

} // namespace NYT::NYTree


namespace NYT {

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

struct TBinaryYsonSerializer
{
    static void Save(TStreamSaveContext& context, const NYTree::TYsonSerializableLite& obj);
    static void Load(TStreamLoadContext& context, NYTree::TYsonSerializableLite& obj);
};

template <class T, class C>
struct TSerializerTraits<
    T,
    C,
    typename std::enable_if_t<std::is_convertible_v<T&, NYTree::TYsonSerializableLite&>>>
{
    typedef TBinaryYsonSerializer TSerializer;
};

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

} // namespace NYT

#define YSON_SERIALIZABLE_INL_H_
#include "yson_serializable-inl.h"
#undef YSON_SERIALIZABLE_INL_H_