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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#include "descriptor.h"
#include <library/cpp/json/json_reader.h>
#include <library/cpp/json/json_writer.h>
#include <library/cpp/protobuf/dynamic_prototype/dynamic_prototype.h>
#include <library/cpp/protobuf/dynamic_prototype/generate_file_descriptor_set.h>
#include <library/cpp/protobuf/json/json2proto.h>
#include <library/cpp/protobuf/json/proto2json.h>
#include <library/cpp/string_utils/base64/base64.h>
#include <util/generic/hash.h>
#include <util/generic/queue.h>
#include <util/generic/set.h>
#include <util/generic/vector.h>
#include <util/stream/mem.h>
#include <util/stream/str.h>
#include <util/stream/zlib.h>
#include <util/string/cast.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
using namespace NProtoBuf;
static TString SerializeFileDescriptorSet(const FileDescriptorSet& proto) {
const auto size = proto.ByteSize();
TTempBuf data(size);
proto.SerializeWithCachedSizesToArray((ui8*)data.Data());
TStringStream str;
{
TZLibCompress comp(&str, ZLib::GZip);
comp.Write(data.Data(), size);
}
return str.Str();
}
static bool ParseFileDescriptorSet(const TStringBuf& data, FileDescriptorSet* proto) {
TMemoryInput input(data.data(), data.size());
TString buf = TZLibDecompress(&input).ReadAll();
if (!proto->ParseFromArray(buf.data(), buf.size())) {
return false;
}
return true;
}
TDynamicInfo::TDynamicInfo(TDynamicPrototypePtr dynamicPrototype)
: DynamicPrototype(dynamicPrototype)
, SkipBytes_(0)
{
}
TDynamicInfo::~TDynamicInfo() {
}
TDynamicInfoRef TDynamicInfo::Create(const TStringBuf& typeConfig) {
auto data = ParseTypeConfig(typeConfig);
const TString& meta = Base64Decode(data.Metadata);
const TString& name = data.MessageName;
FileDescriptorSet set;
if (!ParseFileDescriptorSet(meta, &set)) {
ythrow yexception() << "can't parse metadata";
}
auto info = MakeIntrusive<TDynamicInfo>(TDynamicPrototype::Create(set, name, true));
info->EnumFormat_ = data.EnumFormat;
info->ProtoFormat_ = data.ProtoFormat;
info->Recursion_ = data.Recursion;
info->YtMode_ = data.YtMode;
info->SkipBytes_ = data.SkipBytes;
info->OptionalLists_ = data.OptionalLists;
info->SyntaxAware_ = data.SyntaxAware;
return info;
}
const Descriptor* TDynamicInfo::Descriptor() const {
return DynamicPrototype->GetDescriptor();
}
EEnumFormat TDynamicInfo::GetEnumFormat() const {
return EnumFormat_;
}
ERecursionTraits TDynamicInfo::GetRecursionTraits() const {
return Recursion_;
}
bool TDynamicInfo::GetYtMode() const {
return YtMode_;
}
bool TDynamicInfo::GetOptionalLists() const {
return OptionalLists_;
}
bool TDynamicInfo::GetSyntaxAware() const {
return SyntaxAware_;
}
TAutoPtr<Message> TDynamicInfo::MakeProto() {
return DynamicPrototype->CreateUnsafe();
}
TAutoPtr<Message> TDynamicInfo::Parse(const TStringBuf& data) {
auto mut = MakeProto();
TStringBuf tmp(data);
if (SkipBytes_) {
tmp = TStringBuf(tmp.data() + SkipBytes_, tmp.size() - SkipBytes_);
}
switch (ProtoFormat_) {
case PF_PROTOBIN: {
if (!mut->ParseFromArray(tmp.data(), tmp.size())) {
ythrow yexception() << "can't parse protobin message";
}
break;
}
case PF_PROTOTEXT: {
io::ArrayInputStream si(tmp.data(), tmp.size());
if (!TextFormat::Parse(&si, mut.Get())) {
ythrow yexception() << "can't parse prototext message";
}
break;
}
case PF_JSON: {
NJson::TJsonValue value;
if (NJson::ReadJsonFastTree(tmp, &value)) {
NProtobufJson::Json2Proto(value, *mut);
} else {
ythrow yexception() << "can't parse json value";
}
break;
}
}
return mut;
}
TString TDynamicInfo::Serialize(const Message& proto) {
TString result;
switch (ProtoFormat_) {
case PF_PROTOBIN: {
result.ReserveAndResize(proto.ByteSize());
if (!proto.SerializeToArray(result.begin(), result.size())) {
ythrow yexception() << "can't serialize protobin message";
}
break;
}
case PF_PROTOTEXT: {
if (!TextFormat::PrintToString(proto, &result)) {
ythrow yexception() << "can't serialize prototext message";
}
break;
}
case PF_JSON: {
NJson::TJsonValue value;
NProtobufJson::Proto2Json(proto, value);
result = NJson::WriteJson(value);
break;
}
}
return result;
}
TString GenerateProtobufTypeConfig(
const Descriptor* descriptor,
const TProtoTypeConfigOptions& options) {
NJson::TJsonValue ret(NJson::JSON_MAP);
ret["name"] = descriptor->full_name();
ret["meta"] = Base64Encode(
SerializeFileDescriptorSet(GenerateFileDescriptorSet(descriptor)));
if (options.SkipBytes > 0) {
ret["skip"] = options.SkipBytes;
}
switch (options.ProtoFormat) {
case PF_PROTOBIN:
break;
case PF_PROTOTEXT:
ret["format"] = "prototext";
break;
case PF_JSON:
ret["format"] = "json";
break;
}
if (!options.OptionalLists) {
ret["lists"]["optional"] = false;
}
if (options.SyntaxAware) {
ret["syntax"]["aware"] = options.SyntaxAware;
}
switch (options.EnumFormat) {
case EEnumFormat::Number:
break;
case EEnumFormat::Name:
ret["view"]["enum"] = "name";
break;
case EEnumFormat::FullName:
ret["view"]["enum"] = "full_name";
break;
}
switch (options.Recursion) {
case ERecursionTraits::Fail:
break;
case ERecursionTraits::Ignore:
ret["view"]["recursion"] = "ignore";
break;
case ERecursionTraits::Bytes:
ret["view"]["recursion"] = "bytes";
break;
}
if (options.YtMode) {
ret["view"]["yt_mode"] = true;
}
return NJson::WriteJson(ret, false);
}
TProtoTypeConfig ParseTypeConfig(const TStringBuf& config) {
if (config.empty()) {
ythrow yexception() << "empty metadata";
}
switch (config[0]) {
case '#': {
auto plus = config.find('+');
if (config[0] != '#') {
ythrow yexception() << "unknown version of metadata format";
}
if (plus == TStringBuf::npos) {
ythrow yexception() << "invalid metadata";
}
TProtoTypeConfig result;
result.MessageName = TStringBuf(config.begin() + 1, plus - 1);
result.Metadata = TStringBuf(config.begin() + 1 + plus, config.size() - plus - 1);
result.SkipBytes = 0;
return result;
}
case '{': {
NJson::TJsonValue value;
if (NJson::ReadJsonFastTree(config, &value)) {
TProtoTypeConfig result;
TString protoFormat = value["format"].GetStringSafe("protobin");
TString enumFormat = value["view"]["enum"].GetStringSafe("number");
TString recursion = value["view"]["recursion"].GetStringSafe("fail");
result.MessageName = value["name"].GetString();
result.Metadata = value["meta"].GetString();
result.SkipBytes = value["skip"].GetIntegerSafe(0);
result.OptionalLists = value["lists"]["optional"].GetBooleanSafe(true);
result.SyntaxAware = value["syntax"]["aware"].GetBooleanSafe(false);
result.YtMode = value["view"]["yt_mode"].GetBooleanSafe(false);
if (protoFormat == "protobin") {
result.ProtoFormat = PF_PROTOBIN;
} else if (protoFormat == "prototext") {
result.ProtoFormat = PF_PROTOTEXT;
} else if (protoFormat == "json") {
result.ProtoFormat = PF_JSON;
} else {
ythrow yexception() << "unsupported format " << protoFormat;
}
if (enumFormat == "number") {
result.EnumFormat = EEnumFormat::Number;
} else if (enumFormat == "name") {
result.EnumFormat = EEnumFormat::Name;
} else if (enumFormat == "full_name") {
result.EnumFormat = EEnumFormat::FullName;
} else {
ythrow yexception() << "unsupported enum representation "
<< enumFormat;
}
if (recursion == "fail") {
result.Recursion = ERecursionTraits::Fail;
} else if (recursion == "ignore") {
result.Recursion = ERecursionTraits::Ignore;
} else if (recursion == "bytes") {
result.Recursion = ERecursionTraits::Bytes;
} else {
ythrow yexception() << "unsupported recursion trait "
<< recursion;
}
return result;
} else {
ythrow yexception() << "can't parse json metadata";
}
}
default:
ythrow yexception() << "invalid control char "
<< TStringBuf(config.data(), 1);
}
}
|