protobuf/src/google/protobuf/util/internal/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 <cstdint>
35 #include <functional>
36 #include <string>
37 #include <unordered_map>
38 
39 #include <google/protobuf/stubs/status.h>
40 
41 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/type.pb.h>
43 #include <google/protobuf/util/internal/type_info.h>
44 #include <google/protobuf/util/internal/object_source.h>
45 #include <google/protobuf/util/internal/object_writer.h>
46 #include <google/protobuf/util/type_resolver.h>
47 #include <google/protobuf/stubs/statusor.h>
48 #include <google/protobuf/stubs/strutil.h>
49 #include <google/protobuf/stubs/hash.h>
50 #include <google/protobuf/stubs/status.h>
51 
52 
53 #include <google/protobuf/port_def.inc>
54 
55 namespace google {
56 namespace protobuf {
57 namespace util {
58 namespace converter {
59 
60 class TypeInfo;
61 
62 // An ObjectSource that can parse a stream of bytes as a protocol buffer.
63 // Its WriteTo() method can be given an ObjectWriter.
64 // This implementation uses a google.protobuf.Type for tag and name lookup.
65 // The field names are converted into lower camel-case when writing to the
66 // ObjectWriter.
67 //
68 // Sample usage: (suppose input is: string proto)
69 // ArrayInputStream arr_stream(proto.data(), proto.size());
70 // CodedInputStream in_stream(&arr_stream);
71 // ProtoStreamObjectSource os(&in_stream, /*ServiceTypeInfo*/ typeinfo,
72 // <your message google::protobuf::Type>);
73 //
74 // Status status = os.WriteTo(<some ObjectWriter>);
75 class PROTOBUF_EXPORT ProtoStreamObjectSource : public ObjectSource {
76  public:
77 
78  struct RenderOptions {
79  RenderOptions() = default;
80  RenderOptions(const RenderOptions&) = default;
81 
82  // Sets whether or not to use lowerCamelCase casing for enum values. If set
83  // to false, enum values are output without any case conversions.
84  //
85  // For example, if we have an enum:
86  // enum Type {
87  // ACTION_AND_ADVENTURE = 1;
88  // }
89  // Type type = 20;
90  //
91  // And this option is set to true. Then the rendered "type" field will have
92  // the string "actionAndAdventure".
93  // {
94  // ...
95  // "type": "actionAndAdventure",
96  // ...
97  // }
98  //
99  // If set to false, the rendered "type" field will have the string
100  // "ACTION_AND_ADVENTURE".
101  // {
102  // ...
103  // "type": "ACTION_AND_ADVENTURE",
104  // ...
105  // }
106  bool use_lower_camel_for_enums = false;
107 
108  // Sets whether to always output enums as ints, by default this is off, and
109  // enums are rendered as strings.
110  bool use_ints_for_enums = false;
111 
112  // Whether to preserve proto field names
113  bool preserve_proto_field_names = false;
114 
115  };
116 
124  const RenderOptions& render_options);
125 
126  ~ProtoStreamObjectSource() override;
127 
128  util::Status NamedWriteTo(StringPiece name,
129  ObjectWriter* ow) const override;
130 
131  // Sets the max recursion depth of proto message to be deserialized. Proto
132  // messages over this depth will fail to be deserialized.
133  // Default value is 64.
134  void set_max_recursion_depth(int max_depth) {
135  max_recursion_depth_ = max_depth;
136  }
137 
138  protected:
139  // Writes a proto2 Message to the ObjectWriter. When the given end_tag is
140  // found this method will complete, allowing it to be used for parsing both
141  // nested messages (end with 0) and nested groups (end with group end tag).
142  // The include_start_and_end parameter allows this method to be called when
143  // already inside of an object, and skip calling StartObject and EndObject.
144  virtual util::Status WriteMessage(const google::protobuf::Type& type,
146  const uint32_t end_tag,
147  bool include_start_and_end,
148  ObjectWriter* ow) const;
149 
150  // Renders a repeating field (packed or unpacked). Returns the next tag after
151  // reading all sequential repeating elements. The caller should use this tag
152  // before reading more tags from the stream.
153  virtual util::StatusOr<uint32_t> RenderList(
155  uint32_t list_tag, ObjectWriter* ow) const;
156 
157  // Looks up a field and verify its consistency with wire type in tag.
158  const google::protobuf::Field* FindAndVerifyField(
159  const google::protobuf::Type& type, uint32_t tag) const;
160 
161  // Renders a field value to the ObjectWriter.
162  virtual util::Status RenderField(const google::protobuf::Field* field,
163  StringPiece field_name,
164  ObjectWriter* ow) const;
165 
166  // Reads field value according to Field spec in 'field' and returns the read
167  // value as string. This only works for primitive datatypes (no message
168  // types).
169  const std::string ReadFieldValueAsString(
170  const google::protobuf::Field& field) const;
171 
172 
173  // Returns the input stream.
174  io::CodedInputStream* stream() const { return stream_; }
175 
176  private:
178  const TypeInfo* typeinfo,
180  const RenderOptions& render_options);
181  // Function that renders a well known type with a modified behavior.
182  typedef util::Status (*TypeRenderer)(const ProtoStreamObjectSource*,
183  const google::protobuf::Type&,
185 
186  // TODO(skarvaje): Mark these methods as non-const as they modify internal
187  // state (stream_).
188  //
189  // Renders a NWP map.
190  // Returns the next tag after reading all map entries. The caller should use
191  // this tag before reading more tags from the stream.
193  StringPiece name, uint32_t list_tag,
194  ObjectWriter* ow) const;
195 
196  // Renders a packed repeating field. A packed field is stored as:
197  // {tag length item1 item2 item3} instead of the less efficient
198  // {tag item1 tag item2 tag item3}.
199  util::Status RenderPacked(const google::protobuf::Field* field,
200  ObjectWriter* ow) const;
201 
202  // Renders a google.protobuf.Timestamp value to ObjectWriter
203  static util::Status RenderTimestamp(const ProtoStreamObjectSource* os,
206 
207  // Renders a google.protobuf.Duration value to ObjectWriter
208  static util::Status RenderDuration(const ProtoStreamObjectSource* os,
211 
212  // Following RenderTYPE functions render well known types in
213  // google/protobuf/wrappers.proto corresponding to TYPE.
214  static util::Status RenderDouble(const ProtoStreamObjectSource* os,
217  static util::Status RenderFloat(const ProtoStreamObjectSource* os,
220  static util::Status RenderInt64(const ProtoStreamObjectSource* os,
223  static util::Status RenderUInt64(const ProtoStreamObjectSource* os,
226  static util::Status RenderInt32(const ProtoStreamObjectSource* os,
229  static util::Status RenderUInt32(const ProtoStreamObjectSource* os,
232  static util::Status RenderBool(const ProtoStreamObjectSource* os,
235  static util::Status RenderString(const ProtoStreamObjectSource* os,
238  static util::Status RenderBytes(const ProtoStreamObjectSource* os,
241 
242  // Renders a google.protobuf.Struct to ObjectWriter.
243  static util::Status RenderStruct(const ProtoStreamObjectSource* os,
246 
247  // Helper to render google.protobuf.Struct's Value fields to ObjectWriter.
248  static util::Status RenderStructValue(const ProtoStreamObjectSource* os,
251  ObjectWriter* ow);
252 
253  // Helper to render google.protobuf.Struct's ListValue fields to ObjectWriter.
254  static util::Status RenderStructListValue(const ProtoStreamObjectSource* os,
257  ObjectWriter* ow);
258 
259  // Render the "Any" type.
260  static util::Status RenderAny(const ProtoStreamObjectSource* os,
263 
264  // Render the "FieldMask" type.
265  static util::Status RenderFieldMask(const ProtoStreamObjectSource* os,
268 
269  static std::unordered_map<std::string, TypeRenderer>* renderers_;
270  static void InitRendererMap();
271  static void DeleteRendererMap();
272  static TypeRenderer* FindTypeRenderer(const std::string& type_url);
273 
274  // Same as above but renders all non-message field types. Callers don't call
275  // this function directly. They just use RenderField.
276  util::Status RenderNonMessageField(const google::protobuf::Field* field,
277  StringPiece field_name,
278  ObjectWriter* ow) const;
279 
280 
281  // Utility function to detect proto maps. The 'field' MUST be repeated.
282  bool IsMap(const google::protobuf::Field& field) const;
283 
284  // Utility to read int64 and int32 values from a message type in stream_.
285  // Used for reading google.protobuf.Timestamp and Duration messages.
286  std::pair<int64_t, int32_t> ReadSecondsAndNanos(
287  const google::protobuf::Type& type) const;
288 
289  // Helper function to check recursion depth and increment it. It will return
290  // OkStatus() if the current depth is allowed. Otherwise an error is returned.
291  // type_name and field_name are used for error reporting.
292  util::Status IncrementRecursionDepth(StringPiece type_name,
293  StringPiece field_name) const;
294 
295  // Input stream to read from. Ownership rests with the caller.
296  mutable io::CodedInputStream* stream_;
297 
298  // Type information for all the types used in the descriptor. Used to find
299  // google::protobuf::Type of nested messages/enums.
300  const TypeInfo* typeinfo_;
301 
302  // Whether this class owns the typeinfo_ object. If true the typeinfo_ object
303  // should be deleted in the destructor.
304  bool own_typeinfo_;
305 
306  // google::protobuf::Type of the message source.
308 
309 
311 
312  // Tracks current recursion depth.
313  mutable int recursion_depth_;
314 
315  // Maximum allowed recursion depth.
316  int max_recursion_depth_;
317 
319 };
320 
321 } // namespace converter
322 } // namespace util
323 } // namespace protobuf
324 } // namespace google
325 
326 #include <google/protobuf/port_undef.inc>
327 
328 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_PROTOSTREAM_OBJECTSOURCE_H__
google::protobuf::util::converter::ProtoStreamObjectSource::stream
io::CodedInputStream * stream() const
Definition: protobuf/src/google/protobuf/util/internal/protostream_objectsource.h:174
Type
struct Type Type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:673
stream_
std::unique_ptr< grpc::ClientReaderInterface< OrcaLoadReport > > stream_
Definition: orca_service_end2end_test.cc:89
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
type_
std::string type_
Definition: client_channel_stress_test.cc:212
TypeInfo
Definition: protobuf/php/ext/google/protobuf/def.h:69
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
make_cmakelists.converter
converter
Definition: make_cmakelists.py:317
google::protobuf::util::converter::ObjectWriter
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/object_writer.h:61
setup.name
name
Definition: setup.py:542
google::protobuf::util::converter::ProtoStreamObjectSource::set_max_recursion_depth
void set_max_recursion_depth(int max_depth)
Definition: protobuf/src/google/protobuf/util/internal/protostream_objectsource.h:134
type_resolver
TypeResolver * type_resolver
Definition: bloaty/third_party/protobuf/conformance/conformance_cpp.cc:71
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
google::protobuf::util::converter::ProtoStreamObjectSource::RenderOptions
Definition: protobuf/src/google/protobuf/util/internal/protostream_objectsource.h:78
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
google::protobuf::util::converter::ProtoStreamObjectSource::render_options_
const RenderOptions render_options_
Definition: protobuf/src/google/protobuf/util/internal/protostream_objectsource.h:310
google::protobuf::util::TypeResolver
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/type_resolver.h:52
google::protobuf::StringPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:180
google::protobuf::util::converter::IsMap
bool IsMap(const google::protobuf::Field &field, const google::protobuf::Type &type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/utility.cc:360
GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
#define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/macros.h:45
google::protobuf::util::converter::TypeInfo
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/type_info.h:50
google::protobuf::util::converter::ProtoStreamObjectSource
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/internal/protostream_objectsource.h:72
google::protobuf::util::StatusOr
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/statusor.h:99
Field
struct Field Field
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:647
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::io::CodedInputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream.h:180
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
type_url
string * type_url
Definition: bloaty/third_party/protobuf/conformance/conformance_cpp.cc:72
google::protobuf::util::Status
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/status.h:67
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
google::protobuf::util::converter::ProtoStreamObjectSource::ProtoStreamObjectSource
ProtoStreamObjectSource(io::CodedInputStream *stream, TypeResolver *type_resolver, const google::protobuf::Type &type)
Definition: protobuf/src/google/protobuf/util/internal/protostream_objectsource.h:117
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
type_name
static const char * type_name(int type)
Definition: adig.c:889
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:48