protostream_objectsource.h
Go to the documentation of this file.
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_PROTOSTREAM_OBJECTSOURCE_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
33 
34 #include <functional>
35 #include <string>
36 #include <unordered_map>
37 
48 
49 
50 #include <google/protobuf/port_def.inc>
51 
52 namespace google {
53 namespace protobuf {
54 namespace util {
55 namespace converter {
56 
57 class TypeInfo;
58 
59 // An ObjectSource that can parse a stream of bytes as a protocol buffer.
60 // Its WriteTo() method can be given an ObjectWriter.
61 // This implementation uses a google.protobuf.Type for tag and name lookup.
62 // The field names are converted into lower camel-case when writing to the
63 // ObjectWriter.
64 //
65 // Sample usage: (suppose input is: string proto)
66 // ArrayInputStream arr_stream(proto.data(), proto.size());
67 // CodedInputStream in_stream(&arr_stream);
68 // ProtoStreamObjectSource os(&in_stream, /*ServiceTypeInfo*/ typeinfo,
69 // <your message google::protobuf::Type>);
70 //
71 // Status status = os.WriteTo(<some ObjectWriter>);
72 class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
73  public:
77 
78  ~ProtoStreamObjectSource() override;
79 
80  util::Status NamedWriteTo(StringPiece name,
81  ObjectWriter* ow) const override;
82 
83  // Sets whether or not to use lowerCamelCase casing for enum values. If set to
84  // false, enum values are output without any case conversions.
85  //
86  // For example, if we have an enum:
87  // enum Type {
88  // ACTION_AND_ADVENTURE = 1;
89  // }
90  // Type type = 20;
91  //
92  // And this option is set to true. Then the rendered "type" field will have
93  // the string "actionAndAdventure".
94  // {
95  // ...
96  // "type": "actionAndAdventure",
97  // ...
98  // }
99  //
100  // If set to false, the rendered "type" field will have the string
101  // "ACTION_AND_ADVENTURE".
102  // {
103  // ...
104  // "type": "ACTION_AND_ADVENTURE",
105  // ...
106  // }
108  use_lower_camel_for_enums_ = value;
109  }
110 
111  // Sets whether to always output enums as ints, by default this is off, and
112  // enums are rendered as strings.
113  void set_use_ints_for_enums(bool value) { use_ints_for_enums_ = value; }
114 
115  // Sets whether to use original proto field names
117  preserve_proto_field_names_ = value;
118  }
119 
120  // Sets the max recursion depth of proto message to be deserialized. Proto
121  // messages over this depth will fail to be deserialized.
122  // Default value is 64.
123  void set_max_recursion_depth(int max_depth) {
124  max_recursion_depth_ = max_depth;
125  }
126 
127 
128  protected:
129  // Writes a proto2 Message to the ObjectWriter. When the given end_tag is
130  // found this method will complete, allowing it to be used for parsing both
131  // nested messages (end with 0) and nested groups (end with group end tag).
132  // The include_start_and_end parameter allows this method to be called when
133  // already inside of an object, and skip calling StartObject and EndObject.
134  virtual util::Status WriteMessage(const google::protobuf::Type& descriptor,
136  const uint32 end_tag,
137  bool include_start_and_end,
138  ObjectWriter* ow) const;
139 
140  // Renders a repeating field (packed or unpacked). Returns the next tag after
141  // reading all sequential repeating elements. The caller should use this tag
142  // before reading more tags from the stream.
143  virtual util::StatusOr<uint32> RenderList(
145  uint32 list_tag, ObjectWriter* ow) const;
146 
147  // Looks up a field and verify its consistency with wire type in tag.
148  const google::protobuf::Field* FindAndVerifyField(
149  const google::protobuf::Type& type, uint32 tag) const;
150 
151  // Renders a field value to the ObjectWriter.
152  virtual util::Status RenderField(const google::protobuf::Field* field,
153  StringPiece field_name,
154  ObjectWriter* ow) const;
155 
156  // Reads field value according to Field spec in 'field' and returns the read
157  // value as string. This only works for primitive datatypes (no message
158  // types).
159  const std::string ReadFieldValueAsString(
160  const google::protobuf::Field& field) const;
161 
162 
163  private:
165  const TypeInfo* typeinfo,
167  // Function that renders a well known type with a modified behavior.
168  typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*,
169  const google::protobuf::Type&,
171 
172  // TODO(skarvaje): Mark these methods as non-const as they modify internal
173  // state (stream_).
174  //
175  // Renders a NWP map.
176  // Returns the next tag after reading all map entries. The caller should use
177  // this tag before reading more tags from the stream.
179  StringPiece name, uint32 list_tag,
180  ObjectWriter* ow) const;
181 
182  // Renders a packed repeating field. A packed field is stored as:
183  // {tag length item1 item2 item3} instead of the less efficient
184  // {tag item1 tag item2 tag item3}.
185  util::Status RenderPacked(const google::protobuf::Field* field,
186  ObjectWriter* ow) const;
187 
188 
189  // Renders a google.protobuf.Timestamp value to ObjectWriter
190  static util::Status RenderTimestamp(const ProtoStreamObjectSource* os,
193  ObjectWriter* ow);
194 
195  // Renders a google.protobuf.Duration value to ObjectWriter
196  static util::Status RenderDuration(const ProtoStreamObjectSource* os,
199  ObjectWriter* ow);
200 
201  // Following RenderTYPE functions render well known types in
202  // google/protobuf/wrappers.proto corresponding to TYPE.
203  static util::Status RenderDouble(const ProtoStreamObjectSource* os,
206  static util::Status RenderFloat(const ProtoStreamObjectSource* os,
209  static util::Status RenderInt64(const ProtoStreamObjectSource* os,
212  static util::Status RenderUInt64(const ProtoStreamObjectSource* os,
215  static util::Status RenderInt32(const ProtoStreamObjectSource* os,
218  static util::Status RenderUInt32(const ProtoStreamObjectSource* os,
221  static util::Status RenderBool(const ProtoStreamObjectSource* os,
224  static util::Status RenderString(const ProtoStreamObjectSource* os,
227  static util::Status RenderBytes(const ProtoStreamObjectSource* os,
230 
231  // Renders a google.protobuf.Struct to ObjectWriter.
232  static util::Status RenderStruct(const ProtoStreamObjectSource* os,
235 
236  // Helper to render google.protobuf.Struct's Value fields to ObjectWriter.
237  static util::Status RenderStructValue(const ProtoStreamObjectSource* os,
240  ObjectWriter* ow);
241 
242  // Helper to render google.protobuf.Struct's ListValue fields to ObjectWriter.
243  static util::Status RenderStructListValue(
246 
247  // Render the "Any" type.
248  static util::Status RenderAny(const ProtoStreamObjectSource* os,
251 
252  // Render the "FieldMask" type.
253  static util::Status RenderFieldMask(const ProtoStreamObjectSource* os,
256  ObjectWriter* ow);
257 
258  static std::unordered_map<std::string, TypeRenderer>* renderers_;
259  static void InitRendererMap();
260  static void DeleteRendererMap();
261  static TypeRenderer* FindTypeRenderer(const std::string& type_url);
262 
263  // Same as above but renders all non-message field types. Callers don't call
264  // this function directly. They just use RenderField.
265  util::Status RenderNonMessageField(const google::protobuf::Field* field,
266  StringPiece field_name,
267  ObjectWriter* ow) const;
268 
269 
270  // Utility function to detect proto maps. The 'field' MUST be repeated.
271  bool IsMap(const google::protobuf::Field& field) const;
272 
273  // Utility to read int64 and int32 values from a message type in stream_.
274  // Used for reading google.protobuf.Timestamp and Duration messages.
275  std::pair<int64, int32> ReadSecondsAndNanos(
276  const google::protobuf::Type& type) const;
277 
278  // Helper function to check recursion depth and increment it. It will return
279  // Status::OK if the current depth is allowed. Otherwise an error is returned.
280  // type_name and field_name are used for error reporting.
281  util::Status IncrementRecursionDepth(StringPiece type_name,
282  StringPiece field_name) const;
283 
284  // Input stream to read from. Ownership rests with the caller.
286 
287  // Type information for all the types used in the descriptor. Used to find
288  // google::protobuf::Type of nested messages/enums.
290 
291  // Whether this class owns the typeinfo_ object. If true the typeinfo_ object
292  // should be deleted in the destructor.
294 
295  // google::protobuf::Type of the message source.
297 
298 
299  // Whether to render enums using lowerCamelCase. Defaults to false.
301 
302  // Whether to render enums as ints always. Defaults to false.
304 
305  // Whether to preserve proto field names
307 
308  // Tracks current recursion depth.
309  mutable int recursion_depth_;
310 
311  // Maximum allowed recursion depth.
313 
314  // Whether to render unknown fields.
316 
317  // Whether to render unknown enum values.
319 
320  // Whether to add trailing zeros for timestamp and duration.
322 
323  // Whether output the empty object or not. If false, output JSON string like:
324  // "'objectName' : {}". If true, then no outputs. This only happens when all
325  // the fields of the message are filtered out by field mask.
327 
329 };
330 
331 } // namespace converter
332 } // namespace util
333 } // namespace protobuf
334 } // namespace google
335 
336 #include <google/protobuf/port_undef.inc>
337 
338 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
stream
GLuint GLuint stream
Definition: glcorearb.h:3946
google::protobuf::util::converter::ProtoStreamObjectSource::use_lower_camel_for_enums_
bool use_lower_camel_for_enums_
Definition: protostream_objectsource.h:300
google::protobuf::util::converter::ProtoStreamObjectSource::use_ints_for_enums_
bool use_ints_for_enums_
Definition: protostream_objectsource.h:303
google::protobuf::util::converter::ProtoStreamObjectSource::own_typeinfo_
bool own_typeinfo_
Definition: protostream_objectsource.h:293
google::protobuf::uint32
uint32_t uint32
Definition: protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::util::converter::ProtoStreamObjectSource::set_preserve_proto_field_names
void set_preserve_proto_field_names(bool value)
Definition: protostream_objectsource.h:116
google::protobuf::util::converter::ObjectWriter
Definition: object_writer.h:60
statusor.h
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::util::converter::ProtoStreamObjectSource::set_max_recursion_depth
void set_max_recursion_depth(int max_depth)
Definition: protostream_objectsource.h:123
google::protobuf::util::converter::ProtoStreamObjectSource::renderers_
static std::unordered_map< std::string, TypeRenderer > * renderers_
Definition: protostream_objectsource.h:258
google::protobuf::util::converter::ProtoStreamObjectSource::add_trailing_zeros_for_timestamp_and_duration_
bool add_trailing_zeros_for_timestamp_and_duration_
Definition: protostream_objectsource.h:321
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::util::converter::ProtoStreamObjectSource::set_use_ints_for_enums
void set_use_ints_for_enums(bool value)
Definition: protostream_objectsource.h:113
type_resolver.h
google::protobuf::util::converter::ObjectSource
Definition: object_source.h:53
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
#define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition: macros.h:45
stringpiece.h
google::protobuf::util::TypeResolver
Definition: type_resolver.h:52
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::converter::ProtoStreamObjectSource::recursion_depth_
int recursion_depth_
Definition: protostream_objectsource.h:309
google::protobuf::util::converter::IsMap
bool IsMap(const google::protobuf::Field &field, const google::protobuf::Type &type)
Definition: utility.cc:360
google::protobuf::util::converter::TypeInfo
Definition: type_info.h:49
google::protobuf::util::converter::ProtoStreamObjectSource::render_unknown_enum_values_
bool render_unknown_enum_values_
Definition: protostream_objectsource.h:318
type_info.h
google::protobuf::util::converter::ProtoStreamObjectSource
Definition: protostream_objectsource.h:72
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
google::protobuf::util::StatusOr
Definition: statusor.h:99
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
google::protobuf::util::converter::ProtoStreamObjectSource::suppress_empty_object_
bool suppress_empty_object_
Definition: protostream_objectsource.h:326
Field
struct Field Field
Definition: php/ext/google/protobuf/protobuf.h:638
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::util::converter::ProtoStreamObjectSource::type_
const google::protobuf::Type & type_
Definition: protostream_objectsource.h:296
common.h
object_writer.h
google::protobuf::io::CodedInputStream
Definition: coded_stream.h:173
google::protobuf::util::Status
Definition: status.h:67
type_resolver
TypeResolver * type_resolver
Definition: conformance_cpp.cc:97
hash.h
type.pb.h
Type
struct Type Type
Definition: php/ext/google/protobuf/protobuf.h:664
google::protobuf::util::converter::ProtoStreamObjectSource::preserve_proto_field_names_
bool preserve_proto_field_names_
Definition: protostream_objectsource.h:306
google::protobuf::util::converter::ProtoStreamObjectSource::render_unknown_fields_
bool render_unknown_fields_
Definition: protostream_objectsource.h:315
object_source.h
google::protobuf::util::converter::ProtoStreamObjectSource::typeinfo_
const TypeInfo * typeinfo_
Definition: protostream_objectsource.h:289
type_url
string * type_url
Definition: conformance_cpp.cc:98
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf::util::converter::ProtoStreamObjectSource::set_use_lower_camel_for_enums
void set_use_lower_camel_for_enums(bool value)
Definition: protostream_objectsource.h:107
status.h
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::util::converter::ProtoStreamObjectSource::stream_
io::CodedInputStream * stream_
Definition: protostream_objectsource.h:285
google::protobuf::util::converter::ProtoStreamObjectSource::max_recursion_depth_
int max_recursion_depth_
Definition: protostream_objectsource.h:312


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58