1212
1313#include " absl/strings/string_view.h"
1414#include " google/protobuf/port.h"
15- #include " google/protobuf/arenastring .h"
15+ #include " absl/strings/cord .h"
1616#include " google/protobuf/message_lite.h"
1717
1818// Must be included last.
@@ -36,75 +36,80 @@ PROTOBUF_EXPORT extern const char kTypeGoogleProdComPrefix[];
3636std::string GetTypeUrl (absl::string_view message_name,
3737 absl::string_view type_url_prefix);
3838
39+ template <typename T>
40+ absl::string_view GetAnyMessageName () {
41+ return T::FullMessageName ();
42+ }
43+
3944// Helper class used to implement google::protobuf::Any.
40- class PROTOBUF_EXPORT AnyMetadata {
41- typedef ArenaStringPtr UrlType;
42- typedef ArenaStringPtr ValueType;
43- public:
44- // AnyMetadata does not take ownership of "type_url" and "value".
45- constexpr AnyMetadata (UrlType* type_url, ValueType* value)
46- : type_url_(type_url), value_(value) {}
47- AnyMetadata ( const AnyMetadata&) = delete ;
48- AnyMetadata& operator =( const AnyMetadata&) = delete ;
49-
50- // Packs a message using the default type URL prefix: "type.googleapis.com".
51- // The resulted type URL will be "type.googleapis.com/<message_full_name>".
52- // Returns false if serializing the message failed.
53- template < typename T>
54- bool PackFrom (Arena* arena, const T& message) {
55- return InternalPackFrom (arena, message, kTypeGoogleApisComPrefix ,
56- T::FullMessageName ());
57- }
58-
59- bool PackFrom (Arena* arena, const Message& message);
60-
61- // Packs a message using the given type URL prefix. The type URL will be
62- // constructed by concatenating the message type's full name to the prefix
63- // with an optional "/" separator if the prefix doesn't already end with "/".
64- // For example, both PackFrom(message, "type.googleapis.com") and
65- // PackFrom( message, "type.googleapis.com/") yield the same result type
66- // URL: "type.googleapis.com/<message_full_name>".
67- // Returns false if serializing the message failed.
68- template < typename T>
69- bool PackFrom (Arena* arena, const T& message,
70- absl::string_view type_url_prefix) {
71- return InternalPackFrom (arena, message, type_url_prefix,
72- T::FullMessageName ());
73- }
74-
75- bool PackFrom (Arena* arena, const Message& message,
76- absl::string_view type_url_prefix);
77-
78- // Unpacks the payload into the given message. Returns false if the message's
79- // type doesn't match the type specified in the type URL (i.e., the full
80- // name after the last "/" of the type URL doesn't match the message's actual
81- // full name) or parsing the payload has failed.
82- template < typename T>
83- bool UnpackTo (T* message) const {
84- return InternalUnpackTo ( T::FullMessageName (), message);
85- }
86-
87- bool UnpackTo (Message* message) const ;
88-
89- // Checks whether the type specified in the type URL matches the given type.
90- // A type is considered matching if its full name matches the full name after
91- // the last "/" in the type URL.
92- template < typename T>
93- bool Is () const {
94- return InternalIs ( T::FullMessageName ());
95- }
96-
97- private:
98- bool InternalPackFrom (Arena* arena, const MessageLite& message,
99- absl::string_view type_url_prefix,
100- absl::string_view type_name);
101- bool InternalUnpackTo (absl::string_view type_name,
102- MessageLite* message) const ;
103- bool InternalIs (absl::string_view type_name) const ;
104-
105- UrlType* type_url_;
106- ValueType* value_;
107- };
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>
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>
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>
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>
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
108113
109114// Get the proto type name from Any::type_url value. For example, passing
110115// "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
0 commit comments