Coverage Report

Created: 2023-03-26 07:25

/proc/self/cwd/external/com_google_protobuf/src/google/protobuf/util/internal/protostream_objectwriter.cc
Line
Count
Source (jump to first uncovered line)
1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.  All rights reserved.
3
// https://developers.google.com/protocol-buffers/
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
//     * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
//     * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
14
// distribution.
15
//     * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
#include <google/protobuf/util/internal/protostream_objectwriter.h>
32
33
#include <cstdint>
34
#include <functional>
35
#include <stack>
36
#include <unordered_map>
37
#include <unordered_set>
38
39
#include <google/protobuf/stubs/once.h>
40
#include <google/protobuf/wire_format_lite.h>
41
#include <google/protobuf/util/internal/field_mask_utility.h>
42
#include <google/protobuf/util/internal/object_location_tracker.h>
43
#include <google/protobuf/util/internal/constants.h>
44
#include <google/protobuf/util/internal/utility.h>
45
#include <google/protobuf/stubs/strutil.h>
46
#include <google/protobuf/stubs/status.h>
47
#include <google/protobuf/stubs/statusor.h>
48
#include <google/protobuf/stubs/time.h>
49
#include <google/protobuf/stubs/map_util.h>
50
51
52
#include <google/protobuf/port_def.inc>
53
54
namespace google {
55
namespace protobuf {
56
namespace util {
57
namespace converter {
58
59
using ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite;
60
using std::placeholders::_1;
61
using util::Status;
62
63
64
ProtoStreamObjectWriter::ProtoStreamObjectWriter(
65
    TypeResolver* type_resolver, const google::protobuf::Type& type,
66
    strings::ByteSink* output, ErrorListener* listener,
67
    const ProtoStreamObjectWriter::Options& options)
68
    : ProtoWriter(type_resolver, type, output, listener),
69
      master_type_(type),
70
      current_(nullptr),
71
18.9k
      options_(options) {
72
18.9k
  set_ignore_unknown_fields(options_.ignore_unknown_fields);
73
18.9k
  set_ignore_unknown_enum_values(options_.ignore_unknown_enum_values);
74
18.9k
  set_use_lower_camel_for_enums(options_.use_lower_camel_for_enums);
75
18.9k
  set_case_insensitive_enum_parsing(options_.case_insensitive_enum_parsing);
76
18.9k
  set_use_json_name_in_missing_fields(options.use_json_name_in_missing_fields);
77
18.9k
}
78
79
ProtoStreamObjectWriter::ProtoStreamObjectWriter(
80
    const TypeInfo* typeinfo, const google::protobuf::Type& type,
81
    strings::ByteSink* output, ErrorListener* listener,
82
    const ProtoStreamObjectWriter::Options& options)
83
    : ProtoWriter(typeinfo, type, output, listener),
84
      master_type_(type),
85
      current_(nullptr),
86
0
      options_(options) {
87
0
  set_ignore_unknown_fields(options_.ignore_unknown_fields);
88
0
  set_use_lower_camel_for_enums(options.use_lower_camel_for_enums);
89
0
  set_case_insensitive_enum_parsing(options_.case_insensitive_enum_parsing);
90
0
  set_use_json_name_in_missing_fields(options.use_json_name_in_missing_fields);
91
0
}
92
93
ProtoStreamObjectWriter::ProtoStreamObjectWriter(
94
    const TypeInfo* typeinfo, const google::protobuf::Type& type,
95
    strings::ByteSink* output, ErrorListener* listener)
96
    : ProtoWriter(typeinfo, type, output, listener),
97
      master_type_(type),
98
      current_(nullptr),
99
0
      options_(ProtoStreamObjectWriter::Options::Defaults()) {}
100
101
18.9k
ProtoStreamObjectWriter::~ProtoStreamObjectWriter() {
102
18.9k
  if (current_ == nullptr) return;
103
  // Cleanup explicitly in order to avoid destructor stack overflow when input
104
  // is deeply nested.
105
  // Cast to BaseElement to avoid doing additional checks (like missing fields)
106
  // during pop().
107
2.52k
  std::unique_ptr<BaseElement> element(
108
2.52k
      static_cast<BaseElement*>(current_.get())->pop<BaseElement>());
109
20.2M
  while (element != nullptr) {
110
20.2M
    element.reset(element->pop<BaseElement>());
111
20.2M
  }
112
2.52k
}
113
114
namespace {
115
// Utility method to split a string representation of Timestamp or Duration and
116
// return the parts.
117
void SplitSecondsAndNanos(StringPiece input, StringPiece* seconds,
118
0
                          StringPiece* nanos) {
119
0
  size_t idx = input.rfind('.');
120
0
  if (idx != std::string::npos) {
121
0
    *seconds = input.substr(0, idx);
122
0
    *nanos = input.substr(idx + 1);
123
0
  } else {
124
0
    *seconds = input;
125
0
    *nanos = StringPiece();
126
0
  }
127
0
}
128
129
Status GetNanosFromStringPiece(StringPiece s_nanos,
130
                               const char* parse_failure_message,
131
                               const char* exceeded_limit_message,
132
0
                               int32_t* nanos) {
133
0
  *nanos = 0;
134
135
  // Count the number of leading 0s and consume them.
136
0
  int num_leading_zeros = 0;
137
0
  while (s_nanos.Consume("0")) {
138
0
    num_leading_zeros++;
139
0
  }
140
0
  int32_t i_nanos = 0;
141
  // 's_nanos' contains fractional seconds -- i.e. 'nanos' is equal to
142
  // "0." + s_nanos.ToString() seconds. An int32 is used for the
143
  // conversion to 'nanos', rather than a double, so that there is no
144
  // loss of precision.
145
0
  if (!s_nanos.empty() && !safe_strto32(s_nanos, &i_nanos)) {
146
0
    return util::InvalidArgumentError(parse_failure_message);
147
0
  }
148
0
  if (i_nanos > kNanosPerSecond || i_nanos < 0) {
149
0
    return util::InvalidArgumentError(exceeded_limit_message);
150
0
  }
151
  // s_nanos should only have digits. No whitespace.
152
0
  if (s_nanos.find_first_not_of("0123456789") != StringPiece::npos) {
153
0
    return util::InvalidArgumentError(parse_failure_message);
154
0
  }
155
156
0
  if (i_nanos > 0) {
157
    // 'scale' is the number of digits to the right of the decimal
158
    // point in "0." + s_nanos.ToString()
159
0
    int32_t scale = num_leading_zeros + s_nanos.size();
160
    // 'conversion' converts i_nanos into nanoseconds.
161
    // conversion = kNanosPerSecond / static_cast<int32>(std::pow(10, scale))
162
    // For efficiency, we precompute the conversion factor.
163
0
    int32_t conversion = 0;
164
0
    switch (scale) {
165
0
      case 1:
166
0
        conversion = 100000000;
167
0
        break;
168
0
      case 2:
169
0
        conversion = 10000000;
170
0
        break;
171
0
      case 3:
172
0
        conversion = 1000000;
173
0
        break;
174
0
      case 4:
175
0
        conversion = 100000;
176
0
        break;
177
0
      case 5:
178
0
        conversion = 10000;
179
0
        break;
180
0
      case 6:
181
0
        conversion = 1000;
182
0
        break;
183
0
      case 7:
184
0
        conversion = 100;
185
0
        break;
186
0
      case 8:
187
0
        conversion = 10;
188
0
        break;
189
0
      case 9:
190
0
        conversion = 1;
191
0
        break;
192
0
      default:
193
0
        return util::InvalidArgumentError(exceeded_limit_message);
194
0
    }
195
0
    *nanos = i_nanos * conversion;
196
0
  }
197
198
0
  return Status();
199
0
}
200
201
}  // namespace
202
203
ProtoStreamObjectWriter::AnyWriter::AnyWriter(ProtoStreamObjectWriter* parent)
204
    : parent_(parent),
205
      ow_(),
206
      invalid_(false),
207
      data_(),
208
      output_(&data_),
209
      depth_(0),
210
      is_well_known_type_(false),
211
0
      well_known_type_render_(nullptr) {}
212
213
0
ProtoStreamObjectWriter::AnyWriter::~AnyWriter() {}
214
215
0
void ProtoStreamObjectWriter::AnyWriter::StartObject(StringPiece name) {
216
0
  ++depth_;
217
  // If an object writer is absent, that means we have not called StartAny()
218
  // before reaching here, which happens when we have data before the "@type"
219
  // field.
220
0
  if (ow_ == nullptr) {
221
    // Save data before the "@type" field for later replay.
222
0
    uninterpreted_events_.push_back(Event(Event::START_OBJECT, name));
223
0
  } else if (is_well_known_type_ && depth_ == 1) {
224
    // For well-known types, the only other field besides "@type" should be a
225
    // "value" field.
226
0
    if (name != "value" && !invalid_) {
227
0
      parent_->InvalidValue("Any",
228
0
                            "Expect a \"value\" field for well-known types.");
229
0
      invalid_ = true;
230
0
    }
231
0
    ow_->StartObject("");
232
0
  } else {
233
    // Forward the call to the child writer if:
234
    //   1. the type is not a well-known type.
235
    //   2. or, we are in a nested Any, Struct, or Value object.
236
0
    ow_->StartObject(name);
237
0
  }
238
0
}
239
240
0
bool ProtoStreamObjectWriter::AnyWriter::EndObject() {
241
0
  --depth_;
242
0
  if (ow_ == nullptr) {
243
0
    if (depth_ >= 0) {
244
      // Save data before the "@type" field for later replay.
245
0
      uninterpreted_events_.push_back(Event(Event::END_OBJECT));
246
0
    }
247
0
  } else if (depth_ >= 0 || !is_well_known_type_) {
248
    // As long as depth_ >= 0, we know we haven't reached the end of Any.
249
    // Propagate these EndObject() calls to the contained ow_. For regular
250
    // message types, we propagate the end of Any as well.
251
0
    ow_->EndObject();
252
0
  }
253
  // A negative depth_ implies that we have reached the end of Any
254
  // object. Now we write out its contents.
255
0
  if (depth_ < 0) {
256
0
    WriteAny();
257
0
    return false;
258
0
  }
259
0
  return true;
260
0
}
261
262
0
void ProtoStreamObjectWriter::AnyWriter::StartList(StringPiece name) {
263
0
  ++depth_;
264
0
  if (ow_ == nullptr) {
265
    // Save data before the "@type" field for later replay.
266
0
    uninterpreted_events_.push_back(Event(Event::START_LIST, name));
267
0
  } else if (is_well_known_type_ && depth_ == 1) {
268
0
    if (name != "value" && !invalid_) {
269
0
      parent_->InvalidValue("Any",
270
0
                            "Expect a \"value\" field for well-known types.");
271
0
      invalid_ = true;
272
0
    }
273
0
    ow_->StartList("");
274
0
  } else {
275
0
    ow_->StartList(name);
276
0
  }
277
0
}
278
279
0
void ProtoStreamObjectWriter::AnyWriter::EndList() {
280
0
  --depth_;
281
0
  if (depth_ < 0) {
282
0
    GOOGLE_LOG(DFATAL) << "Mismatched EndList found, should not be possible";
283
0
    depth_ = 0;
284
0
  }
285
0
  if (ow_ == nullptr) {
286
    // Save data before the "@type" field for later replay.
287
0
    uninterpreted_events_.push_back(Event(Event::END_LIST));
288
0
  } else {
289
0
    ow_->EndList();
290
0
  }
291
0
}
292
293
void ProtoStreamObjectWriter::AnyWriter::RenderDataPiece(
294
0
    StringPiece name, const DataPiece& value) {
295
  // Start an Any only at depth_ 0. Other RenderDataPiece calls with "@type"
296
  // should go to the contained ow_ as they indicate nested Anys.
297
0
  if (depth_ == 0 && ow_ == nullptr && name == "@type") {
298
0
    StartAny(value);
299
0
  } else if (ow_ == nullptr) {
300
    // Save data before the "@type" field.
301
0
    uninterpreted_events_.push_back(Event(name, value));
302
0
  } else if (depth_ == 0 && is_well_known_type_) {
303
0
    if (name != "value" && !invalid_) {
304
0
      parent_->InvalidValue("Any",
305
0
                            "Expect a \"value\" field for well-known types.");
306
0
      invalid_ = true;
307
0
    }
308
0
    if (well_known_type_render_ == nullptr) {
309
      // Only Any and Struct don't have a special type render but both of
310
      // them expect a JSON object (i.e., a StartObject() call).
311
0
      if (value.type() != DataPiece::TYPE_NULL && !invalid_) {
312
0
        parent_->InvalidValue("Any", "Expect a JSON object.");
313
0
        invalid_ = true;
314
0
      }
315
0
    } else {
316
0
      ow_->ProtoWriter::StartObject("");
317
0
      Status status = (*well_known_type_render_)(ow_.get(), value);
318
0
      if (!status.ok()) ow_->InvalidValue("Any", status.message());
319
0
      ow_->ProtoWriter::EndObject();
320
0
    }
321
0
  } else {
322
0
    ow_->RenderDataPiece(name, value);
323
0
  }
324
0
}
325
326
0
void ProtoStreamObjectWriter::AnyWriter::StartAny(const DataPiece& value) {
327
  // Figure out the type url. This is a copy-paste from WriteString but we also
328
  // need the value, so we can't just call through to that.
329
0
  if (value.type() == DataPiece::TYPE_STRING) {
330
0
    type_url_ = std::string(value.str());
331
0
  } else {
332
0
    util::StatusOr<std::string> s = value.ToString();
333
0
    if (!s.ok()) {
334
0
      parent_->InvalidValue("String", s.status().message());
335
0
      invalid_ = true;
336
0
      return;
337
0
    }
338
0
    type_url_ = s.value();
339
0
  }
340
  // Resolve the type url, and report an error if we failed to resolve it.
341
0
  util::StatusOr<const google::protobuf::Type*> resolved_type =
342
0
      parent_->typeinfo()->ResolveTypeUrl(type_url_);
343
0
  if (!resolved_type.ok()) {
344
0
    parent_->InvalidValue("Any", resolved_type.status().message());
345
0
    invalid_ = true;
346
0
    return;
347
0
  }
348
  // At this point, type is never null.
349
0
  const google::protobuf::Type* type = resolved_type.value();
350
351
0
  well_known_type_render_ = FindTypeRenderer(type_url_);
352
0
  if (well_known_type_render_ != nullptr ||
353
      // Explicitly list Any and Struct here because they don't have a
354
      // custom renderer.
355
0
      type->name() == kAnyType || type->name() == kStructType) {
356
0
    is_well_known_type_ = true;
357
0
  }
358
359
  // Create our object writer and initialize it with the first StartObject
360
  // call.
361
0
  ow_.reset(new ProtoStreamObjectWriter(parent_->typeinfo(), *type, &output_,
362
0
                                        parent_->listener(),
363
0
                                        parent_->options_));
364
365
  // Don't call StartObject() for well-known types yet. Depending on the
366
  // type of actual data, we may not need to call StartObject(). For
367
  // example:
368
  // {
369
  //   "@type": "type.googleapis.com/google.protobuf.Value",
370
  //   "value": [1, 2, 3],
371
  // }
372
  // With the above JSON representation, we will only call StartList() on the
373
  // contained ow_.
374
0
  if (!is_well_known_type_) {
375
0
    ow_->StartObject("");
376
0
  }
377
378
  // Now we know the proto type and can interpret all data fields we gathered
379
  // before the "@type" field.
380
0
  for (int i = 0; i < uninterpreted_events_.size(); ++i) {
381
0
    uninterpreted_events_[i].Replay(this);
382
0
  }
383
0
}
384
385
0
void ProtoStreamObjectWriter::AnyWriter::WriteAny() {
386
0
  if (ow_ == nullptr) {
387
0
    if (uninterpreted_events_.empty()) {
388
      // We never got any content, so just return immediately, which is
389
      // equivalent to writing an empty Any.
390
0
      return;
391
0
    } else {
392
      // There are uninterpreted data, but we never got a "@type" field.
393
0
      if (!invalid_) {
394
0
        parent_->InvalidValue("Any",
395
0
                              StrCat("Missing @type for any field in ",
396
0
                                           parent_->master_type_.name()));
397
0
        invalid_ = true;
398
0
      }
399
0
      return;
400
0
    }
401
0
  }
402
  // Render the type_url and value fields directly to the stream.
403
  // type_url has tag 1 and value has tag 2.
404
0
  WireFormatLite::WriteString(1, type_url_, parent_->stream());
405
0
  if (!data_.empty()) {
406
0
    WireFormatLite::WriteBytes(2, data_, parent_->stream());
407
0
  }
408
0
}
409
410
void ProtoStreamObjectWriter::AnyWriter::Event::Replay(
411
0
    AnyWriter* writer) const {
412
0
  switch (type_) {
413
0
    case START_OBJECT:
414
0
      writer->StartObject(name_);
415
0
      break;
416
0
    case END_OBJECT:
417
0
      writer->EndObject();
418
0
      break;
419
0
    case START_LIST:
420
0
      writer->StartList(name_);
421
0
      break;
422
0
    case END_LIST:
423
0
      writer->EndList();
424
0
      break;
425
0
    case RENDER_DATA_PIECE:
426
0
      writer->RenderDataPiece(name_, value_);
427
0
      break;
428
0
  }
429
0
}
430
431
0
void ProtoStreamObjectWriter::AnyWriter::Event::DeepCopy() {
432
  // DataPiece only contains a string reference. To make sure the referenced
433
  // string value stays valid, we make a copy of the string value and update
434
  // DataPiece to reference our own copy.
435
0
  if (value_.type() == DataPiece::TYPE_STRING) {
436
0
    StrAppend(&value_storage_, value_.str());
437
0
    value_ = DataPiece(value_storage_, value_.use_strict_base64_decoding());
438
0
  } else if (value_.type() == DataPiece::TYPE_BYTES) {
439
0
    value_storage_ = value_.ToBytes().value();
440
0
    value_ =
441
0
        DataPiece(value_storage_, true, value_.use_strict_base64_decoding());
442
0
  }
443
0
}
444
445
ProtoStreamObjectWriter::Item::Item(ProtoStreamObjectWriter* enclosing,
446
                                    ItemType item_type, bool is_placeholder,
447
                                    bool is_list)
448
    : BaseElement(nullptr),
449
      ow_(enclosing),
450
      any_(),
451
      item_type_(item_type),
452
      is_placeholder_(is_placeholder),
453
15.5k
      is_list_(is_list) {
454
15.5k
  if (item_type_ == ANY) {
455
0
    any_.reset(new AnyWriter(ow_));
456
0
  }
457
15.5k
  if (item_type == MAP) {
458
0
    map_keys_.reset(new std::unordered_set<std::string>);
459
0
  }
460
15.5k
}
461
462
ProtoStreamObjectWriter::Item::Item(ProtoStreamObjectWriter::Item* parent,
463
                                    ItemType item_type, bool is_placeholder,
464
                                    bool is_list)
465
    : BaseElement(parent),
466
      ow_(this->parent()->ow_),
467
      any_(),
468
      item_type_(item_type),
469
      is_placeholder_(is_placeholder),
470
41.0M
      is_list_(is_list) {
471
41.0M
  if (item_type == ANY) {
472
0
    any_.reset(new AnyWriter(ow_));
473
0
  }
474
41.0M
  if (item_type == MAP) {
475
146k
    map_keys_.reset(new std::unordered_set<std::string>);
476
146k
  }
477
41.0M
}
478
479
bool ProtoStreamObjectWriter::Item::InsertMapKeyIfNotPresent(
480
392k
    StringPiece map_key) {
481
392k
  return InsertIfNotPresent(map_keys_.get(), std::string(map_key));
482
392k
}
483
484
485
ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartObject(
486
147k
    StringPiece name) {
487
147k
  if (invalid_depth() > 0) {
488
714
    IncrementInvalidDepth();
489
714
    return this;
490
714
  }
491
492
  // Starting the root message. Create the root Item and return.
493
  // ANY message type does not need special handling, just set the ItemType
494
  // to ANY.
495
147k
  if (current_ == nullptr) {
496
15.2k
    ProtoWriter::StartObject(name);
497
15.2k
    current_.reset(new Item(
498
15.2k
        this, master_type_.name() == kAnyType ? Item::ANY : Item::MESSAGE,
499
15.2k
        false, false));
500
501
    // If master type is a special type that needs extra values to be written to
502
    // stream, we write those values.
503
15.2k
    if (master_type_.name() == kStructType) {
504
      // Struct has a map<string, Value> field called "fields".
505
      // https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/struct.proto
506
      // "fields": [
507
15.2k
      Push("fields", Item::MAP, true, true);
508
15.2k
      return this;
509
15.2k
    }
510
511
0
    if (master_type_.name() == kStructValueType) {
512
      // We got a StartObject call with google.protobuf.Value field. The only
513
      // object within that type is a struct type. So start a struct.
514
      //
515
      // The struct field in Value type is named "struct_value"
516
      // https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/struct.proto
517
      // Also start the map field "fields" within the struct.
518
      // "struct_value": {
519
      //   "fields": [
520
0
      Push("struct_value", Item::MESSAGE, true, false);
521
0
      Push("fields", Item::MAP, true, true);
522
0
      return this;
523
0
    }
524
525
0
    if (master_type_.name() == kStructListValueType) {
526
0
      InvalidValue(kStructListValueType,
527
0
                   "Cannot start root message with ListValue.");
528
0
    }
529
530
0
    return this;
531
0
  }
532
533
  // Send all ANY events to AnyWriter.
534
131k
  if (current_->IsAny()) {
535
0
    current_->any()->StartObject(name);
536
0
    return this;
537
0
  }
538
539
  // If we are within a map, we render name as keys and send StartObject to the
540
  // value field.
541
131k
  if (current_->IsMap()) {
542
5.08k
    if (!ValidMapKey(name)) {
543
446
      IncrementInvalidDepth();
544
446
      return this;
545
446
    }
546
547
    // Map is a repeated field of message type with a "key" and a "value" field.
548
    // https://developers.google.com/protocol-buffers/docs/proto3?hl=en#maps
549
    // message MapFieldEntry {
550
    //   key_type key = 1;
551
    //   value_type value = 2;
552
    // }
553
    //
554
    // repeated MapFieldEntry map_field = N;
555
    //
556
    // That means, we render the following element within a list (hence no
557
    // name):
558
    // { "key": "<name>", "value": {
559
4.64k
    Push("", Item::MESSAGE, false, false);
560
4.64k
    ProtoWriter::RenderDataPiece("key",
561
4.64k
                                 DataPiece(name, use_strict_base64_decoding()));
562
4.64k
    Push("value", IsAny(*Lookup("value")) ? Item::ANY : Item::MESSAGE, true,
563
4.64k
         false);
564
565
    // Make sure we are valid so far after starting map fields.
566
4.64k
    if (invalid_depth() > 0) return this;
567
568
    // If top of stack is g.p.Struct type, start the struct the map field within
569
    // it.
570
4.64k
    if (element() != nullptr && IsStruct(*element()->parent_field())) {
571
      // Render "fields": [
572
0
      Push("fields", Item::MAP, true, true);
573
0
      return this;
574
0
    }
575
576
    // If top of stack is g.p.Value type, start the Struct within it.
577
4.64k
    if (element() != nullptr && IsStructValue(*element()->parent_field())) {
578
      // Render
579
      // "struct_value": {
580
      //   "fields": [
581
4.64k
      Push("struct_value", Item::MESSAGE, true, false);
582
4.64k
      Push("fields", Item::MAP, true, true);
583
4.64k
    }
584
4.64k
    return this;
585
4.64k
  }
586
587
126k
  const google::protobuf::Field* field = BeginNamed(name, false);
588
589
126k
  if (field == nullptr) return this;
590
591
  // Legacy JSON map is a list of key value pairs. Starts a map entry object.
592
126k
  if (options_.use_legacy_json_map_format && name.empty()) {
593
0
    Push(name, IsAny(*field) ? Item::ANY : Item::MESSAGE, false, false);
594
0
    return this;
595
0
  }
596
597
126k
  if (IsMap(*field)) {
598
    // Begin a map. A map is triggered by a StartObject() call if the current
599
    // field has a map type.
600
    // A map type is always repeated, hence set is_list to true.
601
    // Render
602
    // "<name>": [
603
0
    Push(name, Item::MAP, false, true);
604
0
    return this;
605
0
  }
606
607
126k
  if (options_.disable_implicit_message_list) {
608
    // If the incoming object is repeated, the top-level object on stack should
609
    // be list. Report an error otherwise.
610
0
    if (IsRepeated(*field) && !current_->is_list()) {
611
0
      IncrementInvalidDepth();
612
613
0
      if (!options_.suppress_implicit_message_list_error) {
614
0
        InvalidValue(
615
0
            field->name(),
616
0
            "Starting an object in a repeated field but the parent object "
617
0
            "is not a list");
618
0
      }
619
0
      return this;
620
0
    }
621
0
  }
622
623
126k
  if (IsStruct(*field)) {
624
    // Start a struct object.
625
    // Render
626
    // "<name>": {
627
    //   "fields": {
628
0
    Push(name, Item::MESSAGE, false, false);
629
0
    Push("fields", Item::MAP, true, true);
630
0
    return this;
631
0
  }
632
633
126k
  if (IsStructValue(*field)) {
634
    // We got a StartObject call with google.protobuf.Value field.  The only
635
    // object within that type is a struct type. So start a struct.
636
    // Render
637
    // "<name>": {
638
    //   "struct_value": {
639
    //     "fields": {
640
126k
    Push(name, Item::MESSAGE, false, false);
641
126k
    Push("struct_value", Item::MESSAGE, true, false);
642
126k
    Push("fields", Item::MAP, true, true);
643
126k
    return this;
644
126k
  }
645
646
0
  if (field->kind() != google::protobuf::Field::TYPE_GROUP &&
647
0
      field->kind() != google::protobuf::Field::TYPE_MESSAGE) {
648
0
    IncrementInvalidDepth();
649
0
    if (!options_.suppress_object_to_scalar_error) {
650
0
      InvalidValue(field->name(), "Starting an object on a scalar field");
651
0
    }
652
653
0
    return this;
654
0
  }
655
656
  // A regular message type. Pass it directly to ProtoWriter.
657
  // Render
658
  // "<name>": {
659
0
  Push(name, IsAny(*field) ? Item::ANY : Item::MESSAGE, false, false);
660
0
  return this;
661
0
}
662
663
139k
ProtoStreamObjectWriter* ProtoStreamObjectWriter::EndObject() {
664
139k
  if (invalid_depth() > 0) {
665
817
    DecrementInvalidDepth();
666
817
    return this;
667
817
  }
668
669
138k
  if (current_ == nullptr) return this;
670
671
138k
  if (current_->IsAny()) {
672
0
    if (current_->any()->EndObject()) return this;
673
0
  }
674
675
138k
  Pop();
676
677
138k
  return this;
678
138k
}
679
680
681
ProtoStreamObjectWriter* ProtoStreamObjectWriter::StartList(
682
11.3M
    StringPiece name) {
683
11.3M
  if (invalid_depth() > 0) {
684
4.60M
    IncrementInvalidDepth();
685
4.60M
    return this;
686
4.60M
  }
687
688
  // Since we cannot have a top-level repeated item in protobuf, the only way
689
  // this is valid is if we start a special type google.protobuf.ListValue or
690
  // google.protobuf.Value.
691
6.78M
  if (current_ == nullptr) {
692
339
    if (!name.empty()) {
693
0
      InvalidName(name, "Root element should not be named.");
694
0
      IncrementInvalidDepth();
695
0
      return this;
696
0
    }
697
698
    // If master type is a special type that needs extra values to be written to
699
    // stream, we write those values.
700
339
    if (master_type_.name() == kStructValueType) {
701
      // We got a StartList with google.protobuf.Value master type. This means
702
      // we have to start the "list_value" within google.protobuf.Value.
703
      //
704
      // See
705
      // https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/struct.proto
706
      //
707
      // Render
708
      // "<name>": {
709
      //   "list_value": {
710
      //     "values": [  // Start this list.
711
0
      ProtoWriter::StartObject(name);
712
0
      current_.reset(new Item(this, Item::MESSAGE, false, false));
713
0
      Push("list_value", Item::MESSAGE, true, false);
714
0
      Push("values", Item::MESSAGE, true, true);
715
0
      return this;
716
0
    }
717
718
339
    if (master_type_.name() == kStructListValueType) {
719
      // We got a StartList with google.protobuf.ListValue master type. This
720
      // means we have to start the "values" within google.protobuf.ListValue.
721
      //
722
      // Render
723
      // "<name>": {
724
      //   "values": [  // Start this list.
725
0
      ProtoWriter::StartObject(name);
726
0
      current_.reset(new Item(this, Item::MESSAGE, false, false));
727
0
      Push("values", Item::MESSAGE, true, true);
728
0
      return this;
729
0
    }
730
731
    // Send the event to ProtoWriter so proper errors can be reported.
732
    //
733
    // Render a regular list:
734
    // "<name>": [
735
339
    ProtoWriter::StartList(name);
736
339
    current_.reset(new Item(this, Item::MESSAGE, false, true));
737
339
    return this;
738
339
  }
739
740
6.78M
  if (current_->IsAny()) {
741
0
    current_->any()->StartList(name);
742
0
    return this;
743
0
  }
744
745
  // If the top of stack is a map, we are starting a list value within a map.
746
  // Since map does not allow repeated values, this can only happen when the map
747
  // value is of a special type that renders a list in JSON.  These can be one
748
  // of 3 cases:
749
  // i. We are rendering a list value within google.protobuf.Struct
750
  // ii. We are rendering a list value within google.protobuf.Value
751
  // iii. We are rendering a list value with type google.protobuf.ListValue.
752
6.78M
  if (current_->IsMap()) {
753
15.2k
    if (!ValidMapKey(name)) {
754
497
      IncrementInvalidDepth();
755
497
      return this;
756
497
    }
757
758
    // Start the repeated map entry object.
759
    // Render
760
    // { "key": "<name>", "value": {
761
14.7k
    Push("", Item::MESSAGE, false, false);
762
14.7k
    ProtoWriter::RenderDataPiece("key",
763
14.7k
                                 DataPiece(name, use_strict_base64_decoding()));
764
14.7k
    Push("value", Item::MESSAGE, true, false);
765
766
    // Make sure we are valid after pushing all above items.
767
14.7k
    if (invalid_depth() > 0) return this;
768
769
    // case i and ii above. Start "list_value" field within g.p.Value
770
14.7k
    if (element() != nullptr && element()->parent_field() != nullptr) {
771
      // Render
772
      // "list_value": {
773
      //   "values": [  // Start this list
774
14.7k
      if (IsStructValue(*element()->parent_field())) {
775
14.7k
        Push("list_value", Item::MESSAGE, true, false);
776
14.7k
        Push("values", Item::MESSAGE, true, true);
777
14.7k
        return this;
778
14.7k
      }
779
780
      // Render
781
      // "values": [
782
0
      if (IsStructListValue(*element()->parent_field())) {
783
        // case iii above. Bind directly to g.p.ListValue
784
0
        Push("values", Item::MESSAGE, true, true);
785
0
        return this;
786
0
      }
787
0
    }
788
789
    // Report an error.
790
0
    InvalidValue("Map", StrCat("Cannot have repeated items ('", name,
791
0
                                     "') within a map."));
792
0
    return this;
793
14.7k
  }
794
795
  // When name is empty and stack is not empty, we are rendering an item within
796
  // a list.
797
6.77M
  if (name.empty()) {
798
6.77M
    if (element() != nullptr && element()->parent_field() != nullptr) {
799
6.77M
      if (IsStructValue(*element()->parent_field())) {
800
        // Since it is g.p.Value, we bind directly to the list_value.
801
        // Render
802
        // {  // g.p.Value item within the list
803
        //   "list_value": {
804
        //     "values": [
805
6.77M
        Push("", Item::MESSAGE, false, false);
806
6.77M
        Push("list_value", Item::MESSAGE, true, false);
807
6.77M
        Push("values", Item::MESSAGE, true, true);
808
6.77M
        return this;
809
6.77M
      }
810
811
0
      if (IsStructListValue(*element()->parent_field())) {
812
        // Since it is g.p.ListValue, we bind to it directly.
813
        // Render
814
        // {  // g.p.ListValue item within the list
815
        //   "values": [
816
0
        Push("", Item::MESSAGE, false, false);
817
0
        Push("values", Item::MESSAGE, true, true);
818
0
        return this;
819
0
      }
820
0
    }
821
822
    // Pass the event to underlying ProtoWriter.
823
0
    Push(name, Item::MESSAGE, false, true);
824
0
    return this;
825
6.77M
  }
826
827
  // name is not empty
828
0
  const google::protobuf::Field* field = Lookup(name);
829
830
0
  if (field == nullptr) {
831
0
    IncrementInvalidDepth();
832
0
    return this;
833
0
  }
834
835
0
  if (IsStructValue(*field)) {
836
    // If g.p.Value is repeated, start that list. Otherwise, start the
837
    // "list_value" within it.
838
0
    if (IsRepeated(*field)) {
839
      // Render it just like a regular repeated field.
840
      // "<name>": [
841
0
      Push(name, Item::MESSAGE, false, true);
842
0
      return this;
843
0
    }
844
845
    // Start the "list_value" field.
846
    // Render
847
    // "<name>": {
848
    //   "list_value": {
849
    //     "values": [
850
0
    Push(name, Item::MESSAGE, false, false);
851
0
    Push("list_value", Item::MESSAGE, true, false);
852
0
    Push("values", Item::MESSAGE, true, true);
853
0
    return this;
854
0
  }
855
856
0
  if (IsStructListValue(*field)) {
857
    // If g.p.ListValue is repeated, start that list. Otherwise, start the
858
    // "values" within it.
859
0
    if (IsRepeated(*field)) {
860
      // Render it just like a regular repeated field.
861
      // "<name>": [
862
0
      Push(name, Item::MESSAGE, false, true);
863
0
      return this;
864
0
    }
865
866
    // Start the "values" field within g.p.ListValue.
867
    // Render
868
    // "<name>": {
869
    //   "values": [
870
0
    Push(name, Item::MESSAGE, false, false);
871
0
    Push("values", Item::MESSAGE, true, true);
872
0
    return this;
873
0
  }
874
875
  // If we are here, the field should be repeated. Report an error otherwise.
876
0
  if (!IsRepeated(*field)) {
877
0
    IncrementInvalidDepth();
878
0
    InvalidName(name, "Proto field is not repeating, cannot start list.");
879
0
    return this;
880
0
  }
881
882
0
  if (IsMap(*field)) {
883
0
    if (options_.use_legacy_json_map_format) {
884
0
      Push(name, Item::MESSAGE, false, true);
885
0
      return this;
886
0
    }
887
0
    InvalidValue("Map", StrCat("Cannot bind a list to map for field '",
888
0
                                     name, "'."));
889
0
    IncrementInvalidDepth();
890
0
    return this;
891
0
  }
892
893
  // Pass the event to ProtoWriter.
894
  // Render
895
  // "<name>": [
896
0
  Push(name, Item::MESSAGE, false, true);
897
0
  return this;
898
0
}
899
900
114k
ProtoStreamObjectWriter* ProtoStreamObjectWriter::EndList() {
901
114k
  if (invalid_depth() > 0) {
902
82.8k
    DecrementInvalidDepth();
903
82.8k
    return this;
904
82.8k
  }
905
906
31.2k
  if (current_ == nullptr) return this;
907
908
31.2k
  if (current_->IsAny()) {
909
0
    current_->any()->EndList();
910
0
    return this;
911
0
  }
912
913
31.2k
  Pop();
914
31.2k
  return this;
915
31.2k
}
916
917
Status ProtoStreamObjectWriter::RenderStructValue(ProtoStreamObjectWriter* ow,
918
19.9M
                                                  const DataPiece& data) {
919
19.9M
  std::string struct_field_name;
920
19.9M
  switch (data.type()) {
921
0
    case DataPiece::TYPE_INT32: {
922
0
      if (ow->options_.struct_integers_as_strings) {
923
0
        util::StatusOr<int32_t> int_value = data.ToInt32();
924
0
        if (int_value.ok()) {
925
0
          ow->ProtoWriter::RenderDataPiece(
926
0
              "string_value",
927
0
              DataPiece(SimpleDtoa(int_value.value()), true));
928
0
          return Status();
929
0
        }
930
0
      }
931
0
      struct_field_name = "number_value";
932
0
      break;
933
0
    }
934
0
    case DataPiece::TYPE_UINT32: {
935
0
      if (ow->options_.struct_integers_as_strings) {
936
0
        util::StatusOr<uint32_t> int_value = data.ToUint32();
937
0
        if (int_value.ok()) {
938
0
          ow->ProtoWriter::RenderDataPiece(
939
0
              "string_value",
940
0
              DataPiece(SimpleDtoa(int_value.value()), true));
941
0
          return Status();
942
0
        }
943
0
      }
944
0
      struct_field_name = "number_value";
945
0
      break;
946
0
    }
947
2.08k
    case DataPiece::TYPE_INT64: {
948
      // If the option to treat integers as strings is set, then render them as
949
      // strings. Otherwise, fallback to rendering them as double.
950
2.08k
      if (ow->options_.struct_integers_as_strings) {
951
0
        util::StatusOr<int64_t> int_value = data.ToInt64();
952
0
        if (int_value.ok()) {
953
0
          ow->ProtoWriter::RenderDataPiece(
954
0
              "string_value", DataPiece(StrCat(int_value.value()), true));
955
0
          return Status();
956
0
        }
957
0
      }
958
2.08k
      struct_field_name = "number_value";
959
2.08k
      break;
960
2.08k
    }
961
19.3M
    case DataPiece::TYPE_UINT64: {
962
      // If the option to treat integers as strings is set, then render them as
963
      // strings. Otherwise, fallback to rendering them as double.
964
19.3M
      if (ow->options_.struct_integers_as_strings) {
965
0
        util::StatusOr<uint64_t> int_value = data.ToUint64();
966
0
        if (int_value.ok()) {
967
0
          ow->ProtoWriter::RenderDataPiece(
968
0
              "string_value", DataPiece(StrCat(int_value.value()), true));
969
0
          return Status();
970
0
        }
971
0
      }
972
19.3M
      struct_field_name = "number_value";
973
19.3M
      break;
974
19.3M
    }
975
0
    case DataPiece::TYPE_FLOAT: {
976
0
      if (ow->options_.struct_integers_as_strings) {
977
0
        util::StatusOr<float> float_value = data.ToFloat();
978
0
        if (float_value.ok()) {
979
0
          ow->ProtoWriter::RenderDataPiece(
980
0
              "string_value",
981
0
              DataPiece(SimpleDtoa(float_value.value()), true));
982
0
          return Status();
983
0
        }
984
0
      }
985
0
      struct_field_name = "number_value";
986
0
      break;
987
0
    }
988
90.9k
    case DataPiece::TYPE_DOUBLE: {
989
90.9k
      if (ow->options_.struct_integers_as_strings) {
990
0
        util::StatusOr<double> double_value = data.ToDouble();
991
0
        if (double_value.ok()) {
992
0
          ow->ProtoWriter::RenderDataPiece(
993
0
              "string_value",
994
0
              DataPiece(SimpleDtoa(double_value.value()), true));
995
0
          return Status();
996
0
        }
997
0
      }
998
90.9k
      struct_field_name = "number_value";
999
90.9k
      break;
1000
90.9k
    }
1001
355k
    case DataPiece::TYPE_STRING: {
1002
355k
      struct_field_name = "string_value";
1003
355k
      break;
1004
90.9k
    }
1005
3.60k
    case DataPiece::TYPE_BOOL: {
1006
3.60k
      struct_field_name = "bool_value";
1007
3.60k
      break;
1008
90.9k
    }
1009
143k
    case DataPiece::TYPE_NULL: {
1010
143k
      struct_field_name = "null_value";
1011
143k
      break;
1012
90.9k
    }
1013
0
    default: {
1014
0
      return util::InvalidArgumentError(
1015
0
          "Invalid struct data type. Only number, string, boolean or  null "
1016
0
          "values are supported.");
1017
90.9k
    }
1018
19.9M
  }
1019
19.9M
  ow->ProtoWriter::RenderDataPiece(struct_field_name, data);
1020
19.9M
  return Status();
1021
19.9M
}
1022
1023
Status ProtoStreamObjectWriter::RenderTimestamp(ProtoStreamObjectWriter* ow,
1024
0
                                                const DataPiece& data) {
1025
0
  if (data.type() == DataPiece::TYPE_NULL) return Status();
1026
0
  if (data.type() != DataPiece::TYPE_STRING) {
1027
0
    return util::InvalidArgumentError(
1028
0
        StrCat("Invalid data type for timestamp, value is ",
1029
0
                     data.ValueAsStringOrDefault("")));
1030
0
  }
1031
1032
0
  StringPiece value(data.str());
1033
1034
0
  int64 seconds;
1035
0
  int32 nanos;
1036
0
  if (!::google::protobuf::internal::ParseTime(value.ToString(), &seconds,
1037
0
                                               &nanos)) {
1038
0
    return util::InvalidArgumentError(StrCat("Invalid time format: ", value));
1039
0
  }
1040
1041
1042
0
  ow->ProtoWriter::RenderDataPiece("seconds", DataPiece(seconds));
1043
0
  ow->ProtoWriter::RenderDataPiece("nanos", DataPiece(nanos));
1044
0
  return Status();
1045
0
}
1046
1047
static inline util::Status RenderOneFieldPath(ProtoStreamObjectWriter* ow,
1048
0
                                              StringPiece path) {
1049
0
  ow->ProtoWriter::RenderDataPiece(
1050
0
      "paths", DataPiece(ConvertFieldMaskPath(path, &ToSnakeCase), true));
1051
0
  return Status();
1052
0
}
1053
1054
Status ProtoStreamObjectWriter::RenderFieldMask(ProtoStreamObjectWriter* ow,
1055
0
                                                const DataPiece& data) {
1056
0
  if (data.type() == DataPiece::TYPE_NULL) return Status();
1057
0
  if (data.type() != DataPiece::TYPE_STRING) {
1058
0
    return util::InvalidArgumentError(
1059
0
        StrCat("Invalid data type for field mask, value is ",
1060
0
                     data.ValueAsStringOrDefault("")));
1061
0
  }
1062
1063
  // TODO(tsun): figure out how to do proto descriptor based snake case
1064
  // conversions as much as possible. Because ToSnakeCase sometimes returns the
1065
  // wrong value.
1066
0
  return DecodeCompactFieldMaskPaths(data.str(),
1067
0
                                     std::bind(&RenderOneFieldPath, ow, _1));
1068
0
}
1069
1070
Status ProtoStreamObjectWriter::RenderDuration(ProtoStreamObjectWriter* ow,
1071
0
                                               const DataPiece& data) {
1072
0
  if (data.type() == DataPiece::TYPE_NULL) return Status();
1073
0
  if (data.type() != DataPiece::TYPE_STRING) {
1074
0
    return util::InvalidArgumentError(
1075
0
        StrCat("Invalid data type for duration, value is ",
1076
0
                     data.ValueAsStringOrDefault("")));
1077
0
  }
1078
1079
0
  StringPiece value(data.str());
1080
1081
0
  if (!HasSuffixString(value, "s")) {
1082
0
    return util::InvalidArgumentError(
1083
0
        "Illegal duration format; duration must end with 's'");
1084
0
  }
1085
0
  value = value.substr(0, value.size() - 1);
1086
0
  int sign = 1;
1087
0
  if (HasPrefixString(value, "-")) {
1088
0
    sign = -1;
1089
0
    value = value.substr(1);
1090
0
  }
1091
1092
0
  StringPiece s_secs, s_nanos;
1093
0
  SplitSecondsAndNanos(value, &s_secs, &s_nanos);
1094
0
  uint64_t unsigned_seconds;
1095
0
  if (!safe_strtou64(s_secs, &unsigned_seconds)) {
1096
0
    return util::InvalidArgumentError(
1097
0
        "Invalid duration format, failed to parse seconds");
1098
0
  }
1099
1100
0
  int32_t nanos = 0;
1101
0
  Status nanos_status = GetNanosFromStringPiece(
1102
0
      s_nanos, "Invalid duration format, failed to parse nano seconds",
1103
0
      "Duration value exceeds limits", &nanos);
1104
0
  if (!nanos_status.ok()) {
1105
0
    return nanos_status;
1106
0
  }
1107
0
  nanos = sign * nanos;
1108
1109
0
  int64_t seconds = sign * unsigned_seconds;
1110
0
  if (seconds > kDurationMaxSeconds || seconds < kDurationMinSeconds ||
1111
0
      nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
1112
0
    return util::InvalidArgumentError("Duration value exceeds limits");
1113
0
  }
1114
1115
0
  ow->ProtoWriter::RenderDataPiece("seconds", DataPiece(seconds));
1116
0
  ow->ProtoWriter::RenderDataPiece("nanos", DataPiece(nanos));
1117
0
  return Status();
1118
0
}
1119
1120
Status ProtoStreamObjectWriter::RenderWrapperType(ProtoStreamObjectWriter* ow,
1121
0
                                                  const DataPiece& data) {
1122
0
  if (data.type() == DataPiece::TYPE_NULL) return Status();
1123
0
  ow->ProtoWriter::RenderDataPiece("value", data);
1124
0
  return Status();
1125
0
}
1126
1127
ProtoStreamObjectWriter* ProtoStreamObjectWriter::RenderDataPiece(
1128
20.0M
    StringPiece name, const DataPiece& data) {
1129
20.0M
  Status status;
1130
20.0M
  if (invalid_depth() > 0) return this;
1131
1132
19.9M
  if (current_ == nullptr) {
1133
539
    const TypeRenderer* type_renderer =
1134
539
        FindTypeRenderer(GetFullTypeWithUrl(master_type_.name()));
1135
539
    if (type_renderer == nullptr) {
1136
539
      InvalidName(name, "Root element must be a message.");
1137
539
      return this;
1138
539
    }
1139
    // Render the special type.
1140
    // "<name>": {
1141
    //   ... Render special type ...
1142
    // }
1143
0
    ProtoWriter::StartObject(name);
1144
0
    status = (*type_renderer)(this, data);
1145
0
    if (!status.ok()) {
1146
0
      InvalidValue(master_type_.name(),
1147
0
                   StrCat("Field '", name, "', ", status.message()));
1148
0
    }
1149
0
    ProtoWriter::EndObject();
1150
0
    return this;
1151
539
  }
1152
1153
19.9M
  if (current_->IsAny()) {
1154
0
    current_->any()->RenderDataPiece(name, data);
1155
0
    return this;
1156
0
  }
1157
1158
19.9M
  const google::protobuf::Field* field = nullptr;
1159
19.9M
  if (current_->IsMap()) {
1160
372k
    if (!ValidMapKey(name)) return this;
1161
1162
364k
    field = Lookup("value");
1163
364k
    if (field == nullptr) {
1164
0
      GOOGLE_LOG(DFATAL) << "Map does not have a value field.";
1165
0
      return this;
1166
0
    }
1167
1168
364k
    if (options_.ignore_null_value_map_entry) {
1169
      // If we are rendering explicit null values and the backend proto field is
1170
      // not of the google.protobuf.NullType type, interpret null as absence.
1171
0
      if (data.type() == DataPiece::TYPE_NULL &&
1172
0
          field->type_url() != kStructNullValueTypeUrl) {
1173
0
        return this;
1174
0
      }
1175
0
    }
1176
1177
    // Render an item in repeated map list.
1178
    // { "key": "<name>", "value":
1179
364k
    Push("", Item::MESSAGE, false, false);
1180
364k
    ProtoWriter::RenderDataPiece("key",
1181
364k
                                 DataPiece(name, use_strict_base64_decoding()));
1182
1183
364k
    const TypeRenderer* type_renderer = FindTypeRenderer(field->type_url());
1184
364k
    if (type_renderer != nullptr) {
1185
      // Map's value type is a special type. Render it like a message:
1186
      // "value": {
1187
      //   ... Render special type ...
1188
      // }
1189
364k
      Push("value", Item::MESSAGE, true, false);
1190
364k
      status = (*type_renderer)(this, data);
1191
364k
      if (!status.ok()) {
1192
0
        InvalidValue(field->type_url(),
1193
0
                     StrCat("Field '", name, "', ", status.message()));
1194
0
      }
1195
364k
      Pop();
1196
364k
      return this;
1197
364k
    }
1198
1199
    // If we are rendering explicit null values and the backend proto field is
1200
    // not of the google.protobuf.NullType type, we do nothing.
1201
0
    if (data.type() == DataPiece::TYPE_NULL &&
1202
0
        field->type_url() != kStructNullValueTypeUrl) {
1203
0
      Pop();
1204
0
      return this;
1205
0
    }
1206
1207
    // Render the map value as a primitive type.
1208
0
    ProtoWriter::RenderDataPiece("value", data);
1209
0
    Pop();
1210
0
    return this;
1211
0
  }
1212
1213
19.5M
  field = Lookup(name);
1214
19.5M
  if (field == nullptr) return this;
1215
1216
  // Check if the field is of special type. Render it accordingly if so.
1217
19.5M
  const TypeRenderer* type_renderer = FindTypeRenderer(field->type_url());
1218
19.5M
  if (type_renderer != nullptr) {
1219
    // Pass through null value only for google.protobuf.Value. For other
1220
    // types we ignore null value just like for regular field types.
1221
19.5M
    if (data.type() != DataPiece::TYPE_NULL ||
1222
19.5M
        field->type_url() == kStructValueTypeUrl) {
1223
19.5M
      Push(name, Item::MESSAGE, false, false);
1224
19.5M
      status = (*type_renderer)(this, data);
1225
19.5M
      if (!status.ok()) {
1226
0
        InvalidValue(field->type_url(),
1227
0
                     StrCat("Field '", name, "', ", status.message()));
1228
0
      }
1229
19.5M
      Pop();
1230
19.5M
    }
1231
19.5M
    return this;
1232
19.5M
  }
1233
1234
  // If we are rendering explicit null values and the backend proto field is
1235
  // not of the google.protobuf.NullType type, we do nothing.
1236
0
  if (data.type() == DataPiece::TYPE_NULL &&
1237
0
      field->type_url() != kStructNullValueTypeUrl) {
1238
0
    return this;
1239
0
  }
1240
1241
0
  ProtoWriter::RenderDataPiece(name, data);
1242
0
  return this;
1243
0
}
1244
1245
// Map of functions that are responsible for rendering well known type
1246
// represented by the key.
1247
std::unordered_map<std::string, ProtoStreamObjectWriter::TypeRenderer>*
1248
    ProtoStreamObjectWriter::renderers_ = nullptr;
1249
PROTOBUF_NAMESPACE_ID::internal::once_flag writer_renderers_init_;
1250
1251
1
void ProtoStreamObjectWriter::InitRendererMap() {
1252
1
  renderers_ = new std::unordered_map<std::string,
1253
1
                                      ProtoStreamObjectWriter::TypeRenderer>();
1254
1
  (*renderers_)["type.googleapis.com/google.protobuf.Timestamp"] =
1255
1
      &ProtoStreamObjectWriter::RenderTimestamp;
1256
1
  (*renderers_)["type.googleapis.com/google.protobuf.Duration"] =
1257
1
      &ProtoStreamObjectWriter::RenderDuration;
1258
1
  (*renderers_)["type.googleapis.com/google.protobuf.FieldMask"] =
1259
1
      &ProtoStreamObjectWriter::RenderFieldMask;
1260
1
  (*renderers_)["type.googleapis.com/google.protobuf.Double"] =
1261
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1262
1
  (*renderers_)["type.googleapis.com/google.protobuf.Float"] =
1263
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1264
1
  (*renderers_)["type.googleapis.com/google.protobuf.Int64"] =
1265
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1266
1
  (*renderers_)["type.googleapis.com/google.protobuf.UInt64"] =
1267
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1268
1
  (*renderers_)["type.googleapis.com/google.protobuf.Int32"] =
1269
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1270
1
  (*renderers_)["type.googleapis.com/google.protobuf.UInt32"] =
1271
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1272
1
  (*renderers_)["type.googleapis.com/google.protobuf.Bool"] =
1273
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1274
1
  (*renderers_)["type.googleapis.com/google.protobuf.String"] =
1275
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1276
1
  (*renderers_)["type.googleapis.com/google.protobuf.Bytes"] =
1277
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1278
1
  (*renderers_)["type.googleapis.com/google.protobuf.DoubleValue"] =
1279
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1280
1
  (*renderers_)["type.googleapis.com/google.protobuf.FloatValue"] =
1281
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1282
1
  (*renderers_)["type.googleapis.com/google.protobuf.Int64Value"] =
1283
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1284
1
  (*renderers_)["type.googleapis.com/google.protobuf.UInt64Value"] =
1285
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1286
1
  (*renderers_)["type.googleapis.com/google.protobuf.Int32Value"] =
1287
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1288
1
  (*renderers_)["type.googleapis.com/google.protobuf.UInt32Value"] =
1289
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1290
1
  (*renderers_)["type.googleapis.com/google.protobuf.BoolValue"] =
1291
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1292
1
  (*renderers_)["type.googleapis.com/google.protobuf.StringValue"] =
1293
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1294
1
  (*renderers_)["type.googleapis.com/google.protobuf.BytesValue"] =
1295
1
      &ProtoStreamObjectWriter::RenderWrapperType;
1296
1
  (*renderers_)["type.googleapis.com/google.protobuf.Value"] =
1297
1
      &ProtoStreamObjectWriter::RenderStructValue;
1298
1
  ::google::protobuf::internal::OnShutdown(&DeleteRendererMap);
1299
1
}
1300
1301
0
void ProtoStreamObjectWriter::DeleteRendererMap() {
1302
0
  delete ProtoStreamObjectWriter::renderers_;
1303
0
  renderers_ = nullptr;
1304
0
}
1305
1306
ProtoStreamObjectWriter::TypeRenderer*
1307
19.9M
ProtoStreamObjectWriter::FindTypeRenderer(const std::string& type_url) {
1308
19.9M
  PROTOBUF_NAMESPACE_ID::internal::call_once(writer_renderers_init_,
1309
19.9M
                                             InitRendererMap);
1310
19.9M
  return FindOrNull(*renderers_, type_url);
1311
19.9M
}
1312
1313
392k
bool ProtoStreamObjectWriter::ValidMapKey(StringPiece unnormalized_name) {
1314
392k
  if (current_ == nullptr) return true;
1315
1316
392k
  if (!current_->InsertMapKeyIfNotPresent(unnormalized_name)) {
1317
8.79k
    listener()->InvalidName(
1318
8.79k
        location(), unnormalized_name,
1319
8.79k
        StrCat("Repeated map key: '", unnormalized_name,
1320
8.79k
                     "' is already set."));
1321
8.79k
    return false;
1322
8.79k
  }
1323
1324
383k
  return true;
1325
392k
}
1326
1327
void ProtoStreamObjectWriter::Push(
1328
    StringPiece name, Item::ItemType item_type, bool is_placeholder,
1329
41.0M
    bool is_list) {
1330
41.0M
  is_list ? ProtoWriter::StartList(name) : ProtoWriter::StartObject(name);
1331
1332
  // invalid_depth == 0 means it is a successful StartObject or StartList.
1333
41.0M
  if (invalid_depth() == 0)
1334
41.0M
    current_.reset(
1335
41.0M
        new Item(current_.release(), item_type, is_placeholder, is_list));
1336
41.0M
}
1337
1338
20.0M
void ProtoStreamObjectWriter::Pop() {
1339
  // Pop all placeholder items sending StartObject or StartList events to
1340
  // ProtoWriter according to is_list value.
1341
20.7M
  while (current_ != nullptr && current_->is_placeholder()) {
1342
704k
    PopOneElement();
1343
704k
  }
1344
20.0M
  if (current_ != nullptr) {
1345
20.0M
    PopOneElement();
1346
20.0M
  }
1347
20.0M
}
1348
1349
20.7M
void ProtoStreamObjectWriter::PopOneElement() {
1350
20.7M
  current_->is_list() ? ProtoWriter::EndList() : ProtoWriter::EndObject();
1351
20.7M
  current_.reset(current_->pop<Item>());
1352
20.7M
}
1353
1354
126k
bool ProtoStreamObjectWriter::IsMap(const google::protobuf::Field& field) {
1355
126k
  if (field.type_url().empty() ||
1356
126k
      field.kind() != google::protobuf::Field::TYPE_MESSAGE ||
1357
126k
      field.cardinality() != google::protobuf::Field::CARDINALITY_REPEATED) {
1358
0
    return false;
1359
0
  }
1360
126k
  const google::protobuf::Type* field_type =
1361
126k
      typeinfo()->GetTypeByTypeUrl(field.type_url());
1362
1363
126k
  return converter::IsMap(field, *field_type);
1364
126k
}
1365
1366
4.64k
bool ProtoStreamObjectWriter::IsAny(const google::protobuf::Field& field) {
1367
4.64k
  return GetTypeWithoutUrl(field.type_url()) == kAnyType;
1368
4.64k
}
1369
1370
131k
bool ProtoStreamObjectWriter::IsStruct(const google::protobuf::Field& field) {
1371
131k
  return GetTypeWithoutUrl(field.type_url()) == kStructType;
1372
131k
}
1373
1374
bool ProtoStreamObjectWriter::IsStructValue(
1375
6.91M
    const google::protobuf::Field& field) {
1376
6.91M
  return GetTypeWithoutUrl(field.type_url()) == kStructValueType;
1377
6.91M
}
1378
1379
bool ProtoStreamObjectWriter::IsStructListValue(
1380
0
    const google::protobuf::Field& field) {
1381
0
  return GetTypeWithoutUrl(field.type_url()) == kStructListValueType;
1382
0
}
1383
1384
}  // namespace converter
1385
}  // namespace util
1386
}  // namespace protobuf
1387
}  // namespace google