default_value_objectwriter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
  31. #define GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
  32. #include <memory>
  33. #include <stack>
  34. #include <vector>
  35. #include <google/protobuf/stubs/callback.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/util/internal/type_info.h>
  38. #include <google/protobuf/util/internal/datapiece.h>
  39. #include <google/protobuf/util/internal/object_writer.h>
  40. #include <google/protobuf/util/internal/utility.h>
  41. #include <google/protobuf/util/type_resolver.h>
  42. #include <google/protobuf/stubs/stringpiece.h>
  43. namespace google {
  44. namespace protobuf {
  45. namespace util {
  46. namespace converter {
  47. // An ObjectWriter that renders non-repeated primitive fields of proto messages
  48. // with their default values. DefaultValueObjectWriter holds objects, lists and
  49. // fields it receives in a tree structure and writes them out to another
  50. // ObjectWriter when EndObject() is called on the root object. It also writes
  51. // out all non-repeated primitive fields that haven't been explicitly rendered
  52. // with their default values (0 for numbers, "" for strings, etc).
  53. class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
  54. public:
  55. // A Callback function to check whether a field needs to be scrubbed.
  56. //
  57. // Returns true if the field should not be present in the output. Returns
  58. // false otherwise.
  59. //
  60. // The 'path' parameter is a vector of path to the field from root. For
  61. // example: if a nested field "a.b.c" (b is the parent message field of c and
  62. // a is the parent message field of b), then the vector should contain { "a",
  63. // "b", "c" }.
  64. //
  65. // The Field* should point to the google::protobuf::Field of "c".
  66. typedef ResultCallback2<bool /*return*/,
  67. const std::vector<string>& /*path of the field*/,
  68. const google::protobuf::Field* /*field*/>
  69. FieldScrubCallBack;
  70. // A unique pointer to a DefaultValueObjectWriter::FieldScrubCallBack.
  71. typedef std::unique_ptr<FieldScrubCallBack> FieldScrubCallBackPtr;
  72. DefaultValueObjectWriter(TypeResolver* type_resolver,
  73. const google::protobuf::Type& type,
  74. ObjectWriter* ow);
  75. virtual ~DefaultValueObjectWriter();
  76. // ObjectWriter methods.
  77. virtual DefaultValueObjectWriter* StartObject(StringPiece name);
  78. virtual DefaultValueObjectWriter* EndObject();
  79. virtual DefaultValueObjectWriter* StartList(StringPiece name);
  80. virtual DefaultValueObjectWriter* EndList();
  81. virtual DefaultValueObjectWriter* RenderBool(StringPiece name, bool value);
  82. virtual DefaultValueObjectWriter* RenderInt32(StringPiece name, int32 value);
  83. virtual DefaultValueObjectWriter* RenderUint32(StringPiece name,
  84. uint32 value);
  85. virtual DefaultValueObjectWriter* RenderInt64(StringPiece name, int64 value);
  86. virtual DefaultValueObjectWriter* RenderUint64(StringPiece name,
  87. uint64 value);
  88. virtual DefaultValueObjectWriter* RenderDouble(StringPiece name,
  89. double value);
  90. virtual DefaultValueObjectWriter* RenderFloat(StringPiece name, float value);
  91. virtual DefaultValueObjectWriter* RenderString(StringPiece name,
  92. StringPiece value);
  93. virtual DefaultValueObjectWriter* RenderBytes(StringPiece name,
  94. StringPiece value);
  95. virtual DefaultValueObjectWriter* RenderNull(StringPiece name);
  96. // Register the callback for scrubbing of fields. Owership of
  97. // field_scrub_callback pointer is also transferred to this class
  98. void RegisterFieldScrubCallBack(FieldScrubCallBackPtr field_scrub_callback);
  99. // If set to true, empty lists are suppressed from output when default values
  100. // are written.
  101. void set_suppress_empty_list(bool value) { suppress_empty_list_ = value; }
  102. // If set to true, original proto field names are used
  103. void set_preserve_proto_field_names(bool value) {
  104. preserve_proto_field_names_ = value;
  105. }
  106. // If set to true, enums are rendered as ints from output when default values
  107. // are written.
  108. void set_print_enums_as_ints(bool value) {
  109. use_ints_for_enums_ = value;
  110. }
  111. protected:
  112. enum NodeKind {
  113. PRIMITIVE = 0,
  114. OBJECT = 1,
  115. LIST = 2,
  116. MAP = 3,
  117. };
  118. // "Node" represents a node in the tree that holds the input of
  119. // DefaultValueObjectWriter.
  120. class LIBPROTOBUF_EXPORT Node {
  121. public:
  122. Node(const string& name, const google::protobuf::Type* type, NodeKind kind,
  123. const DataPiece& data, bool is_placeholder,
  124. const std::vector<string>& path, bool suppress_empty_list,
  125. FieldScrubCallBack* field_scrub_callback);
  126. Node(const string& name, const google::protobuf::Type* type, NodeKind kind,
  127. const DataPiece& data, bool is_placeholder,
  128. const std::vector<string>& path, bool suppress_empty_list,
  129. bool preserve_proto_field_names, bool use_ints_for_enums,
  130. FieldScrubCallBack* field_scrub_callback);
  131. virtual ~Node() {
  132. for (int i = 0; i < children_.size(); ++i) {
  133. delete children_[i];
  134. }
  135. }
  136. // Adds a child to this node. Takes ownership of this child.
  137. void AddChild(Node* child) { children_.push_back(child); }
  138. // Finds the child given its name.
  139. Node* FindChild(StringPiece name);
  140. // Populates children of this Node based on its type. If there are already
  141. // children created, they will be merged to the result. Caller should pass
  142. // in TypeInfo for looking up types of the children.
  143. virtual void PopulateChildren(const TypeInfo* typeinfo);
  144. // If this node is a leaf (has data), writes the current node to the
  145. // ObjectWriter; if not, then recursively writes the children to the
  146. // ObjectWriter.
  147. virtual void WriteTo(ObjectWriter* ow);
  148. // Accessors
  149. const string& name() const { return name_; }
  150. const std::vector<string>& path() const { return path_; }
  151. const google::protobuf::Type* type() const { return type_; }
  152. void set_type(const google::protobuf::Type* type) { type_ = type; }
  153. NodeKind kind() const { return kind_; }
  154. int number_of_children() const { return children_.size(); }
  155. void set_data(const DataPiece& data) { data_ = data; }
  156. bool is_any() const { return is_any_; }
  157. void set_is_any(bool is_any) { is_any_ = is_any; }
  158. void set_is_placeholder(bool is_placeholder) {
  159. is_placeholder_ = is_placeholder;
  160. }
  161. protected:
  162. // Returns the Value Type of a map given the Type of the map entry and a
  163. // TypeInfo instance.
  164. const google::protobuf::Type* GetMapValueType(
  165. const google::protobuf::Type& entry_type, const TypeInfo* typeinfo);
  166. // Calls WriteTo() on every child in children_.
  167. void WriteChildren(ObjectWriter* ow);
  168. // The name of this node.
  169. string name_;
  170. // google::protobuf::Type of this node. Owned by TypeInfo.
  171. const google::protobuf::Type* type_;
  172. // The kind of this node.
  173. NodeKind kind_;
  174. // Whether this is a node for "Any".
  175. bool is_any_;
  176. // The data of this node when it is a leaf node.
  177. DataPiece data_;
  178. // Children of this node.
  179. std::vector<Node*> children_;
  180. // Whether this node is a placeholder for an object or list automatically
  181. // generated when creating the parent node. Should be set to false after
  182. // the parent node's StartObject()/StartList() method is called with this
  183. // node's name.
  184. bool is_placeholder_;
  185. // Path of the field of this node
  186. std::vector<string> path_;
  187. // Whether to suppress empty list output.
  188. bool suppress_empty_list_;
  189. // Whether to preserve original proto field names
  190. bool preserve_proto_field_names_;
  191. // Whether to always print enums as ints
  192. bool use_ints_for_enums_;
  193. // Pointer to function for determining whether a field needs to be scrubbed
  194. // or not. This callback is owned by the creator of this node.
  195. FieldScrubCallBack* field_scrub_callback_;
  196. private:
  197. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node);
  198. };
  199. // Creates a new Node and returns it. Caller owns memory of returned object.
  200. virtual Node* CreateNewNode(const string& name,
  201. const google::protobuf::Type* type, NodeKind kind,
  202. const DataPiece& data, bool is_placeholder,
  203. const std::vector<string>& path,
  204. bool suppress_empty_list,
  205. FieldScrubCallBack* field_scrub_callback);
  206. // Creates a new Node and returns it. Caller owns memory of returned object.
  207. virtual Node* CreateNewNode(const string& name,
  208. const google::protobuf::Type* type, NodeKind kind,
  209. const DataPiece& data, bool is_placeholder,
  210. const std::vector<string>& path,
  211. bool suppress_empty_list,
  212. bool preserve_proto_field_names,
  213. bool use_ints_for_enums,
  214. FieldScrubCallBack* field_scrub_callback);
  215. // Creates a DataPiece containing the default value of the type of the field.
  216. static DataPiece CreateDefaultDataPieceForField(
  217. const google::protobuf::Field& field, const TypeInfo* typeinfo, bool use_ints_for_enums);
  218. protected:
  219. // Returns a pointer to current Node in tree.
  220. Node* current() { return current_; }
  221. private:
  222. // Populates children of "node" if it is an "any" Node and its real type has
  223. // been given.
  224. void MaybePopulateChildrenOfAny(Node* node);
  225. // Writes the root_ node to ow_ and resets the root_ and current_ pointer to
  226. // nullptr.
  227. void WriteRoot();
  228. // Adds or replaces the data_ of a primitive child node.
  229. void RenderDataPiece(StringPiece name, const DataPiece& data);
  230. // Returns the default enum value as a DataPiece, or the first enum value if
  231. // there is no default. For proto3, where we cannot specify an explicit
  232. // default, a zero value will always be returned.
  233. static DataPiece FindEnumDefault(const google::protobuf::Field& field,
  234. const TypeInfo* typeinfo,
  235. bool use_ints_for_enums);
  236. // Type information for all the types used in the descriptor. Used to find
  237. // google::protobuf::Type of nested messages/enums.
  238. const TypeInfo* typeinfo_;
  239. // Whether the TypeInfo object is owned by this class.
  240. bool own_typeinfo_;
  241. // google::protobuf::Type of the root message type.
  242. const google::protobuf::Type& type_;
  243. // Holds copies of strings passed to RenderString.
  244. std::vector<string*> string_values_;
  245. // The current Node. Owned by its parents.
  246. Node* current_;
  247. // The root Node.
  248. std::unique_ptr<Node> root_;
  249. // The stack to hold the path of Nodes from current_ to root_;
  250. std::stack<Node*> stack_;
  251. // Whether to suppress output of empty lists.
  252. bool suppress_empty_list_;
  253. // Whether to preserve original proto field names
  254. bool preserve_proto_field_names_;
  255. // Whether to always print enums as ints
  256. bool use_ints_for_enums_;
  257. // Unique Pointer to function for determining whether a field needs to be
  258. // scrubbed or not.
  259. FieldScrubCallBackPtr field_scrub_callback_;
  260. ObjectWriter* ow_;
  261. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter);
  262. };
  263. } // namespace converter
  264. } // namespace util
  265. } // namespace protobuf
  266. } // namespace google
  267. #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__