diff options
Diffstat (limited to 'src/google/protobuf/util/type_resolver_util.cc')
-rw-r--r-- | src/google/protobuf/util/type_resolver_util.cc | 56 |
1 files changed, 22 insertions, 34 deletions
diff --git a/src/google/protobuf/util/type_resolver_util.cc b/src/google/protobuf/util/type_resolver_util.cc index 96393903..febaa41f 100644 --- a/src/google/protobuf/util/type_resolver_util.cc +++ b/src/google/protobuf/util/type_resolver_util.cc @@ -54,17 +54,6 @@ using util::Status; using util::error::INVALID_ARGUMENT; using util::error::NOT_FOUND; -bool SplitTypeUrl(const string& type_url, string* url_prefix, - string* message_name) { - size_t pos = type_url.find_last_of("/"); - if (pos == string::npos) { - return false; - } - *url_prefix = type_url.substr(0, pos); - *message_name = type_url.substr(pos + 1); - return true; -} - class DescriptorPoolTypeResolver : public TypeResolver { public: DescriptorPoolTypeResolver(const string& url_prefix, @@ -72,38 +61,27 @@ class DescriptorPoolTypeResolver : public TypeResolver { : url_prefix_(url_prefix), pool_(pool) {} Status ResolveMessageType(const string& type_url, Type* type) { - string url_prefix, message_name; - if (!SplitTypeUrl(type_url, &url_prefix, &message_name) || - url_prefix != url_prefix_) { - return Status(INVALID_ARGUMENT, - StrCat("Invalid type URL, type URLs must be of the form '", - url_prefix_, "/<typename>', got: ", type_url)); + string type_name; + Status status = ParseTypeUrl(type_url, &type_name); + if (!status.ok()) { + return status; } - if (url_prefix != url_prefix_) { - return Status(INVALID_ARGUMENT, - "Cannot resolve types from URL: " + url_prefix); - } - const Descriptor* descriptor = pool_->FindMessageTypeByName(message_name); + + const Descriptor* descriptor = pool_->FindMessageTypeByName(type_name); if (descriptor == NULL) { - return Status(NOT_FOUND, - "Invalid type URL, unknown type: " + message_name); + return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); } ConvertDescriptor(descriptor, type); return Status(); } Status ResolveEnumType(const string& type_url, Enum* enum_type) { - string url_prefix, type_name; - if (!SplitTypeUrl(type_url, &url_prefix, &type_name) || - url_prefix != url_prefix_) { - return Status(INVALID_ARGUMENT, - StrCat("Invalid type URL, type URLs must be of the form '", - url_prefix_, "/<typename>', got: ", type_url)); - } - if (url_prefix != url_prefix_) { - return Status(INVALID_ARGUMENT, - "Cannot resolve types from URL: " + url_prefix); + string type_name; + Status status = ParseTypeUrl(type_url, &type_name); + if (!status.ok()) { + return status; } + const EnumDescriptor* descriptor = pool_->FindEnumTypeByName(type_name); if (descriptor == NULL) { return Status(NOT_FOUND, "Invalid type URL, unknown type: " + type_name); @@ -203,6 +181,16 @@ class DescriptorPoolTypeResolver : public TypeResolver { return url_prefix_ + "/" + descriptor->full_name(); } + Status ParseTypeUrl(const string& type_url, string* type_name) { + if (type_url.substr(0, url_prefix_.size() + 1) != url_prefix_ + "/") { + return Status(INVALID_ARGUMENT, + StrCat("Invalid type URL, type URLs must be of the form '", + url_prefix_, "/<typename>', got: ", type_url)); + } + *type_name = type_url.substr(url_prefix_.size() + 1); + return Status(); + } + string DefaultValueAsString(const FieldDescriptor* descriptor) { switch (descriptor->cpp_type()) { case FieldDescriptor::CPPTYPE_INT32: |