OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 use_lower_camel_for_enums_ = value; | 110 use_lower_camel_for_enums_ = value; |
111 } | 111 } |
112 | 112 |
113 // Sets the max recursion depth of proto message to be deserialized. Proto | 113 // Sets the max recursion depth of proto message to be deserialized. Proto |
114 // messages over this depth will fail to be deserialized. | 114 // messages over this depth will fail to be deserialized. |
115 // Default value is 64. | 115 // Default value is 64. |
116 void set_max_recursion_depth(int max_depth) { | 116 void set_max_recursion_depth(int max_depth) { |
117 max_recursion_depth_ = max_depth; | 117 max_recursion_depth_ = max_depth; |
118 } | 118 } |
119 | 119 |
120 | |
121 protected: | 120 protected: |
122 // Writes a proto2 Message to the ObjectWriter. When the given end_tag is | 121 // Writes a proto2 Message to the ObjectWriter. When the given end_tag is |
123 // found this method will complete, allowing it to be used for parsing both | 122 // found this method will complete, allowing it to be used for parsing both |
124 // nested messages (end with 0) and nested groups (end with group end tag). | 123 // nested messages (end with 0) and nested groups (end with group end tag). |
125 // The include_start_and_end parameter allows this method to be called when | 124 // The include_start_and_end parameter allows this method to be called when |
126 // already inside of an object, and skip calling StartObject and EndObject. | 125 // already inside of an object, and skip calling StartObject and EndObject. |
127 virtual util::Status WriteMessage(const google::protobuf::Type& descriptor, | 126 virtual util::Status WriteMessage(const google::protobuf::Type& descriptor, |
128 StringPiece name, const uint32 end_tag, | 127 StringPiece name, const uint32 end_tag, |
129 bool include_start_and_end, | 128 bool include_start_and_end, |
130 ObjectWriter* ow) const; | 129 ObjectWriter* ow) const; |
131 | 130 |
132 // Renders a repeating field (packed or unpacked). Returns the next tag after | |
133 // reading all sequential repeating elements. The caller should use this tag | |
134 // before reading more tags from the stream. | |
135 virtual util::StatusOr<uint32> RenderList( | |
136 const google::protobuf::Field* field, StringPiece name, uint32 list_tag, | |
137 ObjectWriter* ow) const; | |
138 | |
139 // Looks up a field and verify its consistency with wire type in tag. | |
140 const google::protobuf::Field* FindAndVerifyField( | |
141 const google::protobuf::Type& type, uint32 tag) const; | |
142 | |
143 // Renders a field value to the ObjectWriter. | |
144 util::Status RenderField(const google::protobuf::Field* field, | |
145 StringPiece field_name, ObjectWriter* ow) const; | |
146 | |
147 // Reads field value according to Field spec in 'field' and returns the read | |
148 // value as string. This only works for primitive datatypes (no message | |
149 // types). | |
150 const string ReadFieldValueAsString( | |
151 const google::protobuf::Field& field) const; | |
152 | |
153 | |
154 private: | 131 private: |
155 ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream, | 132 ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream, |
156 const TypeInfo* typeinfo, | 133 const TypeInfo* typeinfo, |
157 const google::protobuf::Type& type); | 134 const google::protobuf::Type& type); |
158 // Function that renders a well known type with a modified behavior. | 135 // Function that renders a well known type with a modified behavior. |
159 typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*, | 136 typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*, |
160 const google::protobuf::Type&, | 137 const google::protobuf::Type&, |
161 StringPiece, ObjectWriter*); | 138 StringPiece, ObjectWriter*); |
162 | 139 |
| 140 // Looks up a field and verify its consistency with wire type in tag. |
| 141 const google::protobuf::Field* FindAndVerifyField( |
| 142 const google::protobuf::Type& type, uint32 tag) const; |
| 143 |
163 // TODO(skarvaje): Mark these methods as non-const as they modify internal | 144 // TODO(skarvaje): Mark these methods as non-const as they modify internal |
164 // state (stream_). | 145 // state (stream_). |
165 // | 146 // |
| 147 // Renders a repeating field (packed or unpacked). |
| 148 // Returns the next tag after reading all sequential repeating elements. The |
| 149 // caller should use this tag before reading more tags from the stream. |
| 150 util::StatusOr<uint32> RenderList(const google::protobuf::Field* field, |
| 151 StringPiece name, uint32 list_tag, |
| 152 ObjectWriter* ow) const; |
166 // Renders a NWP map. | 153 // Renders a NWP map. |
167 // Returns the next tag after reading all map entries. The caller should use | 154 // Returns the next tag after reading all map entries. The caller should use |
168 // this tag before reading more tags from the stream. | 155 // this tag before reading more tags from the stream. |
169 util::StatusOr<uint32> RenderMap(const google::protobuf::Field* field, | 156 util::StatusOr<uint32> RenderMap(const google::protobuf::Field* field, |
170 StringPiece name, uint32 list_tag, | 157 StringPiece name, uint32 list_tag, |
171 ObjectWriter* ow) const; | 158 ObjectWriter* ow) const; |
172 | 159 |
173 // Renders a packed repeating field. A packed field is stored as: | 160 // Renders a packed repeating field. A packed field is stored as: |
174 // {tag length item1 item2 item3} instead of the less efficient | 161 // {tag length item1 item2 item3} instead of the less efficient |
175 // {tag item1 tag item2 tag item3}. | 162 // {tag item1 tag item2 tag item3}. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 // Render the "FieldMask" type. | 226 // Render the "FieldMask" type. |
240 static util::Status RenderFieldMask(const ProtoStreamObjectSource* os, | 227 static util::Status RenderFieldMask(const ProtoStreamObjectSource* os, |
241 const google::protobuf::Type& type, | 228 const google::protobuf::Type& type, |
242 StringPiece name, ObjectWriter* ow); | 229 StringPiece name, ObjectWriter* ow); |
243 | 230 |
244 static hash_map<string, TypeRenderer>* renderers_; | 231 static hash_map<string, TypeRenderer>* renderers_; |
245 static void InitRendererMap(); | 232 static void InitRendererMap(); |
246 static void DeleteRendererMap(); | 233 static void DeleteRendererMap(); |
247 static TypeRenderer* FindTypeRenderer(const string& type_url); | 234 static TypeRenderer* FindTypeRenderer(const string& type_url); |
248 | 235 |
| 236 // Renders a field value to the ObjectWriter. |
| 237 util::Status RenderField(const google::protobuf::Field* field, |
| 238 StringPiece field_name, ObjectWriter* ow) const; |
| 239 |
249 // Same as above but renders all non-message field types. Callers don't call | 240 // Same as above but renders all non-message field types. Callers don't call |
250 // this function directly. They just use RenderField. | 241 // this function directly. They just use RenderField. |
251 util::Status RenderNonMessageField(const google::protobuf::Field* field, | 242 util::Status RenderNonMessageField(const google::protobuf::Field* field, |
252 StringPiece field_name, | 243 StringPiece field_name, |
253 ObjectWriter* ow) const; | 244 ObjectWriter* ow) const; |
254 | 245 |
255 | 246 |
| 247 // Reads field value according to Field spec in 'field' and returns the read |
| 248 // value as string. This only works for primitive datatypes (no message |
| 249 // types). |
| 250 const string ReadFieldValueAsString( |
| 251 const google::protobuf::Field& field) const; |
| 252 |
256 // Utility function to detect proto maps. The 'field' MUST be repeated. | 253 // Utility function to detect proto maps. The 'field' MUST be repeated. |
257 bool IsMap(const google::protobuf::Field& field) const; | 254 bool IsMap(const google::protobuf::Field& field) const; |
258 | 255 |
259 // Utility to read int64 and int32 values from a message type in stream_. | 256 // Utility to read int64 and int32 values from a message type in stream_. |
260 // Used for reading google.protobuf.Timestamp and Duration messages. | 257 // Used for reading google.protobuf.Timestamp and Duration messages. |
261 std::pair<int64, int32> ReadSecondsAndNanos( | 258 std::pair<int64, int32> ReadSecondsAndNanos( |
262 const google::protobuf::Type& type) const; | 259 const google::protobuf::Type& type) const; |
263 | 260 |
264 // Helper function to check recursion depth and increment it. It will return | 261 // Helper function to check recursion depth and increment it. It will return |
265 // Status::OK if the current depth is allowed. Otherwise an error is returned. | 262 // Status::OK if the current depth is allowed. Otherwise an error is returned. |
266 // type_name and field_name are used for error reporting. | 263 // type_name and field_name are used for error reporting. |
267 util::Status IncrementRecursionDepth(StringPiece type_name, | 264 util::Status IncrementRecursionDepth(StringPiece type_name, |
268 StringPiece field_name) const; | 265 StringPiece field_name) const; |
269 | 266 |
270 // Input stream to read from. Ownership rests with the caller. | 267 // Input stream to read from. Ownership rests with the caller. |
271 google::protobuf::io::CodedInputStream* stream_; | 268 google::protobuf::io::CodedInputStream* stream_; |
272 | 269 |
273 // Type information for all the types used in the descriptor. Used to find | 270 // Type information for all the types used in the descriptor. Used to find |
274 // google::protobuf::Type of nested messages/enums. | 271 // google::protobuf::Type of nested messages/enums. |
275 const TypeInfo* typeinfo_; | 272 const TypeInfo* typeinfo_; |
276 | |
277 // Whether this class owns the typeinfo_ object. If true the typeinfo_ object | 273 // Whether this class owns the typeinfo_ object. If true the typeinfo_ object |
278 // should be deleted in the destructor. | 274 // should be deleted in the destructor. |
279 bool own_typeinfo_; | 275 bool own_typeinfo_; |
280 | 276 |
281 // google::protobuf::Type of the message source. | 277 // google::protobuf::Type of the message source. |
282 const google::protobuf::Type& type_; | 278 const google::protobuf::Type& type_; |
283 | 279 |
284 | 280 |
285 // Whether to render enums using lowerCamelCase. Defaults to false. | 281 // Whether to render enums using lowerCamelCase. Defaults to false. |
286 bool use_lower_camel_for_enums_; | 282 bool use_lower_camel_for_enums_; |
287 | 283 |
288 // Tracks current recursion depth. | 284 // Tracks current recursion depth. |
289 mutable int recursion_depth_; | 285 mutable int recursion_depth_; |
290 | 286 |
291 // Maximum allowed recursion depth. | 287 // Maximum allowed recursion depth. |
292 int max_recursion_depth_; | 288 int max_recursion_depth_; |
293 | 289 |
294 // Whether to render unknown fields. | |
295 bool render_unknown_fields_; | |
296 | |
297 // Whether to add trailing zeros for timestamp and duration. | |
298 bool add_trailing_zeros_for_timestamp_and_duration_; | |
299 | |
300 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectSource); | 290 GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectSource); |
301 }; | 291 }; |
302 | 292 |
303 } // namespace converter | 293 } // namespace converter |
304 } // namespace util | 294 } // namespace util |
305 } // namespace protobuf | 295 } // namespace protobuf |
306 | 296 |
307 } // namespace google | 297 } // namespace google |
308 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__ | 298 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__ |
OLD | NEW |