diff options
author | Vasily Gerasimov <UgnineSirdis@gmail.com> | 2022-02-10 16:49:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:09 +0300 |
commit | 6cdc8f140213c595e4ad38bc3d97fcef1146b8c3 (patch) | |
tree | f69637041e6fed76ebae0c74ae1fa0c4be6ab5b4 /library/cpp/protobuf/util | |
parent | e5d4696304c6689379ac7ce334512404d4b7836c (diff) | |
download | ydb-6cdc8f140213c595e4ad38bc3d97fcef1146b8c3.tar.gz |
Restoring authorship annotation for Vasily Gerasimov <UgnineSirdis@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf/util')
-rw-r--r-- | library/cpp/protobuf/util/cast.h | 4 | ||||
-rw-r--r-- | library/cpp/protobuf/util/path.cpp | 34 | ||||
-rw-r--r-- | library/cpp/protobuf/util/path.h | 32 | ||||
-rw-r--r-- | library/cpp/protobuf/util/proto/ya.make | 18 | ||||
-rw-r--r-- | library/cpp/protobuf/util/repeated_field_utils.h | 8 | ||||
-rw-r--r-- | library/cpp/protobuf/util/repeated_field_utils_ut.cpp | 84 | ||||
-rw-r--r-- | library/cpp/protobuf/util/simple_reflection.cpp | 32 | ||||
-rw-r--r-- | library/cpp/protobuf/util/simple_reflection.h | 32 | ||||
-rw-r--r-- | library/cpp/protobuf/util/simple_reflection_ut.cpp | 348 | ||||
-rw-r--r-- | library/cpp/protobuf/util/traits.h | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/util/ut/extensions.proto | 42 | ||||
-rw-r--r-- | library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto | 16 | ||||
-rw-r--r-- | library/cpp/protobuf/util/ut/ya.make | 10 | ||||
-rw-r--r-- | library/cpp/protobuf/util/walk.h | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/util/ya.make | 8 |
15 files changed, 336 insertions, 336 deletions
diff --git a/library/cpp/protobuf/util/cast.h b/library/cpp/protobuf/util/cast.h index 83749dfcee..d368d13766 100644 --- a/library/cpp/protobuf/util/cast.h +++ b/library/cpp/protobuf/util/cast.h @@ -1,11 +1,11 @@ #pragma once -#include "traits.h" +#include "traits.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> -#include <util/generic/cast.h> +#include <util/generic/cast.h> namespace NProtoBuf { // C++ compatible conversions of FieldDescriptor::CppType's diff --git a/library/cpp/protobuf/util/path.cpp b/library/cpp/protobuf/util/path.cpp index efa2a42c8a..8a9c2ba7d7 100644 --- a/library/cpp/protobuf/util/path.cpp +++ b/library/cpp/protobuf/util/path.cpp @@ -1,20 +1,20 @@ -#include "path.h" - -#include <util/generic/yexception.h> - -namespace NProtoBuf { +#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) { @@ -23,10 +23,10 @@ namespace NProtoBuf { 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) { @@ -41,21 +41,21 @@ namespace NProtoBuf { 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(); } -} +} diff --git a/library/cpp/protobuf/util/path.h b/library/cpp/protobuf/util/path.h index 487f643a2d..4fbee86f26 100644 --- a/library/cpp/protobuf/util/path.h +++ b/library/cpp/protobuf/util/path.h @@ -1,11 +1,11 @@ -#pragma once - +#pragma once + #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> - -#include <util/generic/vector.h> - -namespace NProtoBuf { + +#include <util/generic/vector.h> + +namespace NProtoBuf { class TFieldPath { public: TFieldPath(); @@ -13,40 +13,40 @@ namespace NProtoBuf { TFieldPath(const TVector<const FieldDescriptor*>& path); TFieldPath(const TFieldPath&) = default; TFieldPath& operator=(const TFieldPath&) = default; - + bool InitUnsafe(const Descriptor* msgType, const TStringBuf path); // noexcept void Init(const Descriptor* msgType, const TStringBuf& path); // throws - + const TVector<const FieldDescriptor*>& Fields() const { return Path; } - + void AddField(const FieldDescriptor* field) { Path.push_back(field); } - + const Descriptor* ParentType() const { return Empty() ? nullptr : Path.front()->containing_type(); } - + const FieldDescriptor* FieldDescr() const { return Empty() ? nullptr : Path.back(); } - + bool Empty() const { return Path.empty(); } - + explicit operator bool() const { return !Empty(); } - + bool operator!() const { return Empty(); } - + private: TVector<const FieldDescriptor*> Path; }; - + } diff --git a/library/cpp/protobuf/util/proto/ya.make b/library/cpp/protobuf/util/proto/ya.make index 4d68047d8b..3a3d58e486 100644 --- a/library/cpp/protobuf/util/proto/ya.make +++ b/library/cpp/protobuf/util/proto/ya.make @@ -1,11 +1,11 @@ -PROTO_LIBRARY() - -OWNER(mowgli) - -SRCS( - merge.proto -) - +PROTO_LIBRARY() + +OWNER(mowgli) + +SRCS( + merge.proto +) + EXCLUDE_TAGS(GO_PROTO) -END() +END() diff --git a/library/cpp/protobuf/util/repeated_field_utils.h b/library/cpp/protobuf/util/repeated_field_utils.h index c07bd84647..8f7428b5dc 100644 --- a/library/cpp/protobuf/util/repeated_field_utils.h +++ b/library/cpp/protobuf/util/repeated_field_utils.h @@ -38,8 +38,8 @@ namespace NProtoBuf { T* ret = field->Add(); MoveRepeatedFieldItem(field, field->size() - 1, index); return ret; - } - + } + template <typename TRepeated> // suitable both for RepeatedField and RepeatedPtrField static void RemoveRepeatedFieldItem(TRepeated* field, size_t index) { if ((int)index >= field->size()) @@ -70,8 +70,8 @@ namespace NProtoBuf { for (int i = begIndex; i < endIndex; ++i, ++shiftIndex) field->SwapElements(shiftIndex, i); } - } - + } + // Remove several items at once, could be more efficient compared to calling RemoveRepeatedFieldItem several times template <typename TRepeated> static void RemoveRepeatedFieldItems(TRepeated* field, const TVector<size_t>& sortedIndices) { diff --git a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp index 58aaaa9e12..94a494e1a3 100644 --- a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp +++ b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp @@ -1,46 +1,46 @@ -#include "repeated_field_utils.h" +#include "repeated_field_utils.h" #include <library/cpp/protobuf/util/ut/common_ut.pb.h> - + #include <library/cpp/testing/unittest/registar.h> - -using namespace NProtoBuf; - + +using namespace NProtoBuf; + Y_UNIT_TEST_SUITE(RepeatedFieldUtils) { Y_UNIT_TEST(RemoveIf) { - { - NProtobufUtilUt::TWalkTest msg; - msg.AddRepInt(0); - msg.AddRepInt(1); - msg.AddRepInt(2); - msg.AddRepInt(3); - msg.AddRepInt(4); - msg.AddRepInt(5); - auto cond = [](ui32 val) { - return val % 2 == 0; - }; - RemoveRepeatedFieldItemIf(msg.MutableRepInt(), cond); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepIntSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepInt(1)); - UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepInt(2)); - } - - { - NProtobufUtilUt::TWalkTest msg; - msg.AddRepSub()->SetOptInt(0); - msg.AddRepSub()->SetOptInt(1); - msg.AddRepSub()->SetOptInt(2); - msg.AddRepSub()->SetOptInt(3); - msg.AddRepSub()->SetOptInt(4); - msg.AddRepSub()->SetOptInt(5); - auto cond = [](const NProtobufUtilUt::TWalkTest& val) { - return val.GetOptInt() % 2 == 0; - }; - RemoveRepeatedFieldItemIf(msg.MutableRepSub(), cond); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepSubSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepSub(0).GetOptInt()); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepSub(1).GetOptInt()); - UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepSub(2).GetOptInt()); - } - } -} + { + NProtobufUtilUt::TWalkTest msg; + msg.AddRepInt(0); + msg.AddRepInt(1); + msg.AddRepInt(2); + msg.AddRepInt(3); + msg.AddRepInt(4); + msg.AddRepInt(5); + auto cond = [](ui32 val) { + return val % 2 == 0; + }; + RemoveRepeatedFieldItemIf(msg.MutableRepInt(), cond); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepIntSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepInt(1)); + UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepInt(2)); + } + + { + NProtobufUtilUt::TWalkTest msg; + msg.AddRepSub()->SetOptInt(0); + msg.AddRepSub()->SetOptInt(1); + msg.AddRepSub()->SetOptInt(2); + msg.AddRepSub()->SetOptInt(3); + msg.AddRepSub()->SetOptInt(4); + msg.AddRepSub()->SetOptInt(5); + auto cond = [](const NProtobufUtilUt::TWalkTest& val) { + return val.GetOptInt() % 2 == 0; + }; + RemoveRepeatedFieldItemIf(msg.MutableRepSub(), cond); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepSubSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepSub(0).GetOptInt()); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepSub(1).GetOptInt()); + UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepSub(2).GetOptInt()); + } + } +} diff --git a/library/cpp/protobuf/util/simple_reflection.cpp b/library/cpp/protobuf/util/simple_reflection.cpp index d842e9ee44..83afa0ce0c 100644 --- a/library/cpp/protobuf/util/simple_reflection.cpp +++ b/library/cpp/protobuf/util/simple_reflection.cpp @@ -1,10 +1,10 @@ -#include "simple_reflection.h" - -namespace NProtoBuf { +#include "simple_reflection.h" + +namespace NProtoBuf { const Message* GetMessageHelper(const TConstField& curField, bool) { return curField.HasValue() && curField.IsMessage() ? curField.Get<Message>() : nullptr; } - + Message* GetMessageHelper(TMutableField& curField, bool createPath) { if (curField.IsMessage()) { if (!curField.HasValue()) { @@ -13,14 +13,14 @@ namespace NProtoBuf { } else { return curField.MutableMessage(); } - } + } return nullptr; - } - + } + template <class TField, class TMsg> TMaybe<TField> ByPathImpl(TMsg& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath) { if (fieldsPath.empty()) - return TMaybe<TField>(); + return TMaybe<TField>(); TMsg* curParent = &msg; for (size_t i = 0, size = fieldsPath.size(); i < size; ++i) { const FieldDescriptor* field = fieldsPath[i]; @@ -35,36 +35,36 @@ namespace NProtoBuf { return TField(*curParent, fieldsPath.back()); else return TMaybe<TField>(); - } - + } + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TVector<const FieldDescriptor*>& fieldsPath) { return ByPathImpl<TConstField, const Message>(msg, fieldsPath, false); } - + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TStringBuf& path) { TFieldPath fieldPath; if (!fieldPath.InitUnsafe(msg.GetDescriptor(), path)) return TMaybe<TConstField>(); return ByPathImpl<TConstField, const Message>(msg, fieldPath.Fields(), false); } - + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TFieldPath& path) { return ByPathImpl<TConstField, const Message>(msg, path.Fields(), false); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath) { return ByPathImpl<TMutableField, Message>(msg, fieldsPath, createPath); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TStringBuf& path, bool createPath) { TFieldPath fieldPath; if (!fieldPath.InitUnsafe(msg.GetDescriptor(), path)) return TMaybe<TMutableField>(); return ByPathImpl<TMutableField, Message>(msg, fieldPath.Fields(), createPath); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TFieldPath& path, bool createPath) { return ByPathImpl<TMutableField, Message>(msg, path.Fields(), createPath); } -} +} diff --git a/library/cpp/protobuf/util/simple_reflection.h b/library/cpp/protobuf/util/simple_reflection.h index 61e877a787..a5dd46ac79 100644 --- a/library/cpp/protobuf/util/simple_reflection.h +++ b/library/cpp/protobuf/util/simple_reflection.h @@ -1,17 +1,17 @@ #pragma once -#include "cast.h" -#include "path.h" +#include "cast.h" +#include "path.h" #include "traits.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> -#include <util/generic/maybe.h> -#include <util/generic/typetraits.h> -#include <util/generic/vector.h> -#include <util/system/defaults.h> - +#include <util/generic/maybe.h> +#include <util/generic/typetraits.h> +#include <util/generic/vector.h> +#include <util/system/defaults.h> + namespace NProtoBuf { class TConstField { public: @@ -29,7 +29,7 @@ namespace NProtoBuf { const Message& Parent() const { return Msg; } - + const FieldDescriptor* Field() const { return Fd; } @@ -80,7 +80,7 @@ namespace NProtoBuf { bool IsMessage() const { return CppType() == FieldDescriptor::CPPTYPE_MESSAGE; } - + bool HasSameType(const TConstField& other) const { if (CppType() != other.CppType()) return false; @@ -90,7 +90,7 @@ namespace NProtoBuf { return false; return true; } - + protected: bool IsRepeated() const { return Fd->is_repeated(); @@ -137,7 +137,7 @@ namespace NProtoBuf { template <typename T> inline void Add(T value); - + inline void MergeFrom(const TConstField& src); inline void Clear() { @@ -167,17 +167,17 @@ namespace NProtoBuf { return; Refl().SwapElements(Mut(), Fd, index1, index2); } - + inline void Remove(size_t index) { if (index >= Size()) return; - + // Move to the end for (size_t i = index, size = Size(); i < size - 1; ++i) SwapElements(i, i + 1); RemoveLast(); } - + Message* MutableMessage(size_t index = 0) { Y_ASSERT(IsMessage()); if (IsRepeated()) { @@ -193,12 +193,12 @@ namespace NProtoBuf { inline TMsg* AddMessage() { return CheckedCast<TMsg*>(AddMessage()); } - + inline Message* AddMessage() { Y_ASSERT(IsMessage() && IsRepeated()); return Refl().AddMessage(Mut(), Fd); } - + private: Message* Mut() { return const_cast<Message*>(&Msg); diff --git a/library/cpp/protobuf/util/simple_reflection_ut.cpp b/library/cpp/protobuf/util/simple_reflection_ut.cpp index 169d4703c9..e380991c02 100644 --- a/library/cpp/protobuf/util/simple_reflection_ut.cpp +++ b/library/cpp/protobuf/util/simple_reflection_ut.cpp @@ -7,8 +7,8 @@ using namespace NProtoBuf; Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { - static TSample GenSampleForMergeFrom() { - TSample smf; + static TSample GenSampleForMergeFrom() { + TSample smf; smf.SetOneStr("one str"); smf.MutableOneMsg()->AddRepInt(1); smf.AddRepMsg()->AddRepInt(2); @@ -20,8 +20,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromGeneric) { - const TSample src(GenSampleForMergeFrom()); - TSample dst; + const TSample src(GenSampleForMergeFrom()); + TSample dst; const Descriptor* descr = dst.GetDescriptor(); { @@ -52,8 +52,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromSelf) { - const TSample sample(GenSampleForMergeFrom()); - TSample msg(sample); + const TSample sample(GenSampleForMergeFrom()); + TSample msg(sample); const Descriptor* descr = msg.GetDescriptor(); TMutableField oneStr(msg, descr->FindFieldByName("OneStr")); @@ -66,8 +66,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromAnotherFD) { - const TSample sample(GenSampleForMergeFrom()); - TSample msg(GenSampleForMergeFrom()); + const TSample sample(GenSampleForMergeFrom()); + TSample msg(GenSampleForMergeFrom()); const Descriptor* descr = msg.GetDescriptor(); { // string @@ -95,205 +95,205 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { UNIT_ASSERT_VALUES_EQUAL(msg.RepMsgSize(), sample.RepMsgSize() + 1); } } - + Y_UNIT_TEST(RemoveByIndex) { - TSample msg; - - const Descriptor* descr = msg.GetDescriptor(); - { - TMutableField fld(msg, descr->FindFieldByName("RepMsg")); - msg.AddRepMsg()->AddRepInt(1); - msg.AddRepMsg()->AddRepInt(2); - msg.AddRepMsg()->AddRepInt(3); - - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 2, 3 + TSample msg; + + const Descriptor* descr = msg.GetDescriptor(); + { + TMutableField fld(msg, descr->FindFieldByName("RepMsg")); + msg.AddRepMsg()->AddRepInt(1); + msg.AddRepMsg()->AddRepInt(2); + msg.AddRepMsg()->AddRepInt(3); + + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 2, 3 fld.Remove(1); // from middle - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); - - msg.AddRepMsg()->AddRepInt(5); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 3, 5 + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); + + msg.AddRepMsg()->AddRepInt(5); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 3, 5 fld.Remove(2); // from end - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); - msg.ClearRepMsg(); - } - - { - TMutableField fld(msg, descr->FindFieldByName("RepStr")); - msg.AddRepStr("1"); - msg.AddRepStr("2"); - msg.AddRepStr("3"); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepStrSize()); // "1", "2", "3" + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); + msg.ClearRepMsg(); + } + + { + TMutableField fld(msg, descr->FindFieldByName("RepStr")); + msg.AddRepStr("1"); + msg.AddRepStr("2"); + msg.AddRepStr("3"); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepStrSize()); // "1", "2", "3" fld.Remove(0); // from begin - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepStrSize()); - UNIT_ASSERT_VALUES_EQUAL("2", msg.GetRepStr(0)); - UNIT_ASSERT_VALUES_EQUAL("3", msg.GetRepStr(1)); - } - - { - TMutableField fld(msg, descr->FindFieldByName("OneStr")); - msg.SetOneStr("1"); - UNIT_ASSERT(msg.HasOneStr()); - fld.Remove(0); // not repeated - UNIT_ASSERT(!msg.HasOneStr()); - } - } - + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepStrSize()); + UNIT_ASSERT_VALUES_EQUAL("2", msg.GetRepStr(0)); + UNIT_ASSERT_VALUES_EQUAL("3", msg.GetRepStr(1)); + } + + { + TMutableField fld(msg, descr->FindFieldByName("OneStr")); + msg.SetOneStr("1"); + UNIT_ASSERT(msg.HasOneStr()); + fld.Remove(0); // not repeated + UNIT_ASSERT(!msg.HasOneStr()); + } + } + Y_UNIT_TEST(GetFieldByPath) { - // Simple get by path - { - TSample msg; - msg.SetOneStr("1"); - msg.MutableOneMsg()->AddRepInt(2); - msg.MutableOneMsg()->AddRepInt(3); - msg.AddRepMsg()->AddRepInt(4); - msg.MutableRepMsg(0)->AddRepInt(5); - msg.AddRepMsg()->AddRepInt(6); - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + // Simple get by path + { + TSample msg; + msg.SetOneStr("1"); + msg.MutableOneMsg()->AddRepInt(2); + msg.MutableOneMsg()->AddRepInt(3); + msg.AddRepMsg()->AddRepInt(4); + msg.MutableRepMsg(0)->AddRepInt(5); + msg.AddRepMsg()->AddRepInt(6); + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); UNIT_ASSERT_VALUES_EQUAL("1", (field->Get<TString>())); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT(field->IsMessageInstance<TInnerSample>()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "/OneMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT(field->IsMessageInstance<TInnerSample>()); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "/OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>(0)); - UNIT_ASSERT_VALUES_EQUAL(3, field->Get<int>(1)); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); + UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>(0)); + UNIT_ASSERT_VALUES_EQUAL(3, field->Get<int>(1)); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); - UNIT_ASSERT_VALUES_EQUAL(4, field->Get<int>(0)); - UNIT_ASSERT_VALUES_EQUAL(5, field->Get<int>(1)); - } - } - - // get of unset fields - { - TSample msg; - msg.MutableOneMsg(); - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); + UNIT_ASSERT_VALUES_EQUAL(4, field->Get<int>(0)); + UNIT_ASSERT_VALUES_EQUAL(5, field->Get<int>(1)); + } + } + + // get of unset fields + { + TSample msg; + msg.MutableOneMsg(); + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg/RepInt"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(!field); - } - } - - // mutable - { - TSample msg; - msg.MutableOneMsg(); - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(!field); + } + } + + // mutable + { + TSample msg; + msg.MutableOneMsg(); + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); field->Set(TString("zz")); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL("zz", msg.GetOneStr()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL("zz", msg.GetOneStr()); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); field->Set(TString("dd")); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL("dd", msg.GetOneStr()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL("dd", msg.GetOneStr()); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - field->Add(10); - UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(!field); - } - } - - // mutable with path creation - { - TSample msg; - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr", true); - UNIT_ASSERT(field); + field->Add(10); + UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(!field); + } + } + + // mutable with path creation + { + TSample msg; + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr", true); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt", true); - UNIT_ASSERT(field); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt", true); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - UNIT_ASSERT(msg.HasOneMsg()); - field->Add(10); - UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt", true); - TMaybe<TMutableField> fieldCopy = TMutableField::ByPath(msg, "RepMsg/RepInt", true); + UNIT_ASSERT(msg.HasOneMsg()); + field->Add(10); + UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt", true); + TMaybe<TMutableField> fieldCopy = TMutableField::ByPath(msg, "RepMsg/RepInt", true); Y_UNUSED(fieldCopy); - UNIT_ASSERT(field); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.RepMsgSize()); - field->Add(12); - UNIT_ASSERT_VALUES_EQUAL(12, field->Get<int>()); - } - } - - // error - { + UNIT_ASSERT_VALUES_EQUAL(1, msg.RepMsgSize()); + field->Add(12); + UNIT_ASSERT_VALUES_EQUAL(12, field->Get<int>()); + } + } + + // error + { {TSample msg; UNIT_ASSERT(!TConstField::ByPath(msg, "SomeField")); } - + { TSample msg; UNIT_ASSERT(!TMutableField::ByPath(msg, "SomeField/FieldSome")); } - + { TSample msg; UNIT_ASSERT(!TMutableField::ByPath(msg, "SomeField/FieldSome", true)); } } - + // extension { TSample msg; @@ -303,13 +303,13 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { TInnerSample* subMsg = msg.MutableExtension(NExt::SubMsgExt); subMsg->AddRepInt(20); subMsg->SetExtension(NExt::Ext3Field, 54); - + { TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.TTestExt.ExtField"); UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); UNIT_ASSERT_VALUES_EQUAL("ext", field->Get<TString>()); - } + } { TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.ExtField"); UNIT_ASSERT(field); diff --git a/library/cpp/protobuf/util/traits.h b/library/cpp/protobuf/util/traits.h index 50f036d0ea..01b49e4184 100644 --- a/library/cpp/protobuf/util/traits.h +++ b/library/cpp/protobuf/util/traits.h @@ -177,7 +177,7 @@ namespace NProtoBuf { static inline T GetDefault(const FieldDescriptor* field) { return TBaseTraits::GetDefault(field); } - + static inline bool Has(const Message& msg, const FieldDescriptor* field) { return TBaseTraits::Has(msg, field); } diff --git a/library/cpp/protobuf/util/ut/extensions.proto b/library/cpp/protobuf/util/ut/extensions.proto index 4944f0f5ca..0ef6a6fec2 100644 --- a/library/cpp/protobuf/util/ut/extensions.proto +++ b/library/cpp/protobuf/util/ut/extensions.proto @@ -1,22 +1,22 @@ -package NExt; - +package NExt; + import "library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto"; - -message TTestExt { - extend TSample { - optional string ExtField = 100; - } -} - -extend TSample { - optional uint64 ExtField = 150; // the same name, but another full name -} - -extend TSample { - repeated uint64 Ext2Field = 105; - optional TInnerSample SubMsgExt = 111; -} - -extend TInnerSample { - optional uint64 Ext3Field = 100; -} + +message TTestExt { + extend TSample { + optional string ExtField = 100; + } +} + +extend TSample { + optional uint64 ExtField = 150; // the same name, but another full name +} + +extend TSample { + repeated uint64 Ext2Field = 105; + optional TInnerSample SubMsgExt = 111; +} + +extend TInnerSample { + optional uint64 Ext3Field = 100; +} diff --git a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto index cca1dd869a..88e4f0f877 100644 --- a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto +++ b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto @@ -1,16 +1,16 @@ -message TInnerSample { +message TInnerSample { repeated int32 RepInt = 1; - - extensions 100 to 199; + + extensions 100 to 199; } -message TSample { +message TSample { optional string OneStr = 1; - optional TInnerSample OneMsg = 2; - repeated TInnerSample RepMsg = 3; + optional TInnerSample OneMsg = 2; + repeated TInnerSample RepMsg = 3; repeated string RepStr = 4; optional string AnotherOneStr = 5; - + optional int32 OneInt = 6; repeated int32 RepInt = 7; @@ -21,5 +21,5 @@ message TSample { optional EEnum OneEnum = 8; repeated EEnum RepEnum = 9; - extensions 100 to 199; + extensions 100 to 199; } diff --git a/library/cpp/protobuf/util/ut/ya.make b/library/cpp/protobuf/util/ut/ya.make index 701ba9a8c8..dd850af6cb 100644 --- a/library/cpp/protobuf/util/ut/ya.make +++ b/library/cpp/protobuf/util/ut/ya.make @@ -3,16 +3,16 @@ OWNER(nga) UNITTEST_FOR(library/cpp/protobuf/util) SRCS( - extensions.proto + extensions.proto sample_for_is_equal.proto sample_for_simple_reflection.proto common_ut.proto pb_io_ut.cpp - is_equal_ut.cpp + is_equal_ut.cpp iterators_ut.cpp - simple_reflection_ut.cpp - repeated_field_utils_ut.cpp - walk_ut.cpp + simple_reflection_ut.cpp + repeated_field_utils_ut.cpp + walk_ut.cpp merge_ut.cpp ) diff --git a/library/cpp/protobuf/util/walk.h b/library/cpp/protobuf/util/walk.h index d15d76562d..f5559fd907 100644 --- a/library/cpp/protobuf/util/walk.h +++ b/library/cpp/protobuf/util/walk.h @@ -30,4 +30,4 @@ namespace NProtoBuf { // 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); -} +} diff --git a/library/cpp/protobuf/util/ya.make b/library/cpp/protobuf/util/ya.make index b62028af58..6f0299b76b 100644 --- a/library/cpp/protobuf/util/ya.make +++ b/library/cpp/protobuf/util/ya.make @@ -10,14 +10,14 @@ PEERDIR( ) SRCS( - is_equal.cpp + is_equal.cpp iterators.h merge.cpp - path.cpp - pb_io.cpp + path.cpp + pb_io.cpp pb_utils.h repeated_field_utils.h - simple_reflection.cpp + simple_reflection.cpp walk.cpp ) |