aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/format.cpp
blob: d366b326815a2c28a3254e23e031e988eb15fb7f (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
#include "format.h"

#include <util/string/ascii.h>
#include <util/string/split.h>
#include <util/string/strip.h>
#include <util/stream/output.h>
#include <util/string/cast.h>

namespace NMonitoring {
    static ECompression CompressionFromHeader(TStringBuf value) {
        if (value.empty()) {
            return ECompression::UNKNOWN;
        }

        for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
            TStringBuf token = StripString(it.Token());

            if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::IDENTITY)) {
                return ECompression::IDENTITY;
            } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::ZLIB)) {
                return ECompression::ZLIB;
            } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::LZ4)) {
                return ECompression::LZ4;
            } else if (AsciiEqualsIgnoreCase(token, NFormatContentEncoding::ZSTD)) {
                return ECompression::ZSTD;
            }
        }

        return ECompression::UNKNOWN;
    }

    static EFormat FormatFromHttpMedia(TStringBuf value) {
        if (AsciiEqualsIgnoreCase(value, NFormatContenType::SPACK)) {
            return EFormat::SPACK;
        } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::JSON)) {
            return EFormat::JSON;
        } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::PROTOBUF)) {
            return EFormat::PROTOBUF;
        } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::TEXT)) {
            return EFormat::TEXT;
        } else if (AsciiEqualsIgnoreCase(value, NFormatContenType::PROMETHEUS)) {
            return EFormat::PROMETHEUS;
        }

        return EFormat::UNKNOWN;
    }

    EFormat FormatFromAcceptHeader(TStringBuf value) {
        EFormat result{EFormat::UNKNOWN};

        for (const auto& it : StringSplitter(value).Split(',').SkipEmpty()) {
            TStringBuf token = StripString(it.Token());

            result = FormatFromHttpMedia(token);
            if (result != EFormat::UNKNOWN) {
                break;
            }
        }

        return result;
    }

    EFormat FormatFromContentType(TStringBuf value) {
        value = value.NextTok(';');

        return FormatFromHttpMedia(value);
    }

    TStringBuf ContentTypeByFormat(EFormat format) {
        switch (format) {
            case EFormat::SPACK:
                return NFormatContenType::SPACK;
            case EFormat::JSON:
                return NFormatContenType::JSON;
            case EFormat::PROTOBUF:
                return NFormatContenType::PROTOBUF;
            case EFormat::TEXT:
                return NFormatContenType::TEXT;
            case EFormat::PROMETHEUS:
                return NFormatContenType::PROMETHEUS;
            case EFormat::UNKNOWN:
                return TStringBuf();
        }

        Y_FAIL(); // for GCC
    }

    ECompression CompressionFromAcceptEncodingHeader(TStringBuf value) {
        return CompressionFromHeader(value);
    }

    ECompression CompressionFromContentEncodingHeader(TStringBuf value) {
        return CompressionFromHeader(value);
    }

    TStringBuf ContentEncodingByCompression(ECompression compression) {
        switch (compression) {
            case ECompression::IDENTITY:
                return NFormatContentEncoding::IDENTITY;
            case ECompression::ZLIB:
                return NFormatContentEncoding::ZLIB;
            case ECompression::LZ4:
                return NFormatContentEncoding::LZ4;
            case ECompression::ZSTD:
                return NFormatContentEncoding::ZSTD;
            case ECompression::UNKNOWN:
                return TStringBuf();
        }

        Y_FAIL(); // for GCC
    }

}

template <>
NMonitoring::EFormat FromStringImpl<NMonitoring::EFormat>(const char* str, size_t len) {
    using NMonitoring::EFormat;
    TStringBuf value(str, len);
    if (value == TStringBuf("SPACK")) { 
        return EFormat::SPACK;
    } else if (value == TStringBuf("JSON")) { 
        return EFormat::JSON;
    } else if (value == TStringBuf("PROTOBUF")) { 
        return EFormat::PROTOBUF;
    } else if (value == TStringBuf("TEXT")) { 
        return EFormat::TEXT;
    } else if (value == TStringBuf("PROMETHEUS")) { 
        return EFormat::PROMETHEUS;
    } else if (value == TStringBuf("UNKNOWN")) { 
        return EFormat::UNKNOWN;
    }
    ythrow yexception() << "unknown format: " << value;
}

template <>
void Out<NMonitoring::EFormat>(IOutputStream& o, NMonitoring::EFormat f) {
    using NMonitoring::EFormat;
    switch (f) {
        case EFormat::SPACK:
            o << TStringBuf("SPACK"); 
            return;
        case EFormat::JSON:
            o << TStringBuf("JSON"); 
            return;
        case EFormat::PROTOBUF:
            o << TStringBuf("PROTOBUF"); 
            return;
        case EFormat::TEXT:
            o << TStringBuf("TEXT"); 
            return;
        case EFormat::PROMETHEUS:
            o << TStringBuf("PROMETHEUS"); 
            return;
        case EFormat::UNKNOWN:
            o << TStringBuf("UNKNOWN"); 
            return;
    }

    Y_FAIL(); // for GCC
}

template <>
NMonitoring::ECompression FromStringImpl<NMonitoring::ECompression>(const char* str, size_t len) {
    using NMonitoring::ECompression;
    TStringBuf value(str, len);
    if (value == TStringBuf("IDENTITY")) { 
        return ECompression::IDENTITY;
    } else if (value == TStringBuf("ZLIB")) { 
        return ECompression::ZLIB;
    } else if (value == TStringBuf("LZ4")) { 
        return ECompression::LZ4;
    } else if (value == TStringBuf("ZSTD")) { 
        return ECompression::ZSTD;
    } else if (value == TStringBuf("UNKNOWN")) { 
        return ECompression::UNKNOWN;
    }
    ythrow yexception() << "unknown compression: " << value;
}

template <>
void Out<NMonitoring::ECompression>(IOutputStream& o, NMonitoring::ECompression c) {
    using NMonitoring::ECompression;
    switch (c) {
        case ECompression::IDENTITY:
            o << TStringBuf("IDENTITY"); 
            return;
        case ECompression::ZLIB:
            o << TStringBuf("ZLIB"); 
            return;
        case ECompression::LZ4:
            o << TStringBuf("LZ4"); 
            return;
        case ECompression::ZSTD:
            o << TStringBuf("ZSTD"); 
            return;
        case ECompression::UNKNOWN:
            o << TStringBuf("UNKNOWN"); 
            return;
    }

    Y_FAIL(); // for GCC
}