aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/result_format/yql_codec_results.h
blob: 3fae20cf8330fe92b797dcb480b31a4a75ed8f55 (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
#pragma once

#include <library/cpp/yson/writer.h>
#include <util/generic/strbuf.h>
#include <util/string/cast.h>

namespace NYql {
namespace NResult {

// we should not write numbers as numbers ever
// write numbers as strings except Yson value where we use restricted dialect
// write bool as bool
// write null as entity
class TYsonResultWriter {
public:
    static constexpr TStringBuf VoidString = "Void";

public:
    explicit TYsonResultWriter(NYson::TYsonConsumerBase& writer)
        : Writer(writer)
    {
    }
    void OnVoid() {
        Writer.OnStringScalar(VoidString);
    }
    void OnNull() {
        Writer.OnEntity();
    }
    void OnEmptyList() {
        Writer.OnBeginList();
        Writer.OnEndList();
    }
    void OnEmptyDict() {
        Writer.OnBeginList();
        Writer.OnEndList();
    }
    void OnEntity() {
        Writer.OnEntity();
    }
    // numbers
    void OnInt64Scalar(i64 value) {
        WriteNumberAsString(value);
    }
    void OnUint64Scalar(ui64 value) {
        WriteNumberAsString(value);
    }
    void OnFloatScalar(float value) {
        Writer.OnStringScalar(::FloatToString(value));
    }
    void OnDoubleScalar(double value) {
        Writer.OnStringScalar(::FloatToString(value));
    }
    void OnBooleanScalar(bool value) {
        Writer.OnBooleanScalar(value);
    }
    // strings
    void OnStringScalar(TStringBuf value);
    void OnUtf8StringScalar(TStringBuf value) {
        Writer.OnStringScalar(value);
    }
    // list construction
    void OnBeginList() {
        Writer.OnBeginList();
    }
    void OnListItem() {
        Writer.OnListItem();
    }
    void OnEndList() {
        Writer.OnEndList();
    }
    void OnBeginMap() {
        Writer.OnBeginMap();
    }
    void OnKeyedItem(const TStringBuf& key) {
        Writer.OnKeyedItem(key);
    }
    void OnEndMap() {
        Writer.OnEndMap();
    }
    void OnBeginAttributes() {
        Writer.OnBeginAttributes();
    }
    void OnEndAttributes() {
        Writer.OnEndAttributes();
    }
    void OnRaw(TStringBuf ysonNode, NYT::NYson::EYsonType type) {
        Writer.OnRaw(ysonNode, type);
    }

private:
    template <typename T>
    void WriteNumberAsString(T value) {
        Writer.OnStringScalar(::ToString(value));
    }

private:
    NYson::TYsonConsumerBase& Writer;
};
}
}