aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/yson/attribute_consumer.h
blob: 1239476c3ccfd66d47d6c3fce7851e97e344da27 (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
#pragma once

#include "public.h"
#include "async_consumer.h"

namespace NYT::NYson {

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

//! Consumes a map fragment representing the attributes
//! and if the fragment is non-empty then encloses it with angle brackets.
class TAttributeFragmentConsumer
    : public IAsyncYsonConsumer
{
public:
    explicit TAttributeFragmentConsumer(IAsyncYsonConsumer* underlyingConsumer);
    ~TAttributeFragmentConsumer();

    void OnStringScalar(TStringBuf value) override;
    void OnInt64Scalar(i64 value) override;
    void OnUint64Scalar(ui64 value) override;
    void OnDoubleScalar(double value) override;
    void OnBooleanScalar(bool value) override;
    void OnEntity() override;
    void OnBeginList() override;
    void OnListItem() override;
    void OnEndList() override;
    void OnBeginMap() override;
    void OnKeyedItem(TStringBuf key) override;
    void OnEndMap() override;
    void OnBeginAttributes() override;
    void OnEndAttributes() override;
    using IAsyncYsonConsumer::OnRaw;
    void OnRaw(TStringBuf yson, EYsonType type) override;
    void OnRaw(TFuture<TYsonString> asyncStr) override;

    void Finish();

private:
    IAsyncYsonConsumer* const UnderlyingConsumer_;
    bool HasAttributes_ = false;
    bool Finished_ = false;

    void Start();
};

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

//! Consumes an attribute value and if it is non-empty then prepends it with
//! the attribute key.
class TAttributeValueConsumer
    : public IAsyncYsonConsumer
{
public:
    TAttributeValueConsumer(
        IAsyncYsonConsumer* underlyingConsumer,
        TString key);

    void OnStringScalar(TStringBuf value) override;
    void OnInt64Scalar(i64 value) override;
    void OnUint64Scalar(ui64 value) override;
    void OnDoubleScalar(double value) override;
    void OnBooleanScalar(bool value) override;
    void OnEntity() override;
    void OnBeginList() override;
    void OnListItem() override;
    void OnEndList() override;
    void OnBeginMap() override;
    void OnKeyedItem(TStringBuf key) override;
    void OnEndMap() override;
    void OnBeginAttributes() override;
    void OnEndAttributes() override;
    using IYsonConsumer::OnRaw;
    void OnRaw(TStringBuf yson, EYsonType type) override;
    void OnRaw(TFuture<TYsonString> asyncStr) override;

private:
    IAsyncYsonConsumer* const UnderlyingConsumer_;
    const TString Key_;
    bool Empty_ = true;

    void ProduceKeyIfNeeded();

};

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

} // namespace NYT::NYson