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 private: 126 enum NodeKind { 127 PRIMITIVE = 0, 128 OBJECT = 1, 129 LIST = 2, 130 MAP = 3, 131 }; 132 133 // "Node" represents a node in the tree that holds the input of 134 // DefaultValueObjectWriter. 135 class LIBPROTOBUF_EXPORT Node { 136 public: 137 Node(const string& name, const google::protobuf::Type* type, NodeKind kind, 138 const DataPiece& data, bool is_placeholder, const vector<string>& path, 139 FieldScrubCallBack* field_scrub_callback); ~Node()140 virtual ~Node() { 141 for (int i = 0; i < children_.size(); ++i) { 142 delete children_[i]; 143 } 144 } 145 146 // Adds a child to this node. Takes ownership of this child. AddChild(Node * child)147 void AddChild(Node* child) { children_.push_back(child); } 148 149 // Finds the child given its name. 150 Node* FindChild(StringPiece name); 151 152 // Populates children of this Node based on its type. If there are already 153 // children created, they will be merged to the result. Caller should pass 154 // in TypeInfo for looking up types of the children. 155 void PopulateChildren(const TypeInfo* typeinfo); 156 157 // If this node is a leaf (has data), writes the current node to the 158 // ObjectWriter; if not, then recursively writes the children to the 159 // ObjectWriter. 160 void WriteTo(ObjectWriter* ow); 161 162 // Accessors name()163 const string& name() const { return name_; } 164 path()165 const vector<string>& path() const { return path_; } 166 type()167 const google::protobuf::Type* type() const { return type_; } 168 set_type(const google::protobuf::Type * type)169 void set_type(const google::protobuf::Type* type) { type_ = type; } 170 kind()171 NodeKind kind() const { return kind_; } 172 number_of_children()173 int number_of_children() const { return children_.size(); } 174 set_data(const DataPiece & data)175 void set_data(const DataPiece& data) { data_ = data; } 176 is_any()177 bool is_any() const { return is_any_; } 178 set_is_any(bool is_any)179 void set_is_any(bool is_any) { is_any_ = is_any; } 180 set_is_placeholder(bool is_placeholder)181 void set_is_placeholder(bool is_placeholder) { 182 is_placeholder_ = is_placeholder; 183 } 184 185 private: 186 // Returns the Value Type of a map given the Type of the map entry and a 187 // TypeInfo instance. 188 const google::protobuf::Type* GetMapValueType( 189 const google::protobuf::Type& entry_type, const TypeInfo* typeinfo); 190 191 // Calls WriteTo() on every child in children_. 192 void WriteChildren(ObjectWriter* ow); 193 194 // The name of this node. 195 string name_; 196 // google::protobuf::Type of this node. Owned by TypeInfo. 197 const google::protobuf::Type* type_; 198 // The kind of this node. 199 NodeKind kind_; 200 // Whether this is a node for "Any". 201 bool is_any_; 202 // The data of this node when it is a leaf node. 203 DataPiece data_; 204 // Children of this node. 205 std::vector<Node*> children_; 206 // Whether this node is a placeholder for an object or list automatically 207 // generated when creating the parent node. Should be set to false after 208 // the parent node's StartObject()/StartList() method is called with this 209 // node's name. 210 bool is_placeholder_; 211 212 // Path of the field of this node 213 std::vector<string> path_; 214 215 // Pointer to function for determining whether a field needs to be scrubbed 216 // or not. This callback is owned by the creator of this node. 217 FieldScrubCallBack* field_scrub_callback_; 218 219 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node); 220 }; 221 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 226 // Writes the root_ node to ow_ and resets the root_ and current_ pointer to 227 // NULL. 228 void WriteRoot(); 229 230 // Creates a DataPiece containing the default value of the type of the field. 231 static DataPiece CreateDefaultDataPieceForField( 232 const google::protobuf::Field& field, const TypeInfo* typeinfo); 233 234 // Adds or replaces the data_ of a primitive child node. 235 void RenderDataPiece(StringPiece name, const DataPiece& data); 236 237 // Returns the default enum value as a DataPiece, or the first enum value if 238 // there is no default. For proto3, where we cannot specify an explicit 239 // default, a zero value will always be returned. 240 static DataPiece FindEnumDefault(const google::protobuf::Field& field, 241 const TypeInfo* typeinfo); 242 243 // Type information for all the types used in the descriptor. Used to find 244 // google::protobuf::Type of nested messages/enums. 245 const TypeInfo* typeinfo_; 246 // Whether the TypeInfo object is owned by this class. 247 bool own_typeinfo_; 248 // google::protobuf::Type of the root message type. 249 const google::protobuf::Type& type_; 250 // Holds copies of strings passed to RenderString. 251 vector<string*> string_values_; 252 253 // The current Node. Owned by its parents. 254 Node* current_; 255 // The root Node. 256 google::protobuf::scoped_ptr<Node> root_; 257 // The stack to hold the path of Nodes from current_ to root_; 258 std::stack<Node*> stack_; 259 260 // Unique Pointer to function for determining whether a field needs to be 261 // scrubbed or not. 262 FieldScrubCallBackPtr field_scrub_callback_; 263 264 ObjectWriter* ow_; 265 266 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter); 267 }; 268 269 } // namespace converter 270 } // namespace util 271 } // namespace protobuf 272 273 } // namespace google 274 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__ 275