• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef GOOGLE_PROTOBUF_ANY_H__
9 #define GOOGLE_PROTOBUF_ANY_H__
10 
11 #include <string>
12 
13 #include "absl/strings/string_view.h"
14 #include "google/protobuf/port.h"
15 #include "absl/strings/cord.h"
16 #include "google/protobuf/message_lite.h"
17 
18 // Must be included last.
19 #include "google/protobuf/port_def.inc"
20 
21 namespace google {
22 namespace protobuf {
23 
24 class FieldDescriptor;
25 class Message;
26 
27 namespace internal {
28 
29 // "google.protobuf.Any".
30 PROTOBUF_EXPORT extern const char kAnyFullTypeName[];
31 // "type.googleapis.com/".
32 PROTOBUF_EXPORT extern const char kTypeGoogleApisComPrefix[];
33 // "type.googleprod.com/".
34 PROTOBUF_EXPORT extern const char kTypeGoogleProdComPrefix[];
35 
36 std::string GetTypeUrl(absl::string_view message_name,
37                        absl::string_view type_url_prefix);
38 
39 template <typename T>
GetAnyMessageName()40 absl::string_view GetAnyMessageName() {
41   return T::FullMessageName();
42 }
43 
44 // Helper class used to implement google::protobuf::Any.
45 #define URL_TYPE std::string
46 #define VALUE_TYPE std::string
47 
48 // Helper functions that only require 'lite' messages to work.
49 PROTOBUF_EXPORT bool InternalPackFromLite(const MessageLite& message,
50                                           absl::string_view type_url_prefix,
51                                           absl::string_view type_name,
52                                           URL_TYPE* dst_url,
53                                           VALUE_TYPE* dst_value);
54 PROTOBUF_EXPORT bool InternalUnpackToLite(absl::string_view type_name,
55                                           absl::string_view type_url,
56                                           const VALUE_TYPE& value,
57                                           MessageLite* dst_message);
58 PROTOBUF_EXPORT bool InternalIsLite(absl::string_view type_name,
59                                     absl::string_view type_url);
60 
61 // Packs a message using the default type URL prefix: "type.googleapis.com".
62 // The resulted type URL will be "type.googleapis.com/<message_full_name>".
63 // Returns false if serializing the message failed.
64 template <typename T>
InternalPackFrom(const T & message,URL_TYPE * dst_url,VALUE_TYPE * dst_value)65 bool InternalPackFrom(const T& message, URL_TYPE* dst_url,
66                       VALUE_TYPE* dst_value) {
67   return InternalPackFromLite(message, kTypeGoogleApisComPrefix,
68                               GetAnyMessageName<T>(), dst_url, dst_value);
69 }
70 PROTOBUF_EXPORT bool InternalPackFrom(const Message& message, URL_TYPE* dst_url,
71                                       VALUE_TYPE* dst_value);
72 
73 // Packs a message using the given type URL prefix. The type URL will be
74 // constructed by concatenating the message type's full name to the prefix
75 // with an optional "/" separator if the prefix doesn't already end with "/".
76 // For example, both InternalPackFrom(message, "type.googleapis.com") and
77 // InternalPackFrom(message, "type.googleapis.com/") yield the same result type
78 // URL: "type.googleapis.com/<message_full_name>".
79 // Returns false if serializing the message failed.
80 template <typename T>
InternalPackFrom(const T & message,absl::string_view type_url_prefix,URL_TYPE * dst_url,VALUE_TYPE * dst_value)81 bool InternalPackFrom(const T& message, absl::string_view type_url_prefix,
82                       URL_TYPE* dst_url, VALUE_TYPE* dst_value) {
83   return InternalPackFromLite(message, type_url_prefix, GetAnyMessageName<T>(),
84                               dst_url, dst_value);
85 }
86 PROTOBUF_EXPORT bool InternalPackFrom(const Message& message,
87                                       absl::string_view type_url_prefix,
88                                       URL_TYPE* dst_url, VALUE_TYPE* dst_value);
89 
90 // Unpacks the payload into the given message. Returns false if the message's
91 // type doesn't match the type specified in the type URL (i.e., the full
92 // name after the last "/" of the type URL doesn't match the message's actual
93 // full name) or parsing the payload has failed.
94 template <typename T>
InternalUnpackTo(absl::string_view type_url,const VALUE_TYPE & value,T * message)95 bool InternalUnpackTo(absl::string_view type_url, const VALUE_TYPE& value,
96                       T* message) {
97   return InternalUnpackToLite(GetAnyMessageName<T>(), type_url, value, message);
98 }
99 PROTOBUF_EXPORT bool InternalUnpackTo(absl::string_view type_url,
100                                       const VALUE_TYPE& value,
101                                       Message* message);
102 
103 // Checks whether the type specified in the type URL matches the given type.
104 // A type is considered matching if its full name matches the full name after
105 // the last "/" in the type URL.
106 template <typename T>
InternalIs(absl::string_view type_url)107 bool InternalIs(absl::string_view type_url) {
108   return InternalIsLite(GetAnyMessageName<T>(), type_url);
109 }
110 
111 #undef URL_TYPE
112 #undef VALUE_TYPE
113 
114 // Get the proto type name from Any::type_url value. For example, passing
115 // "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
116 // *full_type_name. Returns false if the type_url does not have a "/"
117 // in the type url separating the full type name.
118 //
119 // NOTE: this function is available publicly as a static method on the
120 // generated message type: google::protobuf::Any::ParseAnyTypeUrl()
121 bool ParseAnyTypeUrl(absl::string_view type_url, std::string* full_type_name);
122 
123 // Get the proto type name and prefix from Any::type_url value. For example,
124 // passing "type.googleapis.com/rpc.QueryOrigin" will return
125 // "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in
126 // *full_type_name. Returns false if the type_url does not have a "/" in the
127 // type url separating the full type name.
128 bool ParseAnyTypeUrl(absl::string_view type_url, std::string* url_prefix,
129                      std::string* full_type_name);
130 
131 // See if message is of type google.protobuf.Any, if so, return the descriptors
132 // for "type_url" and "value" fields.
133 bool GetAnyFieldDescriptors(const Message& message,
134                             const FieldDescriptor** type_url_field,
135                             const FieldDescriptor** value_field);
136 
137 }  // namespace internal
138 }  // namespace protobuf
139 }  // namespace google
140 
141 #include "google/protobuf/port_undef.inc"
142 
143 #endif  // GOOGLE_PROTOBUF_ANY_H__
144