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