Browse Source

Restoring authorship annotation for <svkrasnov@yandex-team.ru>. Commit 2 of 2.

svkrasnov 3 years ago
parent
commit
16dee8c560

+ 18 - 18
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;
 

+ 72 - 72
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

+ 22 - 22
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);
 }

+ 43 - 43
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);
+    }
 }

+ 3 - 3
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)