OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 17 matching lines...) Expand all Loading... |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 #include <google/protobuf/any.h> | 31 #include <google/protobuf/any.h> |
32 | 32 |
33 namespace google { | 33 namespace google { |
34 namespace protobuf { | 34 namespace protobuf { |
35 namespace internal { | 35 namespace internal { |
36 | 36 |
37 namespace { | 37 namespace { |
38 string GetTypeUrl(const Descriptor* message) { | 38 string GetTypeUrl(const Descriptor* message, |
39 return string(kTypeGoogleApisComPrefix) + message->full_name(); | 39 const string& type_url_prefix) { |
| 40 if (!type_url_prefix.empty() && |
| 41 type_url_prefix[type_url_prefix.size() - 1] == '/') { |
| 42 return type_url_prefix + message->full_name(); |
| 43 } else { |
| 44 return type_url_prefix + "/" + message->full_name(); |
| 45 } |
40 } | 46 } |
41 | |
42 } // namespace | 47 } // namespace |
43 | 48 |
44 const char kAnyFullTypeName[] = "google.protobuf.Any"; | 49 const char kAnyFullTypeName[] = "google.protobuf.Any"; |
45 const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; | 50 const char kTypeGoogleApisComPrefix[] = "type.googleapis.com/"; |
46 const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; | 51 const char kTypeGoogleProdComPrefix[] = "type.googleprod.com/"; |
47 | 52 |
48 AnyMetadata::AnyMetadata(UrlType* type_url, ValueType* value) | 53 AnyMetadata::AnyMetadata(UrlType* type_url, ValueType* value) |
49 : type_url_(type_url), value_(value) { | 54 : type_url_(type_url), value_(value) { |
50 } | 55 } |
51 | 56 |
52 void AnyMetadata::PackFrom(const Message& message) { | 57 void AnyMetadata::PackFrom(const Message& message) { |
| 58 PackFrom(message, kTypeGoogleApisComPrefix); |
| 59 } |
| 60 |
| 61 void AnyMetadata::PackFrom(const Message& message, |
| 62 const string& type_url_prefix) { |
53 type_url_->SetNoArena(&::google::protobuf::internal::GetEmptyString(), | 63 type_url_->SetNoArena(&::google::protobuf::internal::GetEmptyString(), |
54 GetTypeUrl(message.GetDescriptor())); | 64 GetTypeUrl(message.GetDescriptor(), type_url_prefix)); |
55 message.SerializeToString(value_->MutableNoArena( | 65 message.SerializeToString(value_->MutableNoArena( |
56 &::google::protobuf::internal::GetEmptyStringAlreadyInited())); | 66 &::google::protobuf::internal::GetEmptyStringAlreadyInited())); |
57 } | 67 } |
58 | 68 |
59 bool AnyMetadata::UnpackTo(Message* message) const { | 69 bool AnyMetadata::UnpackTo(Message* message) const { |
60 if (!InternalIs(message->GetDescriptor())) { | 70 if (!InternalIs(message->GetDescriptor())) { |
61 return false; | 71 return false; |
62 } | 72 } |
63 return message->ParseFromString( | 73 return message->ParseFromString( |
64 value_->GetNoArena(&::google::protobuf::internal::GetEmptyString())); | 74 value_->GetNoArena(&::google::protobuf::internal::GetEmptyString())); |
65 } | 75 } |
66 | 76 |
67 bool AnyMetadata::InternalIs(const Descriptor* descriptor) const { | 77 bool AnyMetadata::InternalIs(const Descriptor* descriptor) const { |
68 const string type_url = type_url_->GetNoArena( | 78 const string type_url = type_url_->GetNoArena( |
69 &::google::protobuf::internal::GetEmptyString()); | 79 &::google::protobuf::internal::GetEmptyString()); |
70 const string full_name = descriptor->full_name(); | 80 string full_name; |
71 if (type_url.length() < full_name.length()) { | 81 if (!ParseAnyTypeUrl(type_url, &full_name)) { |
72 return false; | 82 return false; |
73 } | 83 } |
74 return (0 == type_url.compare( | 84 return full_name == descriptor->full_name(); |
75 type_url.length() - full_name.length(), | |
76 full_name.length(), | |
77 full_name)); | |
78 } | 85 } |
79 | 86 |
80 bool ParseAnyTypeUrl(const string& type_url, string* full_type_name) { | 87 bool ParseAnyTypeUrl(const string& type_url, string* full_type_name) { |
81 static const char* prefix[] = { | 88 size_t pos = type_url.find_last_of("/"); |
82 kTypeGoogleApisComPrefix, | 89 if (pos == string::npos || pos + 1 == type_url.size()) { |
83 kTypeGoogleProdComPrefix | 90 return false; |
84 }; | |
85 for (int i = 0; i < 2; i++) { | |
86 const int prefix_len = strlen(prefix[i]); | |
87 if (strncmp(type_url.c_str(), prefix[i], prefix_len) == 0) { | |
88 full_type_name->assign(type_url.data() + prefix_len, | |
89 type_url.size() - prefix_len); | |
90 return true; | |
91 } | |
92 } | 91 } |
93 return false; | 92 *full_type_name = type_url.substr(pos + 1); |
| 93 return true; |
94 } | 94 } |
95 | 95 |
96 | 96 |
97 bool GetAnyFieldDescriptors(const Message& message, | 97 bool GetAnyFieldDescriptors(const Message& message, |
98 const FieldDescriptor** type_url_field, | 98 const FieldDescriptor** type_url_field, |
99 const FieldDescriptor** value_field) { | 99 const FieldDescriptor** value_field) { |
100 const Descriptor* descriptor = message.GetDescriptor(); | 100 const Descriptor* descriptor = message.GetDescriptor(); |
101 if (descriptor->full_name() != kAnyFullTypeName) { | 101 if (descriptor->full_name() != kAnyFullTypeName) { |
102 return false; | 102 return false; |
103 } | 103 } |
104 *type_url_field = descriptor->FindFieldByNumber(1); | 104 *type_url_field = descriptor->FindFieldByNumber(1); |
105 *value_field = descriptor->FindFieldByNumber(2); | 105 *value_field = descriptor->FindFieldByNumber(2); |
106 return (*type_url_field != NULL && | 106 return (*type_url_field != NULL && |
107 (*type_url_field)->type() == FieldDescriptor::TYPE_STRING && | 107 (*type_url_field)->type() == FieldDescriptor::TYPE_STRING && |
108 *value_field != NULL && | 108 *value_field != NULL && |
109 (*value_field)->type() == FieldDescriptor::TYPE_BYTES); | 109 (*value_field)->type() == FieldDescriptor::TYPE_BYTES); |
110 } | 110 } |
111 | 111 |
112 } // namespace internal | 112 } // namespace internal |
113 } // namespace protobuf | 113 } // namespace protobuf |
114 } // namespace google | 114 } // namespace google |
OLD | NEW |