diff options
author | svkrasnov <svkrasnov@yandex-team.ru> | 2022-02-10 16:50:36 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:36 +0300 |
commit | 16dee8c560e5d85ff488841421cc9e2bff3da817 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp | |
parent | f844999843f80df761f7bbd58b0d2ca479f2dd32 (diff) | |
download | ydb-16dee8c560e5d85ff488841421cc9e2bff3da817.tar.gz |
Restoring authorship annotation for <svkrasnov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/protobuf/util/ut/common_ut.proto | 36 | ||||
-rw-r--r-- | library/cpp/protobuf/util/walk.cpp | 144 | ||||
-rw-r--r-- | library/cpp/protobuf/util/walk.h | 44 | ||||
-rw-r--r-- | library/cpp/protobuf/util/walk_ut.cpp | 86 | ||||
-rw-r--r-- | library/cpp/protobuf/util/ya.make | 6 |
5 files changed, 158 insertions, 158 deletions
diff --git a/library/cpp/protobuf/util/ut/common_ut.proto b/library/cpp/protobuf/util/ut/common_ut.proto index 766d4e34da..9cf803ffbf 100644 --- a/library/cpp/protobuf/util/ut/common_ut.proto +++ b/library/cpp/protobuf/util/ut/common_ut.proto @@ -18,25 +18,25 @@ message TWalkTest { repeated TWalkTest RepSub = 6; } -message TWalkTestCyclic { - optional TNested OptNested = 1; - repeated uint64 OptInt64 = 2; - optional TWalkTestCyclic OptSub = 3; - optional TEnum OptEnum = 4; +message TWalkTestCyclic { + optional TNested OptNested = 1; + repeated uint64 OptInt64 = 2; + optional TWalkTestCyclic OptSub = 3; + optional TEnum OptEnum = 4; + + message TNested { + optional uint32 OptInt32 = 1; + optional TWalkTestCyclic OptSubNested = 2; + repeated string RepStr = 3; + optional TNested OptNested = 4; + } + enum TEnum { + A = 0; + B = 1; + C = 2; + } +} - message TNested { - optional uint32 OptInt32 = 1; - optional TWalkTestCyclic OptSubNested = 2; - repeated string RepStr = 3; - optional TNested OptNested = 4; - } - enum TEnum { - A = 0; - B = 1; - C = 2; - } -} - message TMergeTestNoMerge { option (DontMerge) = true; diff --git a/library/cpp/protobuf/util/walk.cpp b/library/cpp/protobuf/util/walk.cpp index e1c2aef73d..b65ec03e04 100644 --- a/library/cpp/protobuf/util/walk.cpp +++ b/library/cpp/protobuf/util/walk.cpp @@ -1,72 +1,72 @@ -#include "walk.h" - -#include <util/generic/hash_set.h> - -namespace { - using namespace NProtoBuf; - - template <typename TMessage, typename TOnField> - void DoWalkReflection(TMessage& msg, TOnField& onField) { - const Descriptor* descr = msg.GetDescriptor(); - for (int i1 = 0; i1 < descr->field_count(); ++i1) { - const FieldDescriptor* fd = descr->field(i1); - if (!onField(msg, fd)) { - continue; - } - - std::conditional_t<std::is_const_v<TMessage>, TConstField, TMutableField> ff(msg, fd); - if (ff.IsMessage()) { - for (size_t i2 = 0; i2 < ff.Size(); ++i2) { - if constexpr (std::is_const_v<TMessage>) { - WalkReflection(*ff.template Get<Message>(i2), onField); - } else { - WalkReflection(*ff.MutableMessage(i2), onField); - } - } - } - } - } - - void DoWalkSchema(const Descriptor* descriptor, - std::function<bool(const FieldDescriptor*)>& onField, - THashSet<const Descriptor*>& visited) - { - if (!visited.emplace(descriptor).second) { - return; - } - for (int i1 = 0; i1 < descriptor->field_count(); ++i1) { - const FieldDescriptor* fd = descriptor->field(i1); - if (!onField(fd)) { - continue; - } - - if (fd->type() == FieldDescriptor::Type::TYPE_MESSAGE) { - DoWalkSchema(fd->message_type(), onField, visited); - } - } - visited.erase(descriptor); - } - -} - -namespace NProtoBuf { - void WalkReflection(Message& msg, - std::function<bool(Message&, const FieldDescriptor*)> onField) - { - DoWalkReflection(msg, onField); - } - - void WalkReflection(const Message& msg, - std::function<bool(const Message&, const FieldDescriptor*)> onField) - { - DoWalkReflection(msg, onField); - } - - void WalkSchema(const Descriptor* descriptor, - std::function<bool(const FieldDescriptor*)> onField) - { - THashSet<const Descriptor*> visited; - DoWalkSchema(descriptor, onField, visited); - } - -} // namespace NProtoBuf +#include "walk.h" + +#include <util/generic/hash_set.h> + +namespace { + using namespace NProtoBuf; + + template <typename TMessage, typename TOnField> + void DoWalkReflection(TMessage& msg, TOnField& onField) { + const Descriptor* descr = msg.GetDescriptor(); + for (int i1 = 0; i1 < descr->field_count(); ++i1) { + const FieldDescriptor* fd = descr->field(i1); + if (!onField(msg, fd)) { + continue; + } + + std::conditional_t<std::is_const_v<TMessage>, TConstField, TMutableField> ff(msg, fd); + if (ff.IsMessage()) { + for (size_t i2 = 0; i2 < ff.Size(); ++i2) { + if constexpr (std::is_const_v<TMessage>) { + WalkReflection(*ff.template Get<Message>(i2), onField); + } else { + WalkReflection(*ff.MutableMessage(i2), onField); + } + } + } + } + } + + void DoWalkSchema(const Descriptor* descriptor, + std::function<bool(const FieldDescriptor*)>& onField, + THashSet<const Descriptor*>& visited) + { + if (!visited.emplace(descriptor).second) { + return; + } + for (int i1 = 0; i1 < descriptor->field_count(); ++i1) { + const FieldDescriptor* fd = descriptor->field(i1); + if (!onField(fd)) { + continue; + } + + if (fd->type() == FieldDescriptor::Type::TYPE_MESSAGE) { + DoWalkSchema(fd->message_type(), onField, visited); + } + } + visited.erase(descriptor); + } + +} + +namespace NProtoBuf { + void WalkReflection(Message& msg, + std::function<bool(Message&, const FieldDescriptor*)> onField) + { + DoWalkReflection(msg, onField); + } + + void WalkReflection(const Message& msg, + std::function<bool(const Message&, const FieldDescriptor*)> onField) + { + DoWalkReflection(msg, onField); + } + + void WalkSchema(const Descriptor* descriptor, + std::function<bool(const FieldDescriptor*)> onField) + { + THashSet<const Descriptor*> visited; + DoWalkSchema(descriptor, onField, visited); + } + +} // namespace NProtoBuf diff --git a/library/cpp/protobuf/util/walk.h b/library/cpp/protobuf/util/walk.h index e630986240..d15d76562d 100644 --- a/library/cpp/protobuf/util/walk.h +++ b/library/cpp/protobuf/util/walk.h @@ -5,29 +5,29 @@ #include <google/protobuf/message.h> #include <google/protobuf/descriptor.h> -#include <functional> - +#include <functional> + namespace NProtoBuf { // Apply @onField processor to each field in @msg (even empty) - // Do not walk deeper the field if the field is an empty message + // Do not walk deeper the field if the field is an empty message + // Returned bool defines if we should walk down deeper to current node children (true), or not (false) + void WalkReflection(Message& msg, + std::function<bool(Message&, const FieldDescriptor*)> onField); + void WalkReflection(const Message& msg, + std::function<bool(const Message&, const FieldDescriptor*)> onField); + + template <typename TOnField> + inline void WalkReflection(Message& msg, TOnField& onField) { // is used when TOnField is a callable class instance + WalkReflection(msg, std::function<bool(Message&, const FieldDescriptor*)>(std::ref(onField))); + } + template <typename TOnField> + inline void WalkReflection(const Message& msg, TOnField& onField) { + WalkReflection(msg, std::function<bool(const Message&, const FieldDescriptor*)>(std::ref(onField))); + } + + // Apply @onField processor to each descriptor of a field + // Walk every field including nested messages. Avoid cyclic fields pointing to themselves // Returned bool defines if we should walk down deeper to current node children (true), or not (false) - void WalkReflection(Message& msg, - std::function<bool(Message&, const FieldDescriptor*)> onField); - void WalkReflection(const Message& msg, - std::function<bool(const Message&, const FieldDescriptor*)> onField); - - template <typename TOnField> - inline void WalkReflection(Message& msg, TOnField& onField) { // is used when TOnField is a callable class instance - WalkReflection(msg, std::function<bool(Message&, const FieldDescriptor*)>(std::ref(onField))); - } - template <typename TOnField> - inline void WalkReflection(const Message& msg, TOnField& onField) { - WalkReflection(msg, std::function<bool(const Message&, const FieldDescriptor*)>(std::ref(onField))); - } - - // Apply @onField processor to each descriptor of a field - // Walk every field including nested messages. Avoid cyclic fields pointing to themselves - // Returned bool defines if we should walk down deeper to current node children (true), or not (false) - void WalkSchema(const Descriptor* descriptor, - std::function<bool(const FieldDescriptor*)> onField); + void WalkSchema(const Descriptor* descriptor, + std::function<bool(const FieldDescriptor*)> onField); } diff --git a/library/cpp/protobuf/util/walk_ut.cpp b/library/cpp/protobuf/util/walk_ut.cpp index 76b833325b..2ea6071b17 100644 --- a/library/cpp/protobuf/util/walk_ut.cpp +++ b/library/cpp/protobuf/util/walk_ut.cpp @@ -51,17 +51,17 @@ Y_UNIT_TEST_SUITE(ProtobufWalk) { return true; } - struct TestStruct { - bool Ok = false; - - TestStruct() = default; - bool operator()(Message&, const FieldDescriptor*) { - Ok = true; - return false; - } - }; - - Y_UNIT_TEST(TestWalkRefl) { + struct TestStruct { + bool Ok = false; + + TestStruct() = default; + bool operator()(Message&, const FieldDescriptor*) { + Ok = true; + return false; + } + }; + + Y_UNIT_TEST(TestWalkRefl) { NProtobufUtilUt::TWalkTest p; InitProto(p); @@ -123,36 +123,36 @@ Y_UNIT_TEST_SUITE(ProtobufWalk) { UNIT_ASSERT(p.RepSubSize() == 2); } } - - Y_UNIT_TEST(TestMutableCallable) { - TestStruct testStruct; - NProtobufUtilUt::TWalkTest p; - InitProto(p); - - WalkReflection(p, testStruct); - UNIT_ASSERT(testStruct.Ok); - } - - Y_UNIT_TEST(TestWalkDescr) { - NProtobufUtilUt::TWalkTestCyclic p; - - TStringBuilder printedSchema; - auto func = [&](const FieldDescriptor* desc) mutable { - printedSchema << desc->DebugString(); - return true; - }; - WalkSchema(p.GetDescriptor(), func); - - TString schema = - "optional .NProtobufUtilUt.TWalkTestCyclic.TNested OptNested = 1;\n" - "optional uint32 OptInt32 = 1;\n" - "optional .NProtobufUtilUt.TWalkTestCyclic OptSubNested = 2;\n" - "repeated string RepStr = 3;\n" - "optional .NProtobufUtilUt.TWalkTestCyclic.TNested OptNested = 4;\n" - "repeated uint64 OptInt64 = 2;\n" - "optional .NProtobufUtilUt.TWalkTestCyclic OptSub = 3;\n" - "optional .NProtobufUtilUt.TWalkTestCyclic.TEnum OptEnum = 4;\n"; - - UNIT_ASSERT_STRINGS_EQUAL(printedSchema, schema); - } + + Y_UNIT_TEST(TestMutableCallable) { + TestStruct testStruct; + NProtobufUtilUt::TWalkTest p; + InitProto(p); + + WalkReflection(p, testStruct); + UNIT_ASSERT(testStruct.Ok); + } + + Y_UNIT_TEST(TestWalkDescr) { + NProtobufUtilUt::TWalkTestCyclic p; + + TStringBuilder printedSchema; + auto func = [&](const FieldDescriptor* desc) mutable { + printedSchema << desc->DebugString(); + return true; + }; + WalkSchema(p.GetDescriptor(), func); + + TString schema = + "optional .NProtobufUtilUt.TWalkTestCyclic.TNested OptNested = 1;\n" + "optional uint32 OptInt32 = 1;\n" + "optional .NProtobufUtilUt.TWalkTestCyclic OptSubNested = 2;\n" + "repeated string RepStr = 3;\n" + "optional .NProtobufUtilUt.TWalkTestCyclic.TNested OptNested = 4;\n" + "repeated uint64 OptInt64 = 2;\n" + "optional .NProtobufUtilUt.TWalkTestCyclic OptSub = 3;\n" + "optional .NProtobufUtilUt.TWalkTestCyclic.TEnum OptEnum = 4;\n"; + + UNIT_ASSERT_STRINGS_EQUAL(printedSchema, schema); + } } diff --git a/library/cpp/protobuf/util/ya.make b/library/cpp/protobuf/util/ya.make index b0c986fb9b..b62028af58 100644 --- a/library/cpp/protobuf/util/ya.make +++ b/library/cpp/protobuf/util/ya.make @@ -18,9 +18,9 @@ SRCS( pb_utils.h repeated_field_utils.h simple_reflection.cpp - walk.cpp + walk.cpp ) END() - -RECURSE_FOR_TESTS(ut) + +RECURSE_FOR_TESTS(ut) |