protostream_objectsource.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_PROTOSTREAM_OBJECTSOURCE_H__
  31. #define GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
  32. #include <functional>
  33. #include <google/protobuf/stubs/hash.h>
  34. #include <string>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/type.pb.h>
  37. #include <google/protobuf/util/internal/object_source.h>
  38. #include <google/protobuf/util/internal/object_writer.h>
  39. #include <google/protobuf/util/internal/type_info.h>
  40. #include <google/protobuf/util/type_resolver.h>
  41. #include <google/protobuf/stubs/stringpiece.h>
  42. #include <google/protobuf/stubs/status.h>
  43. #include <google/protobuf/stubs/statusor.h>
  44. namespace google {
  45. namespace protobuf {
  46. class Field;
  47. class Type;
  48. } // namespace protobuf
  49. namespace protobuf {
  50. namespace util {
  51. namespace converter {
  52. class TypeInfo;
  53. // An ObjectSource that can parse a stream of bytes as a protocol buffer.
  54. // Its WriteTo() method can be given an ObjectWriter.
  55. // This implementation uses a google.protobuf.Type for tag and name lookup.
  56. // The field names are converted into lower camel-case when writing to the
  57. // ObjectWriter.
  58. //
  59. // Sample usage: (suppose input is: string proto)
  60. // ArrayInputStream arr_stream(proto.data(), proto.size());
  61. // CodedInputStream in_stream(&arr_stream);
  62. // ProtoStreamObjectSource os(&in_stream, /*ServiceTypeInfo*/ typeinfo,
  63. // <your message google::protobuf::Type>);
  64. //
  65. // Status status = os.WriteTo(<some ObjectWriter>);
  66. class LIBPROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
  67. public:
  68. ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream,
  69. TypeResolver* type_resolver,
  70. const google::protobuf::Type& type);
  71. virtual ~ProtoStreamObjectSource();
  72. virtual util::Status NamedWriteTo(StringPiece name, ObjectWriter* ow) const;
  73. // Sets whether or not to use lowerCamelCase casing for enum values. If set to
  74. // false, enum values are output without any case conversions.
  75. //
  76. // For example, if we have an enum:
  77. // enum Type {
  78. // ACTION_AND_ADVENTURE = 1;
  79. // }
  80. // Type type = 20;
  81. //
  82. // And this option is set to true. Then the rendered "type" field will have
  83. // the string "actionAndAdventure".
  84. // {
  85. // ...
  86. // "type": "actionAndAdventure",
  87. // ...
  88. // }
  89. //
  90. // If set to false, the rendered "type" field will have the string
  91. // "ACTION_AND_ADVENTURE".
  92. // {
  93. // ...
  94. // "type": "ACTION_AND_ADVENTURE",
  95. // ...
  96. // }
  97. void set_use_lower_camel_for_enums(bool value) {
  98. use_lower_camel_for_enums_ = value;
  99. }
  100. // Sets whether to always output enums as ints, by default this is off, and
  101. // enums are rendered as strings.
  102. void set_use_ints_for_enums(bool value) { use_ints_for_enums_ = value; }
  103. // Sets whether to use original proto field names
  104. void set_preserve_proto_field_names(bool value) {
  105. preserve_proto_field_names_ = value;
  106. }
  107. // Sets the max recursion depth of proto message to be deserialized. Proto
  108. // messages over this depth will fail to be deserialized.
  109. // Default value is 64.
  110. void set_max_recursion_depth(int max_depth) {
  111. max_recursion_depth_ = max_depth;
  112. }
  113. protected:
  114. // Writes a proto2 Message to the ObjectWriter. When the given end_tag is
  115. // found this method will complete, allowing it to be used for parsing both
  116. // nested messages (end with 0) and nested groups (end with group end tag).
  117. // The include_start_and_end parameter allows this method to be called when
  118. // already inside of an object, and skip calling StartObject and EndObject.
  119. virtual util::Status WriteMessage(const google::protobuf::Type& descriptor,
  120. StringPiece name, const uint32 end_tag,
  121. bool include_start_and_end,
  122. ObjectWriter* ow) const;
  123. // Renders a repeating field (packed or unpacked). Returns the next tag after
  124. // reading all sequential repeating elements. The caller should use this tag
  125. // before reading more tags from the stream.
  126. virtual util::StatusOr<uint32> RenderList(
  127. const google::protobuf::Field* field, StringPiece name, uint32 list_tag,
  128. ObjectWriter* ow) const;
  129. // Looks up a field and verify its consistency with wire type in tag.
  130. const google::protobuf::Field* FindAndVerifyField(
  131. const google::protobuf::Type& type, uint32 tag) const;
  132. // Renders a field value to the ObjectWriter.
  133. util::Status RenderField(const google::protobuf::Field* field,
  134. StringPiece field_name, ObjectWriter* ow) const;
  135. // Reads field value according to Field spec in 'field' and returns the read
  136. // value as string. This only works for primitive datatypes (no message
  137. // types).
  138. const string ReadFieldValueAsString(
  139. const google::protobuf::Field& field) const;
  140. private:
  141. ProtoStreamObjectSource(google::protobuf::io::CodedInputStream* stream,
  142. const TypeInfo* typeinfo,
  143. const google::protobuf::Type& type);
  144. // Function that renders a well known type with a modified behavior.
  145. typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*,
  146. const google::protobuf::Type&,
  147. StringPiece, ObjectWriter*);
  148. // TODO(skarvaje): Mark these methods as non-const as they modify internal
  149. // state (stream_).
  150. //
  151. // Renders a NWP map.
  152. // Returns the next tag after reading all map entries. The caller should use
  153. // this tag before reading more tags from the stream.
  154. util::StatusOr<uint32> RenderMap(const google::protobuf::Field* field,
  155. StringPiece name, uint32 list_tag,
  156. ObjectWriter* ow) const;
  157. // Renders a packed repeating field. A packed field is stored as:
  158. // {tag length item1 item2 item3} instead of the less efficient
  159. // {tag item1 tag item2 tag item3}.
  160. util::Status RenderPacked(const google::protobuf::Field* field,
  161. ObjectWriter* ow) const;
  162. // Renders a google.protobuf.Timestamp value to ObjectWriter
  163. static util::Status RenderTimestamp(const ProtoStreamObjectSource* os,
  164. const google::protobuf::Type& type,
  165. StringPiece name, ObjectWriter* ow);
  166. // Renders a google.protobuf.Duration value to ObjectWriter
  167. static util::Status RenderDuration(const ProtoStreamObjectSource* os,
  168. const google::protobuf::Type& type,
  169. StringPiece name, ObjectWriter* ow);
  170. // Following RenderTYPE functions render well known types in
  171. // google/protobuf/wrappers.proto corresponding to TYPE.
  172. static util::Status RenderDouble(const ProtoStreamObjectSource* os,
  173. const google::protobuf::Type& type,
  174. StringPiece name, ObjectWriter* ow);
  175. static util::Status RenderFloat(const ProtoStreamObjectSource* os,
  176. const google::protobuf::Type& type,
  177. StringPiece name, ObjectWriter* ow);
  178. static util::Status RenderInt64(const ProtoStreamObjectSource* os,
  179. const google::protobuf::Type& type,
  180. StringPiece name, ObjectWriter* ow);
  181. static util::Status RenderUInt64(const ProtoStreamObjectSource* os,
  182. const google::protobuf::Type& type,
  183. StringPiece name, ObjectWriter* ow);
  184. static util::Status RenderInt32(const ProtoStreamObjectSource* os,
  185. const google::protobuf::Type& type,
  186. StringPiece name, ObjectWriter* ow);
  187. static util::Status RenderUInt32(const ProtoStreamObjectSource* os,
  188. const google::protobuf::Type& type,
  189. StringPiece name, ObjectWriter* ow);
  190. static util::Status RenderBool(const ProtoStreamObjectSource* os,
  191. const google::protobuf::Type& type,
  192. StringPiece name, ObjectWriter* ow);
  193. static util::Status RenderString(const ProtoStreamObjectSource* os,
  194. const google::protobuf::Type& type,
  195. StringPiece name, ObjectWriter* ow);
  196. static util::Status RenderBytes(const ProtoStreamObjectSource* os,
  197. const google::protobuf::Type& type,
  198. StringPiece name, ObjectWriter* ow);
  199. // Renders a google.protobuf.Struct to ObjectWriter.
  200. static util::Status RenderStruct(const ProtoStreamObjectSource* os,
  201. const google::protobuf::Type& type,
  202. StringPiece name, ObjectWriter* ow);
  203. // Helper to render google.protobuf.Struct's Value fields to ObjectWriter.
  204. static util::Status RenderStructValue(const ProtoStreamObjectSource* os,
  205. const google::protobuf::Type& type,
  206. StringPiece name, ObjectWriter* ow);
  207. // Helper to render google.protobuf.Struct's ListValue fields to ObjectWriter.
  208. static util::Status RenderStructListValue(
  209. const ProtoStreamObjectSource* os, const google::protobuf::Type& type,
  210. StringPiece name, ObjectWriter* ow);
  211. // Render the "Any" type.
  212. static util::Status RenderAny(const ProtoStreamObjectSource* os,
  213. const google::protobuf::Type& type,
  214. StringPiece name, ObjectWriter* ow);
  215. // Render the "FieldMask" type.
  216. static util::Status RenderFieldMask(const ProtoStreamObjectSource* os,
  217. const google::protobuf::Type& type,
  218. StringPiece name, ObjectWriter* ow);
  219. static hash_map<string, TypeRenderer>* renderers_;
  220. static void InitRendererMap();
  221. static void DeleteRendererMap();
  222. static TypeRenderer* FindTypeRenderer(const string& type_url);
  223. // Same as above but renders all non-message field types. Callers don't call
  224. // this function directly. They just use RenderField.
  225. util::Status RenderNonMessageField(const google::protobuf::Field* field,
  226. StringPiece field_name,
  227. ObjectWriter* ow) const;
  228. // Utility function to detect proto maps. The 'field' MUST be repeated.
  229. bool IsMap(const google::protobuf::Field& field) const;
  230. // Utility to read int64 and int32 values from a message type in stream_.
  231. // Used for reading google.protobuf.Timestamp and Duration messages.
  232. std::pair<int64, int32> ReadSecondsAndNanos(
  233. const google::protobuf::Type& type) const;
  234. // Helper function to check recursion depth and increment it. It will return
  235. // Status::OK if the current depth is allowed. Otherwise an error is returned.
  236. // type_name and field_name are used for error reporting.
  237. util::Status IncrementRecursionDepth(StringPiece type_name,
  238. StringPiece field_name) const;
  239. // Input stream to read from. Ownership rests with the caller.
  240. google::protobuf::io::CodedInputStream* stream_;
  241. // Type information for all the types used in the descriptor. Used to find
  242. // google::protobuf::Type of nested messages/enums.
  243. const TypeInfo* typeinfo_;
  244. // Whether this class owns the typeinfo_ object. If true the typeinfo_ object
  245. // should be deleted in the destructor.
  246. bool own_typeinfo_;
  247. // google::protobuf::Type of the message source.
  248. const google::protobuf::Type& type_;
  249. // Whether to render enums using lowerCamelCase. Defaults to false.
  250. bool use_lower_camel_for_enums_;
  251. // Whether to render enums as ints always. Defaults to false.
  252. bool use_ints_for_enums_;
  253. // Whether to preserve proto field names
  254. bool preserve_proto_field_names_;
  255. // Tracks current recursion depth.
  256. mutable int recursion_depth_;
  257. // Maximum allowed recursion depth.
  258. int max_recursion_depth_;
  259. // Whether to render unknown fields.
  260. bool render_unknown_fields_;
  261. // Whether to render unknown enum values.
  262. bool render_unknown_enum_values_;
  263. // Whether to add trailing zeros for timestamp and duration.
  264. bool add_trailing_zeros_for_timestamp_and_duration_;
  265. GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(ProtoStreamObjectSource);
  266. };
  267. } // namespace converter
  268. } // namespace util
  269. } // namespace protobuf
  270. } // namespace google
  271. #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__