diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/protobuf/util/path.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/protobuf/util/path.cpp')
-rw-r--r-- | library/cpp/protobuf/util/path.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/library/cpp/protobuf/util/path.cpp b/library/cpp/protobuf/util/path.cpp new file mode 100644 index 0000000000..efa2a42c8a --- /dev/null +++ b/library/cpp/protobuf/util/path.cpp @@ -0,0 +1,61 @@ +#include "path.h" + +#include <util/generic/yexception.h> + +namespace NProtoBuf { + TFieldPath::TFieldPath() { + } + + TFieldPath::TFieldPath(const Descriptor* msgType, const TStringBuf& path) { + Init(msgType, path); + } + + TFieldPath::TFieldPath(const TVector<const FieldDescriptor*>& path) + : Path(path) + { + } + + bool TFieldPath::InitUnsafe(const Descriptor* msgType, TStringBuf path) { + Path.clear(); + while (path) { + TStringBuf next; + while (!next && path) + next = path.NextTok('/'); + if (!next) + return true; + + if (!msgType) // need field but no message type + return false; + + TString nextStr(next); + const FieldDescriptor* field = msgType->FindFieldByName(nextStr); + if (!field) { + // Try to find extension field by FindAllExtensions() + const DescriptorPool* pool = msgType->file()->pool(); + Y_ASSERT(pool); // never NULL by protobuf docs + TVector<const FieldDescriptor*> extensions; + pool->FindAllExtensions(msgType, &extensions); // find all extensions of this extendee + for (const FieldDescriptor* ext : extensions) { + if (ext->full_name() == nextStr || ext->name() == nextStr) { + if (field) + return false; // ambiguity + field = ext; + } + } + } + + if (!field) + return false; + + Path.push_back(field); + msgType = field->type() == FieldDescriptor::TYPE_MESSAGE ? field->message_type() : nullptr; + } + return true; + } + + void TFieldPath::Init(const Descriptor* msgType, const TStringBuf& path) { + if (!InitUnsafe(msgType, path)) + ythrow yexception() << "Failed to resolve path \"" << path << "\" relative to " << msgType->full_name(); + } + +} |