Coverage Report

Created: 2024-07-27 06:53

/src/LPM/external.protobuf/include/google/protobuf/any.h
Line
Count
Source (jump to first uncovered line)
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 "google/protobuf/port.h"
14
#include "google/protobuf/arenastring.h"
15
#include "google/protobuf/message_lite.h"
16
17
// Must be included last.
18
#include "google/protobuf/port_def.inc"
19
20
namespace google {
21
namespace protobuf {
22
23
class FieldDescriptor;
24
class Message;
25
26
namespace internal {
27
28
// "google.protobuf.Any".
29
PROTOBUF_EXPORT extern const char kAnyFullTypeName[];
30
// "type.googleapis.com/".
31
PROTOBUF_EXPORT extern const char kTypeGoogleApisComPrefix[];
32
// "type.googleprod.com/".
33
PROTOBUF_EXPORT extern const char kTypeGoogleProdComPrefix[];
34
35
std::string GetTypeUrl(absl::string_view message_name,
36
                       absl::string_view type_url_prefix);
37
38
// Helper class used to implement google::protobuf::Any.
39
class PROTOBUF_EXPORT AnyMetadata {
40
  typedef ArenaStringPtr UrlType;
41
  typedef ArenaStringPtr ValueType;
42
 public:
43
  // AnyMetadata does not take ownership of "type_url" and "value".
44
  constexpr AnyMetadata(UrlType* type_url, ValueType* value)
45
0
      : type_url_(type_url), value_(value) {}
46
  AnyMetadata(const AnyMetadata&) = delete;
47
  AnyMetadata& operator=(const AnyMetadata&) = delete;
48
49
  // Packs a message using the default type URL prefix: "type.googleapis.com".
50
  // The resulted type URL will be "type.googleapis.com/<message_full_name>".
51
  // Returns false if serializing the message failed.
52
  template <typename T>
53
  bool PackFrom(Arena* arena, const T& message) {
54
    return InternalPackFrom(arena, message, kTypeGoogleApisComPrefix,
55
                            T::FullMessageName());
56
  }
57
58
  bool PackFrom(Arena* arena, const Message& message);
59
60
  // Packs a message using the given type URL prefix. The type URL will be
61
  // constructed by concatenating the message type's full name to the prefix
62
  // with an optional "/" separator if the prefix doesn't already end with "/".
63
  // For example, both PackFrom(message, "type.googleapis.com") and
64
  // PackFrom(message, "type.googleapis.com/") yield the same result type
65
  // URL: "type.googleapis.com/<message_full_name>".
66
  // Returns false if serializing the message failed.
67
  template <typename T>
68
  bool PackFrom(Arena* arena, const T& message,
69
                absl::string_view type_url_prefix) {
70
    return InternalPackFrom(arena, message, type_url_prefix,
71
                            T::FullMessageName());
72
  }
73
74
  bool PackFrom(Arena* arena, const Message& message,
75
                absl::string_view type_url_prefix);
76
77
  // Unpacks the payload into the given message. Returns false if the message's
78
  // type doesn't match the type specified in the type URL (i.e., the full
79
  // name after the last "/" of the type URL doesn't match the message's actual
80
  // full name) or parsing the payload has failed.
81
  template <typename T>
82
  bool UnpackTo(T* message) const {
83
    return InternalUnpackTo(T::FullMessageName(), message);
84
  }
85
86
  bool UnpackTo(Message* message) const;
87
88
  // Checks whether the type specified in the type URL matches the given type.
89
  // A type is considered matching if its full name matches the full name after
90
  // the last "/" in the type URL.
91
  template <typename T>
92
  bool Is() const {
93
    return InternalIs(T::FullMessageName());
94
  }
95
96
 private:
97
  bool InternalPackFrom(Arena* arena, const MessageLite& message,
98
                        absl::string_view type_url_prefix,
99
                        absl::string_view type_name);
100
  bool InternalUnpackTo(absl::string_view type_name,
101
                        MessageLite* message) const;
102
  bool InternalIs(absl::string_view type_name) const;
103
104
  UrlType* type_url_;
105
  ValueType* value_;
106
};
107
108
// Get the proto type name from Any::type_url value. For example, passing
109
// "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
110
// *full_type_name. Returns false if the type_url does not have a "/"
111
// in the type url separating the full type name.
112
//
113
// NOTE: this function is available publicly as a static method on the
114
// generated message type: google::protobuf::Any::ParseAnyTypeUrl()
115
bool ParseAnyTypeUrl(absl::string_view type_url, std::string* full_type_name);
116
117
// Get the proto type name and prefix from Any::type_url value. For example,
118
// passing "type.googleapis.com/rpc.QueryOrigin" will return
119
// "type.googleapis.com/" in *url_prefix and "rpc.QueryOrigin" in
120
// *full_type_name. Returns false if the type_url does not have a "/" in the
121
// type url separating the full type name.
122
bool ParseAnyTypeUrl(absl::string_view type_url, std::string* url_prefix,
123
                     std::string* full_type_name);
124
125
// See if message is of type google.protobuf.Any, if so, return the descriptors
126
// for "type_url" and "value" fields.
127
bool GetAnyFieldDescriptors(const Message& message,
128
                            const FieldDescriptor** type_url_field,
129
                            const FieldDescriptor** value_field);
130
131
}  // namespace internal
132
}  // namespace protobuf
133
}  // namespace google
134
135
#include "google/protobuf/port_undef.inc"
136
137
#endif  // GOOGLE_PROTOBUF_ANY_H__