default_value_objectwriter.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 #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
33 
34 #include <memory>
35 #include <stack>
36 #include <vector>
37 
46 
47 #include <google/protobuf/port_def.inc>
48 
49 namespace google {
50 namespace protobuf {
51 namespace util {
52 namespace converter {
53 
54 // An ObjectWriter that renders non-repeated primitive fields of proto messages
55 // with their default values. DefaultValueObjectWriter holds objects, lists and
56 // fields it receives in a tree structure and writes them out to another
57 // ObjectWriter when EndObject() is called on the root object. It also writes
58 // out all non-repeated primitive fields that haven't been explicitly rendered
59 // with their default values (0 for numbers, "" for strings, etc).
60 class PROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
61  public:
62  // A Callback function to check whether a field needs to be scrubbed.
63  //
64  // Returns true if the field should not be present in the output. Returns
65  // false otherwise.
66  //
67  // The 'path' parameter is a vector of path to the field from root. For
68  // example: if a nested field "a.b.c" (b is the parent message field of c and
69  // a is the parent message field of b), then the vector should contain { "a",
70  // "b", "c" }.
71  //
72  // The Field* should point to the google::protobuf::Field of "c".
73  typedef ResultCallback2<bool /*return*/,
74  const std::vector<std::string>& /*path of the field*/,
75  const google::protobuf::Field* /*field*/>
77 
78  // A unique pointer to a DefaultValueObjectWriter::FieldScrubCallBack.
79  typedef std::unique_ptr<FieldScrubCallBack> FieldScrubCallBackPtr;
80 
83  ObjectWriter* ow);
84 
85  virtual ~DefaultValueObjectWriter();
86 
87  // ObjectWriter methods.
88  DefaultValueObjectWriter* StartObject(StringPiece name) override;
89 
90  DefaultValueObjectWriter* EndObject() override;
91 
92  DefaultValueObjectWriter* StartList(StringPiece name) override;
93 
94  DefaultValueObjectWriter* EndList() override;
95 
97  bool value) override;
98 
100  int32 value) override;
101 
103  uint32 value) override;
104 
106  int64 value) override;
107 
109  uint64 value) override;
110 
112  double value) override;
113 
115  float value) override;
116 
118  StringPiece value) override;
120  StringPiece value) override;
121 
122  virtual DefaultValueObjectWriter* RenderNull(StringPiece name);
123 
124  // Register the callback for scrubbing of fields. Owership of
125  // field_scrub_callback pointer is also transferred to this class
126  void RegisterFieldScrubCallBack(FieldScrubCallBackPtr field_scrub_callback);
127 
128  // If set to true, empty lists are suppressed from output when default values
129  // are written.
130  void set_suppress_empty_list(bool value) { suppress_empty_list_ = value; }
131 
132  // If set to true, original proto field names are used
134  preserve_proto_field_names_ = value;
135  }
136 
137  // If set to true, enums are rendered as ints from output when default values
138  // are written.
139  void set_print_enums_as_ints(bool value) { use_ints_for_enums_ = value; }
140 
141  protected:
142  enum NodeKind {
143  PRIMITIVE = 0,
144  OBJECT = 1,
145  LIST = 2,
146  MAP = 3,
147  };
148 
149  // "Node" represents a node in the tree that holds the input of
150  // DefaultValueObjectWriter.
151  class PROTOBUF_EXPORT Node {
152  public:
154  NodeKind kind, const DataPiece& data, bool is_placeholder,
155  const std::vector<std::string>& path, bool suppress_empty_list,
156  bool preserve_proto_field_names, bool use_ints_for_enums,
157  FieldScrubCallBack* field_scrub_callback);
158  virtual ~Node() {
159  for (int i = 0; i < children_.size(); ++i) {
160  delete children_[i];
161  }
162  }
163 
164  // Adds a child to this node. Takes ownership of this child.
165  void AddChild(Node* child) { children_.push_back(child); }
166 
167  // Finds the child given its name.
168  Node* FindChild(StringPiece name);
169 
170  // Populates children of this Node based on its type. If there are already
171  // children created, they will be merged to the result. Caller should pass
172  // in TypeInfo for looking up types of the children.
173  virtual void PopulateChildren(const TypeInfo* typeinfo);
174 
175  // If this node is a leaf (has data), writes the current node to the
176  // ObjectWriter; if not, then recursively writes the children to the
177  // ObjectWriter.
178  virtual void WriteTo(ObjectWriter* ow);
179 
180  // Accessors
181  const std::string& name() const { return name_; }
182 
183  const std::vector<std::string>& path() const { return path_; }
184 
185  const google::protobuf::Type* type() const { return type_; }
186 
187  void set_type(const google::protobuf::Type* type) { type_ = type; }
188 
189  NodeKind kind() const { return kind_; }
190 
191  int number_of_children() const { return children_.size(); }
192 
193  void set_data(const DataPiece& data) { data_ = data; }
194 
195  bool is_any() const { return is_any_; }
196 
197  void set_is_any(bool is_any) { is_any_ = is_any; }
198 
199  void set_is_placeholder(bool is_placeholder) {
200  is_placeholder_ = is_placeholder;
201  }
202 
203  protected:
204  // Returns the Value Type of a map given the Type of the map entry and a
205  // TypeInfo instance.
206  const google::protobuf::Type* GetMapValueType(
207  const google::protobuf::Type& entry_type, const TypeInfo* typeinfo);
208 
209  // Calls WriteTo() on every child in children_.
210  void WriteChildren(ObjectWriter* ow);
211 
212  // The name of this node.
214  // google::protobuf::Type of this node. Owned by TypeInfo.
216  // The kind of this node.
218  // Whether this is a node for "Any".
219  bool is_any_;
220  // The data of this node when it is a leaf node.
222  // Children of this node.
223  std::vector<Node*> children_;
224  // Whether this node is a placeholder for an object or list automatically
225  // generated when creating the parent node. Should be set to false after
226  // the parent node's StartObject()/StartList() method is called with this
227  // node's name.
229 
230  // Path of the field of this node
231  std::vector<std::string> path_;
232 
233  // Whether to suppress empty list output.
235 
236  // Whether to preserve original proto field names
238 
239  // Whether to always print enums as ints
241 
242  // Pointer to function for determining whether a field needs to be scrubbed
243  // or not. This callback is owned by the creator of this node.
245 
246  private:
248  };
249 
250  // Creates a new Node and returns it. Caller owns memory of returned object.
251  virtual Node* CreateNewNode(const std::string& name,
252  const google::protobuf::Type* type, NodeKind kind,
253  const DataPiece& data, bool is_placeholder,
254  const std::vector<std::string>& path,
255  bool suppress_empty_list,
256  bool preserve_proto_field_names,
257  bool use_ints_for_enums,
258  FieldScrubCallBack* field_scrub_callback);
259 
260  // Creates a DataPiece containing the default value of the type of the field.
262  const google::protobuf::Field& field, const TypeInfo* typeinfo) {
263  return CreateDefaultDataPieceForField(field, typeinfo, false);
264  }
265 
266  // Same as the above but with a flag to use ints instead of enum names.
267  static DataPiece CreateDefaultDataPieceForField(
268  const google::protobuf::Field& field, const TypeInfo* typeinfo,
269  bool use_ints_for_enums);
270 
271  protected:
272  // Returns a pointer to current Node in tree.
273  Node* current() { return current_; }
274 
275  private:
276  // Populates children of "node" if it is an "any" Node and its real type has
277  // been given.
278  void MaybePopulateChildrenOfAny(Node* node);
279 
280  // Writes the root_ node to ow_ and resets the root_ and current_ pointer to
281  // nullptr.
282  void WriteRoot();
283 
284  // Adds or replaces the data_ of a primitive child node.
285  void RenderDataPiece(StringPiece name, const DataPiece& data);
286 
287  // Returns the default enum value as a DataPiece, or the first enum value if
288  // there is no default. For proto3, where we cannot specify an explicit
289  // default, a zero value will always be returned.
290  static DataPiece FindEnumDefault(const google::protobuf::Field& field,
291  const TypeInfo* typeinfo,
292  bool use_ints_for_enums);
293 
294  // Type information for all the types used in the descriptor. Used to find
295  // google::protobuf::Type of nested messages/enums.
297  // Whether the TypeInfo object is owned by this class.
299  // google::protobuf::Type of the root message type.
301  // Holds copies of strings passed to RenderString.
302  std::vector<std::unique_ptr<std::string>> string_values_;
303 
304  // The current Node. Owned by its parents.
306  // The root Node.
307  std::unique_ptr<Node> root_;
308  // The stack to hold the path of Nodes from current_ to root_;
309  std::stack<Node*> stack_;
310 
311  // Whether to suppress output of empty lists.
313 
314  // Whether to preserve original proto field names
316 
317  // Whether to always print enums as ints
319 
320  // Unique Pointer to function for determining whether a field needs to be
321  // scrubbed or not.
323 
325 
327 };
328 
329 } // namespace converter
330 } // namespace util
331 } // namespace protobuf
332 } // namespace google
333 
334 #include <google/protobuf/port_undef.inc>
335 
336 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
google::protobuf::util::converter::DefaultValueObjectWriter::suppress_empty_list_
bool suppress_empty_list_
Definition: default_value_objectwriter.h:312
google::protobuf::util::converter::DefaultValueObjectWriter::FieldScrubCallBack
ResultCallback2< bool, const std::vector< std::string > &, const google::protobuf::Field * > FieldScrubCallBack
Definition: default_value_objectwriter.h:76
google::protobuf::util::converter::DefaultValueObjectWriter::Node::set_type
void set_type(const google::protobuf::Type *type)
Definition: default_value_objectwriter.h:187
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
google::protobuf::util::converter::DefaultValueObjectWriter
Definition: default_value_objectwriter.h:60
google::protobuf::util::converter::DefaultValueObjectWriter::current
Node * current()
Definition: default_value_objectwriter.h:273
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::util::converter::DefaultValueObjectWriter::NodeKind
NodeKind
Definition: default_value_objectwriter.h:142
google::protobuf::util::converter::DefaultValueObjectWriter::Node::set_is_any
void set_is_any(bool is_any)
Definition: default_value_objectwriter.h:197
google::protobuf::util::converter::DefaultValueObjectWriter::CreateDefaultDataPieceForField
static DataPiece CreateDefaultDataPieceForField(const google::protobuf::Field &field, const TypeInfo *typeinfo)
Definition: default_value_objectwriter.h:261
google::protobuf::util::converter::DefaultValueObjectWriter::typeinfo_
const TypeInfo * typeinfo_
Definition: default_value_objectwriter.h:296
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::util::converter::DefaultValueObjectWriter::field_scrub_callback_
FieldScrubCallBackPtr field_scrub_callback_
Definition: default_value_objectwriter.h:322
google::protobuf::util::converter::ObjectWriter
Definition: object_writer.h:60
google::protobuf::util::converter::DefaultValueObjectWriter::Node::AddChild
void AddChild(Node *child)
Definition: default_value_objectwriter.h:165
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::util::converter::DefaultValueObjectWriter::Node::~Node
virtual ~Node()
Definition: default_value_objectwriter.h:158
google::protobuf::util::converter::DefaultValueObjectWriter::Node::number_of_children
int number_of_children() const
Definition: default_value_objectwriter.h:191
google::protobuf::util::converter::DefaultValueObjectWriter::string_values_
std::vector< std::unique_ptr< std::string > > string_values_
Definition: default_value_objectwriter.h:302
google::protobuf::util::converter::DefaultValueObjectWriter::Node::path_
std::vector< std::string > path_
Definition: default_value_objectwriter.h:231
utility.h
google::protobuf::util::converter::DefaultValueObjectWriter::set_print_enums_as_ints
void set_print_enums_as_ints(bool value)
Definition: default_value_objectwriter.h:139
google::protobuf::ResultCallback2
Definition: callback.h:109
google::protobuf::util::converter::DefaultValueObjectWriter::Node::children_
std::vector< Node * > children_
Definition: default_value_objectwriter.h:223
type_resolver.h
callback.h
google::protobuf::util::converter::DefaultValueObjectWriter::Node::is_any_
bool is_any_
Definition: default_value_objectwriter.h:219
stringpiece.h
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::util::converter::DefaultValueObjectWriter::Node::is_placeholder_
bool is_placeholder_
Definition: default_value_objectwriter.h:228
google::protobuf::util::TypeResolver
Definition: type_resolver.h:52
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
google::protobuf::util::converter::DefaultValueObjectWriter::Node::path
const std::vector< std::string > & path() const
Definition: default_value_objectwriter.h:183
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::converter::DefaultValueObjectWriter::current_
Node * current_
Definition: default_value_objectwriter.h:305
google::protobuf::util::converter::DefaultValueObjectWriter::Node::use_ints_for_enums_
bool use_ints_for_enums_
Definition: default_value_objectwriter.h:240
google::protobuf::util::converter::DefaultValueObjectWriter::FieldScrubCallBackPtr
std::unique_ptr< FieldScrubCallBack > FieldScrubCallBackPtr
Definition: default_value_objectwriter.h:79
google::protobuf::util::converter::TypeInfo
Definition: type_info.h:49
googletest-filter-unittest.child
child
Definition: googletest-filter-unittest.py:62
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
google::protobuf::util::converter::DefaultValueObjectWriter::preserve_proto_field_names_
bool preserve_proto_field_names_
Definition: default_value_objectwriter.h:315
google::protobuf::util::converter::DefaultValueObjectWriter::Node::is_any
bool is_any() const
Definition: default_value_objectwriter.h:195
type_info.h
name_
string name_
Definition: googletest.cc:182
google::protobuf::util::converter::DefaultValueObjectWriter::type_
const google::protobuf::Type & type_
Definition: default_value_objectwriter.h:300
google::protobuf::util::converter::DefaultValueObjectWriter::Node::set_data
void set_data(const DataPiece &data)
Definition: default_value_objectwriter.h:193
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::util::converter::DefaultValueObjectWriter::root_
std::unique_ptr< Node > root_
Definition: default_value_objectwriter.h:307
datapiece.h
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::util::converter::DefaultValueObjectWriter::use_ints_for_enums_
bool use_ints_for_enums_
Definition: default_value_objectwriter.h:318
Field
struct Field Field
Definition: php/ext/google/protobuf/protobuf.h:638
google::protobuf::util::converter::DefaultValueObjectWriter::Node::name
const std::string & name() const
Definition: default_value_objectwriter.h:181
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::util::converter::DefaultValueObjectWriter::Node::preserve_proto_field_names_
bool preserve_proto_field_names_
Definition: default_value_objectwriter.h:237
google::protobuf::util::converter::DefaultValueObjectWriter::ow_
ObjectWriter * ow_
Definition: default_value_objectwriter.h:324
google::protobuf::util::converter::DefaultValueObjectWriter::set_suppress_empty_list
void set_suppress_empty_list(bool value)
Definition: default_value_objectwriter.h:130
google::protobuf::util::converter::DefaultValueObjectWriter::Node::suppress_empty_list_
bool suppress_empty_list_
Definition: default_value_objectwriter.h:234
google::protobuf::util::converter::DefaultValueObjectWriter::Node::data_
DataPiece data_
Definition: default_value_objectwriter.h:221
common.h
google::protobuf::util::converter::DefaultValueObjectWriter::Node::name_
std::string name_
Definition: default_value_objectwriter.h:213
object_writer.h
google::protobuf::util::converter::DefaultValueObjectWriter::own_typeinfo_
bool own_typeinfo_
Definition: default_value_objectwriter.h:298
type_resolver
TypeResolver * type_resolver
Definition: conformance_cpp.cc:97
Type
struct Type Type
Definition: php/ext/google/protobuf/protobuf.h:664
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::util::converter::DefaultValueObjectWriter::Node
Definition: default_value_objectwriter.h:151
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::util::converter::DefaultValueObjectWriter::Node::kind_
NodeKind kind_
Definition: default_value_objectwriter.h:217
google::protobuf::util::converter::DataPiece
Definition: datapiece.h:59
google::protobuf::util::converter::DefaultValueObjectWriter::Node::type_
const google::protobuf::Type * type_
Definition: default_value_objectwriter.h:215
google::protobuf::util::converter::DefaultValueObjectWriter::stack_
std::stack< Node * > stack_
Definition: default_value_objectwriter.h:309
google::protobuf::util::converter::DefaultValueObjectWriter::Node::field_scrub_callback_
FieldScrubCallBack * field_scrub_callback_
Definition: default_value_objectwriter.h:244
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::util::converter::DefaultValueObjectWriter::Node::set_is_placeholder
void set_is_placeholder(bool is_placeholder)
Definition: default_value_objectwriter.h:199
google::protobuf::util::converter::DefaultValueObjectWriter::set_preserve_proto_field_names
void set_preserve_proto_field_names(bool value)
Definition: default_value_objectwriter.h:133
google::protobuf::util::converter::DefaultValueObjectWriter::Node::type
const google::protobuf::Type * type() const
Definition: default_value_objectwriter.h:185
google::protobuf::util::converter::DefaultValueObjectWriter::Node::kind
NodeKind kind() const
Definition: default_value_objectwriter.h:189


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49