aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/pb_io.h
blob: aad964555be1e90bb38868c434ff265c20824854 (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
#pragma once

#include <util/generic/fwd.h>
#include <util/generic/flags.h>

struct IBinSaver;

namespace google {
    namespace protobuf {
        class Message;
    }
}

namespace NProtoBuf {
    using Message = ::google::protobuf::Message;
}

class IInputStream;
class IOutputStream;

namespace NProtoBuf {
    /* Parse base64 URL encoded serialized message from string.
     */
    void ParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven = false);
    bool TryParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven = false);
    template <typename T>
    static T ParseFromBase64String(const TStringBuf& dataBase64, bool allowUneven = false) {
        T m;
        ParseFromBase64String(dataBase64, m, allowUneven);
        return m;
    }

    /* Serialize message into string and apply base64 URL encoding.
     */
    TString SerializeToBase64String(const Message& m);
    void SerializeToBase64String(const Message& m, TString& dataBase64);
    bool TrySerializeToBase64String(const Message& m, TString& dataBase64);

    const TString ShortUtf8DebugString(const Message& message);
 
    bool MergePartialFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage); 
    bool MergeFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage); 
}

int operator&(NProtoBuf::Message& m, IBinSaver& f);

// Write a textual representation of the given message to the given file.
void SerializeToTextFormat(const NProtoBuf::Message& m, const TString& fileName);
void SerializeToTextFormat(const NProtoBuf::Message& m, IOutputStream& out);

// Write a textual representation of the given message to the given output stream
// with flags UseShortRepeatedPrimitives and UseUtf8StringEscaping set to true.
void SerializeToTextFormatPretty(const NProtoBuf::Message& m, IOutputStream& out);

// Write a textual representation of the given message to the given output stream
// use enum id instead of enum name for all enum fields.
void SerializeToTextFormatWithEnumId(const NProtoBuf::Message& m, IOutputStream& out);

enum class EParseFromTextFormatOption : ui64 {
    // Unknown fields will be ignored by the parser
    AllowUnknownField = 1
};

Y_DECLARE_FLAGS(EParseFromTextFormatOptions, EParseFromTextFormatOption);

// Parse a text-format protocol message from the given file into message object.
void ParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
                         const EParseFromTextFormatOptions options = {});
// NOTE: will read `in` till the end.
void ParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
                         const EParseFromTextFormatOptions options = {});

/* @return              `true` if parsing was successfull and `false` otherwise.
 *
 * @see `ParseFromTextFormat`
 */
bool TryParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
                            const EParseFromTextFormatOptions options = {});
// NOTE: will read `in` till the end.
bool TryParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
                            const EParseFromTextFormatOptions options = {});

// @see `ParseFromTextFormat`
template <typename T>
static T ParseFromTextFormat(const TString& fileName,
                             const EParseFromTextFormatOptions options = {}) {
    T message;
    ParseFromTextFormat(fileName, message, options);
    return message;
}

// @see `ParseFromTextFormat`
// NOTE: will read `in` till the end.
template <typename T>
static T ParseFromTextFormat(IInputStream& in,
                             const EParseFromTextFormatOptions options = {}) {
    T message;
    ParseFromTextFormat(in, message, options);
    return message;
}

// Merge a text-format protocol message from the given file into message object.
//
// NOTE: Even when parsing failed and exception was thrown `m` may be different from its original
// value. User must implement transactional logic around `MergeFromTextFormat` by himself.
void MergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
                         const EParseFromTextFormatOptions options = {});
// NOTE: will read `in` till the end.
void MergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
                         const EParseFromTextFormatOptions options = {});
/* @return              `true` if parsing was successfull and `false` otherwise.
 *
 * @see `MergeFromTextFormat`
 */
bool TryMergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
                            const EParseFromTextFormatOptions options = {});
// NOTE: will read `in` till the end.
bool TryMergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
                            const EParseFromTextFormatOptions options = {});

// @see `MergeFromTextFormat`
template <typename T>
static T MergeFromTextFormat(const TString& fileName,
                             const EParseFromTextFormatOptions options = {}) {
    T message;
    MergeFromTextFormat(fileName, message, options);
    return message;
}

// @see `MergeFromTextFormat`
// NOTE: will read `in` till the end.
template <typename T>
static T MergeFromTextFormat(IInputStream& in,
                             const EParseFromTextFormatOptions options = {}) {
    T message;
    MergeFromTextFormat(in, message, options);
    return message;
}