-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
parser_traits.h
400 lines (354 loc) · 13.2 KB
/
parser_traits.h
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef GOOGLE_PROTOBUF_JSON_INTERNAL_PARSER_TRAITS_H__
#define GOOGLE_PROTOBUF_JSON_INTERNAL_PARSER_TRAITS_H__
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include "google/protobuf/type.pb.h"
#include "absl/base/attributes.h"
#include "absl/base/casts.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/json/internal/descriptor_traits.h"
#include "google/protobuf/wire_format_lite.h"
#include "google/protobuf/stubs/status_macros.h"
// Must be included last.
#include "google/protobuf/port_def.inc"
namespace google {
namespace protobuf {
namespace json_internal {
using ::google::protobuf::internal::WireFormatLite;
// See the comment in json_util2_parser.cc for more information.
//
// The type traits in this file describe how to parse to a protobuf
// representation used by the JSON API, either via proto reflection or by
// emitting wire format to an output stream.
// Helper alias templates to avoid needing to write `typename` in function
// signatures.
template <typename Traits>
using Msg = typename Traits::Msg;
struct ParseProto2Descriptor : Proto2Descriptor {
// A message value that fields can be written to, but not read from.
class Msg {
public:
explicit Msg(Message* msg) : msg_(msg) {}
private:
friend ParseProto2Descriptor;
Message* msg_;
// Because `msg` might already have oneofs set, we need to track which were
// set *during* the parse separately.
absl::flat_hash_set<int> parsed_oneofs_indices_;
absl::flat_hash_set<int> parsed_fields_;
};
static bool HasParsed(Field f, const Msg& msg,
bool allow_repeated_non_oneof) {
if (f->real_containing_oneof()) {
return msg.parsed_oneofs_indices_.contains(
f->real_containing_oneof()->index());
}
if (allow_repeated_non_oneof) {
return false;
}
return msg.parsed_fields_.contains(f->number());
}
/// Functions for writing fields. ///
// Marks a field as having been "seen". This will clear the field if it is
// the first occurrence thereof.
//
// All setters call this function automatically, but it may also be called
// eagerly to clear a pre-existing value that might not be overwritten, such
// as when parsing a repeated field.
static void RecordAsSeen(Field f, Msg& msg) {
bool inserted = msg.parsed_fields_.insert(f->number()).second;
if (inserted) {
msg.msg_->GetReflection()->ClearField(msg.msg_, f);
}
if (f->real_containing_oneof() != nullptr) {
msg.parsed_oneofs_indices_.insert(f->real_containing_oneof()->index());
}
}
// Adds a new message and calls body on it.
//
// Body should have a signature `absl::Status(const Desc&, Msg&)`.
template <typename F>
static absl::Status NewMsg(Field f, Msg& msg, F body) {
RecordAsSeen(f, msg);
Message* new_msg;
if (f->is_repeated()) {
new_msg = msg.msg_->GetReflection()->AddMessage(msg.msg_, f);
} else {
new_msg = msg.msg_->GetReflection()->MutableMessage(msg.msg_, f);
}
Msg wrapper(new_msg);
return body(*f->message_type(), wrapper);
}
// Adds a new dynamic message with the given type name and calls body on it.
//
// Body should have a signature `absl::Status(const Desc&, Msg&)`.
template <typename F>
static absl::Status NewDynamic(Field f, const std::string& type_url, Msg& msg,
F body) {
RecordAsSeen(f, msg);
return WithDynamicType(
*f->containing_type(), type_url, [&](const Desc& desc) -> absl::Status {
DynamicMessageFactory factory;
std::unique_ptr<Message> dynamic(factory.GetPrototype(&desc)->New());
Msg wrapper(dynamic.get());
RETURN_IF_ERROR(body(desc, wrapper));
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddString(
msg.msg_, f, dynamic->SerializePartialAsString());
} else {
msg.msg_->GetReflection()->SetString(
msg.msg_, f, dynamic->SerializePartialAsString());
}
return absl::OkStatus();
});
}
static void SetFloat(Field f, Msg& msg, float x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddFloat(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetFloat(msg.msg_, f, x);
}
}
static void SetDouble(Field f, Msg& msg, double x) {
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddDouble(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetDouble(msg.msg_, f, x);
}
}
static void SetInt64(Field f, Msg& msg, int64_t x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddInt64(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetInt64(msg.msg_, f, x);
}
}
static void SetUInt64(Field f, Msg& msg, uint64_t x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddUInt64(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetUInt64(msg.msg_, f, x);
}
}
static void SetInt32(Field f, Msg& msg, int32_t x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddInt32(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetInt32(msg.msg_, f, x);
}
}
static void SetUInt32(Field f, Msg& msg, uint32_t x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddUInt32(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetUInt32(msg.msg_, f, x);
}
}
static void SetBool(Field f, Msg& msg, bool x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddBool(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetBool(msg.msg_, f, x);
}
}
static void SetString(Field f, Msg& msg, absl::string_view x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddString(msg.msg_, f, std::string(x));
} else {
msg.msg_->GetReflection()->SetString(msg.msg_, f, std::string(x));
}
}
static void SetEnum(Field f, Msg& msg, int32_t x) {
RecordAsSeen(f, msg);
if (f->is_repeated()) {
msg.msg_->GetReflection()->AddEnumValue(msg.msg_, f, x);
} else {
msg.msg_->GetReflection()->SetEnumValue(msg.msg_, f, x);
}
}
};
// Traits for proto3-ish deserialization.
//
// This includes a rudimentary proto serializer, since message fields are
// written directly instead of being reflectively written to a proto field.
//
// See MessageTraits for API docs.
struct ParseProto3Type : Proto3Type {
class Msg {
public:
explicit Msg(io::ZeroCopyOutputStream* stream) : stream_(stream) {}
private:
friend ParseProto3Type;
io::CodedOutputStream stream_;
absl::flat_hash_set<int32_t> parsed_oneofs_indices_;
absl::flat_hash_set<int32_t> parsed_fields_;
};
static bool HasParsed(Field f, const Msg& msg,
bool allow_repeated_non_oneof) {
if (f->proto().oneof_index() != 0) {
return msg.parsed_oneofs_indices_.contains(f->proto().oneof_index());
}
if (allow_repeated_non_oneof) {
return false;
}
return msg.parsed_fields_.contains(f->proto().number());
}
/// Functions for writing fields. ///
static void RecordAsSeen(Field f, Msg& msg) {
msg.parsed_fields_.insert(f->proto().number());
if (f->proto().oneof_index() != 0) {
msg.parsed_oneofs_indices_.insert(f->proto().oneof_index());
}
}
template <typename F>
static absl::Status NewMsg(Field f, Msg& msg, F body) {
return NewDynamic(f, f->proto().type_url(), msg, body);
}
template <typename F>
static absl::Status NewDynamic(Field f, const std::string& type_url, Msg& msg,
F body) {
RecordAsSeen(f, msg);
return WithDynamicType(
f->parent(), type_url, [&](const Desc& desc) -> absl::Status {
if (f->proto().kind() == google::protobuf::Field::TYPE_GROUP) {
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_START_GROUP);
RETURN_IF_ERROR(body(desc, msg));
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_END_GROUP);
return absl::OkStatus();
}
std::string out;
io::StringOutputStream stream(&out);
Msg new_msg(&stream);
RETURN_IF_ERROR(body(desc, new_msg));
new_msg.stream_.Trim(); // Should probably be called "Flush()".
absl::string_view written(
out.data(), static_cast<size_t>(new_msg.stream_.ByteCount()));
SetString(f, msg, written);
return absl::OkStatus();
});
}
static void SetFloat(Field f, Msg& msg, float x) {
RecordAsSeen(f, msg);
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_FIXED32);
msg.stream_.WriteLittleEndian32(absl::bit_cast<uint32_t>(x));
}
static void SetDouble(Field f, Msg& msg, double x) {
RecordAsSeen(f, msg);
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_FIXED64);
msg.stream_.WriteLittleEndian64(absl::bit_cast<uint64_t>(x));
}
static void SetInt64(Field f, Msg& msg, int64_t x) {
SetInt<int64_t, google::protobuf::Field::TYPE_INT64,
google::protobuf::Field::TYPE_SFIXED64,
google::protobuf::Field::TYPE_SINT64>(f, msg, x);
}
static void SetUInt64(Field f, Msg& msg, uint64_t x) {
SetInt<uint64_t, google::protobuf::Field::TYPE_UINT64,
google::protobuf::Field::TYPE_FIXED64,
google::protobuf::Field::TYPE_UNKNOWN>(f, msg, x);
}
static void SetInt32(Field f, Msg& msg, int32_t x) {
SetInt<int32_t, google::protobuf::Field::TYPE_INT32,
google::protobuf::Field::TYPE_SFIXED32,
google::protobuf::Field::TYPE_SINT32>(f, msg, x);
}
static void SetUInt32(Field f, Msg& msg, uint32_t x) {
SetInt<uint32_t, google::protobuf::Field::TYPE_UINT32,
google::protobuf::Field::TYPE_FIXED32,
google::protobuf::Field::TYPE_UNKNOWN>(f, msg, x);
}
static void SetBool(Field f, Msg& msg, bool x) {
RecordAsSeen(f, msg);
msg.stream_.WriteTag(f->proto().number() << 3);
char b = x ? 0x01 : 0x00;
msg.stream_.WriteRaw(&b, 1);
}
static void SetString(Field f, Msg& msg, absl::string_view x) {
RecordAsSeen(f, msg);
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
msg.stream_.WriteVarint64(static_cast<uint64_t>(x.size()));
msg.stream_.WriteRaw(x.data(), x.size());
}
static void SetEnum(Field f, Msg& msg, int32_t x) {
RecordAsSeen(f, msg);
msg.stream_.WriteTag(f->proto().number() << 3);
// Sign extension is deliberate here.
msg.stream_.WriteVarint32(x);
}
private:
using Kind = google::protobuf::Field::Kind;
// Sets a field of *some* integer type, with the given kinds for the possible
// encodings. This avoids quadruplicating this code in the helpers for the
// four major integer types.
template <typename Int, Kind varint, Kind fixed, Kind zigzag>
static void SetInt(Field f, Msg& msg, Int x) {
RecordAsSeen(f, msg);
switch (f->proto().kind()) {
case zigzag:
// Regardless of the integer type, ZigZag64 will do the right thing,
// because ZigZag is not dependent on the width of the integer: it is
// always `2 * abs(n) + (n < 0)`.
x = static_cast<Int>(
internal::WireFormatLite::ZigZagEncode64(static_cast<int64_t>(x)));
ABSL_FALLTHROUGH_INTENDED;
case varint:
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_VARINT);
if (sizeof(Int) == 4) {
msg.stream_.WriteVarint32(static_cast<uint32_t>(x));
} else {
msg.stream_.WriteVarint64(static_cast<uint64_t>(x));
}
break;
case fixed: {
if (sizeof(Int) == 4) {
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_FIXED32);
msg.stream_.WriteLittleEndian32(static_cast<uint32_t>(x));
} else {
msg.stream_.WriteTag(f->proto().number() << 3 |
WireFormatLite::WIRETYPE_FIXED64);
msg.stream_.WriteLittleEndian64(static_cast<uint64_t>(x));
}
break;
}
default: { // Unreachable.
}
}
}
};
} // namespace json_internal
} // namespace protobuf
} // namespace google
#include "google/protobuf/port_undef.inc"
#endif // GOOGLE_PROTOBUF_JSON_INTERNAL_PARSER_TRAITS_H__