protobuf/src/google/protobuf/text_format.h
Go to the documentation of this file.
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 // Author: jschorr@google.com (Joseph Schorr)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // Utilities for printing and parsing protocol messages in a human-readable,
36 // text-based format.
37 
38 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
39 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
40 
41 #include <map>
42 #include <memory>
43 #include <string>
44 #include <vector>
45 
46 #include <google/protobuf/stubs/common.h>
47 #include <google/protobuf/descriptor.h>
48 #include <google/protobuf/message.h>
49 #include <google/protobuf/message_lite.h>
50 #include <google/protobuf/port.h>
51 
52 #include <google/protobuf/port_def.inc>
53 
54 #ifdef SWIG
55 #error "You cannot SWIG proto headers"
56 #endif
57 
58 namespace google {
59 namespace protobuf {
60 
61 namespace io {
62 class ErrorCollector; // tokenizer.h
63 }
64 
65 // This class implements protocol buffer text format, colloquially known as text
66 // proto. Printing and parsing protocol messages in text format is useful for
67 // debugging and human editing of messages.
68 //
69 // This class is really a namespace that contains only static methods.
70 class PROTOBUF_EXPORT TextFormat {
71  public:
72  // Outputs a textual representation of the given message to the given
73  // output stream. Returns false if printing fails.
74  static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
75 
76  // Print the fields in an UnknownFieldSet. They are printed by tag number
77  // only. Embedded messages are heuristically identified by attempting to
78  // parse them. Returns false if printing fails.
79  static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
81 
82  // Like Print(), but outputs directly to a string.
83  // Note: output will be cleared prior to printing, and will be left empty
84  // even if printing fails. Returns false if printing fails.
85  static bool PrintToString(const Message& message, std::string* output);
86 
87  // Like PrintUnknownFields(), but outputs directly to a string. Returns
88  // false if printing fails.
89  static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
91 
92  // Outputs a textual representation of the value of the field supplied on
93  // the message supplied. For non-repeated fields, an index of -1 must
94  // be supplied. Note that this method will print the default value for a
95  // field if it is not set.
96  static void PrintFieldValueToString(const Message& message,
97  const FieldDescriptor* field, int index,
99 
100  class PROTOBUF_EXPORT BaseTextGenerator {
101  public:
102  virtual ~BaseTextGenerator();
103 
104  virtual void Indent() {}
105  virtual void Outdent() {}
106  // Returns the current indentation size in characters.
107  virtual size_t GetCurrentIndentationSize() const { return 0; }
108 
109  // Print text to the output stream.
110  virtual void Print(const char* text, size_t size) = 0;
111 
112  void PrintString(const std::string& str) { Print(str.data(), str.size()); }
113 
114  template <size_t n>
115  void PrintLiteral(const char (&text)[n]) {
116  Print(text, n - 1); // n includes the terminating zero character.
117  }
118  };
119 
120  // The default printer that converts scalar values from fields into their
121  // string representation.
122  // You can derive from this FastFieldValuePrinter if you want to have fields
123  // to be printed in a different way and register it at the Printer.
124  class PROTOBUF_EXPORT FastFieldValuePrinter {
125  public:
126  FastFieldValuePrinter();
127  virtual ~FastFieldValuePrinter();
128  virtual void PrintBool(bool val, BaseTextGenerator* generator) const;
129  virtual void PrintInt32(int32_t val, BaseTextGenerator* generator) const;
130  virtual void PrintUInt32(uint32_t val, BaseTextGenerator* generator) const;
131  virtual void PrintInt64(int64_t val, BaseTextGenerator* generator) const;
132  virtual void PrintUInt64(uint64_t val, BaseTextGenerator* generator) const;
133  virtual void PrintFloat(float val, BaseTextGenerator* generator) const;
134  virtual void PrintDouble(double val, BaseTextGenerator* generator) const;
135  virtual void PrintString(const std::string& val,
136  BaseTextGenerator* generator) const;
137  virtual void PrintBytes(const std::string& val,
138  BaseTextGenerator* generator) const;
139  virtual void PrintEnum(int32_t val, const std::string& name,
140  BaseTextGenerator* generator) const;
141  virtual void PrintFieldName(const Message& message, int field_index,
142  int field_count, const Reflection* reflection,
143  const FieldDescriptor* field,
144  BaseTextGenerator* generator) const;
145  virtual void PrintFieldName(const Message& message,
146  const Reflection* reflection,
147  const FieldDescriptor* field,
148  BaseTextGenerator* generator) const;
149  virtual void PrintMessageStart(const Message& message, int field_index,
150  int field_count, bool single_line_mode,
151  BaseTextGenerator* generator) const;
152  // Allows to override the logic on how to print the content of a message.
153  // Return false to use the default printing logic. Note that it is legal for
154  // this function to print something and then return false to use the default
155  // content printing (although at that point it would behave similarly to
156  // PrintMessageStart).
157  virtual bool PrintMessageContent(const Message& message, int field_index,
158  int field_count, bool single_line_mode,
159  BaseTextGenerator* generator) const;
160  virtual void PrintMessageEnd(const Message& message, int field_index,
161  int field_count, bool single_line_mode,
162  BaseTextGenerator* generator) const;
163 
164  private:
165  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastFieldValuePrinter);
166  };
167 
168  // Deprecated: please use FastFieldValuePrinter instead.
169  class PROTOBUF_EXPORT FieldValuePrinter {
170  public:
172  virtual ~FieldValuePrinter();
173  virtual std::string PrintBool(bool val) const;
174  virtual std::string PrintInt32(int32_t val) const;
175  virtual std::string PrintUInt32(uint32_t val) const;
176  virtual std::string PrintInt64(int64_t val) const;
177  virtual std::string PrintUInt64(uint64_t val) const;
178  virtual std::string PrintFloat(float val) const;
179  virtual std::string PrintDouble(double val) const;
180  virtual std::string PrintString(const std::string& val) const;
181  virtual std::string PrintBytes(const std::string& val) const;
182  virtual std::string PrintEnum(int32_t val, const std::string& name) const;
183  virtual std::string PrintFieldName(const Message& message,
184  const Reflection* reflection,
185  const FieldDescriptor* field) const;
186  virtual std::string PrintMessageStart(const Message& message,
187  int field_index, int field_count,
188  bool single_line_mode) const;
189  virtual std::string PrintMessageEnd(const Message& message, int field_index,
190  int field_count,
191  bool single_line_mode) const;
192 
193  private:
196  };
197 
198  class PROTOBUF_EXPORT MessagePrinter {
199  public:
201  virtual ~MessagePrinter() {}
202  virtual void Print(const Message& message, bool single_line_mode,
203  BaseTextGenerator* generator) const = 0;
204 
205  private:
207  };
208 
209  // Interface that Printers or Parsers can use to find extensions, or types
210  // referenced in Any messages.
211  class PROTOBUF_EXPORT Finder {
212  public:
213  virtual ~Finder();
214 
215  // Try to find an extension of *message by fully-qualified field
216  // name. Returns nullptr if no extension is known for this name or number.
217  // The base implementation uses the extensions already known by the message.
218  virtual const FieldDescriptor* FindExtension(Message* message,
219  const std::string& name) const;
220 
221  // Similar to FindExtension, but uses a Descriptor and the extension number
222  // instead of using a Message and the name when doing the look up.
223  virtual const FieldDescriptor* FindExtensionByNumber(
224  const Descriptor* descriptor, int number) const;
225 
226  // Find the message type for an Any proto.
227  // Returns nullptr if no message is known for this name.
228  // The base implementation only accepts prefixes of type.googleprod.com/ or
229  // type.googleapis.com/, and searches the DescriptorPool of the parent
230  // message.
231  virtual const Descriptor* FindAnyType(const Message& message,
232  const std::string& prefix,
233  const std::string& name) const;
234 
235  // Find the message factory for the given extension field. This can be used
236  // to generalize the Parser to add extension fields to a message in the same
237  // way as the "input" message for the Parser.
238  virtual MessageFactory* FindExtensionFactory(
239  const FieldDescriptor* field) const;
240  };
241 
242  // Class for those users which require more fine-grained control over how
243  // a protobuffer message is printed out.
244  class PROTOBUF_EXPORT Printer {
245  public:
246  Printer();
247 
248  // Like TextFormat::Print
249  bool Print(const Message& message, io::ZeroCopyOutputStream* output) const;
250  // Like TextFormat::PrintUnknownFields
251  bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
253  // Like TextFormat::PrintToString
254  bool PrintToString(const Message& message, std::string* output) const;
255  // Like TextFormat::PrintUnknownFieldsToString
256  bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
257  std::string* output) const;
258  // Like TextFormat::PrintFieldValueToString
259  void PrintFieldValueToString(const Message& message,
260  const FieldDescriptor* field, int index,
261  std::string* output) const;
262 
263  // Adjust the initial indent level of all output. Each indent level is
264  // equal to two spaces.
265  void SetInitialIndentLevel(int indent_level) {
266  initial_indent_level_ = indent_level;
267  }
268 
269  // If printing in single line mode, then the entire message will be output
270  // on a single line with no line breaks.
271  void SetSingleLineMode(bool single_line_mode) {
272  single_line_mode_ = single_line_mode;
273  }
274 
275  bool IsInSingleLineMode() const { return single_line_mode_; }
276 
277  // If use_field_number is true, uses field number instead of field name.
278  void SetUseFieldNumber(bool use_field_number) {
279  use_field_number_ = use_field_number;
280  }
281 
282  // Set true to print repeated primitives in a format like:
283  // field_name: [1, 2, 3, 4]
284  // instead of printing each value on its own line. Short format applies
285  // only to primitive values -- i.e. everything except strings and
286  // sub-messages/groups.
287  void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) {
288  use_short_repeated_primitives_ = use_short_repeated_primitives;
289  }
290 
291  // Set true to output UTF-8 instead of ASCII. The only difference
292  // is that bytes >= 0x80 in string fields will not be escaped,
293  // because they are assumed to be part of UTF-8 multi-byte
294  // sequences. This will change the default FastFieldValuePrinter.
295  void SetUseUtf8StringEscaping(bool as_utf8);
296 
297  // Set the default FastFieldValuePrinter that is used for all fields that
298  // don't have a field-specific printer registered.
299  // Takes ownership of the printer.
300  void SetDefaultFieldValuePrinter(const FastFieldValuePrinter* printer);
301 
302  PROTOBUF_DEPRECATED_MSG("Please use FastFieldValuePrinter")
303  void SetDefaultFieldValuePrinter(const FieldValuePrinter* printer);
304 
305  // Sets whether we want to hide unknown fields or not.
306  // Usually unknown fields are printed in a generic way that includes the
307  // tag number of the field instead of field name. However, sometimes it
308  // is useful to be able to print the message without unknown fields (e.g.
309  // for the python protobuf version to maintain consistency between its pure
310  // python and c++ implementations).
311  void SetHideUnknownFields(bool hide) { hide_unknown_fields_ = hide; }
312 
313  // If print_message_fields_in_index_order is true, fields of a proto message
314  // will be printed using the order defined in source code instead of the
315  // field number, extensions will be printed at the end of the message
316  // and their relative order is determined by the extension number.
317  // By default, use the field number order.
319  bool print_message_fields_in_index_order) {
320  print_message_fields_in_index_order_ =
321  print_message_fields_in_index_order;
322  }
323 
324  // If expand==true, expand google.protobuf.Any payloads. The output
325  // will be of form
326  // [type_url] { <value_printed_in_text> }
327  //
328  // If expand==false, print Any using the default printer. The output will
329  // look like
330  // type_url: "<type_url>" value: "serialized_content"
331  void SetExpandAny(bool expand) { expand_any_ = expand; }
332 
333  // Set how parser finds message for Any payloads.
334  void SetFinder(const Finder* finder) { finder_ = finder; }
335 
336  // If non-zero, we truncate all string fields that are longer than
337  // this threshold. This is useful when the proto message has very long
338  // strings, e.g., dump of encoded image file.
339  //
340  // NOTE(hfgong): Setting a non-zero value breaks round-trip safe
341  // property of TextFormat::Printer. That is, from the printed message, we
342  // cannot fully recover the original string field any more.
344  const int64_t truncate_string_field_longer_than) {
345  truncate_string_field_longer_than_ = truncate_string_field_longer_than;
346  }
347 
348  // Register a custom field-specific FastFieldValuePrinter for fields
349  // with a particular FieldDescriptor.
350  // Returns "true" if the registration succeeded, or "false", if there is
351  // already a printer for that FieldDescriptor.
352  // Takes ownership of the printer on successful registration.
353  bool RegisterFieldValuePrinter(const FieldDescriptor* field,
354  const FastFieldValuePrinter* printer);
355 
356  PROTOBUF_DEPRECATED_MSG("Please use FastFieldValuePrinter")
357  bool RegisterFieldValuePrinter(const FieldDescriptor* field,
358  const FieldValuePrinter* printer);
359 
360  // Register a custom message-specific MessagePrinter for messages with a
361  // particular Descriptor.
362  // Returns "true" if the registration succeeded, or "false" if there is
363  // already a printer for that Descriptor.
364  bool RegisterMessagePrinter(const Descriptor* descriptor,
365  const MessagePrinter* printer);
366 
367  private:
368  friend std::string Message::DebugString() const;
369  friend std::string Message::ShortDebugString() const;
370  friend std::string Message::Utf8DebugString() const;
371 
372  // Sets whether *DebugString should insert a silent marker.
373  void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; }
374 
375  // Forward declaration of an internal class used to print the text
376  // output to the OutputStream (see text_format.cc for implementation).
377  class TextGenerator;
378 
379  // Forward declaration of an internal class used to print field values for
380  // DebugString APIs (see text_format.cc for implementation).
381  class DebugStringFieldValuePrinter;
382 
383  // Forward declaration of an internal class used to print UTF-8 escaped
384  // strings (see text_format.cc for implementation).
386 
387  static const char* const kDoNotParse;
388 
389  // Internal Print method, used for writing to the OutputStream via
390  // the TextGenerator class.
391  void Print(const Message& message, TextGenerator* generator) const;
392 
393  // Print a single field.
394  void PrintField(const Message& message, const Reflection* reflection,
395  const FieldDescriptor* field,
396  TextGenerator* generator) const;
397 
398  // Print a repeated primitive field in short form.
399  void PrintShortRepeatedField(const Message& message,
400  const Reflection* reflection,
401  const FieldDescriptor* field,
402  TextGenerator* generator) const;
403 
404  // Print the name of a field -- i.e. everything that comes before the
405  // ':' for a single name/value pair.
406  void PrintFieldName(const Message& message, int field_index,
407  int field_count, const Reflection* reflection,
408  const FieldDescriptor* field,
409  TextGenerator* generator) const;
410 
411  // Outputs a textual representation of the value of the field supplied on
412  // the message supplied or the default value if not set.
413  void PrintFieldValue(const Message& message, const Reflection* reflection,
414  const FieldDescriptor* field, int index,
415  TextGenerator* generator) const;
416 
417  // Print the fields in an UnknownFieldSet. They are printed by tag number
418  // only. Embedded messages are heuristically identified by attempting to
419  // parse them (subject to the recursion budget).
420  void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
421  TextGenerator* generator,
422  int recursion_budget) const;
423 
424  bool PrintAny(const Message& message, TextGenerator* generator) const;
425 
427  const FieldDescriptor* field) const {
428  auto it = custom_printers_.find(field);
429  return it == custom_printers_.end() ? default_field_value_printer_.get()
430  : it->second.get();
431  }
432 
433  int initial_indent_level_;
434  bool single_line_mode_;
435  bool use_field_number_;
436  bool use_short_repeated_primitives_;
438  bool hide_unknown_fields_;
439  bool print_message_fields_in_index_order_;
440  bool expand_any_;
442 
443  std::unique_ptr<const FastFieldValuePrinter> default_field_value_printer_;
444  typedef std::map<const FieldDescriptor*,
445  std::unique_ptr<const FastFieldValuePrinter>>
447  CustomPrinterMap custom_printers_;
448 
449  typedef std::map<const Descriptor*, std::unique_ptr<const MessagePrinter>>
451  CustomMessagePrinterMap custom_message_printers_;
452 
453  const Finder* finder_;
454  };
455 
456  // Parses a text-format protocol message from the given input stream to
457  // the given message object. This function parses the human-readable format
458  // written by Print(). Returns true on success. The message is cleared first,
459  // even if the function fails -- See Merge() to avoid this behavior.
460  //
461  // Example input: "user {\n id: 123 extra { gender: MALE language: 'en' }\n}"
462  //
463  // One use for this function is parsing handwritten strings in test code.
464  // Another use is to parse the output from google::protobuf::Message::DebugString()
465  // (or ShortDebugString()), because these functions output using
466  // google::protobuf::TextFormat::Print().
467  //
468  // If you would like to read a protocol buffer serialized in the
469  // (non-human-readable) binary wire format, see
470  // google::protobuf::MessageLite::ParseFromString().
472  // Like Parse(), but reads directly from a string.
474 
475  // Like Parse(), but the data is merged into the given message, as if
476  // using Message::MergeFrom().
478  // Like Merge(), but reads directly from a string.
480 
481  // Parse the given text as a single field value and store it into the
482  // given field of the given message. If the field is a repeated field,
483  // the new value will be added to the end
484  static bool ParseFieldValueFromString(const std::string& input,
485  const FieldDescriptor* field,
486  Message* message);
487 
488  // A location in the parsed text.
489  struct ParseLocation {
490  int line;
491  int column;
492 
493  ParseLocation() : line(-1), column(-1) {}
494  ParseLocation(int line_param, int column_param)
495  : line(line_param), column(column_param) {}
496  };
497 
498  // A range of locations in the parsed text, including `start` and excluding
499  // `end`.
505  : start(start_param), end(end_param) {}
506  };
507 
508  // Data structure which is populated with the locations of each field
509  // value parsed from the text.
510  class PROTOBUF_EXPORT ParseInfoTree {
511  public:
512  ParseInfoTree() = default;
513  ParseInfoTree(const ParseInfoTree&) = delete;
514  ParseInfoTree& operator=(const ParseInfoTree&) = delete;
515 
516  // Returns the parse location range for index-th value of the field in
517  // the parsed text. If none exists, returns a location with start and end
518  // line -1. Index should be -1 for not-repeated fields.
519  ParseLocationRange GetLocationRange(const FieldDescriptor* field,
520  int index) const;
521 
522  // Returns the starting parse location for index-th value of the field in
523  // the parsed text. If none exists, returns a location with line = -1. Index
524  // should be -1 for not-repeated fields.
526  return GetLocationRange(field, index).start;
527  }
528 
529  // Returns the parse info tree for the given field, which must be a message
530  // type. The nested information tree is owned by the root tree and will be
531  // deleted when it is deleted.
532  ParseInfoTree* GetTreeForNested(const FieldDescriptor* field,
533  int index) const;
534 
535  private:
536  // Allow the text format parser to record information into the tree.
537  friend class TextFormat;
538 
539  // Records the starting and ending locations of a single value for a field.
540  void RecordLocation(const FieldDescriptor* field, ParseLocationRange range);
541 
542  // Create and records a nested tree for a nested message field.
543  ParseInfoTree* CreateNested(const FieldDescriptor* field);
544 
545  // Defines the map from the index-th field descriptor to its parse location.
546  typedef std::map<const FieldDescriptor*, std::vector<ParseLocationRange>>
548 
549  // Defines the map from the index-th field descriptor to the nested parse
550  // info tree.
551  typedef std::map<const FieldDescriptor*,
552  std::vector<std::unique_ptr<ParseInfoTree>>>
554 
555  LocationMap locations_;
556  NestedMap nested_;
557  };
558 
559  // For more control over parsing, use this class.
560  class PROTOBUF_EXPORT Parser {
561  public:
562  Parser();
563  ~Parser();
564 
565  // Like TextFormat::Parse().
567  // Like TextFormat::ParseFromString().
569  // Like TextFormat::Merge().
571  // Like TextFormat::MergeFromString().
573 
574  // Set where to report parse errors. If nullptr (the default), errors will
575  // be printed to stderr.
576  void RecordErrorsTo(io::ErrorCollector* error_collector) {
577  error_collector_ = error_collector;
578  }
579 
580  // Set how parser finds extensions. If nullptr (the default), the
581  // parser will use the standard Reflection object associated with
582  // the message being parsed.
583  void SetFinder(const Finder* finder) { finder_ = finder; }
584 
585  // Sets where location information about the parse will be written. If
586  // nullptr
587  // (the default), then no location will be written.
588  void WriteLocationsTo(ParseInfoTree* tree) { parse_info_tree_ = tree; }
589 
590  // Normally parsing fails if, after parsing, output->IsInitialized()
591  // returns false. Call AllowPartialMessage(true) to skip this check.
592  void AllowPartialMessage(bool allow) { allow_partial_ = allow; }
593 
594  // Allow field names to be matched case-insensitively.
595  // This is not advisable if there are fields that only differ in case, or
596  // if you want to enforce writing in the canonical form.
597  // This is 'false' by default.
598  void AllowCaseInsensitiveField(bool allow) {
599  allow_case_insensitive_field_ = allow;
600  }
601 
602  // Like TextFormat::ParseFieldValueFromString
603  bool ParseFieldValueFromString(const std::string& input,
604  const FieldDescriptor* field,
605  Message* output);
606 
607  // When an unknown extension is met, parsing will fail if this option is
608  // set to false (the default). If true, unknown extensions will be ignored
609  // and a warning message will be generated.
610  // Beware! Setting this option true may hide some errors (e.g. spelling
611  // error on extension name). This allows data loss; unlike binary format,
612  // text format cannot preserve unknown extensions. Avoid using this option
613  // if possible.
614  void AllowUnknownExtension(bool allow) { allow_unknown_extension_ = allow; }
615 
616  // When an unknown field is met, parsing will fail if this option is set
617  // to false (the default). If true, unknown fields will be ignored and
618  // a warning message will be generated.
619  // Beware! Setting this option true may hide some errors (e.g. spelling
620  // error on field name). This allows data loss; unlike binary format, text
621  // format cannot preserve unknown fields. Avoid using this option
622  // if possible.
623  void AllowUnknownField(bool allow) { allow_unknown_field_ = allow; }
624 
625 
626  void AllowFieldNumber(bool allow) { allow_field_number_ = allow; }
627 
628  // Sets maximum recursion depth which parser can use. This is effectively
629  // the maximum allowed nesting of proto messages.
630  void SetRecursionLimit(int limit) { recursion_limit_ = limit; }
631 
632  private:
633  // Forward declaration of an internal class used to parse text
634  // representations (see text_format.cc for implementation).
635  class ParserImpl;
636 
637  // Like TextFormat::Merge(). The provided implementation is used
638  // to do the parsing.
639  bool MergeUsingImpl(io::ZeroCopyInputStream* input, Message* output,
640  ParserImpl* parser_impl);
641 
643  const Finder* finder_;
644  ParseInfoTree* parse_info_tree_;
645  bool allow_partial_;
646  bool allow_case_insensitive_field_;
647  bool allow_unknown_field_;
648  bool allow_unknown_extension_;
649  bool allow_unknown_enum_;
650  bool allow_field_number_;
651  bool allow_relaxed_whitespace_;
652  bool allow_singular_overwrites_;
653  int recursion_limit_;
654  };
655 
656 
657  private:
658  // Hack: ParseInfoTree declares TextFormat as a friend which should extend
659  // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some
660  // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide
661  // helpers for ParserImpl to call methods of ParseInfoTree.
662  static inline void RecordLocation(ParseInfoTree* info_tree,
663  const FieldDescriptor* field,
664  ParseLocationRange location);
665  static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree,
666  const FieldDescriptor* field);
667 
669 };
670 
672  const FieldDescriptor* field,
673  ParseLocationRange location) {
674  info_tree->RecordLocation(field, location);
675 }
676 
678  ParseInfoTree* info_tree, const FieldDescriptor* field) {
679  return info_tree->CreateNested(field);
680 }
681 
682 } // namespace protobuf
683 } // namespace google
684 
685 #include <google/protobuf/port_undef.inc>
686 
687 #endif // GOOGLE_PROTOBUF_TEXT_FORMAT_H__
xds_interop_client.str
str
Definition: xds_interop_client.py:487
google::protobuf::TextFormat::MessagePrinter::~MessagePrinter
virtual ~MessagePrinter()
Definition: protobuf/src/google/protobuf/text_format.h:201
google::protobuf::TextFormat::ParseInfoTree::RecordLocation
void RecordLocation(const FieldDescriptor *field, ParseLocation location)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:128
google::protobuf::TextFormat::Printer::SetSingleLineMode
void SetSingleLineMode(bool single_line_mode)
Definition: protobuf/src/google/protobuf/text_format.h:271
regen-readme.it
it
Definition: regen-readme.py:15
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::TextFormat::Printer::insert_silent_marker_
bool insert_silent_marker_
Definition: protobuf/src/google/protobuf/text_format.h:437
google::protobuf::TextFormat::MessagePrinter
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:190
google::protobuf::TextFormat::Printer::IsInSingleLineMode
bool IsInSingleLineMode() const
Definition: protobuf/src/google/protobuf/text_format.h:275
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/macros.h:40
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
bool
bool
Definition: setup_once.h:312
google::protobuf::TextFormat::Parser::SetFinder
void SetFinder(const Finder *finder)
Definition: protobuf/src/google/protobuf/text_format.h:583
google::protobuf::TextFormat::Printer::kDoNotParse
static const char *const kDoNotParse
Definition: protobuf/src/google/protobuf/text_format.h:385
google::protobuf::TextFormat::Printer::CustomPrinterMap
std::map< const FieldDescriptor *, std::unique_ptr< const FastFieldValuePrinter > > CustomPrinterMap
Definition: protobuf/src/google/protobuf/text_format.h:446
google::protobuf::TextFormat::Parser::AllowPartialMessage
void AllowPartialMessage(bool allow)
Definition: protobuf/src/google/protobuf/text_format.h:592
google::protobuf::TextFormat::ParseInfoTree::LocationMap
std::map< const FieldDescriptor *, std::vector< ParseLocationRange > > LocationMap
Definition: protobuf/src/google/protobuf/text_format.h:547
google::protobuf::TextFormat::Printer::GetFieldPrinter
const FastFieldValuePrinter * GetFieldPrinter(const FieldDescriptor *field) const
Definition: protobuf/src/google/protobuf/text_format.h:426
google::protobuf::TextFormat::BaseTextGenerator::GetCurrentIndentationSize
virtual size_t GetCurrentIndentationSize() const
Definition: protobuf/src/google/protobuf/text_format.h:107
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
google::protobuf::TextFormat::Printer::truncate_string_field_longer_than_
int64_t truncate_string_field_longer_than_
Definition: protobuf/src/google/protobuf/text_format.h:441
google::protobuf::TextFormat::Printer::SetUseFieldNumber
void SetUseFieldNumber(bool use_field_number)
Definition: protobuf/src/google/protobuf/text_format.h:278
google::protobuf::TextFormat::ParseLocationRange::end
ParseLocation end
Definition: protobuf/src/google/protobuf/text_format.h:502
google::protobuf::TextFormat::BaseTextGenerator::PrintLiteral
void PrintLiteral(const char(&text)[n])
Definition: protobuf/src/google/protobuf/text_format.h:115
testing::gtest_printers_test::Print
std::string Print(const T &value)
Definition: bloaty/third_party/googletest/googletest/test/googletest-printers-test.cc:233
google::protobuf.text_format.PrintField
def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, message_formatter=None, print_unknown_fields=False)
Definition: bloaty/third_party/protobuf/python/google/protobuf/text_format.py:236
google::protobuf::TextFormat::Parser
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:511
DebugString
std::string DebugString(const google::protobuf::Message &message)
Definition: bloaty/tests/test.h:60
google::protobuf::TextFormat::Parser::AllowCaseInsensitiveField
void AllowCaseInsensitiveField(bool allow)
Definition: protobuf/src/google/protobuf/text_format.h:598
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::TextFormat::Parser::AllowUnknownField
void AllowUnknownField(bool allow)
Definition: protobuf/src/google/protobuf/text_format.h:623
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
setup.name
name
Definition: setup.py:542
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
google::protobuf::TextFormat::CreateNested
static ParseInfoTree * CreateNested(ParseInfoTree *info_tree, const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:621
google::protobuf::TextFormat::ParseLocation
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:458
google::protobuf::Reflection
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:397
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:2512
google::protobuf::MessageFactory
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:1066
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
google::protobuf::TextFormat::ParseLocationRange::ParseLocationRange
ParseLocationRange(ParseLocation start_param, ParseLocation end_param)
Definition: protobuf/src/google/protobuf/text_format.h:504
google::protobuf::TextFormat
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:70
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
start
static uint64_t start
Definition: benchmark-pound.c:74
google::protobuf::python::cdescriptor_pool::FindExtensionByNumber
static PyObject * FindExtensionByNumber(PyObject *self, PyObject *args)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor_pool.cc:420
google::protobuf::TextFormat::Parser::WriteLocationsTo
void WriteLocationsTo(ParseInfoTree *tree)
Definition: protobuf/src/google/protobuf/text_format.h:588
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
google::protobuf::ConstStringParam
const std::string & ConstStringParam
Definition: third_party/protobuf/src/google/protobuf/stubs/port.h:129
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
google::protobuf::TextFormat::ParseInfoTree
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:469
google::protobuf::TextFormat::RecordLocation
static void RecordLocation(ParseInfoTree *info_tree, const FieldDescriptor *field, ParseLocation location)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:615
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf::TextFormat::BaseTextGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:100
google::protobuf::TextFormat::Printer::SetFinder
void SetFinder(const Finder *finder)
Definition: protobuf/src/google/protobuf/text_format.h:334
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf::TextFormat::MessagePrinter::MessagePrinter
MessagePrinter()
Definition: protobuf/src/google/protobuf/text_format.h:200
grpc::protobuf::TextFormat
GRPC_CUSTOM_TEXTFORMAT TextFormat
Definition: config_grpc_cli.h:59
google::protobuf::TextFormat::Printer::SetExpandAny
void SetExpandAny(bool expand)
Definition: protobuf/src/google/protobuf/text_format.h:331
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
google::protobuf::TextFormat::ParseLocationRange
Definition: protobuf/src/google/protobuf/text_format.h:500
PrintString
static void PrintString(int max_cols, absl::string_view *str, protobuf::io::Printer *printer)
Definition: upbc.cc:72
io
absl::flags_internal::Parse
bool Parse(FlagOpFn op, absl::string_view text, void *dst, std::string *error)
Definition: abseil-cpp/absl/flags/internal/flag.h:125
google::protobuf::TextFormat::ParseInfoTree::CreateNested
ParseInfoTree * CreateNested(const FieldDescriptor *field)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:133
google::protobuf::TextFormat::ParseInfoTree::LocationMap
std::map< const FieldDescriptor *, std::vector< ParseLocation > > LocationMap
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:498
google::protobuf::TextFormat::Parser::RecordErrorsTo
void RecordErrorsTo(io::ErrorCollector *error_collector)
Definition: protobuf/src/google/protobuf/text_format.h:576
google::protobuf::TextFormat::BaseTextGenerator::Indent
virtual void Indent()
Definition: protobuf/src/google/protobuf/text_format.h:104
google::protobuf.text_format.Merge
def Merge(text, message, allow_unknown_extension=False, allow_field_number=False, descriptor_pool=None, allow_unknown_field=False)
Definition: bloaty/third_party/protobuf/python/google/protobuf/text_format.py:659
google::protobuf::TextFormat::Printer::CustomMessagePrinterMap
std::map< const Descriptor *, std::unique_ptr< const MessagePrinter > > CustomMessagePrinterMap
Definition: protobuf/src/google/protobuf/text_format.h:450
google::protobuf::TextFormat::Finder
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:203
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
google::protobuf::TextFormat::Printer::SetPrintMessageFieldsInIndexOrder
void SetPrintMessageFieldsInIndexOrder(bool print_message_fields_in_index_order)
Definition: protobuf/src/google/protobuf/text_format.h:318
google::protobuf::TextFormat::FastFieldValuePrinter
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:124
google::protobuf::TextFormat::ParseInfoTree::GetLocation
ParseLocation GetLocation(const FieldDescriptor *field, int index) const
Definition: protobuf/src/google/protobuf/text_format.h:525
google::protobuf::io::ZeroCopyInputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h:126
google::protobuf.internal.python_message.MergeFromString
MergeFromString
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:1137
google::protobuf::Message
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:205
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::TextFormat::ParseLocationRange::start
ParseLocation start
Definition: protobuf/src/google/protobuf/text_format.h:501
google::protobuf::TextFormat::Printer::SetInitialIndentLevel
void SetInitialIndentLevel(int indent_level)
Definition: protobuf/src/google/protobuf/text_format.h:265
google::protobuf::TextFormat::ParseLocation::ParseLocation
ParseLocation()
Definition: protobuf/src/google/protobuf/text_format.h:493
google::protobuf::python::cmessage::ParseFromString
static PyObject * ParseFromString(CMessage *self, PyObject *arg)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:1975
google::protobuf::TextFormat::BaseTextGenerator::PrintString
void PrintString(const std::string &str)
Definition: protobuf/src/google/protobuf/text_format.h:112
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
google::protobuf.text_format.PrintFieldValue
def PrintFieldValue(field, value, out, indent=0, as_utf8=False, as_one_line=False, use_short_repeated_primitives=False, pointy_brackets=False, use_index_order=False, float_format=None, double_format=None, message_formatter=None, print_unknown_fields=False)
Definition: bloaty/third_party/protobuf/python/google/protobuf/text_format.py:258
google::protobuf::TextFormat::Parser::SetRecursionLimit
void SetRecursionLimit(int limit)
Definition: protobuf/src/google/protobuf/text_format.h:630
google::protobuf::io::ErrorCollector
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer.h:66
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
google::protobuf::UnknownFieldSet
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:81
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
regen-readme.line
line
Definition: regen-readme.py:30
google::protobuf::TextFormat::Printer::FastFieldValuePrinterUtf8Escaping
Definition: protobuf/src/google/protobuf/text_format.cc:1526
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
google::protobuf::TextFormat::Parser::AllowUnknownExtension
void AllowUnknownExtension(bool allow)
Definition: protobuf/src/google/protobuf/text_format.h:614
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
google::protobuf::TextFormat::FieldValuePrinter::delegate_
FastFieldValuePrinter delegate_
Definition: protobuf/src/google/protobuf/text_format.h:194
google::protobuf::TextFormat::BaseTextGenerator::Outdent
virtual void Outdent()
Definition: protobuf/src/google/protobuf/text_format.h:105
google::protobuf::TextFormat::ParseInfoTree::NestedMap
std::map< const FieldDescriptor *, std::vector< std::unique_ptr< ParseInfoTree > > > NestedMap
Definition: protobuf/src/google/protobuf/text_format.h:553
google::protobuf::TextFormat::ParseLocationRange::ParseLocationRange
ParseLocationRange()
Definition: protobuf/src/google/protobuf/text_format.h:503
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
google::protobuf::TextFormat::Printer::SetTruncateStringFieldLongerThan
void SetTruncateStringFieldLongerThan(const int64_t truncate_string_field_longer_than)
Definition: protobuf/src/google/protobuf/text_format.h:343
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
google::protobuf::TextFormat::FieldValuePrinter
Definition: protobuf/src/google/protobuf/text_format.h:169
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
error_collector_
MockErrorCollector error_collector_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/importer_unittest.cc:129
google::protobuf::TextFormat::ParseLocation::ParseLocation
ParseLocation(int line_param, int column_param)
Definition: protobuf/src/google/protobuf/text_format.h:494
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::TextFormat::Parser::AllowFieldNumber
void AllowFieldNumber(bool allow)
Definition: protobuf/src/google/protobuf/text_format.h:626
Message
Definition: protobuf/php/ext/google/protobuf/message.c:53
google::protobuf::TextFormat::Printer::SetUseShortRepeatedPrimitives
void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives)
Definition: protobuf/src/google/protobuf/text_format.h:287
testing::PrintToString
::std::string PrintToString(const T &value)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-printers.h:915


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:34