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