Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-28 09:14:00

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_ANY_H__
0009 #define GOOGLE_PROTOBUF_ANY_H__
0010 
0011 #include <string>
0012 
0013 #include "absl/strings/string_view.h"
0014 #include "google/protobuf/port.h"
0015 #include "google/protobuf/message_lite.h"
0016 
0017 // Must be included last.
0018 #include "google/protobuf/port_def.inc"
0019 
0020 namespace google {
0021 namespace protobuf {
0022 
0023 class FieldDescriptor;
0024 class Message;
0025 
0026 namespace internal {
0027 
0028 // "google.protobuf.Any".
0029 PROTOBUF_EXPORT extern const char kAnyFullTypeName[];
0030 // "type.googleapis.com/".
0031 PROTOBUF_EXPORT extern const char kTypeGoogleApisComPrefix[];
0032 // "type.googleprod.com/".
0033 PROTOBUF_EXPORT extern const char kTypeGoogleProdComPrefix[];
0034 
0035 std::string GetTypeUrl(absl::string_view message_name,
0036                        absl::string_view type_url_prefix);
0037 
0038 template <typename T>
0039 absl::string_view GetAnyMessageName() {
0040   return T::FullMessageName();
0041 }
0042 
0043 // Helper class used to implement google::protobuf::Any.
0044 #define URL_TYPE std::string
0045 #define VALUE_TYPE std::string
0046 
0047 // Helper functions that only require 'lite' messages to work.
0048 PROTOBUF_EXPORT bool InternalPackFromLite(
0049     const MessageLite& message, absl::string_view type_url_prefix,
0050     absl::string_view type_name, URL_TYPE* PROTOBUF_NONNULL dst_url,
0051     VALUE_TYPE* PROTOBUF_NONNULL dst_value);
0052 PROTOBUF_EXPORT bool InternalUnpackToLite(
0053     absl::string_view type_name, absl::string_view type_url,
0054     const VALUE_TYPE& value, MessageLite* PROTOBUF_NONNULL dst_message);
0055 PROTOBUF_EXPORT bool InternalIsLite(absl::string_view type_name,
0056                                     absl::string_view type_url);
0057 
0058 // Packs a message using the default type URL prefix: "type.googleapis.com".
0059 // The resulted type URL will be "type.googleapis.com/<message_full_name>".
0060 // Returns false if serializing the message failed.
0061 template <typename T>
0062 bool InternalPackFrom(const T& message, URL_TYPE* PROTOBUF_NONNULL dst_url,
0063                       VALUE_TYPE* PROTOBUF_NONNULL dst_value) {
0064   return InternalPackFromLite(message, kTypeGoogleApisComPrefix,
0065                               GetAnyMessageName<T>(), dst_url, dst_value);
0066 }
0067 PROTOBUF_EXPORT bool InternalPackFrom(const Message& message,
0068                                       URL_TYPE* PROTOBUF_NONNULL dst_url,
0069                                       VALUE_TYPE* PROTOBUF_NONNULL dst_value);
0070 
0071 // Packs a message using the given type URL prefix. The type URL will be
0072 // constructed by concatenating the message type's full name to the prefix
0073 // with an optional "/" separator if the prefix doesn't already end with "/".
0074 // For example, both InternalPackFrom(message, "type.googleapis.com") and
0075 // InternalPackFrom(message, "type.googleapis.com/") yield the same result type
0076 // URL: "type.googleapis.com/<message_full_name>".
0077 // Returns false if serializing the message failed.
0078 template <typename T>
0079 bool InternalPackFrom(const T& message, absl::string_view type_url_prefix,
0080                       URL_TYPE* PROTOBUF_NONNULL dst_url,
0081                       VALUE_TYPE* PROTOBUF_NONNULL dst_value) {
0082   return InternalPackFromLite(message, type_url_prefix, GetAnyMessageName<T>(),
0083                               dst_url, dst_value);
0084 }
0085 PROTOBUF_EXPORT bool InternalPackFrom(const Message& message,
0086                                       absl::string_view type_url_prefix,
0087                                       URL_TYPE* PROTOBUF_NONNULL dst_url,
0088                                       VALUE_TYPE* PROTOBUF_NONNULL dst_value);
0089 
0090 // Unpacks the payload into the given message. Returns false if the message's
0091 // type doesn't match the type specified in the type URL (i.e., the full
0092 // name after the last "/" of the type URL doesn't match the message's actual
0093 // full name) or parsing the payload has failed.
0094 template <typename T>
0095 bool InternalUnpackTo(absl::string_view type_url, const VALUE_TYPE& value,
0096                       T* PROTOBUF_NONNULL message) {
0097   return InternalUnpackToLite(GetAnyMessageName<T>(), type_url, value, message);
0098 }
0099 PROTOBUF_EXPORT bool InternalUnpackTo(absl::string_view type_url,
0100                                       const VALUE_TYPE& value,
0101                                       Message* PROTOBUF_NONNULL message);
0102 
0103 // Checks whether the type specified in the type URL matches the given type.
0104 // A type is considered matching if its full name matches the full name after
0105 // the last "/" in the type URL.
0106 template <typename T>
0107 bool InternalIs(absl::string_view type_url) {
0108   return InternalIsLite(GetAnyMessageName<T>(), type_url);
0109 }
0110 
0111 #undef URL_TYPE
0112 #undef VALUE_TYPE
0113 
0114 // Get the proto type name from Any::type_url value. For example, passing
0115 // "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
0116 // *full_type_name. Returns false if the type_url does not have a "/"
0117 // in the type url separating the full type name.
0118 //
0119 // NOTE: this function is available publicly as a static method on the
0120 // generated message type: google::protobuf::Any::ParseAnyTypeUrl()
0121 bool ParseAnyTypeUrl(absl::string_view type_url,
0122                      std::string* PROTOBUF_NONNULL full_type_name);
0123 
0124 // Get the proto type name and prefix from Any::type_url value. For example,
0125 // passing "type.googleapis.com/rpc.QueryOrigin" will return
0126 // "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in
0127 // *full_type_name. Returns false if the type_url does not have a "/" in the
0128 // type url separating the full type name.
0129 bool ParseAnyTypeUrl(absl::string_view type_url,
0130                      std::string* PROTOBUF_NULLABLE url_prefix,
0131                      std::string* PROTOBUF_NONNULL full_type_name);
0132 
0133 // See if message is of type google.protobuf.Any, if so, return the descriptors
0134 // for "type_url" and "value" fields.
0135 bool GetAnyFieldDescriptors(
0136     const Message& message,
0137     const FieldDescriptor* PROTOBUF_NULLABLE* PROTOBUF_NONNULL type_url_field,
0138     const FieldDescriptor* PROTOBUF_NULLABLE* PROTOBUF_NONNULL value_field);
0139 
0140 }  // namespace internal
0141 }  // namespace protobuf
0142 }  // namespace google
0143 
0144 #include "google/protobuf/port_undef.inc"
0145 
0146 #endif  // GOOGLE_PROTOBUF_ANY_H__