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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 using google::protobuf::Enum; | 47 using google::protobuf::Enum; |
48 using google::protobuf::EnumValue; | 48 using google::protobuf::EnumValue; |
49 using google::protobuf::Field; | 49 using google::protobuf::Field; |
50 using google::protobuf::Option; | 50 using google::protobuf::Option; |
51 using google::protobuf::Type; | 51 using google::protobuf::Type; |
52 | 52 |
53 using util::Status; | 53 using util::Status; |
54 using util::error::INVALID_ARGUMENT; | 54 using util::error::INVALID_ARGUMENT; |
55 using util::error::NOT_FOUND; | 55 using util::error::NOT_FOUND; |
56 | 56 |
| 57 bool SplitTypeUrl(const string& type_url, string* url_prefix, |
| 58 string* message_name) { |
| 59 size_t pos = type_url.find_last_of("/"); |
| 60 if (pos == string::npos) { |
| 61 return false; |
| 62 } |
| 63 *url_prefix = type_url.substr(0, pos); |
| 64 *message_name = type_url.substr(pos + 1); |
| 65 return true; |
| 66 } |
| 67 |
57 class DescriptorPoolTypeResolver : public TypeResolver { | 68 class DescriptorPoolTypeResolver : public TypeResolver { |
58 public: | 69 public: |
59 DescriptorPoolTypeResolver(const string& url_prefix, | 70 DescriptorPoolTypeResolver(const string& url_prefix, |
60 const DescriptorPool* pool) | 71 const DescriptorPool* pool) |
61 : url_prefix_(url_prefix), pool_(pool) {} | 72 : url_prefix_(url_prefix), pool_(pool) {} |
62 | 73 |
63 Status ResolveMessageType(const string& type_url, Type* type) { | 74 Status ResolveMessageType(const string& type_url, Type* type) { |
64 string type_name; | 75 string url_prefix, message_name; |
65 Status status = ParseTypeUrl(type_url, &type_name); | 76 if (!SplitTypeUrl(type_url, &url_prefix, &message_name) || |
66 if (!status.ok()) { | 77 url_prefix != url_prefix_) { |
67 return status; | 78 return Status(INVALID_ARGUMENT, |
| 79 StrCat("Invalid type URL, type URLs must be of the form '", |
| 80 url_prefix_, "/<typename>', got: ", type_url)); |
68 } | 81 } |
69 | 82 if (url_prefix != url_prefix_) { |
70 const Descriptor* descriptor = pool_->FindMessageTypeByName(type_name); | 83 return Status(INVALID_ARGUMENT, |
| 84 "Cannot resolve types from URL: " + url_prefix); |
| 85 } |
| 86 const Descriptor* descriptor = pool_->FindMessageTypeByName(message_name); |
71 if (descriptor == NULL) { | 87 if (descriptor == NULL) { |
72 return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); | 88 return Status(NOT_FOUND, |
| 89 "Invalid type URL, unknown type: " + message_name); |
73 } | 90 } |
74 ConvertDescriptor(descriptor, type); | 91 ConvertDescriptor(descriptor, type); |
75 return Status(); | 92 return Status(); |
76 } | 93 } |
77 | 94 |
78 Status ResolveEnumType(const string& type_url, Enum* enum_type) { | 95 Status ResolveEnumType(const string& type_url, Enum* enum_type) { |
79 string type_name; | 96 string url_prefix, type_name; |
80 Status status = ParseTypeUrl(type_url, &type_name); | 97 if (!SplitTypeUrl(type_url, &url_prefix, &type_name) || |
81 if (!status.ok()) { | 98 url_prefix != url_prefix_) { |
82 return status; | 99 return Status(INVALID_ARGUMENT, |
| 100 StrCat("Invalid type URL, type URLs must be of the form '", |
| 101 url_prefix_, "/<typename>', got: ", type_url)); |
83 } | 102 } |
84 | 103 if (url_prefix != url_prefix_) { |
| 104 return Status(INVALID_ARGUMENT, |
| 105 "Cannot resolve types from URL: " + url_prefix); |
| 106 } |
85 const EnumDescriptor* descriptor = pool_->FindEnumTypeByName(type_name); | 107 const EnumDescriptor* descriptor = pool_->FindEnumTypeByName(type_name); |
86 if (descriptor == NULL) { | 108 if (descriptor == NULL) { |
87 return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); | 109 return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); |
88 } | 110 } |
89 ConvertEnumDescriptor(descriptor, enum_type); | 111 ConvertEnumDescriptor(descriptor, enum_type); |
90 return Status(); | 112 return Status(); |
91 } | 113 } |
92 | 114 |
93 private: | 115 private: |
94 void ConvertDescriptor(const Descriptor* descriptor, Type* type) { | 116 void ConvertDescriptor(const Descriptor* descriptor, Type* type) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 196 } |
175 | 197 |
176 string GetTypeUrl(const Descriptor* descriptor) { | 198 string GetTypeUrl(const Descriptor* descriptor) { |
177 return url_prefix_ + "/" + descriptor->full_name(); | 199 return url_prefix_ + "/" + descriptor->full_name(); |
178 } | 200 } |
179 | 201 |
180 string GetTypeUrl(const EnumDescriptor* descriptor) { | 202 string GetTypeUrl(const EnumDescriptor* descriptor) { |
181 return url_prefix_ + "/" + descriptor->full_name(); | 203 return url_prefix_ + "/" + descriptor->full_name(); |
182 } | 204 } |
183 | 205 |
184 Status ParseTypeUrl(const string& type_url, string* type_name) { | |
185 if (type_url.substr(0, url_prefix_.size() + 1) != url_prefix_ + "/") { | |
186 return Status(INVALID_ARGUMENT, | |
187 StrCat("Invalid type URL, type URLs must be of the form '", | |
188 url_prefix_, "/<typename>', got: ", type_url)); | |
189 } | |
190 *type_name = type_url.substr(url_prefix_.size() + 1); | |
191 return Status(); | |
192 } | |
193 | |
194 string DefaultValueAsString(const FieldDescriptor* descriptor) { | 206 string DefaultValueAsString(const FieldDescriptor* descriptor) { |
195 switch (descriptor->cpp_type()) { | 207 switch (descriptor->cpp_type()) { |
196 case FieldDescriptor::CPPTYPE_INT32: | 208 case FieldDescriptor::CPPTYPE_INT32: |
197 return SimpleItoa(descriptor->default_value_int32()); | 209 return SimpleItoa(descriptor->default_value_int32()); |
198 break; | 210 break; |
199 case FieldDescriptor::CPPTYPE_INT64: | 211 case FieldDescriptor::CPPTYPE_INT64: |
200 return SimpleItoa(descriptor->default_value_int64()); | 212 return SimpleItoa(descriptor->default_value_int64()); |
201 break; | 213 break; |
202 case FieldDescriptor::CPPTYPE_UINT32: | 214 case FieldDescriptor::CPPTYPE_UINT32: |
203 return SimpleItoa(descriptor->default_value_uint32()); | 215 return SimpleItoa(descriptor->default_value_uint32()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 } // namespace | 250 } // namespace |
239 | 251 |
240 TypeResolver* NewTypeResolverForDescriptorPool(const string& url_prefix, | 252 TypeResolver* NewTypeResolverForDescriptorPool(const string& url_prefix, |
241 const DescriptorPool* pool) { | 253 const DescriptorPool* pool) { |
242 return new DescriptorPoolTypeResolver(url_prefix, pool); | 254 return new DescriptorPoolTypeResolver(url_prefix, pool); |
243 } | 255 } |
244 | 256 |
245 } // namespace util | 257 } // namespace util |
246 } // namespace protobuf | 258 } // namespace protobuf |
247 } // namespace google | 259 } // namespace google |
OLD | NEW |