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 | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/protobuf/util')
33 files changed, 3262 insertions, 0 deletions
diff --git a/library/cpp/protobuf/util/cast.h b/library/cpp/protobuf/util/cast.h new file mode 100644 index 0000000000..83749dfcee --- /dev/null +++ b/library/cpp/protobuf/util/cast.h @@ -0,0 +1,156 @@ +#pragma once + +#include "traits.h" + +#include <google/protobuf/descriptor.h> +#include <google/protobuf/message.h> + +#include <util/generic/cast.h> + +namespace NProtoBuf { + // C++ compatible conversions of FieldDescriptor::CppType's + + using ECppType = FieldDescriptor::CppType; + + namespace NCast { + template <ECppType src, ECppType dst> + struct TIsCompatibleCppType { + enum { + Result = src == dst || + (TIsNumericCppType<src>::Result && TIsNumericCppType<dst>::Result) + }; + }; + + template <ECppType src, ECppType dst> + struct TIsEnumToNumericCppType { + enum { + Result = (src == FieldDescriptor::CPPTYPE_ENUM && TIsNumericCppType<dst>::Result) + }; + }; + + template <ECppType src, ECppType dst, bool compatible> // compatible == true + struct TCompatCastBase { + static const bool IsCompatible = true; + + typedef typename TCppTypeTraits<src>::T TSrc; + typedef typename TCppTypeTraits<dst>::T TDst; + + static inline TDst Cast(TSrc value) { + return value; + } + }; + + template <ECppType src, ECppType dst> // compatible == false + struct TCompatCastBase<src, dst, false> { + static const bool IsCompatible = false; + + typedef typename TCppTypeTraits<src>::T TSrc; + typedef typename TCppTypeTraits<dst>::T TDst; + + static inline TDst Cast(TSrc) { + ythrow TBadCastException() << "Incompatible FieldDescriptor::CppType conversion: #" + << (size_t)src << " to #" << (size_t)dst; + } + }; + + template <ECppType src, ECppType dst, bool isEnumToNum> // enum -> numeric + struct TCompatCastImpl { + static const bool IsCompatible = true; + + typedef typename TCppTypeTraits<dst>::T TDst; + + static inline TDst Cast(const EnumValueDescriptor* value) { + Y_ASSERT(value != nullptr); + return value->number(); + } + }; + + template <ECppType src, ECppType dst> + struct TCompatCastImpl<src, dst, false>: public TCompatCastBase<src, dst, TIsCompatibleCppType<src, dst>::Result> { + using TCompatCastBase<src, dst, TIsCompatibleCppType<src, dst>::Result>::IsCompatible; + }; + + template <ECppType src, ECppType dst> + struct TCompatCast: public TCompatCastImpl<src, dst, TIsEnumToNumericCppType<src, dst>::Result> { + typedef TCompatCastImpl<src, dst, TIsEnumToNumericCppType<src, dst>::Result> TBase; + + typedef typename TCppTypeTraits<src>::T TSrc; + typedef typename TCppTypeTraits<dst>::T TDst; + + using TBase::Cast; + using TBase::IsCompatible; + + inline bool Try(TSrc value, TDst& res) { + if (IsCompatible) { + res = Cast(value); + return true; + } + return false; + } + }; + + } + + template <ECppType src, ECppType dst> + inline typename TCppTypeTraits<dst>::T CompatCast(typename TCppTypeTraits<src>::T value) { + return NCast::TCompatCast<src, dst>::Cast(value); + } + + template <ECppType src, ECppType dst> + inline bool TryCompatCast(typename TCppTypeTraits<src>::T value, typename TCppTypeTraits<dst>::T& res) { + return NCast::TCompatCast<src, dst>::Try(value, res); + } + + // Message static/dynamic checked casts + + template <typename TpMessage> + inline const TpMessage* TryCast(const Message* msg) { + if (!msg || TpMessage::descriptor() != msg->GetDescriptor()) + return NULL; + return CheckedCast<const TpMessage*>(msg); + } + + template <typename TpMessage> + inline const TpMessage* TryCast(const Message* msg, const TpMessage*& ret) { + ret = TryCast<TpMessage>(msg); + return ret; + } + + template <typename TpMessage> + inline TpMessage* TryCast(Message* msg) { + if (!msg || TpMessage::descriptor() != msg->GetDescriptor()) + return nullptr; + return CheckedCast<TpMessage*>(msg); + } + + template <typename TpMessage> + inline TpMessage* TryCast(Message* msg, TpMessage*& ret) { + ret = TryCast<TpMessage>(msg); + return ret; + } + + // specialize for Message itself + + template <> + inline const Message* TryCast<Message>(const Message* msg) { + return msg; + } + + template <> + inline Message* TryCast<Message>(Message* msg) { + return msg; + } + + // Binary serialization compatible conversion + inline bool TryBinaryCast(const Message* from, Message* to, TString* buffer = nullptr) { + TString tmpbuf; + if (!buffer) + buffer = &tmpbuf; + + if (!from->SerializeToString(buffer)) + return false; + + return to->ParseFromString(*buffer); + } + +} diff --git a/library/cpp/protobuf/util/is_equal.cpp b/library/cpp/protobuf/util/is_equal.cpp new file mode 100644 index 0000000000..227408006e --- /dev/null +++ b/library/cpp/protobuf/util/is_equal.cpp @@ -0,0 +1,163 @@ +#include "is_equal.h" +#include "traits.h" + +#include <google/protobuf/descriptor.h> + +#include <util/generic/yexception.h> +#include <util/string/cast.h> +#include <util/string/vector.h> + +namespace NProtoBuf { + template <bool useDefault> + static bool IsEqualImpl(const Message& m1, const Message& m2, TVector<TString>* differentPath); + + namespace { + template <FieldDescriptor::CppType CppType, bool useDefault> + struct TCompareValue { + typedef typename TCppTypeTraits<CppType>::T T; + static inline bool IsEqual(T value1, T value2, TVector<TString>*) { + return value1 == value2; + } + }; + + template <bool useDefault> + struct TCompareValue<FieldDescriptor::CPPTYPE_MESSAGE, useDefault> { + static inline bool IsEqual(const Message* value1, const Message* value2, TVector<TString>* differentPath) { + return NProtoBuf::IsEqualImpl<useDefault>(*value1, *value2, differentPath); + } + }; + + template <FieldDescriptor::CppType CppType, bool useDefault> + class TCompareField { + typedef TCppTypeTraits<CppType> TTraits; + typedef TCompareValue<CppType, useDefault> TCompare; + + public: + static inline bool IsEqual(const Message& m1, const Message& m2, const FieldDescriptor& field, TVector<TString>* differentPath) { + if (field.is_repeated()) + return IsEqualRepeated(m1, m2, &field, differentPath); + else + return IsEqualSingle(m1, m2, &field, differentPath); + } + + private: + static bool IsEqualSingle(const Message& m1, const Message& m2, const FieldDescriptor* field, TVector<TString>* differentPath) { + bool has1 = m1.GetReflection()->HasField(m1, field); + bool has2 = m2.GetReflection()->HasField(m2, field); + + if (has1 != has2) { + if (!useDefault || field->is_required()) { + return false; + } + } else if (!has1) + return true; + + return TCompare::IsEqual(TTraits::Get(m1, field), + TTraits::Get(m2, field), + differentPath); + } + + static bool IsEqualRepeated(const Message& m1, const Message& m2, const FieldDescriptor* field, TVector<TString>* differentPath) { + int fieldSize = m1.GetReflection()->FieldSize(m1, field); + if (fieldSize != m2.GetReflection()->FieldSize(m2, field)) + return false; + for (int i = 0; i < fieldSize; ++i) + if (!IsEqualRepeatedValue(m1, m2, field, i, differentPath)) { + if (!!differentPath) { + differentPath->push_back(ToString(i)); + } + return false; + } + return true; + } + + static inline bool IsEqualRepeatedValue(const Message& m1, const Message& m2, const FieldDescriptor* field, int index, TVector<TString>* differentPath) { + return TCompare::IsEqual(TTraits::GetRepeated(m1, field, index), + TTraits::GetRepeated(m2, field, index), + differentPath); + } + }; + + template <bool useDefault> + bool IsEqualField(const Message& m1, const Message& m2, const FieldDescriptor& field, TVector<TString>* differentPath) { +#define CASE_CPPTYPE(cpptype) \ + case FieldDescriptor::CPPTYPE_##cpptype: { \ + bool r = TCompareField<FieldDescriptor::CPPTYPE_##cpptype, useDefault>::IsEqual(m1, m2, field, differentPath); \ + if (!r && !!differentPath) { \ + differentPath->push_back(field.name()); \ + } \ + return r; \ + } + + switch (field.cpp_type()) { + CASE_CPPTYPE(INT32) + CASE_CPPTYPE(INT64) + CASE_CPPTYPE(UINT32) + CASE_CPPTYPE(UINT64) + CASE_CPPTYPE(DOUBLE) + CASE_CPPTYPE(FLOAT) + CASE_CPPTYPE(BOOL) + CASE_CPPTYPE(ENUM) + CASE_CPPTYPE(STRING) + CASE_CPPTYPE(MESSAGE) + default: + ythrow yexception() << "Unsupported cpp-type field comparison"; + } + +#undef CASE_CPPTYPE + } + } + + template <bool useDefault> + bool IsEqualImpl(const Message& m1, const Message& m2, TVector<TString>* differentPath) { + const Descriptor* descr = m1.GetDescriptor(); + if (descr != m2.GetDescriptor()) { + return false; + } + for (int i = 0; i < descr->field_count(); ++i) + if (!IsEqualField<useDefault>(m1, m2, *descr->field(i), differentPath)) { + return false; + } + return true; + } + + bool IsEqual(const Message& m1, const Message& m2) { + return IsEqualImpl<false>(m1, m2, nullptr); + } + + bool IsEqual(const Message& m1, const Message& m2, TString* differentPath) { + TVector<TString> differentPathVector; + TVector<TString>* differentPathVectorPtr = !!differentPath ? &differentPathVector : nullptr; + bool r = IsEqualImpl<false>(m1, m2, differentPathVectorPtr); + if (!r && differentPath) { + *differentPath = JoinStrings(differentPathVector.rbegin(), differentPathVector.rend(), "/"); + } + return r; + } + + bool IsEqualDefault(const Message& m1, const Message& m2) { + return IsEqualImpl<true>(m1, m2, nullptr); + } + + template <bool useDefault> + static bool IsEqualFieldImpl( + const Message& m1, + const Message& m2, + const FieldDescriptor& field, + TVector<TString>* differentPath) { + const Descriptor* descr = m1.GetDescriptor(); + if (descr != m2.GetDescriptor()) { + return false; + } + return IsEqualField<useDefault>(m1, m2, field, differentPath); + } + + bool IsEqualField(const Message& m1, const Message& m2, const FieldDescriptor& field) { + return IsEqualFieldImpl<false>(m1, m2, field, nullptr); + } + + bool IsEqualFieldDefault(const Message& m1, const Message& m2, const FieldDescriptor& field) { + return IsEqualFieldImpl<true>(m1, m2, field, nullptr); + } + +} diff --git a/library/cpp/protobuf/util/is_equal.h b/library/cpp/protobuf/util/is_equal.h new file mode 100644 index 0000000000..13c0aae63d --- /dev/null +++ b/library/cpp/protobuf/util/is_equal.h @@ -0,0 +1,33 @@ +#pragma once + +#include <util/generic/fwd.h> + +namespace google { + namespace protobuf { + class Message; + class FieldDescriptor; + } +} + +namespace NProtoBuf { + using ::google::protobuf::FieldDescriptor; + using ::google::protobuf::Message; +} + +namespace NProtoBuf { + // Reflection-based equality check for arbitrary protobuf messages + + // Strict comparison: optional field without value is NOT equal to + // a field with explicitly set default value. + bool IsEqual(const Message& m1, const Message& m2); + bool IsEqual(const Message& m1, const Message& m2, TString* differentPath); + + bool IsEqualField(const Message& m1, const Message& m2, const FieldDescriptor& field); + + // Non-strict version: optional field without explicit value is compared + // using its default value. + bool IsEqualDefault(const Message& m1, const Message& m2); + + bool IsEqualFieldDefault(const Message& m1, const Message& m2, const FieldDescriptor& field); + +} diff --git a/library/cpp/protobuf/util/is_equal_ut.cpp b/library/cpp/protobuf/util/is_equal_ut.cpp new file mode 100644 index 0000000000..3ca4c90dd5 --- /dev/null +++ b/library/cpp/protobuf/util/is_equal_ut.cpp @@ -0,0 +1,88 @@ +#include "is_equal.h" +#include <library/cpp/protobuf/util/ut/sample_for_is_equal.pb.h> + +#include <library/cpp/testing/unittest/registar.h> + +#include <google/protobuf/descriptor.h> + +Y_UNIT_TEST_SUITE(ProtobufIsEqual) { + const ::google::protobuf::Descriptor* Descr = TSampleForIsEqual::descriptor(); + const ::google::protobuf::FieldDescriptor* NameDescr = Descr->field(0); + const ::google::protobuf::FieldDescriptor* InnerDescr = Descr->field(1); + + Y_UNIT_TEST(CheckDescriptors) { + UNIT_ASSERT(Descr); + UNIT_ASSERT(NameDescr); + UNIT_ASSERT_VALUES_EQUAL(NameDescr->name(), "Name"); + UNIT_ASSERT_VALUES_EQUAL(InnerDescr->name(), "Inner"); + } + + Y_UNIT_TEST(IsEqual1) { + TSampleForIsEqual a; + TSampleForIsEqual b; + + a.SetName("aaa"); + b.SetName("bbb"); + + TString path; + + bool equal = NProtoBuf::IsEqual(a, b, &path); + UNIT_ASSERT(!equal); + UNIT_ASSERT_VALUES_EQUAL("Name", path); + + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); + } + + Y_UNIT_TEST(IsEqual2) { + TSampleForIsEqual a; + TSampleForIsEqual b; + + a.MutableInner()->SetBrbrbr("aaa"); + b.MutableInner()->SetBrbrbr("bbb"); + + TString path; + + bool equal = NProtoBuf::IsEqual(a, b, &path); + UNIT_ASSERT(!equal); + UNIT_ASSERT_VALUES_EQUAL("Inner/Brbrbr", path); + + bool equalField = NProtoBuf::IsEqualField(a, b, *InnerDescr); + UNIT_ASSERT(!equalField); + } + + Y_UNIT_TEST(IsEqual3) { + TSampleForIsEqual a; + TSampleForIsEqual b; + + a.SetName("aaa"); + a.MutableInner()->SetBrbrbr("bbb"); + + b.SetName("aaa"); + b.MutableInner()->SetBrbrbr("bbb"); + + TString path; + + UNIT_ASSERT(NProtoBuf::IsEqual(a, b)); + UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *NameDescr)); + UNIT_ASSERT(NProtoBuf::IsEqualField(a, b, *InnerDescr)); + + b.MutableInner()->SetBrbrbr("ccc"); + UNIT_ASSERT(!NProtoBuf::IsEqual(a, b)); + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *InnerDescr)); + + b.SetName("ccc"); + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); + } + + Y_UNIT_TEST(IsEqualDefault) { + TSampleForIsEqual a; + TSampleForIsEqual b; + + a.SetName(""); + UNIT_ASSERT(NProtoBuf::IsEqualDefault(a, b)); + UNIT_ASSERT(!NProtoBuf::IsEqual(a, b)); + + UNIT_ASSERT(!NProtoBuf::IsEqualField(a, b, *NameDescr)); + UNIT_ASSERT(NProtoBuf::IsEqualFieldDefault(a, b, *NameDescr)); + } +} diff --git a/library/cpp/protobuf/util/iterators.h b/library/cpp/protobuf/util/iterators.h new file mode 100644 index 0000000000..6d53ac71b1 --- /dev/null +++ b/library/cpp/protobuf/util/iterators.h @@ -0,0 +1,53 @@ +#pragma once + +#include <google/protobuf/descriptor.h> + +namespace NProtoBuf { + class TFieldsIterator { + public: + explicit TFieldsIterator(const NProtoBuf::Descriptor* descriptor, int position = 0) + : Descriptor(descriptor) + , Position(position) + { } + + TFieldsIterator& operator++() { + ++Position; + return *this; + } + + TFieldsIterator& operator++(int) { + auto& ret = *this; + ++*this; + return ret; + } + + const NProtoBuf::FieldDescriptor* operator*() const { + return Descriptor->field(Position); + } + + bool operator== (const TFieldsIterator& other) const { + return Position == other.Position && Descriptor == other.Descriptor; + } + + bool operator!= (const TFieldsIterator& other) const { + return !(*this == other); + } + + private: + const NProtoBuf::Descriptor* Descriptor = nullptr; + int Position = 0; + }; +} + +// Namespaces required by `range-based for` ADL: +namespace google { + namespace protobuf { + NProtoBuf::TFieldsIterator begin(const NProtoBuf::Descriptor& descriptor) { + return NProtoBuf::TFieldsIterator(&descriptor); + } + + NProtoBuf::TFieldsIterator end(const NProtoBuf::Descriptor& descriptor) { + return NProtoBuf::TFieldsIterator(&descriptor, descriptor.field_count()); + } + } +} diff --git a/library/cpp/protobuf/util/iterators_ut.cpp b/library/cpp/protobuf/util/iterators_ut.cpp new file mode 100644 index 0000000000..9ebcff2963 --- /dev/null +++ b/library/cpp/protobuf/util/iterators_ut.cpp @@ -0,0 +1,52 @@ +#include "iterators.h" +#include "simple_reflection.h" +#include <library/cpp/protobuf/util/ut/common_ut.pb.h> + +#include <library/cpp/testing/unittest/registar.h> + +#include <util/generic/algorithm.h> + +using NProtoBuf::TFieldsIterator; +using NProtoBuf::TConstField; + +Y_UNIT_TEST_SUITE(Iterators) { + Y_UNIT_TEST(Count) { + const NProtobufUtilUt::TWalkTest proto; + const NProtoBuf::Descriptor* d = proto.GetDescriptor(); + TFieldsIterator dbegin(d), dend(d, d->field_count()); + size_t steps = 0; + + UNIT_ASSERT_EQUAL(dbegin, begin(*d)); + UNIT_ASSERT_EQUAL(dend, end(*d)); + + for (; dbegin != dend; ++dbegin) + ++steps; + UNIT_ASSERT_VALUES_EQUAL(steps, d->field_count()); + } + + Y_UNIT_TEST(RangeFor) { + size_t steps = 0, values = 0; + NProtobufUtilUt::TWalkTest proto; + proto.SetOptStr("yandex"); + for (const auto& field : *proto.GetDescriptor()) { + values += TConstField(proto, field).HasValue(); + ++steps; + } + UNIT_ASSERT_VALUES_EQUAL(steps, proto.GetDescriptor()->field_count()); + UNIT_ASSERT_VALUES_EQUAL(values, 1); + } + + Y_UNIT_TEST(AnyOf) { + NProtobufUtilUt::TWalkTest proto; + const NProtoBuf::Descriptor* d = proto.GetDescriptor(); + TFieldsIterator begin(d), end(d, d->field_count()); + UNIT_ASSERT(!AnyOf(begin, end, [&proto](const NProtoBuf::FieldDescriptor* f){ + return TConstField(proto, f).HasValue(); + })); + + proto.SetOptStr("yandex"); + UNIT_ASSERT(AnyOf(begin, end, [&proto](const NProtoBuf::FieldDescriptor* f){ + return TConstField(proto, f).HasValue(); + })); + } +} diff --git a/library/cpp/protobuf/util/merge.cpp b/library/cpp/protobuf/util/merge.cpp new file mode 100644 index 0000000000..dc2b9cc806 --- /dev/null +++ b/library/cpp/protobuf/util/merge.cpp @@ -0,0 +1,46 @@ +#include "merge.h" +#include "simple_reflection.h" + +#include <google/protobuf/message.h> + +#include <library/cpp/protobuf/util/proto/merge.pb.h> + +namespace NProtoBuf { + void RewriteMerge(const Message& src, Message& dst) { + const Descriptor* d = src.GetDescriptor(); + Y_ASSERT(d == dst.GetDescriptor()); + + for (int i = 0; i < d->field_count(); ++i) { + if (TConstField(src, d->field(i)).Has()) + TMutableField(dst, d->field(i)).Clear(); + } + + dst.MergeFrom(src); + } + + static void ClearNonMergeable(const Message& src, Message& dst) { + const Descriptor* d = src.GetDescriptor(); + if (d->options().GetExtension(DontMerge)) { + dst.Clear(); + return; + } + + for (int i = 0; i < d->field_count(); ++i) { + const FieldDescriptor* fd = d->field(i); + TConstField srcField(src, fd); + if (srcField.Has()) { + TMutableField dstField(dst, fd); + if (fd->options().GetExtension(DontMergeField)) + dstField.Clear(); + else if (!fd->is_repeated() && dstField.IsMessage() && dstField.Has()) + ClearNonMergeable(*srcField.Get<const Message*>(), *dstField.MutableMessage()); + } + } + } + + void CustomMerge(const Message& src, Message& dst) { + ClearNonMergeable(src, dst); + dst.MergeFrom(src); + } + +} diff --git a/library/cpp/protobuf/util/merge.h b/library/cpp/protobuf/util/merge.h new file mode 100644 index 0000000000..924975f141 --- /dev/null +++ b/library/cpp/protobuf/util/merge.h @@ -0,0 +1,22 @@ +#pragma once + +namespace google { + namespace protobuf { + class Message; + } +} + +namespace NProtoBuf { + using Message = ::google::protobuf::Message; +} + +namespace NProtoBuf { + // Similiar to Message::MergeFrom, overwrites existing repeated fields + // and embedded messages completely instead of recursive merging. + void RewriteMerge(const Message& src, Message& dst); + + // Does standard MergeFrom() by default, except messages/fields marked with DontMerge or DontMergeField option. + // Such fields are merged using RewriteMerge() (i.e. destination is cleared before merging anything from source) + void CustomMerge(const Message& src, Message& dst); + +} diff --git a/library/cpp/protobuf/util/merge_ut.cpp b/library/cpp/protobuf/util/merge_ut.cpp new file mode 100644 index 0000000000..22217db183 --- /dev/null +++ b/library/cpp/protobuf/util/merge_ut.cpp @@ -0,0 +1,83 @@ +#include "merge.h" +#include <library/cpp/protobuf/util/ut/common_ut.pb.h> + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NProtoBuf; + +Y_UNIT_TEST_SUITE(ProtobufMerge) { + static void InitProto(NProtobufUtilUt::TMergeTest & p, bool isSrc) { + size_t start = isSrc ? 0 : 100; + + p.AddMergeInt(start + 1); + p.AddMergeInt(start + 2); + + p.AddNoMergeInt(start + 3); + p.AddNoMergeInt(start + 4); + + NProtobufUtilUt::TMergeTestMerge* m = p.MutableMergeSub(); + m->SetA(start + 5); + m->AddB(start + 6); + m->AddB(start + 7); + m->AddC(start + 14); + + if (!isSrc) { + // only for dst + NProtobufUtilUt::TMergeTestMerge* mm1 = p.AddNoMergeRepSub(); + mm1->SetA(start + 8); + mm1->AddB(start + 9); + mm1->AddB(start + 10); + } + + NProtobufUtilUt::TMergeTestNoMerge* mm3 = p.MutableNoMergeOptSub(); + mm3->SetA(start + 11); + mm3->AddB(start + 12); + mm3->AddB(start + 13); + } + + Y_UNIT_TEST(CustomMerge) { + NProtobufUtilUt::TMergeTest src, dst; + InitProto(src, true); + InitProto(dst, false); + + // Cerr << "\nsrc: " << src.ShortDebugString() << Endl; + // Cerr << "dst: " << dst.ShortDebugString() << Endl; + NProtoBuf::CustomMerge(src, dst); + // Cerr << "dst2:" << dst.ShortDebugString() << Endl; + + // repeated uint32 MergeInt = 1; + UNIT_ASSERT_EQUAL(dst.MergeIntSize(), 4); + UNIT_ASSERT_EQUAL(dst.GetMergeInt(0), 101); + UNIT_ASSERT_EQUAL(dst.GetMergeInt(1), 102); + UNIT_ASSERT_EQUAL(dst.GetMergeInt(2), 1); + UNIT_ASSERT_EQUAL(dst.GetMergeInt(3), 2); + + // repeated uint32 NoMergeInt = 2 [(DontMergeField)=true]; + UNIT_ASSERT_EQUAL(dst.NoMergeIntSize(), 2); + UNIT_ASSERT_EQUAL(dst.GetNoMergeInt(0), 3); + UNIT_ASSERT_EQUAL(dst.GetNoMergeInt(1), 4); + + // optional TMergeTestMerge MergeSub = 3; + UNIT_ASSERT_EQUAL(dst.GetMergeSub().GetA(), 5); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().BSize(), 4); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().GetB(0), 106); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().GetB(1), 107); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().GetB(2), 6); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().GetB(3), 7); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().CSize(), 1); + UNIT_ASSERT_EQUAL(dst.GetMergeSub().GetC(0), 14); + + // repeated TMergeTestMerge NoMergeRepSub = 4 [(DontMergeField)=true]; + UNIT_ASSERT_EQUAL(dst.NoMergeRepSubSize(), 1); + UNIT_ASSERT_EQUAL(dst.GetNoMergeRepSub(0).GetA(), 108); + UNIT_ASSERT_EQUAL(dst.GetNoMergeRepSub(0).BSize(), 2); + UNIT_ASSERT_EQUAL(dst.GetNoMergeRepSub(0).GetB(0), 109); + UNIT_ASSERT_EQUAL(dst.GetNoMergeRepSub(0).GetB(1), 110); + + // optional TMergeTestNoMerge NoMergeOptSub = 5; + UNIT_ASSERT_EQUAL(dst.GetNoMergeOptSub().GetA(), 11); + UNIT_ASSERT_EQUAL(dst.GetNoMergeOptSub().BSize(), 2); + UNIT_ASSERT_EQUAL(dst.GetNoMergeOptSub().GetB(0), 12); + UNIT_ASSERT_EQUAL(dst.GetNoMergeOptSub().GetB(1), 13); + } +} 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(); + } + +} diff --git a/library/cpp/protobuf/util/path.h b/library/cpp/protobuf/util/path.h new file mode 100644 index 0000000000..487f643a2d --- /dev/null +++ b/library/cpp/protobuf/util/path.h @@ -0,0 +1,52 @@ +#pragma once + +#include <google/protobuf/descriptor.h> +#include <google/protobuf/message.h> + +#include <util/generic/vector.h> + +namespace NProtoBuf { + class TFieldPath { + public: + TFieldPath(); + TFieldPath(const Descriptor* msgType, const TStringBuf& path); // throws exception if path doesn't exist + 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/pb_io.cpp b/library/cpp/protobuf/util/pb_io.cpp new file mode 100644 index 0000000000..6270ee0624 --- /dev/null +++ b/library/cpp/protobuf/util/pb_io.cpp @@ -0,0 +1,221 @@ +#include "pb_io.h" + +#include <library/cpp/binsaver/bin_saver.h> +#include <library/cpp/string_utils/base64/base64.h> + +#include <google/protobuf/message.h> +#include <google/protobuf/messagext.h> +#include <google/protobuf/text_format.h> + +#include <util/generic/string.h> +#include <util/stream/file.h> +#include <util/stream/str.h> +#include <util/string/cast.h> + +namespace NProtoBuf { + + class TEnumIdValuePrinter : public google::protobuf::TextFormat::FastFieldValuePrinter { + public: + void PrintEnum(int32 val, const TString& /*name*/, google::protobuf::TextFormat::BaseTextGenerator* generator) const override { + generator->PrintString(ToString(val)); + } + }; + + void ParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven) { + if (!m.ParseFromString(allowUneven ? Base64DecodeUneven(dataBase64) : Base64StrictDecode(dataBase64))) { + ythrow yexception() << "can't parse " << m.GetTypeName() << " from base64-encoded string"; + } + } + + bool TryParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven) { + try { + ParseFromBase64String(dataBase64, m, allowUneven); + return true; + } catch (const std::exception&) { + return false; + } + } + + void SerializeToBase64String(const Message& m, TString& dataBase64) { + TString rawData; + if (!m.SerializeToString(&rawData)) { + ythrow yexception() << "can't serialize " << m.GetTypeName(); + } + + Base64EncodeUrl(rawData, dataBase64); + } + + TString SerializeToBase64String(const Message& m) { + TString s; + SerializeToBase64String(m, s); + return s; + } + + bool TrySerializeToBase64String(const Message& m, TString& dataBase64) { + try { + SerializeToBase64String(m, dataBase64); + return true; + } catch (const std::exception&) { + return false; + } + } + + const TString ShortUtf8DebugString(const Message& message) { + TextFormat::Printer printer; + printer.SetSingleLineMode(true); + printer.SetUseUtf8StringEscaping(true); + TString result; + printer.PrintToString(message, &result); + return result; + } + + bool MergePartialFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage) { + google::protobuf::io::CodedInputStream input(reinterpret_cast<const ui8*>(serializedProtoMessage.data()), serializedProtoMessage.size()); + bool ok = m.MergePartialFromCodedStream(&input); + ok = ok && input.ConsumedEntireMessage(); + return ok; + } + + bool MergeFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage) { + return MergePartialFromString(m, serializedProtoMessage) && m.IsInitialized(); + } +} + +int operator&(NProtoBuf::Message& m, IBinSaver& f) { + TStringStream ss; + if (f.IsReading()) { + f.Add(0, &ss.Str()); + m.ParseFromArcadiaStream(&ss); + } else { + m.SerializeToArcadiaStream(&ss); + f.Add(0, &ss.Str()); + } + return 0; +} + +void SerializeToTextFormat(const NProtoBuf::Message& m, IOutputStream& out) { + NProtoBuf::io::TCopyingOutputStreamAdaptor adaptor(&out); + + if (!NProtoBuf::TextFormat::Print(m, &adaptor)) { + ythrow yexception() << "SerializeToTextFormat failed on Print"; + } +} + +void SerializeToTextFormat(const NProtoBuf::Message& m, const TString& fileName) { + /* TUnbufferedFileOutput is unbuffered, but TCopyingOutputStreamAdaptor adds + * a buffer on top of it. */ + TUnbufferedFileOutput stream(fileName); + SerializeToTextFormat(m, stream); +} + +void SerializeToTextFormatWithEnumId(const NProtoBuf::Message& m, IOutputStream& out) { + google::protobuf::TextFormat::Printer printer; + printer.SetDefaultFieldValuePrinter(new NProtoBuf::TEnumIdValuePrinter()); + NProtoBuf::io::TCopyingOutputStreamAdaptor adaptor(&out); + + if (!printer.Print(m, &adaptor)) { + ythrow yexception() << "SerializeToTextFormatWithEnumId failed on Print"; + } +} + +void SerializeToTextFormatPretty(const NProtoBuf::Message& m, IOutputStream& out) { + google::protobuf::TextFormat::Printer printer; + printer.SetUseUtf8StringEscaping(true); + printer.SetUseShortRepeatedPrimitives(true); + + NProtoBuf::io::TCopyingOutputStreamAdaptor adaptor(&out); + + if (!printer.Print(m, &adaptor)) { + ythrow yexception() << "SerializeToTextFormatPretty failed on Print"; + } +} + +static void ConfigureParser(const EParseFromTextFormatOptions options, + NProtoBuf::TextFormat::Parser& p) { + if (options & EParseFromTextFormatOption::AllowUnknownField) { + p.AllowUnknownField(true); + } +} + +void ParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + NProtoBuf::io::TCopyingInputStreamAdaptor adaptor(&in); + NProtoBuf::TextFormat::Parser p; + ConfigureParser(options, p); + + if (!p.Parse(&adaptor, &m)) { + // remove everything that may have been read + m.Clear(); + ythrow yexception() << "ParseFromTextFormat failed on Parse for " << m.GetTypeName(); + } +} + +void ParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + /* TUnbufferedFileInput is unbuffered, but TCopyingInputStreamAdaptor adds + * a buffer on top of it. */ + TUnbufferedFileInput stream(fileName); + ParseFromTextFormat(stream, m, options); +} + +bool TryParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + try { + ParseFromTextFormat(fileName, m, options); + } catch (std::exception&) { + return false; + } + + return true; +} + +bool TryParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + try { + ParseFromTextFormat(in, m, options); + } catch (std::exception&) { + return false; + } + + return true; +} + +void MergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + NProtoBuf::io::TCopyingInputStreamAdaptor adaptor(&in); + NProtoBuf::TextFormat::Parser p; + ConfigureParser(options, p); + if (!p.Merge(&adaptor, &m)) { + ythrow yexception() << "MergeFromTextFormat failed on Merge for " << m.GetTypeName(); + } +} + +void MergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + /* TUnbufferedFileInput is unbuffered, but TCopyingInputStreamAdaptor adds + * a buffer on top of it. */ + TUnbufferedFileInput stream(fileName); + MergeFromTextFormat(stream, m, options); +} + +bool TryMergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + try { + MergeFromTextFormat(fileName, m, options); + } catch (std::exception&) { + return false; + } + + return true; +} + +bool TryMergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options) { + try { + MergeFromTextFormat(in, m, options); + } catch (std::exception&) { + return false; + } + + return true; +} diff --git a/library/cpp/protobuf/util/pb_io.h b/library/cpp/protobuf/util/pb_io.h new file mode 100644 index 0000000000..493c84cb5f --- /dev/null +++ b/library/cpp/protobuf/util/pb_io.h @@ -0,0 +1,138 @@ +#pragma once + +#include <util/generic/fwd.h> +#include <util/generic/flags.h> + +struct IBinSaver; + +namespace google { + namespace protobuf { + class Message; + } +} + +namespace NProtoBuf { + using Message = ::google::protobuf::Message; +} + +class IInputStream; +class IOutputStream; + +namespace NProtoBuf { + /* Parse base64 URL encoded serialized message from string. + */ + void ParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven = false); + bool TryParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven = false); + template <typename T> + static T ParseFromBase64String(const TStringBuf& dataBase64, bool allowUneven = false) { + T m; + ParseFromBase64String(dataBase64, m, allowUneven); + return m; + } + + /* Serialize message into string and apply base64 URL encoding. + */ + TString SerializeToBase64String(const Message& m); + void SerializeToBase64String(const Message& m, TString& dataBase64); + bool TrySerializeToBase64String(const Message& m, TString& dataBase64); + + const TString ShortUtf8DebugString(const Message& message); + + bool MergePartialFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage); + bool MergeFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage); +} + +int operator&(NProtoBuf::Message& m, IBinSaver& f); + +// Write a textual representation of the given message to the given file. +void SerializeToTextFormat(const NProtoBuf::Message& m, const TString& fileName); +void SerializeToTextFormat(const NProtoBuf::Message& m, IOutputStream& out); + +// Write a textual representation of the given message to the given output stream +// with flags UseShortRepeatedPrimitives and UseUtf8StringEscaping set to true. +void SerializeToTextFormatPretty(const NProtoBuf::Message& m, IOutputStream& out); + +// Write a textual representation of the given message to the given output stream +// use enum id instead of enum name for all enum fields. +void SerializeToTextFormatWithEnumId(const NProtoBuf::Message& m, IOutputStream& out); + +enum class EParseFromTextFormatOption : ui64 { + // Unknown fields will be ignored by the parser + AllowUnknownField = 1 +}; + +Y_DECLARE_FLAGS(EParseFromTextFormatOptions, EParseFromTextFormatOption); + +// Parse a text-format protocol message from the given file into message object. +void ParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); +// NOTE: will read `in` till the end. +void ParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); + +/* @return `true` if parsing was successfull and `false` otherwise. + * + * @see `ParseFromTextFormat` + */ +bool TryParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); +// NOTE: will read `in` till the end. +bool TryParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); + +// @see `ParseFromTextFormat` +template <typename T> +static T ParseFromTextFormat(const TString& fileName, + const EParseFromTextFormatOptions options = {}) { + T message; + ParseFromTextFormat(fileName, message, options); + return message; +} + +// @see `ParseFromTextFormat` +// NOTE: will read `in` till the end. +template <typename T> +static T ParseFromTextFormat(IInputStream& in, + const EParseFromTextFormatOptions options = {}) { + T message; + ParseFromTextFormat(in, message, options); + return message; +} + +// Merge a text-format protocol message from the given file into message object. +// +// NOTE: Even when parsing failed and exception was thrown `m` may be different from its original +// value. User must implement transactional logic around `MergeFromTextFormat` by himself. +void MergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); +// NOTE: will read `in` till the end. +void MergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); +/* @return `true` if parsing was successfull and `false` otherwise. + * + * @see `MergeFromTextFormat` + */ +bool TryMergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); +// NOTE: will read `in` till the end. +bool TryMergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m, + const EParseFromTextFormatOptions options = {}); + +// @see `MergeFromTextFormat` +template <typename T> +static T MergeFromTextFormat(const TString& fileName, + const EParseFromTextFormatOptions options = {}) { + T message; + MergeFromTextFormat(fileName, message, options); + return message; +} + +// @see `MergeFromTextFormat` +// NOTE: will read `in` till the end. +template <typename T> +static T MergeFromTextFormat(IInputStream& in, + const EParseFromTextFormatOptions options = {}) { + T message; + MergeFromTextFormat(in, message, options); + return message; +} diff --git a/library/cpp/protobuf/util/pb_io_ut.cpp b/library/cpp/protobuf/util/pb_io_ut.cpp new file mode 100644 index 0000000000..875d6dc602 --- /dev/null +++ b/library/cpp/protobuf/util/pb_io_ut.cpp @@ -0,0 +1,418 @@ +#include "pb_io.h" + +#include "is_equal.h" + +#include <library/cpp/protobuf/util/ut/common_ut.pb.h> + +#include <library/cpp/testing/unittest/registar.h> + +#include <util/folder/path.h> +#include <util/folder/tempdir.h> +#include <util/stream/file.h> +#include <util/stream/str.h> + +static NProtobufUtilUt::TTextTest GetCorrectMessage() { + NProtobufUtilUt::TTextTest m; + m.SetFoo(42); + return m; +} + +static NProtobufUtilUt::TTextEnumTest GetCorrectEnumMessage() { + NProtobufUtilUt::TTextEnumTest m; + m.SetSlot(NProtobufUtilUt::TTextEnumTest::EET_SLOT_1); + return m; +} + +static const TString CORRECT_MESSAGE = + R"(Foo: 42 +)"; +static const TString CORRECT_ENUM_NAME_MESSAGE = + R"(Slot: EET_SLOT_1 +)"; +static const TString CORRECT_ENUM_ID_MESSAGE = + R"(Slot: 1 +)"; + +static const TString INCORRECT_MESSAGE = + R"(Bar: 1 +)"; +static const TString INCORRECT_ENUM_NAME_MESSAGE = + R"(Slot: EET_SLOT_3 +)"; +static const TString INCORRECT_ENUM_ID_MESSAGE = + R"(Slot: 3 +)"; + +static const TString CORRECT_BASE64_MESSAGE = "CCo,"; + +static const TString CORRECT_UNEVEN_BASE64_MESSAGE = "CCo"; + +static const TString INCORRECT_BASE64_MESSAGE = "CC"; + +Y_UNIT_TEST_SUITE(TTestProtoBufIO) { + Y_UNIT_TEST(TestBase64) { + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(NProtoBuf::TryParseFromBase64String(CORRECT_BASE64_MESSAGE, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(!NProtoBuf::TryParseFromBase64String(INCORRECT_BASE64_MESSAGE, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(NProtoBuf::TryParseFromBase64String(CORRECT_UNEVEN_BASE64_MESSAGE , message, true)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(!NProtoBuf::TryParseFromBase64String(CORRECT_UNEVEN_BASE64_MESSAGE , message, false)); + } + { + UNIT_ASSERT_VALUES_EQUAL(CORRECT_BASE64_MESSAGE, NProtoBuf::SerializeToBase64String(GetCorrectMessage())); + } + { + const auto m = NProtoBuf::ParseFromBase64String<NProtobufUtilUt::TTextTest>(CORRECT_BASE64_MESSAGE); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + } + + Y_UNIT_TEST(TestParseFromTextFormat) { + TTempDir tempDir; + const TFsPath correctFileName = TFsPath{tempDir()} / "correct.pb.txt"; + const TFsPath incorrectFileName = TFsPath{tempDir()} / "incorrect.pb.txt"; + + TFileOutput{correctFileName}.Write(CORRECT_MESSAGE); + TFileOutput{incorrectFileName}.Write(INCORRECT_MESSAGE); + + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(TryParseFromTextFormat(correctFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(!TryParseFromTextFormat(incorrectFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{CORRECT_MESSAGE}; + UNIT_ASSERT(TryParseFromTextFormat(in, message)); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{INCORRECT_MESSAGE}; + UNIT_ASSERT(!TryParseFromTextFormat(in, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_NO_EXCEPTION(TryParseFromTextFormat(incorrectFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(!TryParseFromTextFormat("this_file_doesnt_exists", message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_NO_EXCEPTION(TryParseFromTextFormat("this_file_doesnt_exists", message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat("this_file_doesnt_exists", message), TFileError); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(correctFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(incorrectFileName, message), yexception); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{CORRECT_MESSAGE}; + UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(in, message)); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{INCORRECT_MESSAGE}; + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(in, message), yexception); + } + { + NProtobufUtilUt::TTextTest m; + const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) { + mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(correctFileName); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + { + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextTest>(incorrectFileName), yexception); + } + { + NProtobufUtilUt::TTextTest m; + TStringInput in{CORRECT_MESSAGE}; + const auto f = [&in](NProtobufUtilUt::TTextTest& mm) { + mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>(in); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + { + TStringInput in{INCORRECT_MESSAGE}; + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextTest>(in), yexception); + } + { + const TFsPath correctFileName2 = TFsPath{tempDir()} / "serialized.pb.txt"; + const auto original = GetCorrectMessage(); + UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, correctFileName2)); + const auto serializedStr = TUnbufferedFileInput{correctFileName2}.ReadAll(); + UNIT_ASSERT_VALUES_EQUAL(serializedStr, CORRECT_MESSAGE); + } + { + const auto original = GetCorrectMessage(); + TStringStream out; + UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out)); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_MESSAGE); + } + { + NProtobufUtilUt::TTextTest m; + const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) { + mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>( + correctFileName, + EParseFromTextFormatOption::AllowUnknownField); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + { + const NProtobufUtilUt::TTextTest empty; + NProtobufUtilUt::TTextTest m; + const auto f = [&incorrectFileName](NProtobufUtilUt::TTextTest& mm) { + mm = ParseFromTextFormat<NProtobufUtilUt::TTextTest>( + incorrectFileName, + EParseFromTextFormatOption::AllowUnknownField); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(empty, m)); + } + } + + Y_UNIT_TEST(TestSerializeToTextFormatWithEnumId) { + TTempDir tempDir; + const TFsPath correctNameFileName = TFsPath{tempDir()} / "correct_name.pb.txt"; + const TFsPath incorrectNameFileName = TFsPath{tempDir()} / "incorrect_name.pb.txt"; + const TFsPath correctIdFileName = TFsPath{tempDir()} / "correct_id.pb.txt"; + const TFsPath incorrectIdFileName = TFsPath{tempDir()} / "incorrect_id.pb.txt"; + + TFileOutput{correctNameFileName}.Write(CORRECT_ENUM_NAME_MESSAGE); + TFileOutput{incorrectNameFileName}.Write(INCORRECT_ENUM_NAME_MESSAGE); + TFileOutput{correctIdFileName}.Write(CORRECT_ENUM_ID_MESSAGE); + TFileOutput{incorrectIdFileName}.Write(INCORRECT_ENUM_ID_MESSAGE); + + { + NProtobufUtilUt::TTextEnumTest message; + for (auto correct_message: {CORRECT_ENUM_ID_MESSAGE, CORRECT_ENUM_NAME_MESSAGE}) { + TStringInput in{correct_message}; + UNIT_ASSERT_NO_EXCEPTION(ParseFromTextFormat(in, message)); + } + } + { + NProtobufUtilUt::TTextEnumTest message; + for (auto incorrect_message: {INCORRECT_ENUM_ID_MESSAGE, INCORRECT_ENUM_NAME_MESSAGE}) { + TStringInput in{incorrect_message}; + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat(in, message), yexception); + } + } + { + const auto f = [](NProtobufUtilUt::TTextEnumTest& mm, const TString fileName) { + mm = ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(fileName); + }; + for (auto fileName: {correctIdFileName, correctNameFileName}) { + NProtobufUtilUt::TTextEnumTest m; + UNIT_ASSERT_NO_EXCEPTION(f(m, fileName)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectEnumMessage(), m)); + } + } + { + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(incorrectIdFileName), yexception); + UNIT_ASSERT_EXCEPTION(ParseFromTextFormat<NProtobufUtilUt::TTextEnumTest>(incorrectNameFileName), yexception); + } + { + const auto original = GetCorrectEnumMessage(); + TStringStream out; + UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out)); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_ENUM_NAME_MESSAGE); + } + { + const auto original = GetCorrectEnumMessage(); + TStringStream out; + UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormatWithEnumId(original, out)); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_ENUM_ID_MESSAGE); + } + } + + Y_UNIT_TEST(TestMergeFromTextFormat) { + // + // Tests cases below are identical to `Parse` tests + // + TTempDir tempDir; + const TFsPath correctFileName = TFsPath{tempDir()} / "correct.pb.txt"; + const TFsPath incorrectFileName = TFsPath{tempDir()} / "incorrect.pb.txt"; + + TFileOutput{correctFileName}.Write(CORRECT_MESSAGE); + TFileOutput{incorrectFileName}.Write(INCORRECT_MESSAGE); + + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(TryMergeFromTextFormat(correctFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(!TryMergeFromTextFormat(incorrectFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{CORRECT_MESSAGE}; + UNIT_ASSERT(TryMergeFromTextFormat(in, message)); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{INCORRECT_MESSAGE}; + UNIT_ASSERT(!TryMergeFromTextFormat(in, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_NO_EXCEPTION(TryMergeFromTextFormat(incorrectFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT(!TryMergeFromTextFormat("this_file_doesnt_exists", message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_NO_EXCEPTION(TryMergeFromTextFormat("this_file_doesnt_exists", message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_EXCEPTION(MergeFromTextFormat("this_file_doesnt_exists", message), TFileError); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_NO_EXCEPTION(MergeFromTextFormat(correctFileName, message)); + } + { + NProtobufUtilUt::TTextTest message; + UNIT_ASSERT_EXCEPTION(MergeFromTextFormat(incorrectFileName, message), yexception); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{CORRECT_MESSAGE}; + UNIT_ASSERT_NO_EXCEPTION(MergeFromTextFormat(in, message)); + } + { + NProtobufUtilUt::TTextTest message; + TStringInput in{INCORRECT_MESSAGE}; + UNIT_ASSERT_EXCEPTION(MergeFromTextFormat(in, message), yexception); + } + { + NProtobufUtilUt::TTextTest m; + const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) { + mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(correctFileName); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + { + UNIT_ASSERT_EXCEPTION(MergeFromTextFormat<NProtobufUtilUt::TTextTest>(incorrectFileName), yexception); + } + { + NProtobufUtilUt::TTextTest m; + TStringInput in{CORRECT_MESSAGE}; + const auto f = [&in](NProtobufUtilUt::TTextTest& mm) { + mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>(in); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + { + TStringInput in{INCORRECT_MESSAGE}; + UNIT_ASSERT_EXCEPTION(MergeFromTextFormat<NProtobufUtilUt::TTextTest>(in), yexception); + } + { + const TFsPath correctFileName2 = TFsPath{tempDir()} / "serialized.pb.txt"; + const auto original = GetCorrectMessage(); + UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, correctFileName2)); + const auto serializedStr = TUnbufferedFileInput{correctFileName2}.ReadAll(); + UNIT_ASSERT_VALUES_EQUAL(serializedStr, CORRECT_MESSAGE); + } + { + const auto original = GetCorrectMessage(); + TStringStream out; + UNIT_ASSERT_NO_EXCEPTION(SerializeToTextFormat(original, out)); + UNIT_ASSERT_VALUES_EQUAL(out.Str(), CORRECT_MESSAGE); + } + { + NProtobufUtilUt::TTextTest m; + const auto f = [&correctFileName](NProtobufUtilUt::TTextTest& mm) { + mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>( + correctFileName, + EParseFromTextFormatOption::AllowUnknownField); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(GetCorrectMessage(), m)); + } + { + const NProtobufUtilUt::TTextTest empty; + NProtobufUtilUt::TTextTest m; + const auto f = [&incorrectFileName](NProtobufUtilUt::TTextTest& mm) { + mm = MergeFromTextFormat<NProtobufUtilUt::TTextTest>( + incorrectFileName, + EParseFromTextFormatOption::AllowUnknownField); + }; + UNIT_ASSERT_NO_EXCEPTION(f(m)); + UNIT_ASSERT(NProtoBuf::IsEqual(empty, m)); + } + + // + // Test cases for `Merge` + // + { + NProtobufUtilUt::TTextTest message; + message.SetFoo(100500); + TStringInput in{CORRECT_MESSAGE}; + UNIT_ASSERT(TryMergeFromTextFormat(in, message)); + UNIT_ASSERT(NProtoBuf::IsEqual(message, GetCorrectMessage())); + } + } + + Y_UNIT_TEST(TestMergeFromString) { + NProtobufUtilUt::TMergeTest message; + NProtobufUtilUt::TMergeTest messageFirstHalf; + NProtobufUtilUt::TMergeTest messageSecondHalf; + + for (ui32 v = ~0; v != 0; v >>= 1) { + message.AddMergeInt(v); + (v > 0xffff ? messageFirstHalf : messageSecondHalf).AddMergeInt(v); + } + + const TString full = message.SerializeAsString(); + + { + NProtobufUtilUt::TMergeTest m1; + UNIT_ASSERT(NProtoBuf::MergeFromString(m1, full)); + UNIT_ASSERT(NProtoBuf::IsEqual(message, m1)); + } + { + NProtobufUtilUt::TMergeTest m2; + TStringBuf s0 = TStringBuf(full).SubStr(0, 3); + TStringBuf s1 = TStringBuf(full).SubStr(3); + // объединение результатов двух MergePartialFromString не эквивалентно вызову MergePartialFromString от объединения строк + UNIT_ASSERT(!(NProtoBuf::MergePartialFromString(m2, s0) && NProtoBuf::MergePartialFromString(m2, s1))); + } + { + NProtobufUtilUt::TMergeTest m3; + UNIT_ASSERT(NProtoBuf::MergePartialFromString(m3, messageFirstHalf.SerializeAsString())); + UNIT_ASSERT(NProtoBuf::MergeFromString(m3, messageSecondHalf.SerializeAsString())); + UNIT_ASSERT(NProtoBuf::IsEqual(message, m3)); + } + } +} diff --git a/library/cpp/protobuf/util/pb_utils.h b/library/cpp/protobuf/util/pb_utils.h new file mode 100644 index 0000000000..9e9a110b48 --- /dev/null +++ b/library/cpp/protobuf/util/pb_utils.h @@ -0,0 +1,11 @@ +#pragma once + +#define UPDATE_PB_FIELD_MAX(PBMESS, FIELD, VAL) \ + if ((VAL) > (PBMESS).Get##FIELD()) { \ + (PBMESS).Set##FIELD(VAL); \ + } + +#define UPDATE_OPT_PB_FIELD_MAX(PBMESS, FIELD, VAL) \ + if (!(PBMESS).Has##FIELD() || ((VAL) > (PBMESS).Get##FIELD())) { \ + (PBMESS).Set##FIELD(VAL); \ + } diff --git a/library/cpp/protobuf/util/proto/merge.proto b/library/cpp/protobuf/util/proto/merge.proto new file mode 100644 index 0000000000..a937041c07 --- /dev/null +++ b/library/cpp/protobuf/util/proto/merge.proto @@ -0,0 +1,11 @@ +import "google/protobuf/descriptor.proto"; + +// These meta-options are used for selecting proper merging method, see merge.h + +extend google.protobuf.MessageOptions { + optional bool DontMerge = 54287; +} + +extend google.protobuf.FieldOptions { + optional bool DontMergeField = 54288; +} diff --git a/library/cpp/protobuf/util/proto/ya.make b/library/cpp/protobuf/util/proto/ya.make new file mode 100644 index 0000000000..4d68047d8b --- /dev/null +++ b/library/cpp/protobuf/util/proto/ya.make @@ -0,0 +1,11 @@ +PROTO_LIBRARY() + +OWNER(mowgli) + +SRCS( + merge.proto +) + +EXCLUDE_TAGS(GO_PROTO) + +END() diff --git a/library/cpp/protobuf/util/repeated_field_utils.h b/library/cpp/protobuf/util/repeated_field_utils.h new file mode 100644 index 0000000000..c07bd84647 --- /dev/null +++ b/library/cpp/protobuf/util/repeated_field_utils.h @@ -0,0 +1,96 @@ +#pragma once + +#include <google/protobuf/repeated_field.h> +#include <util/generic/vector.h> + +template <typename T> +void RemoveRepeatedPtrFieldElement(google::protobuf::RepeatedPtrField<T>* repeated, unsigned index) { + google::protobuf::RepeatedPtrField<T> r; + Y_ASSERT(index < (unsigned)repeated->size()); + for (unsigned i = 0; i < (unsigned)repeated->size(); ++i) { + if (i == index) { + continue; + } + r.Add()->Swap(repeated->Mutable(i)); + } + r.Swap(repeated); +} + +namespace NProtoBuf { + /// Move item to specified position + template <typename TRepeated> + static void MoveRepeatedFieldItem(TRepeated* field, size_t indexFrom, size_t indexTo) { + if (!field->size() || indexFrom >= static_cast<size_t>(field->size()) || indexFrom == indexTo) + return; + if (indexTo >= static_cast<size_t>(field->size())) + indexTo = field->size() - 1; + if (indexFrom > indexTo) { + for (size_t i = indexFrom; i > indexTo; --i) + field->SwapElements(i, i - 1); + } else { + for (size_t i = indexFrom; i < indexTo; ++i) + field->SwapElements(i, i + 1); + } + } + + template <typename T> + static T* InsertRepeatedFieldItem(NProtoBuf::RepeatedPtrField<T>* field, size_t index) { + 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()) + return; + + for (int i = index + 1; i < field->size(); ++i) + field->SwapElements(i - 1, i); + + field->RemoveLast(); + } + + template <typename TRepeated, typename TPred> // suitable both for RepeatedField and RepeatedPtrField + static void RemoveRepeatedFieldItemIf(TRepeated* repeated, TPred p) { + auto last = std::remove_if(repeated->begin(), repeated->end(), p); + if (last != repeated->end()) { + size_t countToRemove = repeated->end() - last; + while (countToRemove--) + repeated->RemoveLast(); + } + } + + namespace NImpl { + template <typename TRepeated> + static void ShiftLeft(TRepeated* field, int begIndex, int endIndex, size_t shiftSize) { + Y_ASSERT(begIndex <= field->size()); + Y_ASSERT(endIndex <= field->size()); + size_t shiftIndex = (int)shiftSize < begIndex ? begIndex - shiftSize : 0; + 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) { + if (sortedIndices.empty()) + return; + + size_t shift = 1; + for (size_t i = 1; i < sortedIndices.size(); ++i, ++shift) + NImpl::ShiftLeft(field, sortedIndices[i - 1] + 1, sortedIndices[i], shift); + NImpl::ShiftLeft(field, sortedIndices.back() + 1, field->size(), shift); + + for (; shift > 0; --shift) + field->RemoveLast(); + } + + template <typename TRepeated> + static void ReverseRepeatedFieldItems(TRepeated* field) { + for (int i1 = 0, i2 = field->size() - 1; i1 < i2; ++i1, --i2) + field->SwapElements(i1, i2); + } + +} diff --git a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp new file mode 100644 index 0000000000..58aaaa9e12 --- /dev/null +++ b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp @@ -0,0 +1,46 @@ +#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; + +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()); + } + } +} diff --git a/library/cpp/protobuf/util/simple_reflection.cpp b/library/cpp/protobuf/util/simple_reflection.cpp new file mode 100644 index 0000000000..d842e9ee44 --- /dev/null +++ b/library/cpp/protobuf/util/simple_reflection.cpp @@ -0,0 +1,70 @@ +#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()) { + if (createPath) + return curField.Field()->is_repeated() ? curField.AddMessage() : curField.MutableMessage(); + } 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>(); + TMsg* curParent = &msg; + for (size_t i = 0, size = fieldsPath.size(); i < size; ++i) { + const FieldDescriptor* field = fieldsPath[i]; + if (!curParent) + return TMaybe<TField>(); + TField curField(*curParent, field); + if (size - i == 1) // last element in path + return curField; + curParent = GetMessageHelper(curField, createPath); + } + if (curParent) + 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 new file mode 100644 index 0000000000..61e877a787 --- /dev/null +++ b/library/cpp/protobuf/util/simple_reflection.h @@ -0,0 +1,289 @@ +#pragma once + +#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> + +namespace NProtoBuf { + class TConstField { + public: + TConstField(const Message& msg, const FieldDescriptor* fd) + : Msg(msg) + , Fd(fd) + { + Y_ASSERT(Fd && Fd->containing_type() == Msg.GetDescriptor()); + } + + static TMaybe<TConstField> ByPath(const Message& msg, const TStringBuf& path); + static TMaybe<TConstField> ByPath(const Message& msg, const TVector<const FieldDescriptor*>& fieldsPath); + static TMaybe<TConstField> ByPath(const Message& msg, const TFieldPath& fieldsPath); + + const Message& Parent() const { + return Msg; + } + + const FieldDescriptor* Field() const { + return Fd; + } + + bool HasValue() const { + return IsRepeated() ? Refl().FieldSize(Msg, Fd) > 0 + : Refl().HasField(Msg, Fd); + } + + // deprecated, use HasValue() instead + bool Has() const { + return HasValue(); + } + + size_t Size() const { + return IsRepeated() ? Refl().FieldSize(Msg, Fd) + : (Refl().HasField(Msg, Fd) ? 1 : 0); + } + + template <typename T> + inline typename TSelectCppType<T>::T Get(size_t index = 0) const; + + template <typename TMsg> + inline const TMsg* GetAs(size_t index = 0) const { + // casting version of Get + return IsMessageInstance<TMsg>() ? CheckedCast<const TMsg*>(Get<const Message*>(index)) : nullptr; + } + + template <typename T> + bool IsInstance() const { + return CppType() == TSelectCppType<T>::Result; + } + + template <typename TMsg> + bool IsMessageInstance() const { + return IsMessage() && Fd->message_type() == TMsg::descriptor(); + } + + template <typename TMsg> + bool IsInstance(std::enable_if_t<std::is_base_of<Message, TMsg>::value && !std::is_same<Message, TMsg>::value, void>* = NULL) const { // template will be selected when specifying Message children types + return IsMessage() && Fd->message_type() == TMsg::descriptor(); + } + + bool IsString() const { + return CppType() == FieldDescriptor::CPPTYPE_STRING; + } + + bool IsMessage() const { + return CppType() == FieldDescriptor::CPPTYPE_MESSAGE; + } + + bool HasSameType(const TConstField& other) const { + if (CppType() != other.CppType()) + return false; + if (IsMessage() && Field()->message_type() != other.Field()->message_type()) + return false; + if (CppType() == FieldDescriptor::CPPTYPE_ENUM && Field()->enum_type() != other.Field()->enum_type()) + return false; + return true; + } + + protected: + bool IsRepeated() const { + return Fd->is_repeated(); + } + + FieldDescriptor::CppType CppType() const { + return Fd->cpp_type(); + } + + const Reflection& Refl() const { + return *Msg.GetReflection(); + } + + [[noreturn]] void RaiseUnknown() const { + ythrow yexception() << "Unknown field cpp-type: " << (size_t)CppType(); + } + + bool IsSameField(const TConstField& other) const { + return &Parent() == &other.Parent() && Field() == other.Field(); + } + + protected: + const Message& Msg; + const FieldDescriptor* Fd; + }; + + class TMutableField: public TConstField { + public: + TMutableField(Message& msg, const FieldDescriptor* fd) + : TConstField(msg, fd) + { + } + + static TMaybe<TMutableField> ByPath(Message& msg, const TStringBuf& path, bool createPath = false); + static TMaybe<TMutableField> ByPath(Message& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath = false); + static TMaybe<TMutableField> ByPath(Message& msg, const TFieldPath& fieldsPath, bool createPath = false); + + Message* MutableParent() { + return Mut(); + } + + template <typename T> + inline void Set(T value, size_t index = 0); + + template <typename T> + inline void Add(T value); + + inline void MergeFrom(const TConstField& src); + + inline void Clear() { + Refl().ClearField(Mut(), Fd); + } + /* + void Swap(TMutableField& f) { + Y_ASSERT(Field() == f.Field()); + + // not implemented yet, TODO: implement when Reflection::Mutable(Ptr)RepeatedField + // is ported into arcadia protobuf library from up-stream. + } +*/ + inline void RemoveLast() { + Y_ASSERT(HasValue()); + if (IsRepeated()) + Refl().RemoveLast(Mut(), Fd); + else + Clear(); + } + + inline void SwapElements(size_t index1, size_t index2) { + Y_ASSERT(IsRepeated()); + Y_ASSERT(index1 < Size()); + Y_ASSERT(index2 < Size()); + if (index1 == index2) + 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()) { + Y_ASSERT(index < Size()); + return Refl().MutableRepeatedMessage(Mut(), Fd, index); + } else { + Y_ASSERT(index == 0); + return Refl().MutableMessage(Mut(), Fd); + } + } + + template <typename TMsg> + 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); + } + + template <typename T> + inline void MergeValue(T srcValue); + }; + + // template implementations + + template <typename T> + inline typename TSelectCppType<T>::T TConstField::Get(size_t index) const { + Y_ASSERT(index < Size() || !Fd->is_repeated() && index == 0); // Get for single fields is always allowed because of default values +#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \ + case CPPTYPE: \ + return CompatCast<CPPTYPE, TSelectCppType<T>::Result>(TSimpleFieldTraits<CPPTYPE>::Get(Msg, Fd, index)); + switch (CppType()) { + APPLY_TMP_MACRO_FOR_ALL_CPPTYPES() + default: + RaiseUnknown(); + } +#undef TMP_MACRO_FOR_CPPTYPE + } + + template <typename T> + inline void TMutableField::Set(T value, size_t index) { + Y_ASSERT(!IsRepeated() && index == 0 || index < Size()); +#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \ + case CPPTYPE: \ + TSimpleFieldTraits<CPPTYPE>::Set(*Mut(), Fd, CompatCast<TSelectCppType<T>::Result, CPPTYPE>(value), index); \ + break; + switch (CppType()) { + APPLY_TMP_MACRO_FOR_ALL_CPPTYPES() + default: + RaiseUnknown(); + } +#undef TMP_MACRO_FOR_CPPTYPE + } + + template <typename T> + inline void TMutableField::Add(T value) { +#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \ + case CPPTYPE: \ + TSimpleFieldTraits<CPPTYPE>::Add(*Mut(), Fd, CompatCast<TSelectCppType<T>::Result, CPPTYPE>(value)); \ + break; + switch (CppType()) { + APPLY_TMP_MACRO_FOR_ALL_CPPTYPES() + default: + RaiseUnknown(); + } +#undef TMP_MACRO_FOR_CPPTYPE + } + + template <typename T> + inline void TMutableField::MergeValue(T srcValue) { + Add(srcValue); + } + + template <> + inline void TMutableField::MergeValue<const Message*>(const Message* srcValue) { + if (IsRepeated()) { + Add(srcValue); + } else { + MutableMessage()->MergeFrom(*srcValue); + } + } + + inline void TMutableField::MergeFrom(const TConstField& src) { + Y_ASSERT(HasSameType(src)); + if (IsSameField(src)) + return; +#define TMP_MACRO_FOR_CPPTYPE(CPPTYPE) \ + case CPPTYPE: { \ + for (size_t itemIdx = 0; itemIdx < src.Size(); ++itemIdx) { \ + MergeValue(TSimpleFieldTraits<CPPTYPE>::Get(src.Parent(), src.Field(), itemIdx)); \ + } \ + break; \ + } + switch (CppType()) { + APPLY_TMP_MACRO_FOR_ALL_CPPTYPES() + default: + RaiseUnknown(); + } +#undef TMP_MACRO_FOR_CPPTYPE + } + +} diff --git a/library/cpp/protobuf/util/simple_reflection_ut.cpp b/library/cpp/protobuf/util/simple_reflection_ut.cpp new file mode 100644 index 0000000000..169d4703c9 --- /dev/null +++ b/library/cpp/protobuf/util/simple_reflection_ut.cpp @@ -0,0 +1,359 @@ +#include "simple_reflection.h" +#include <library/cpp/protobuf/util/ut/sample_for_simple_reflection.pb.h> +#include <library/cpp/protobuf/util/ut/extensions.pb.h> + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NProtoBuf; + +Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { + static TSample GenSampleForMergeFrom() { + TSample smf; + smf.SetOneStr("one str"); + smf.MutableOneMsg()->AddRepInt(1); + smf.AddRepMsg()->AddRepInt(2); + smf.AddRepMsg()->AddRepInt(3); + smf.AddRepStr("one rep str"); + smf.AddRepStr("two rep str"); + smf.SetAnotherOneStr("another one str"); + return smf; + } + + Y_UNIT_TEST(MergeFromGeneric) { + const TSample src(GenSampleForMergeFrom()); + TSample dst; + const Descriptor* descr = dst.GetDescriptor(); + + { + TMutableField dstOneStr(dst, descr->FindFieldByName("OneStr")); + TConstField srcOneStr(src, descr->FindFieldByName("OneStr")); + dstOneStr.MergeFrom(srcOneStr); + UNIT_ASSERT_VALUES_EQUAL(dst.GetOneStr(), src.GetOneStr()); + } + + { // MergeFrom for single message fields acts like a Message::MergeFrom + TMutableField dstOneMsg(dst, descr->FindFieldByName("OneMsg")); + dstOneMsg.MergeFrom(TConstField(src, descr->FindFieldByName("OneMsg"))); + UNIT_ASSERT_VALUES_EQUAL(dst.GetOneMsg().RepIntSize(), src.GetOneMsg().RepIntSize()); + dstOneMsg.MergeFrom(TConstField(src, descr->FindFieldByName("OneMsg"))); + UNIT_ASSERT_VALUES_EQUAL(dst.GetOneMsg().RepIntSize(), src.GetOneMsg().RepIntSize() * 2); + } + + { // MergeFrom for repeated fields acts like append + TMutableField dstRepMsg(dst, descr->FindFieldByName("RepMsg")); + dstRepMsg.MergeFrom(TConstField(src, descr->FindFieldByName("RepMsg"))); + UNIT_ASSERT_VALUES_EQUAL(dst.RepMsgSize(), src.RepMsgSize()); + dstRepMsg.MergeFrom(TConstField(src, descr->FindFieldByName("RepMsg"))); + UNIT_ASSERT_VALUES_EQUAL(dst.RepMsgSize(), src.RepMsgSize() * 2); + for (size_t repMsgIndex = 0; repMsgIndex < dst.RepMsgSize(); ++repMsgIndex) { + UNIT_ASSERT_VALUES_EQUAL(dst.GetRepMsg(repMsgIndex).RepIntSize(), src.GetRepMsg(0).RepIntSize()); + } + } + } + + Y_UNIT_TEST(MergeFromSelf) { + const TSample sample(GenSampleForMergeFrom()); + TSample msg(sample); + const Descriptor* descr = msg.GetDescriptor(); + + TMutableField oneStr(msg, descr->FindFieldByName("OneStr")); + oneStr.MergeFrom(oneStr); + UNIT_ASSERT_VALUES_EQUAL(msg.GetOneStr(), sample.GetOneStr()); + + TMutableField oneMsg(msg, descr->FindFieldByName("OneMsg")); + oneMsg.MergeFrom(oneMsg); // nothing should change + UNIT_ASSERT_VALUES_EQUAL(msg.GetOneMsg().RepIntSize(), sample.GetOneMsg().RepIntSize()); + } + + Y_UNIT_TEST(MergeFromAnotherFD) { + const TSample sample(GenSampleForMergeFrom()); + TSample msg(GenSampleForMergeFrom()); + const Descriptor* descr = msg.GetDescriptor(); + + { // string + TMutableField oneStr(msg, descr->FindFieldByName("OneStr")); + TMutableField repStr(msg, descr->FindFieldByName("RepStr")); + TMutableField anotherOneStr(msg, descr->FindFieldByName("AnotherOneStr")); + oneStr.MergeFrom(anotherOneStr); + UNIT_ASSERT_VALUES_EQUAL(msg.GetOneStr(), sample.GetAnotherOneStr()); + oneStr.MergeFrom(repStr); + const size_t sampleRepStrSize = sample.RepStrSize(); + UNIT_ASSERT_VALUES_EQUAL(msg.GetOneStr(), sample.GetRepStr(sampleRepStrSize - 1)); + repStr.MergeFrom(anotherOneStr); + UNIT_ASSERT_VALUES_EQUAL(msg.RepStrSize(), sampleRepStrSize + 1); + UNIT_ASSERT_VALUES_EQUAL(msg.GetRepStr(sampleRepStrSize), msg.GetAnotherOneStr()); + } + + { // Message + TMutableField oneMsg(msg, descr->FindFieldByName("OneMsg")); + TMutableField repMsg(msg, descr->FindFieldByName("RepMsg")); + oneMsg.MergeFrom(repMsg); + const size_t oneMsgRepIntSize = sample.GetOneMsg().RepIntSize(); + const size_t sizeOfAllRepIntsInRepMsg = sample.RepMsgSize(); + UNIT_ASSERT_VALUES_EQUAL(msg.GetOneMsg().RepIntSize(), oneMsgRepIntSize + sizeOfAllRepIntsInRepMsg); + repMsg.MergeFrom(oneMsg); + 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 + 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 + 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" + 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()); + } + } + + 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); + UNIT_ASSERT(field->HasValue()); + UNIT_ASSERT_VALUES_EQUAL("1", (field->Get<TString>())); + } + + { + 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->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(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(!field->HasValue()); + } + + { + 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); + 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(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(!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); + UNIT_ASSERT(!field->HasValue()); + } + + { + 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); + Y_UNUSED(fieldCopy); + 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 + { + {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; + msg.SetExtension(NExt::TTestExt::ExtField, "ext"); + msg.SetExtension(NExt::ExtField, 2); + msg.AddExtension(NExt::Ext2Field, 33); + 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); + UNIT_ASSERT(field->HasValue()); + UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>()); + } + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "ExtField"); // ambiguity + UNIT_ASSERT(!field); + } + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.Ext2Field"); + UNIT_ASSERT(field); + UNIT_ASSERT(field->HasValue()); + UNIT_ASSERT_VALUES_EQUAL(33, field->Get<int>()); + } + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "Ext2Field"); + UNIT_ASSERT(field); + UNIT_ASSERT(field->HasValue()); + UNIT_ASSERT_VALUES_EQUAL(33, field->Get<int>()); + } + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "SubMsgExt"); + UNIT_ASSERT(field); + UNIT_ASSERT(field->HasValue()); + const TInnerSample* subMsg2 = field->GetAs<TInnerSample>(); + UNIT_ASSERT(subMsg2); + UNIT_ASSERT_VALUES_EQUAL(1, subMsg2->RepIntSize()); + UNIT_ASSERT_VALUES_EQUAL(20, subMsg2->GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(54, subMsg2->GetExtension(NExt::Ext3Field)); + } + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "SubMsgExt/Ext3Field"); + UNIT_ASSERT(field); + UNIT_ASSERT(field->HasValue()); + UNIT_ASSERT_VALUES_EQUAL(54, field->Get<int>()); + } + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "SubMsgExt/RepInt"); + UNIT_ASSERT(field); + UNIT_ASSERT(field->HasValue()); + UNIT_ASSERT_VALUES_EQUAL(20, field->Get<int>()); + } +} +} +} diff --git a/library/cpp/protobuf/util/sort.h b/library/cpp/protobuf/util/sort.h new file mode 100644 index 0000000000..985ba6f689 --- /dev/null +++ b/library/cpp/protobuf/util/sort.h @@ -0,0 +1,28 @@ +#pragma once + +#include <google/protobuf/message.h> + +#include <util/generic/vector.h> +#include <util/generic/algorithm.h> + +namespace NProtoBuf { + // TComparePtr is something like: + // typedef bool (*TComparePtr)(const Message* msg1, const Message* msg2); + // typedef bool (*TComparePtr)(const TProto* msg1, const TProto* msg2); + + template <typename TProto, typename TComparePtr> + void SortMessages(RepeatedPtrField<TProto>& msgs, TComparePtr cmp) { + TVector<TProto*> ptrs; + ptrs.reserve(msgs.size()); + while (msgs.size()) { + ptrs.push_back(msgs.ReleaseLast()); + } + + ::StableSort(ptrs.begin(), ptrs.end(), cmp); + + for (size_t i = 0; i < ptrs.size(); ++i) { + msgs.AddAllocated(ptrs[i]); + } + } + +} diff --git a/library/cpp/protobuf/util/traits.h b/library/cpp/protobuf/util/traits.h new file mode 100644 index 0000000000..50f036d0ea --- /dev/null +++ b/library/cpp/protobuf/util/traits.h @@ -0,0 +1,320 @@ +#pragma once + +#include <util/generic/typetraits.h> + +#include <google/protobuf/descriptor.h> +#include <google/protobuf/message.h> + +namespace NProtoBuf { +// this nasty windows.h macro interfers with protobuf::Reflection::GetMessage() +#if defined(GetMessage) +#undef GetMessage +#endif + + struct TCppTypeTraitsBase { + static inline bool Has(const Message& msg, const FieldDescriptor* field) { // non-repeated + return msg.GetReflection()->HasField(msg, field); + } + static inline size_t Size(const Message& msg, const FieldDescriptor* field) { // repeated + return msg.GetReflection()->FieldSize(msg, field); + } + + static inline void Clear(Message& msg, const FieldDescriptor* field) { + msg.GetReflection()->ClearField(&msg, field); + } + + static inline void RemoveLast(Message& msg, const FieldDescriptor* field) { + msg.GetReflection()->RemoveLast(&msg, field); + } + + static inline void SwapElements(Message& msg, const FieldDescriptor* field, int index1, int index2) { + msg.GetReflection()->SwapElements(&msg, field, index1, index2); + } + }; + + // default value accessor + template <FieldDescriptor::CppType cpptype> + struct TCppTypeTraitsDefault; + +#define DECLARE_CPPTYPE_DEFAULT(cpptype, method) \ + template <> \ + struct TCppTypeTraitsDefault<cpptype> { \ + static auto GetDefault(const FieldDescriptor* fd) \ + -> decltype(fd->default_value_##method()) { \ + Y_ASSERT(fd); \ + return fd->default_value_##method(); \ + } \ + }; + + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_INT32, int32); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_INT64, int64); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_UINT32, uint32); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_UINT64, uint64); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_FLOAT, float); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_DOUBLE, double); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_BOOL, bool); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_ENUM, enum); + DECLARE_CPPTYPE_DEFAULT(FieldDescriptor::CppType::CPPTYPE_STRING, string); + +#undef DECLARE_CPPTYPE_DEFAULT + + // getters/setters of field with specified CppType + template <FieldDescriptor::CppType cpptype> + struct TCppTypeTraits : TCppTypeTraitsBase { + static const FieldDescriptor::CppType CppType = cpptype; + + struct T {}; + static T Get(const Message& msg, const FieldDescriptor* field); + static T GetRepeated(const Message& msg, const FieldDescriptor* field, int index); + static T GetDefault(const FieldDescriptor* field); + + static void Set(Message& msg, const FieldDescriptor* field, T value); + static void AddRepeated(Message& msg, const FieldDescriptor* field, T value); + static void SetRepeated(Message& msg, const FieldDescriptor* field, int index, T value); + }; + + // any type T -> CppType + template <typename T> + struct TSelectCppType { + //static const FieldDescriptor::CppType Result = FieldDescriptor::MAX_CPPTYPE; + }; + +#define DECLARE_CPPTYPE_TRAITS(cpptype, type, method) \ + template <> \ + struct TCppTypeTraits<cpptype>: public TCppTypeTraitsBase { \ + typedef type T; \ + static const FieldDescriptor::CppType CppType = cpptype; \ + \ + static inline T Get(const Message& msg, const FieldDescriptor* field) { \ + return msg.GetReflection()->Get##method(msg, field); \ + } \ + static inline T GetRepeated(const Message& msg, const FieldDescriptor* field, int index) { \ + return msg.GetReflection()->GetRepeated##method(msg, field, index); \ + } \ + static inline T GetDefault(const FieldDescriptor* field) { \ + return TCppTypeTraitsDefault<cpptype>::GetDefault(field); \ + } \ + static inline void Set(Message& msg, const FieldDescriptor* field, T value) { \ + msg.GetReflection()->Set##method(&msg, field, value); \ + } \ + static inline void AddRepeated(Message& msg, const FieldDescriptor* field, T value) { \ + msg.GetReflection()->Add##method(&msg, field, value); \ + } \ + static inline void SetRepeated(Message& msg, const FieldDescriptor* field, int index, T value) { \ + msg.GetReflection()->SetRepeated##method(&msg, field, index, value); \ + } \ + }; \ + template <> \ + struct TSelectCppType<type> { \ + static const FieldDescriptor::CppType Result = cpptype; \ + typedef type T; \ + }; + + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_INT32, i32, Int32); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_INT64, i64, Int64); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_UINT32, ui32, UInt32); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_UINT64, ui64, UInt64); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_DOUBLE, double, Double); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_FLOAT, float, Float); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_BOOL, bool, Bool); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_ENUM, const EnumValueDescriptor*, Enum); + DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_STRING, TString, String); + //DECLARE_CPPTYPE_TRAITS(FieldDescriptor::CPPTYPE_MESSAGE, const Message&, Message); + +#undef DECLARE_CPPTYPE_TRAITS + + // specialization for message pointer + template <> + struct TCppTypeTraits<FieldDescriptor::CPPTYPE_MESSAGE>: public TCppTypeTraitsBase { + typedef const Message* T; + static const FieldDescriptor::CppType CppType = FieldDescriptor::CPPTYPE_MESSAGE; + + static inline T Get(const Message& msg, const FieldDescriptor* field) { + return &(msg.GetReflection()->GetMessage(msg, field)); + } + static inline T GetRepeated(const Message& msg, const FieldDescriptor* field, int index) { + return &(msg.GetReflection()->GetRepeatedMessage(msg, field, index)); + } + static inline Message* Set(Message& msg, const FieldDescriptor* field, const Message* value) { + Message* ret = msg.GetReflection()->MutableMessage(&msg, field); + ret->CopyFrom(*value); + return ret; + } + static inline Message* AddRepeated(Message& msg, const FieldDescriptor* field, const Message* value) { + Message* ret = msg.GetReflection()->AddMessage(&msg, field); + ret->CopyFrom(*value); + return ret; + } + static inline Message* SetRepeated(Message& msg, const FieldDescriptor* field, int index, const Message* value) { + Message* ret = msg.GetReflection()->MutableRepeatedMessage(&msg, field, index); + ret->CopyFrom(*value); + return ret; + } + }; + + template <> + struct TSelectCppType<const Message*> { + static const FieldDescriptor::CppType Result = FieldDescriptor::CPPTYPE_MESSAGE; + typedef const Message* T; + }; + + template <> + struct TSelectCppType<Message> { + static const FieldDescriptor::CppType Result = FieldDescriptor::CPPTYPE_MESSAGE; + typedef const Message* T; + }; + + template <FieldDescriptor::CppType CppType, bool Repeated> + struct TFieldTraits { + typedef TCppTypeTraits<CppType> TBaseTraits; + typedef typename TBaseTraits::T T; + + static inline T Get(const Message& msg, const FieldDescriptor* field, size_t index = 0) { + Y_ASSERT(index == 0); + return TBaseTraits::Get(msg, field); + } + + 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); + } + + static inline size_t Size(const Message& msg, const FieldDescriptor* field) { + return Has(msg, field); + } + + static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { + Y_ASSERT(index == 0); + TBaseTraits::Set(msg, field, value); + } + + static inline void Add(Message& msg, const FieldDescriptor* field, T value) { + TBaseTraits::Set(msg, field, value); + } + }; + + template <FieldDescriptor::CppType CppType> + struct TFieldTraits<CppType, true> { + typedef TCppTypeTraits<CppType> TBaseTraits; + typedef typename TBaseTraits::T T; + + static inline T Get(const Message& msg, const FieldDescriptor* field, size_t index = 0) { + return TBaseTraits::GetRepeated(msg, field, index); + } + + static inline T GetDefault(const FieldDescriptor* field) { + return TBaseTraits::GetDefault(field); + } + + static inline size_t Size(const Message& msg, const FieldDescriptor* field) { + return TBaseTraits::Size(msg, field); + } + + static inline bool Has(const Message& msg, const FieldDescriptor* field) { + return Size(msg, field) > 0; + } + + static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { + TBaseTraits::SetRepeated(msg, field, index, value); + } + + static inline void Add(Message& msg, const FieldDescriptor* field, T value) { + TBaseTraits::AddRepeated(msg, field, value); + } + }; + + // Simpler interface at the cost of checking is_repeated() on each call + template <FieldDescriptor::CppType CppType> + struct TSimpleFieldTraits { + typedef TFieldTraits<CppType, true> TRepeated; + typedef TFieldTraits<CppType, false> TSingle; + typedef typename TRepeated::T T; + + static inline size_t Size(const Message& msg, const FieldDescriptor* field) { + if (field->is_repeated()) + return TRepeated::Size(msg, field); + else + return TSingle::Size(msg, field); + } + + static inline bool Has(const Message& msg, const FieldDescriptor* field) { + if (field->is_repeated()) + return TRepeated::Has(msg, field); + else + return TSingle::Has(msg, field); + } + + static inline T Get(const Message& msg, const FieldDescriptor* field, size_t index = 0) { + Y_ASSERT(index < Size(msg, field) || !field->is_repeated() && index == 0); // Get for single fields is always allowed because of default values + if (field->is_repeated()) + return TRepeated::Get(msg, field, index); + else + return TSingle::Get(msg, field, index); + } + + static inline T GetDefault(const FieldDescriptor* field) { + return TSingle::GetDefault(field); + } + + static inline void Set(Message& msg, const FieldDescriptor* field, T value, size_t index = 0) { + Y_ASSERT(!field->is_repeated() && index == 0 || index < Size(msg, field)); + if (field->is_repeated()) + TRepeated::Set(msg, field, value, index); + else + TSingle::Set(msg, field, value, index); + } + + static inline void Add(Message& msg, const FieldDescriptor* field, T value) { + if (field->is_repeated()) + TRepeated::Add(msg, field, value); + else + TSingle::Add(msg, field, value); + } + }; + + // some cpp-type groups + + template <FieldDescriptor::CppType CppType> + struct TIsIntegerCppType { + enum { + Result = CppType == FieldDescriptor::CPPTYPE_INT32 || + CppType == FieldDescriptor::CPPTYPE_INT64 || + CppType == FieldDescriptor::CPPTYPE_UINT32 || + CppType == FieldDescriptor::CPPTYPE_UINT64 + }; + }; + + template <FieldDescriptor::CppType CppType> + struct TIsFloatCppType { + enum { + Result = CppType == FieldDescriptor::CPPTYPE_FLOAT || + CppType == FieldDescriptor::CPPTYPE_DOUBLE + }; + }; + + template <FieldDescriptor::CppType CppType> + struct TIsNumericCppType { + enum { + Result = CppType == FieldDescriptor::CPPTYPE_BOOL || + TIsIntegerCppType<CppType>::Result || + TIsFloatCppType<CppType>::Result + }; + }; + + // a helper macro for splitting flow by cpp-type (e.g. in a switch) + +#define APPLY_TMP_MACRO_FOR_ALL_CPPTYPES() \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_INT32) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_INT64) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_UINT32) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_UINT64) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_DOUBLE) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_FLOAT) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_BOOL) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_ENUM) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_STRING) \ + TMP_MACRO_FOR_CPPTYPE(NProtoBuf::FieldDescriptor::CPPTYPE_MESSAGE) +} diff --git a/library/cpp/protobuf/util/ut/common_ut.proto b/library/cpp/protobuf/util/ut/common_ut.proto new file mode 100644 index 0000000000..9cf803ffbf --- /dev/null +++ b/library/cpp/protobuf/util/ut/common_ut.proto @@ -0,0 +1,72 @@ +import "google/protobuf/descriptor.proto"; +import "library/cpp/protobuf/util/proto/merge.proto"; + +package NProtobufUtilUt; + +extend google.protobuf.FieldOptions { + optional bool XXX = 53772; +} + +message TWalkTest { + optional uint32 OptInt = 1 [(XXX)=true]; + repeated uint32 RepInt = 2; + + optional string OptStr = 3; + repeated string RepStr = 4 [(XXX)=true]; + + optional TWalkTest OptSub = 5 [(XXX)=true]; + repeated TWalkTest RepSub = 6; +} + +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 TMergeTestNoMerge { + option (DontMerge) = true; + + optional uint32 A = 1; + repeated uint32 B = 2; +} + +message TMergeTestMerge { + optional uint32 A = 1; + repeated uint32 B = 2; + repeated uint32 C = 3 [(DontMergeField)=true]; +} + +message TMergeTest { + repeated uint32 MergeInt = 1; + repeated uint32 NoMergeInt = 2 [(DontMergeField)=true]; + + optional TMergeTestMerge MergeSub = 3; + repeated TMergeTestMerge NoMergeRepSub = 4 [(DontMergeField)=true]; + optional TMergeTestNoMerge NoMergeOptSub = 5; +} + +message TTextTest { + optional uint32 Foo = 1; +} + +message TTextEnumTest { + enum EnumTest { + EET_SLOT_1 = 1; + EET_SLOT_2 = 2; + } + optional EnumTest Slot = 1; +} diff --git a/library/cpp/protobuf/util/ut/extensions.proto b/library/cpp/protobuf/util/ut/extensions.proto new file mode 100644 index 0000000000..4944f0f5ca --- /dev/null +++ b/library/cpp/protobuf/util/ut/extensions.proto @@ -0,0 +1,22 @@ +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; +} diff --git a/library/cpp/protobuf/util/ut/sample_for_is_equal.proto b/library/cpp/protobuf/util/ut/sample_for_is_equal.proto new file mode 100644 index 0000000000..a91c16deaa --- /dev/null +++ b/library/cpp/protobuf/util/ut/sample_for_is_equal.proto @@ -0,0 +1,8 @@ +message TInner { + optional string Brbrbr = 3; +} + +message TSampleForIsEqual { + optional string Name = 1; + optional TInner Inner = 5; +} diff --git a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto new file mode 100644 index 0000000000..cca1dd869a --- /dev/null +++ b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto @@ -0,0 +1,25 @@ +message TInnerSample { + repeated int32 RepInt = 1; + + extensions 100 to 199; +} + +message TSample { + optional string OneStr = 1; + optional TInnerSample OneMsg = 2; + repeated TInnerSample RepMsg = 3; + repeated string RepStr = 4; + optional string AnotherOneStr = 5; + + optional int32 OneInt = 6; + repeated int32 RepInt = 7; + + enum EEnum { + V1 = 1; + V2 = 2; + } + optional EEnum OneEnum = 8; + repeated EEnum RepEnum = 9; + + extensions 100 to 199; +} diff --git a/library/cpp/protobuf/util/ut/ya.make b/library/cpp/protobuf/util/ut/ya.make new file mode 100644 index 0000000000..701ba9a8c8 --- /dev/null +++ b/library/cpp/protobuf/util/ut/ya.make @@ -0,0 +1,19 @@ +OWNER(nga) + +UNITTEST_FOR(library/cpp/protobuf/util) + +SRCS( + extensions.proto + sample_for_is_equal.proto + sample_for_simple_reflection.proto + common_ut.proto + pb_io_ut.cpp + is_equal_ut.cpp + iterators_ut.cpp + simple_reflection_ut.cpp + repeated_field_utils_ut.cpp + walk_ut.cpp + merge_ut.cpp +) + +END() diff --git a/library/cpp/protobuf/util/walk.cpp b/library/cpp/protobuf/util/walk.cpp new file mode 100644 index 0000000000..b65ec03e04 --- /dev/null +++ b/library/cpp/protobuf/util/walk.cpp @@ -0,0 +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 diff --git a/library/cpp/protobuf/util/walk.h b/library/cpp/protobuf/util/walk.h new file mode 100644 index 0000000000..d15d76562d --- /dev/null +++ b/library/cpp/protobuf/util/walk.h @@ -0,0 +1,33 @@ +#pragma once + +#include "simple_reflection.h" + +#include <google/protobuf/message.h> +#include <google/protobuf/descriptor.h> + +#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 + // 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); +} diff --git a/library/cpp/protobuf/util/walk_ut.cpp b/library/cpp/protobuf/util/walk_ut.cpp new file mode 100644 index 0000000000..2ea6071b17 --- /dev/null +++ b/library/cpp/protobuf/util/walk_ut.cpp @@ -0,0 +1,158 @@ +#include "walk.h" +#include "simple_reflection.h" +#include <library/cpp/protobuf/util/ut/common_ut.pb.h> + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NProtoBuf; + +Y_UNIT_TEST_SUITE(ProtobufWalk) { + static void InitProto(NProtobufUtilUt::TWalkTest & p, int level = 0) { + p.SetOptInt(1); + p.AddRepInt(2); + p.AddRepInt(3); + + p.SetOptStr("123"); + p.AddRepStr("*"); + p.AddRepStr("abcdef"); + p.AddRepStr("1234"); + + if (level == 0) { + InitProto(*p.MutableOptSub(), 1); + InitProto(*p.AddRepSub(), 1); + InitProto(*p.AddRepSub(), 1); + } + } + + static bool IncreaseInts(Message & msg, const FieldDescriptor* fd) { + TMutableField f(msg, fd); + if (f.IsInstance<ui32>()) { + for (size_t i = 0; i < f.Size(); ++i) + f.Set(f.Get<ui64>(i) + 1, i); // ui64 should be ok! + } + return true; + } + + static bool RepeatString1(Message & msg, const FieldDescriptor* fd) { + TMutableField f(msg, fd); + if (f.IsString()) { + for (size_t i = 0; i < f.Size(); ++i) + if (f.Get<TString>(i).StartsWith('1')) + f.Set(f.Get<TString>(i) + f.Get<TString>(i), i); + } + return true; + } + + static bool ClearXXX(Message & msg, const FieldDescriptor* fd) { + const FieldOptions& opt = fd->options(); + if (opt.HasExtension(NProtobufUtilUt::XXX) && opt.GetExtension(NProtobufUtilUt::XXX)) + TMutableField(msg, fd).Clear(); + + return true; + } + + 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); + + { + UNIT_ASSERT_EQUAL(p.GetOptInt(), 1); + UNIT_ASSERT_EQUAL(p.RepIntSize(), 2); + UNIT_ASSERT_EQUAL(p.GetRepInt(0), 2); + UNIT_ASSERT_EQUAL(p.GetRepInt(1), 3); + + WalkReflection(p, IncreaseInts); + + UNIT_ASSERT_EQUAL(p.GetOptInt(), 2); + UNIT_ASSERT_EQUAL(p.RepIntSize(), 2); + UNIT_ASSERT_EQUAL(p.GetRepInt(0), 3); + UNIT_ASSERT_EQUAL(p.GetRepInt(1), 4); + + UNIT_ASSERT_EQUAL(p.GetOptSub().GetOptInt(), 2); + UNIT_ASSERT_EQUAL(p.GetOptSub().RepIntSize(), 2); + UNIT_ASSERT_EQUAL(p.GetOptSub().GetRepInt(0), 3); + UNIT_ASSERT_EQUAL(p.GetOptSub().GetRepInt(1), 4); + + UNIT_ASSERT_EQUAL(p.RepSubSize(), 2); + UNIT_ASSERT_EQUAL(p.GetRepSub(1).GetOptInt(), 2); + UNIT_ASSERT_EQUAL(p.GetRepSub(1).RepIntSize(), 2); + UNIT_ASSERT_EQUAL(p.GetRepSub(1).GetRepInt(0), 3); + UNIT_ASSERT_EQUAL(p.GetRepSub(1).GetRepInt(1), 4); + } + { + UNIT_ASSERT_EQUAL(p.GetOptStr(), "123"); + UNIT_ASSERT_EQUAL(p.GetRepStr(2), "1234"); + + WalkReflection(p, RepeatString1); + + UNIT_ASSERT_EQUAL(p.GetOptStr(), "123123"); + UNIT_ASSERT_EQUAL(p.RepStrSize(), 3); + UNIT_ASSERT_EQUAL(p.GetRepStr(0), "*"); + UNIT_ASSERT_EQUAL(p.GetRepStr(1), "abcdef"); + UNIT_ASSERT_EQUAL(p.GetRepStr(2), "12341234"); + + UNIT_ASSERT_EQUAL(p.RepSubSize(), 2); + UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetOptStr(), "123123"); + UNIT_ASSERT_EQUAL(p.GetRepSub(0).RepStrSize(), 3); + UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetRepStr(0), "*"); + UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetRepStr(1), "abcdef"); + UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetRepStr(2), "12341234"); + } + { + UNIT_ASSERT(p.HasOptInt()); + UNIT_ASSERT(p.RepStrSize() == 3); + UNIT_ASSERT(p.HasOptSub()); + + WalkReflection(p, ClearXXX); + + UNIT_ASSERT(!p.HasOptInt()); + UNIT_ASSERT(p.RepIntSize() == 2); + UNIT_ASSERT(p.HasOptStr()); + UNIT_ASSERT(p.RepStrSize() == 0); + UNIT_ASSERT(!p.HasOptSub()); + 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); + } +} diff --git a/library/cpp/protobuf/util/ya.make b/library/cpp/protobuf/util/ya.make new file mode 100644 index 0000000000..b62028af58 --- /dev/null +++ b/library/cpp/protobuf/util/ya.make @@ -0,0 +1,26 @@ +LIBRARY() + +OWNER(mowgli) + +PEERDIR( + contrib/libs/protobuf + library/cpp/binsaver + library/cpp/protobuf/util/proto + library/cpp/string_utils/base64 +) + +SRCS( + is_equal.cpp + iterators.h + merge.cpp + path.cpp + pb_io.cpp + pb_utils.h + repeated_field_utils.h + simple_reflection.cpp + walk.cpp +) + +END() + +RECURSE_FOR_TESTS(ut) |