diff options
author | romanenko-ii <romanenko-ii@yandex-team.ru> | 2022-02-10 16:50:37 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:37 +0300 |
commit | 5ca39c6275c4cb3911098b94ffa1516f68b7d5e8 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/protobuf/util | |
parent | 4ee92606ffd02329182167a0db41aae105a62c6c (diff) | |
download | ydb-5ca39c6275c4cb3911098b94ffa1516f68b7d5e8.tar.gz |
Restoring authorship annotation for <romanenko-ii@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/protobuf/util')
-rw-r--r-- | library/cpp/protobuf/util/pb_io.cpp | 38 | ||||
-rw-r--r-- | library/cpp/protobuf/util/pb_io.h | 8 | ||||
-rw-r--r-- | library/cpp/protobuf/util/pb_io_ut.cpp | 144 | ||||
-rw-r--r-- | library/cpp/protobuf/util/ut/common_ut.proto | 16 |
4 files changed, 103 insertions, 103 deletions
diff --git a/library/cpp/protobuf/util/pb_io.cpp b/library/cpp/protobuf/util/pb_io.cpp index 650d76cbab..6270ee0624 100644 --- a/library/cpp/protobuf/util/pb_io.cpp +++ b/library/cpp/protobuf/util/pb_io.cpp @@ -10,17 +10,17 @@ #include <util/generic/string.h> #include <util/stream/file.h> #include <util/stream/str.h> -#include <util/string/cast.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)); - } - }; - + + 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"; @@ -108,16 +108,16 @@ void SerializeToTextFormat(const NProtoBuf::Message& m, const TString& 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 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); diff --git a/library/cpp/protobuf/util/pb_io.h b/library/cpp/protobuf/util/pb_io.h index a3c204be64..493c84cb5f 100644 --- a/library/cpp/protobuf/util/pb_io.h +++ b/library/cpp/protobuf/util/pb_io.h @@ -52,10 +52,10 @@ void SerializeToTextFormat(const NProtoBuf::Message& m, IOutputStream& out); // 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); - +// 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 diff --git a/library/cpp/protobuf/util/pb_io_ut.cpp b/library/cpp/protobuf/util/pb_io_ut.cpp index 9aa0d8d43a..875d6dc602 100644 --- a/library/cpp/protobuf/util/pb_io_ut.cpp +++ b/library/cpp/protobuf/util/pb_io_ut.cpp @@ -17,31 +17,31 @@ static NProtobufUtilUt::TTextTest GetCorrectMessage() { return m; } -static NProtobufUtilUt::TTextEnumTest GetCorrectEnumMessage() { - NProtobufUtilUt::TTextEnumTest m; - m.SetSlot(NProtobufUtilUt::TTextEnumTest::EET_SLOT_1); - 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 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 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,"; @@ -196,60 +196,60 @@ Y_UNIT_TEST_SUITE(TTestProtoBufIO) { } } - 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(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 diff --git a/library/cpp/protobuf/util/ut/common_ut.proto b/library/cpp/protobuf/util/ut/common_ut.proto index d9101498c9..9cf803ffbf 100644 --- a/library/cpp/protobuf/util/ut/common_ut.proto +++ b/library/cpp/protobuf/util/ut/common_ut.proto @@ -62,11 +62,11 @@ message TMergeTest { message TTextTest { optional uint32 Foo = 1; } - -message TTextEnumTest { - enum EnumTest { - EET_SLOT_1 = 1; - EET_SLOT_2 = 2; - } - optional EnumTest Slot = 1; -} + +message TTextEnumTest { + enum EnumTest { + EET_SLOT_1 = 1; + EET_SLOT_2 = 2; + } + optional EnumTest Slot = 1; +} |