diff options
author | Vasily Gerasimov <UgnineSirdis@gmail.com> | 2022-02-10 16:49:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:09 +0300 |
commit | 6cdc8f140213c595e4ad38bc3d97fcef1146b8c3 (patch) | |
tree | f69637041e6fed76ebae0c74ae1fa0c4be6ab5b4 /library/cpp/protobuf | |
parent | e5d4696304c6689379ac7ce334512404d4b7836c (diff) | |
download | ydb-6cdc8f140213c595e4ad38bc3d97fcef1146b8c3.tar.gz |
Restoring authorship annotation for Vasily Gerasimov <UgnineSirdis@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf')
37 files changed, 694 insertions, 694 deletions
diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index dc84fb4d5d..3985b917bc 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -1,19 +1,19 @@ -#pragma once - -#include "string_transform.h" +#pragma once + +#include "string_transform.h" #include "name_generator.h" - -#include <util/generic/vector.h> + +#include <util/generic/vector.h> #include <util/generic/yexception.h> - + #include <functional> -namespace NProtobufJson { +namespace NProtobufJson { struct TProto2JsonConfig { using TSelf = TProto2JsonConfig; - + bool FormatOutput = false; - + enum MissingKeyMode { // Skip missing keys MissingKeySkip = 0, @@ -32,10 +32,10 @@ namespace NProtobufJson { }; MissingKeyMode MissingSingleKeyMode = MissingKeySkip; MissingKeyMode MissingRepeatedKeyMode = MissingKeySkip; - + /// Add null value for missing fields (false by default). bool AddMissingFields = false; - + enum EnumValueMode { EnumNumber = 0, // default EnumName, @@ -44,7 +44,7 @@ namespace NProtobufJson { EnumFullNameLowerCase, }; EnumValueMode EnumMode = EnumNumber; - + enum FldNameMode { FieldNameOriginalCase = 0, // default FieldNameLowerCase, @@ -54,7 +54,7 @@ namespace NProtobufJson { FieldNameSnakeCaseDense // ABC -> abc, UserID -> user_id }; FldNameMode FieldNameMode = FieldNameOriginalCase; - + enum ExtFldNameMode { ExtFldNameFull = 0, // default, field.full_name() ExtFldNameShort // field.name() @@ -64,14 +64,14 @@ namespace NProtobufJson { /// Use 'json_name' protobuf option for field name, mutually exclusive /// with FieldNameMode. bool UseJsonName = false; - + /// Transforms will be applied only to string values (== protobuf fields of string / bytes type). /// yajl_encode_string will be used if no transforms are specified. TVector<TStringTransformPtr> StringTransforms; /// Print map as object, otherwise print it as array of key/value objects bool MapAsObject = false; - + /// Stringify long integers which are not exactly representable by float or double values enum EStringifyLongNumbersMode { StringifyLongNumbersNever = 0, // default @@ -97,34 +97,34 @@ namespace NProtobufJson { MissingSingleKeyMode = mode; return *this; } - + TSelf& SetMissingRepeatedKeyMode(MissingKeyMode mode) { MissingRepeatedKeyMode = mode; return *this; } - + TSelf& SetAddMissingFields(bool add) { AddMissingFields = add; return *this; } - + TSelf& SetEnumMode(EnumValueMode mode) { EnumMode = mode; return *this; } - + TSelf& SetFieldNameMode(FldNameMode mode) { Y_ENSURE(mode == FieldNameOriginalCase || !UseJsonName, "FieldNameMode and UseJsonName are mutually exclusive"); FieldNameMode = mode; return *this; } - + TSelf& SetUseJsonName(bool jsonName) { Y_ENSURE(!jsonName || FieldNameMode == FieldNameOriginalCase, "FieldNameMode and UseJsonName are mutually exclusive"); UseJsonName = jsonName; return *this; } - + TSelf& SetExtensionFieldNameMode(ExtFldNameMode mode) { ExtensionFieldNameMode = mode; return *this; diff --git a/library/cpp/protobuf/json/field_option.h b/library/cpp/protobuf/json/field_option.h index c8a8bfbff5..3a2db53a92 100644 --- a/library/cpp/protobuf/json/field_option.h +++ b/library/cpp/protobuf/json/field_option.h @@ -1,10 +1,10 @@ -#pragma once - +#pragma once + #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> - -namespace NProtobufJson { + +namespace NProtobufJson { // Functor that defines whether given field has some option set to true // // Example: @@ -20,13 +20,13 @@ namespace NProtobufJson { , Positive(positive) { } - + bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) const { const NProtoBuf::FieldOptions& opt = field->options(); const bool val = opt.GetExtension(Option); return Positive ? val : !val; } - + private: const TFieldOptionExtensionId& Option; bool Positive; @@ -35,6 +35,6 @@ namespace NProtobufJson { template <typename TFieldOptionExtensionId> TFieldOptionFunctor<TFieldOptionExtensionId> MakeFieldOptionFunctor(const TFieldOptionExtensionId& option, bool positive = true) { return TFieldOptionFunctor<TFieldOptionExtensionId>(option, positive); - } - -} + } + +} diff --git a/library/cpp/protobuf/json/filter.h b/library/cpp/protobuf/json/filter.h index 9a3ddb54fe..7033998e14 100644 --- a/library/cpp/protobuf/json/filter.h +++ b/library/cpp/protobuf/json/filter.h @@ -1,27 +1,27 @@ -#pragma once - -#include "config.h" -#include "proto2json_printer.h" -#include "json_output_create.h" - -#include <util/generic/yexception.h> -#include <util/generic/utility.h> - -#include <functional> - -namespace NProtobufJson { +#pragma once + +#include "config.h" +#include "proto2json_printer.h" +#include "json_output_create.h" + +#include <util/generic/yexception.h> +#include <util/generic/utility.h> + +#include <functional> + +namespace NProtobufJson { template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant class TFilteringPrinter: public TBasePrinter { public: using TFieldPredicate = std::function<bool(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*)>; - + template <typename... TArgs> TFilteringPrinter(TFieldPredicate isPrinted, TArgs&&... args) : TBasePrinter(std::forward<TArgs>(args)...) , IsPrinted(std::move(isPrinted)) { } - + virtual void PrintField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, @@ -37,12 +37,12 @@ namespace NProtobufJson { inline void PrintWithFilter(const NProtoBuf::Message& msg, TFilteringPrinter<>::TFieldPredicate filter, IJsonOutput& output, const TProto2JsonConfig& config = TProto2JsonConfig()) { TFilteringPrinter<> printer(std::move(filter), config); printer.Print(msg, output); - } - + } + inline TString PrintWithFilter(const NProtoBuf::Message& msg, TFilteringPrinter<>::TFieldPredicate filter, const TProto2JsonConfig& config = TProto2JsonConfig()) { TString ret; PrintWithFilter(msg, std::move(filter), *CreateJsonMapOutput(ret, config), config); return ret; - } - -} + } + +} diff --git a/library/cpp/protobuf/json/inline.h b/library/cpp/protobuf/json/inline.h index e2d7bb6ef0..03a4db8101 100644 --- a/library/cpp/protobuf/json/inline.h +++ b/library/cpp/protobuf/json/inline.h @@ -23,10 +23,10 @@ // // 4) And then serialize it to json string with inlining, e.g.: // -// Cout << NProtobufJson::PrintInlined(o, MakeFieldOptionFunctor(this_is_json)) << Endl; -// -// 5) Alternatively you can specify a some more abstract functor for defining raw json fields +// Cout << NProtobufJson::PrintInlined(o, MakeFieldOptionFunctor(this_is_json)) << Endl; // +// 5) Alternatively you can specify a some more abstract functor for defining raw json fields +// // which will print following json to stdout: // {"A":{"inner":"value"}} // instead of @@ -35,17 +35,17 @@ // // See ut/inline_ut.cpp for additional examples of usage. -#include "config.h" -#include "proto2json_printer.h" -#include "json_output_create.h" +#include "config.h" +#include "proto2json_printer.h" +#include "json_output_create.h" #include <library/cpp/protobuf/util/simple_reflection.h> -#include <util/generic/maybe.h> -#include <util/generic/yexception.h> -#include <util/generic/utility.h> +#include <util/generic/maybe.h> +#include <util/generic/yexception.h> +#include <util/generic/utility.h> -#include <functional> +#include <functional> namespace NProtobufJson { template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant @@ -100,7 +100,7 @@ namespace NProtobufJson { private: TFieldPredicate IsInlined; }; - + inline void PrintInlined(const NProtoBuf::Message& msg, TInliningPrinter<>::TFieldPredicate isInlined, IJsonOutput& output, const TProto2JsonConfig& config = TProto2JsonConfig()) { TInliningPrinter<> printer(std::move(isInlined), config); printer.Print(msg, output); diff --git a/library/cpp/protobuf/json/json_output.h b/library/cpp/protobuf/json/json_output.h index df143af57a..2fc7934171 100644 --- a/library/cpp/protobuf/json/json_output.h +++ b/library/cpp/protobuf/json/json_output.h @@ -1,9 +1,9 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/strbuf.h> - -namespace NProtobufJson { +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/strbuf.h> + +namespace NProtobufJson { class IJsonOutput { public: template <typename T> @@ -15,7 +15,7 @@ namespace NProtobufJson { DoWriteNull(); return *this; } - + IJsonOutput& BeginList() { DoBeginList(); return *this; @@ -24,7 +24,7 @@ namespace NProtobufJson { DoEndList(); return *this; } - + IJsonOutput& BeginObject() { DoBeginObject(); return *this; @@ -37,15 +37,15 @@ namespace NProtobufJson { DoEndObject(); return *this; } - + IJsonOutput& WriteRawJson(const TStringBuf& str) { DoWriteRawJson(str); return *this; } - + virtual ~IJsonOutput() { } - + protected: virtual void DoWrite(const TStringBuf& s) = 0; virtual void DoWrite(const TString& s) = 0; @@ -63,17 +63,17 @@ namespace NProtobufJson { virtual void DoWrite(double f) = 0; virtual void DoWrite(bool b) = 0; virtual void DoWriteNull() = 0; - + virtual void DoBeginList() = 0; virtual void DoEndList() = 0; - + virtual void DoBeginObject() = 0; virtual void DoWriteKey(const TStringBuf& key) = 0; virtual void DoEndObject() = 0; - + virtual void DoWriteRawJson(const TStringBuf& str) = 0; }; - + using TJsonMapOutputPtr = THolder<IJsonOutput>; - + } diff --git a/library/cpp/protobuf/json/json_output_create.cpp b/library/cpp/protobuf/json/json_output_create.cpp index 378e4ea65a..0c1575d0ca 100644 --- a/library/cpp/protobuf/json/json_output_create.cpp +++ b/library/cpp/protobuf/json/json_output_create.cpp @@ -1,18 +1,18 @@ -#include "json_output_create.h" - -#include "config.h" -#include "json_writer_output.h" -#include "json_value_output.h" - -namespace NProtobufJson { +#include "json_output_create.h" + +#include "config.h" +#include "json_writer_output.h" +#include "json_value_output.h" + +namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const NJson::TJsonWriterConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonWriter& writer) { return MakeHolder<TBaseJsonWriterOutput>(writer); } - + TJsonMapOutputPtr CreateJsonMapOutput(TString& str, const TProto2JsonConfig& config) { return MakeHolder<TJsonStringWriterOutput>(&str, config); } @@ -20,13 +20,13 @@ namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(TStringStream& out, const TProto2JsonConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const TProto2JsonConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonValue& json) { return MakeHolder<TJsonValueOutput>(json); } - -} + +} diff --git a/library/cpp/protobuf/json/json_output_create.h b/library/cpp/protobuf/json/json_output_create.h index ad3889f5e9..9f7b195cc7 100644 --- a/library/cpp/protobuf/json/json_output_create.h +++ b/library/cpp/protobuf/json/json_output_create.h @@ -1,22 +1,22 @@ -#pragma once - -#include "config.h" -#include "json_output.h" - -namespace NJson { +#pragma once + +#include "config.h" +#include "json_output.h" + +namespace NJson { class TJsonValue; class TJsonWriter; struct TJsonWriterConfig; } - + class IOutputStream; -class TStringStream; - -namespace NProtobufJson { +class TStringStream; + +namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const NJson::TJsonWriterConfig& config); TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonWriter& writer); TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const TProto2JsonConfig& config = TProto2JsonConfig()); TJsonMapOutputPtr CreateJsonMapOutput(TString& str, const TProto2JsonConfig& config = TProto2JsonConfig()); TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonValue& json); - + } diff --git a/library/cpp/protobuf/json/json_value_output.cpp b/library/cpp/protobuf/json/json_value_output.cpp index d845cc1c74..33a0f3d339 100644 --- a/library/cpp/protobuf/json/json_value_output.cpp +++ b/library/cpp/protobuf/json/json_value_output.cpp @@ -1,12 +1,12 @@ #include "json_value_output.h" #include <library/cpp/json/json_reader.h> - -namespace NProtobufJson { + +namespace NProtobufJson { template <typename T> void TJsonValueOutput::WriteImpl(const T& t) { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { Context.top().Value = t; Context.pop(); @@ -14,23 +14,23 @@ namespace NProtobufJson { Context.top().Value.AppendValue(t); } } - + void TJsonValueOutput::DoWrite(const TStringBuf& s) { WriteImpl(s); } - + void TJsonValueOutput::DoWrite(const TString& s) { WriteImpl(s); } - + void TJsonValueOutput::DoWrite(int i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(unsigned int i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(long long i) { WriteImpl(i); } @@ -38,26 +38,26 @@ namespace NProtobufJson { void TJsonValueOutput::DoWrite(unsigned long long i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(float f) { WriteImpl(f); } - + void TJsonValueOutput::DoWrite(double f) { WriteImpl(f); } - + void TJsonValueOutput::DoWrite(bool b) { WriteImpl(b); } - + void TJsonValueOutput::DoWriteNull() { WriteImpl(NJson::JSON_NULL); } - + void TJsonValueOutput::DoBeginList() { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { Context.top().Type = TContext::JSON_ARRAY; Context.top().Value.SetType(NJson::JSON_ARRAY); @@ -65,15 +65,15 @@ namespace NProtobufJson { Context.emplace(TContext::JSON_ARRAY, Context.top().Value.AppendValue(NJson::JSON_ARRAY)); } } - + void TJsonValueOutput::DoEndList() { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY); Context.pop(); } - + void TJsonValueOutput::DoBeginObject() { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { Context.top().Type = TContext::JSON_MAP; Context.top().Value.SetType(NJson::JSON_MAP); @@ -81,20 +81,20 @@ namespace NProtobufJson { Context.emplace(TContext::JSON_MAP, Context.top().Value.AppendValue(NJson::JSON_MAP)); } } - + void TJsonValueOutput::DoWriteKey(const TStringBuf& key) { Y_ASSERT(Context.top().Type == TContext::JSON_MAP); Context.emplace(TContext::JSON_AFTER_KEY, Context.top().Value[key]); } - + void TJsonValueOutput::DoEndObject() { Y_ASSERT(Context.top().Type == TContext::JSON_MAP); Context.pop(); } - + void TJsonValueOutput::DoWriteRawJson(const TStringBuf& str) { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { NJson::ReadJsonTree(str, &Context.top().Value); Context.pop(); @@ -102,5 +102,5 @@ namespace NProtobufJson { NJson::ReadJsonTree(str, &Context.top().Value.AppendValue(NJson::JSON_UNDEFINED)); } } - -} + +} diff --git a/library/cpp/protobuf/json/json_value_output.h b/library/cpp/protobuf/json/json_value_output.h index 3fc6ff2ab0..8d71d36af4 100644 --- a/library/cpp/protobuf/json/json_value_output.h +++ b/library/cpp/protobuf/json/json_value_output.h @@ -1,12 +1,12 @@ -#pragma once - +#pragma once + #include "json_output.h" #include <library/cpp/json/writer/json_value.h> - + #include <util/generic/stack.h> -namespace NProtobufJson { +namespace NProtobufJson { class TJsonValueOutput: public IJsonOutput { public: TJsonValueOutput(NJson::TJsonValue& value) @@ -14,7 +14,7 @@ namespace NProtobufJson { { Context.emplace(TContext::JSON_AFTER_KEY, Root); } - + void DoWrite(const TStringBuf& s) override; void DoWrite(const TString& s) override; void DoWrite(int i) override; @@ -25,27 +25,27 @@ namespace NProtobufJson { void DoWrite(double f) override; void DoWrite(bool b) override; void DoWriteNull() override; - + void DoBeginList() override; void DoEndList() override; - + void DoBeginObject() override; void DoWriteKey(const TStringBuf& key) override; void DoEndObject() override; - + void DoWriteRawJson(const TStringBuf& str) override; - + private: template <typename T> void WriteImpl(const T& t); - + struct TContext { enum EType { JSON_MAP, JSON_ARRAY, JSON_AFTER_KEY, }; - + TContext(EType type, NJson::TJsonValue& value) : Type(type) , Value(value) @@ -55,9 +55,9 @@ namespace NProtobufJson { EType Type; NJson::TJsonValue& Value; }; - + NJson::TJsonValue& Root; TStack<TContext, TVector<TContext>> Context; }; - + } diff --git a/library/cpp/protobuf/json/json_writer_output.cpp b/library/cpp/protobuf/json/json_writer_output.cpp index 288f645bab..d6258eae1b 100644 --- a/library/cpp/protobuf/json/json_writer_output.cpp +++ b/library/cpp/protobuf/json/json_writer_output.cpp @@ -1,6 +1,6 @@ -#include "json_writer_output.h" - -namespace NProtobufJson { +#include "json_writer_output.h" + +namespace NProtobufJson { NJson::TJsonWriterConfig TJsonWriterOutput::CreateJsonWriterConfig(const TProto2JsonConfig& config) { NJson::TJsonWriterConfig jsonConfig; jsonConfig.FormatOutput = config.FormatOutput; @@ -8,15 +8,15 @@ namespace NProtobufJson { jsonConfig.ValidateUtf8 = false; jsonConfig.DontEscapeStrings = false; jsonConfig.WriteNanAsString = config.WriteNanAsString; - + for (size_t i = 0; i < config.StringTransforms.size(); ++i) { Y_ASSERT(config.StringTransforms[i]); if (config.StringTransforms[i]->GetType() == IStringTransform::EscapeTransform) { jsonConfig.DontEscapeStrings = true; break; } - } + } return jsonConfig; - } + } -} +} diff --git a/library/cpp/protobuf/json/json_writer_output.h b/library/cpp/protobuf/json/json_writer_output.h index 3d8a2daa56..a634d9d565 100644 --- a/library/cpp/protobuf/json/json_writer_output.h +++ b/library/cpp/protobuf/json/json_writer_output.h @@ -1,21 +1,21 @@ -#pragma once - -#include "json_output.h" -#include "config.h" - +#pragma once + +#include "json_output.h" +#include "config.h" + #include <library/cpp/json/json_writer.h> - + #include <util/string/builder.h> -#include <util/generic/store_policy.h> - -namespace NProtobufJson { +#include <util/generic/store_policy.h> + +namespace NProtobufJson { class TBaseJsonWriterOutput: public IJsonOutput { public: TBaseJsonWriterOutput(NJson::TJsonWriter& writer) : Writer(writer) { } - + private: void DoWrite(int i) override { Writer.Write(i); @@ -47,14 +47,14 @@ namespace NProtobufJson { void DoWrite(const TString& s) override { Writer.Write(s); } - + void DoBeginList() override { Writer.OpenArray(); } void DoEndList() override { Writer.CloseArray(); } - + void DoBeginObject() override { Writer.OpenMap(); } @@ -64,14 +64,14 @@ namespace NProtobufJson { void DoEndObject() override { Writer.CloseMap(); } - + void DoWriteRawJson(const TStringBuf& str) override { Writer.UnsafeWrite(str); } - + NJson::TJsonWriter& Writer; }; - + class TJsonWriterOutput: public TEmbedPolicy<NJson::TJsonWriter>, public TBaseJsonWriterOutput { public: TJsonWriterOutput(IOutputStream* outputStream, const NJson::TJsonWriterConfig& cfg) @@ -79,17 +79,17 @@ namespace NProtobufJson { , TBaseJsonWriterOutput(*Ptr()) { } - + TJsonWriterOutput(IOutputStream* outputStream, const TProto2JsonConfig& cfg) : TEmbedPolicy<NJson::TJsonWriter>(outputStream, CreateJsonWriterConfig(cfg)) , TBaseJsonWriterOutput(*Ptr()) { } - + private: static NJson::TJsonWriterConfig CreateJsonWriterConfig(const TProto2JsonConfig& cfg); }; - + class TJsonStringWriterOutput: public TEmbedPolicy<TStringOutput>, public TJsonWriterOutput { public: template <typename TConfig> @@ -99,5 +99,5 @@ namespace NProtobufJson { { } }; - + } diff --git a/library/cpp/protobuf/json/proto2json.cpp b/library/cpp/protobuf/json/proto2json.cpp index 3d76a91686..728b98d1f9 100644 --- a/library/cpp/protobuf/json/proto2json.cpp +++ b/library/cpp/protobuf/json/proto2json.cpp @@ -1,8 +1,8 @@ #include "proto2json.h" -#include "json_output_create.h" -#include "proto2json_printer.h" - +#include "json_output_create.h" +#include "proto2json_printer.h" + #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_value.h> #include <library/cpp/json/json_writer.h> @@ -13,7 +13,7 @@ #include <util/stream/str.h> #include <util/system/yassert.h> -namespace NProtobufJson { +namespace NProtobufJson { void Proto2Json(const NProtoBuf::Message& proto, IJsonOutput& jsonOutput, const TProto2JsonConfig& config, bool closeMap) { TProto2JsonPrinter printer(config); diff --git a/library/cpp/protobuf/json/proto2json.h b/library/cpp/protobuf/json/proto2json.h index 89a1781a40..191c406e5e 100644 --- a/library/cpp/protobuf/json/proto2json.h +++ b/library/cpp/protobuf/json/proto2json.h @@ -1,18 +1,18 @@ #pragma once -#include "config.h" -#include "json_output.h" +#include "config.h" +#include "json_output.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> - + #include <util/generic/fwd.h> #include <util/generic/vector.h> #include <util/generic/yexception.h> #include <util/stream/str.h> -#include <functional> +#include <functional> namespace NJson { class TJsonValue; diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 6123eab0f2..833a0d294b 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -1,14 +1,14 @@ -#include "proto2json_printer.h" -#include "config.h" +#include "proto2json_printer.h" +#include "config.h" #include "util.h" - -#include <util/generic/yexception.h> + +#include <util/generic/yexception.h> #include <util/string/ascii.h> #include <util/string/cast.h> - -namespace NProtobufJson { + +namespace NProtobufJson { using namespace NProtoBuf; - + class TJsonKeyBuilder { public: TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf) @@ -30,7 +30,7 @@ namespace NProtobufJson { NewKeyBuf = NewKeyStr; return; } - + switch (config.FieldNameMode) { case TProto2JsonConfig::FieldNameOriginalCase: { NewKeyBuf = field.name(); @@ -43,14 +43,14 @@ namespace NProtobufJson { NewKeyBuf = NewKeyStr; break; } - + case TProto2JsonConfig::FieldNameUpperCase: { NewKeyStr = field.name(); NewKeyStr.to_upper(); NewKeyBuf = NewKeyStr; break; } - + case TProto2JsonConfig::FieldNameCamelCase: { NewKeyStr = field.name(); if (!NewKeyStr.empty()) { @@ -59,7 +59,7 @@ namespace NProtobufJson { NewKeyBuf = NewKeyStr; break; } - + case TProto2JsonConfig::FieldNameSnakeCase: { NewKeyStr = field.name(); ToSnakeCase(&NewKeyStr); @@ -81,7 +81,7 @@ namespace NProtobufJson { const TStringBuf& GetKey() const { return NewKeyBuf; - } + } private: TStringBuf NewKeyBuf; @@ -91,26 +91,26 @@ namespace NProtobufJson { TProto2JsonPrinter::TProto2JsonPrinter(const TProto2JsonConfig& cfg) : Config(cfg) { - } - + } + TProto2JsonPrinter::~TProto2JsonPrinter() { - } - + } + TStringBuf TProto2JsonPrinter::MakeKey(const FieldDescriptor& field) { return TJsonKeyBuilder(field, GetConfig(), TmpBuf).GetKey(); } - + template <bool InMapContext, typename T> std::enable_if_t<InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& json, const TStringBuf& key, const T& value) { json.WriteKey(key).Write(value); } - + template <bool InMapContext, typename T> std::enable_if_t<!InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& array, const TStringBuf& key, const T& value) { Y_ASSERT(!key); array.Write(value); } - + template <bool InMapContext> void TProto2JsonPrinter::PrintStringValue(const FieldDescriptor& field, const TStringBuf& key, const TString& value, @@ -131,7 +131,7 @@ namespace NProtobufJson { WriteWithMaybeEmptyKey<InMapContext>(json, key, value); } } - + template <bool InMapContext> void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key, const EnumValueDescriptor* value, @@ -146,52 +146,52 @@ namespace NProtobufJson { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number()); break; } - + case TProto2JsonConfig::EnumName: { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->name()); break; } - + case TProto2JsonConfig::EnumFullName: { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->full_name()); break; - } - + } + case TProto2JsonConfig::EnumNameLowerCase: { TString newName = value->name(); newName.to_lower(); WriteWithMaybeEmptyKey<InMapContext>(json, key, newName); break; } - + case TProto2JsonConfig::EnumFullNameLowerCase: { TString newName = value->full_name(); newName.to_lower(); WriteWithMaybeEmptyKey<InMapContext>(json, key, newName); break; } - + default: Y_VERIFY_DEBUG(false, "Unknown EnumMode."); } - } - + } + void TProto2JsonPrinter::PrintSingleField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, TStringBuf key) { Y_VERIFY(!field.is_repeated(), "field is repeated."); - + if (!key) { key = MakeKey(field); } - + #define FIELD_TO_JSON(EProtoCppType, ProtoGet) \ case FieldDescriptor::EProtoCppType: { \ json.WriteKey(key).Write(reflection->ProtoGet(proto, &field)); \ break; \ - } - + } + #define INT_FIELD_TO_JSON(EProtoCppType, ProtoGet) \ case FieldDescriptor::EProtoCppType: { \ const auto value = reflection->ProtoGet(proto, &field); \ @@ -204,7 +204,7 @@ namespace NProtobufJson { } const Reflection* reflection = proto.GetReflection(); - + bool shouldPrintField = reflection->HasField(proto, &field); if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) { if (field.has_default_value()) { @@ -225,25 +225,25 @@ namespace NProtobufJson { FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble); FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat); FIELD_TO_JSON(CPPTYPE_BOOL, GetBool); - + case FieldDescriptor::CPPTYPE_MESSAGE: { json.WriteKey(key); Print(reflection->GetMessage(proto, &field), json); break; } - + case FieldDescriptor::CPPTYPE_ENUM: { PrintEnumValue<true>(key, reflection->GetEnum(proto, &field), json); break; } - + case FieldDescriptor::CPPTYPE_STRING: { TString scratch; const TString& value = reflection->GetStringReference(proto, &field, &scratch); PrintStringValue<true>(field, key, value, json); break; } - + default: ythrow yexception() << "Unknown protobuf field type: " << static_cast<int>(field.cpp_type()) << "."; @@ -254,36 +254,36 @@ namespace NProtobufJson { json.WriteKey(key).WriteNull(); break; } - + case TProto2JsonConfig::MissingKeySkip: case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired: default: break; } - } + } #undef FIELD_TO_JSON } - + void TProto2JsonPrinter::PrintRepeatedField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, TStringBuf key) { Y_VERIFY(field.is_repeated(), "field isn't repeated."); - + const bool isMap = field.is_map() && GetConfig().MapAsObject; if (!key) { key = MakeKey(field); - } - + } + #define REPEATED_FIELD_TO_JSON(EProtoCppType, ProtoGet) \ case FieldDescriptor::EProtoCppType: { \ - for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \ + for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \ json.Write(reflection->ProtoGet(proto, &field, i)); \ break; \ - } - + } + const Reflection* reflection = proto.GetReflection(); - + if (reflection->FieldSize(proto, &field) > 0) { json.WriteKey(key); if (isMap) { @@ -291,7 +291,7 @@ namespace NProtobufJson { } else { json.BeginList(); } - + switch (field.cpp_type()) { REPEATED_FIELD_TO_JSON(CPPTYPE_INT32, GetRepeatedInt32); REPEATED_FIELD_TO_JSON(CPPTYPE_INT64, GetRepeatedInt64); @@ -300,7 +300,7 @@ namespace NProtobufJson { REPEATED_FIELD_TO_JSON(CPPTYPE_DOUBLE, GetRepeatedDouble); REPEATED_FIELD_TO_JSON(CPPTYPE_FLOAT, GetRepeatedFloat); REPEATED_FIELD_TO_JSON(CPPTYPE_BOOL, GetRepeatedBool); - + case FieldDescriptor::CPPTYPE_MESSAGE: { if (isMap) { for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { @@ -334,7 +334,7 @@ namespace NProtobufJson { ythrow yexception() << "Unknown protobuf field type: " << static_cast<int>(field.cpp_type()) << "."; } - + if (isMap) { json.EndObject(); } else { @@ -346,7 +346,7 @@ namespace NProtobufJson { json.WriteKey(key).WriteNull(); break; } - + case TProto2JsonConfig::MissingKeyDefault: { json.WriteKey(key); if (isMap) { @@ -361,12 +361,12 @@ namespace NProtobufJson { case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired: default: break; - } - } - + } + } + #undef REPEATED_FIELD_TO_JSON } - + void TProto2JsonPrinter::PrintKeyValue(const NProtoBuf::Message& proto, IJsonOutput& json) { const FieldDescriptor* keyField = proto.GetDescriptor()->FindFieldByName("key"); @@ -376,7 +376,7 @@ namespace NProtobufJson { Y_VERIFY(valueField, "Map entry value field not found."); PrintField(proto, *valueField, json, key); } - + TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field) { const Reflection* reflection = proto.GetReflection(); @@ -433,11 +433,11 @@ namespace NProtobufJson { break; default: ythrow yexception() << "Unsupported key type."; - } - + } + return result; - } - + } + void TProto2JsonPrinter::PrintField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, @@ -449,13 +449,13 @@ namespace NProtobufJson { else PrintSingleField(proto, field, json, key); } - + void TProto2JsonPrinter::Print(const Message& proto, IJsonOutput& json, bool closeMap) { const Descriptor* descriptor = proto.GetDescriptor(); Y_ASSERT(descriptor); - + json.BeginObject(); - + // Iterate over all non-extension fields for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) { const FieldDescriptor* field = descriptor->field(f); @@ -485,8 +485,8 @@ namespace NProtobufJson { if (closeMap) { json.EndObject(); } - } - + } + template <class T, class U> std::enable_if_t<!std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) { return value >= -range && value <= range; @@ -514,4 +514,4 @@ namespace NProtobufJson { return false; } -} +} diff --git a/library/cpp/protobuf/json/proto2json_printer.h b/library/cpp/protobuf/json/proto2json_printer.h index 9dc5aa86c6..866fd8b545 100644 --- a/library/cpp/protobuf/json/proto2json_printer.h +++ b/library/cpp/protobuf/json/proto2json_printer.h @@ -1,62 +1,62 @@ -#pragma once - -#include "json_output.h" - +#pragma once + +#include "json_output.h" + #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> - -#include <util/generic/strbuf.h> + +#include <util/generic/strbuf.h> #include <util/generic/string.h> - -namespace NProtobufJson { + +namespace NProtobufJson { struct TProto2JsonConfig; - + class TProto2JsonPrinter { public: TProto2JsonPrinter(const TProto2JsonConfig& config); virtual ~TProto2JsonPrinter(); - + virtual void Print(const NProtoBuf::Message& proto, IJsonOutput& json, bool closeMap = true); - + virtual const TProto2JsonConfig& GetConfig() const { return Config; } - + protected: virtual TStringBuf MakeKey(const NProtoBuf::FieldDescriptor& field); - + virtual void PrintField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, TStringBuf key = {}); - + void PrintRepeatedField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, TStringBuf key = {}); - + void PrintSingleField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, TStringBuf key = {}); - + void PrintKeyValue(const NProtoBuf::Message& proto, IJsonOutput& json); - + TString MakeKey(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field); - + template <bool InMapContext> void PrintEnumValue(const TStringBuf& key, const NProtoBuf::EnumValueDescriptor* value, IJsonOutput& json); - + template <bool InMapContext> void PrintStringValue(const NProtoBuf::FieldDescriptor& field, const TStringBuf& key, const TString& value, IJsonOutput& json); - + template <class T> bool NeedStringifyNumber(T value) const; @@ -64,5 +64,5 @@ namespace NProtobufJson { const TProto2JsonConfig& Config; TString TmpBuf; }; - + } diff --git a/library/cpp/protobuf/json/ut/filter_ut.cpp b/library/cpp/protobuf/json/ut/filter_ut.cpp index 95c227666f..a5f17f0230 100644 --- a/library/cpp/protobuf/json/ut/filter_ut.cpp +++ b/library/cpp/protobuf/json/ut/filter_ut.cpp @@ -1,47 +1,47 @@ #include <library/cpp/protobuf/json/ut/filter_ut.pb.h> - + #include <library/cpp/protobuf/json/filter.h> #include <library/cpp/protobuf/json/field_option.h> #include <library/cpp/protobuf/json/proto2json.h> #include <library/cpp/testing/unittest/registar.h> - -using namespace NProtobufJson; - -static NProtobufJsonUt::TFilterTest GetTestMsg() { - NProtobufJsonUt::TFilterTest msg; - msg.SetOptFiltered("1"); - msg.SetNotFiltered("23"); - msg.AddRepFiltered(45); - msg.AddRepFiltered(67); - msg.MutableInner()->AddNumber(100); - msg.MutableInner()->AddNumber(200); - msg.MutableInner()->SetInnerFiltered(235); - return msg; -} - + +using namespace NProtobufJson; + +static NProtobufJsonUt::TFilterTest GetTestMsg() { + NProtobufJsonUt::TFilterTest msg; + msg.SetOptFiltered("1"); + msg.SetNotFiltered("23"); + msg.AddRepFiltered(45); + msg.AddRepFiltered(67); + msg.MutableInner()->AddNumber(100); + msg.MutableInner()->AddNumber(200); + msg.MutableInner()->SetInnerFiltered(235); + return msg; +} + Y_UNIT_TEST_SUITE(TProto2JsonFilterTest){ Y_UNIT_TEST(TestFilterPrinter){ - NProtobufJsonUt::TFilterTest msg = GetTestMsg(); + NProtobufJsonUt::TFilterTest msg = GetTestMsg(); { TString expected = R"({"OptFiltered":"1","NotFiltered":"23","RepFiltered":[45,67],)" R"("Inner":{"Number":[100,200],"InnerFiltered":235}})"; TString my = Proto2Json(msg); UNIT_ASSERT_STRINGS_EQUAL(my, expected); } - + { TString expected = R"({"NotFiltered":"23",)" R"("Inner":{"Number":[100,200]}})"; TString my = PrintWithFilter(msg, MakeFieldOptionFunctor(NProtobufJsonUt::filter_test, false)); UNIT_ASSERT_STRINGS_EQUAL(my, expected); } - + { TString expected = R"({"OptFiltered":"1","RepFiltered":[45,67]})"; TString my = PrintWithFilter(msg, MakeFieldOptionFunctor(NProtobufJsonUt::filter_test)); UNIT_ASSERT_STRINGS_EQUAL(my, expected); } - + { TString expected = R"({"OptFiltered":"1","NotFiltered":"23",)" R"("Inner":{"Number":[100,200]}})"; @@ -66,25 +66,25 @@ Y_UNIT_TEST(NoUnnecessaryCopyFunctor) { struct TFunctorMock { TFunctorMock(size_t* copyCount) : CopyCount(copyCount) - { + { UNIT_ASSERT(*CopyCount <= 1); - } - + } + TFunctorMock(const TFunctorMock& f) : CopyCount(f.CopyCount) - { + { ++*CopyCount; - } - + } + TFunctorMock(TFunctorMock&& f) = default; - + bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) const { return false; } - + size_t* CopyCount; }; - + TProto2JsonConfig cfg; TFilteringPrinter<> printer(TFunctorMock(&CopyCount), cfg); UNIT_ASSERT(CopyCount <= 1); diff --git a/library/cpp/protobuf/json/ut/filter_ut.proto b/library/cpp/protobuf/json/ut/filter_ut.proto index 29d630ade4..c4866ed813 100644 --- a/library/cpp/protobuf/json/ut/filter_ut.proto +++ b/library/cpp/protobuf/json/ut/filter_ut.proto @@ -1,20 +1,20 @@ import "google/protobuf/descriptor.proto"; - -package NProtobufJsonUt; - -extend google.protobuf.FieldOptions { - optional bool filter_test = 58255; - optional bool export_test = 58256; -} - -message TFilterTest { - optional string OptFiltered = 1 [(filter_test) = true, (export_test) = true]; - optional string NotFiltered = 2 [(export_test) = true]; - repeated uint64 RepFiltered = 3 [(filter_test) = true]; - - message TInner { - repeated uint32 Number = 1 [(export_test) = true]; - optional int32 InnerFiltered = 2 [(filter_test) = true]; - } - optional TInner Inner = 4 [(export_test) = true]; -} + +package NProtobufJsonUt; + +extend google.protobuf.FieldOptions { + optional bool filter_test = 58255; + optional bool export_test = 58256; +} + +message TFilterTest { + optional string OptFiltered = 1 [(filter_test) = true, (export_test) = true]; + optional string NotFiltered = 2 [(export_test) = true]; + repeated uint64 RepFiltered = 3 [(filter_test) = true]; + + message TInner { + repeated uint32 Number = 1 [(export_test) = true]; + optional int32 InnerFiltered = 2 [(filter_test) = true]; + } + optional TInner Inner = 4 [(export_test) = true]; +} diff --git a/library/cpp/protobuf/json/ut/inline_ut.cpp b/library/cpp/protobuf/json/ut/inline_ut.cpp index c29ad32e7d..048e3fa275 100644 --- a/library/cpp/protobuf/json/ut/inline_ut.cpp +++ b/library/cpp/protobuf/json/ut/inline_ut.cpp @@ -9,42 +9,42 @@ using namespace NProtobufJson; -static NProtobufJsonUt::TInlineTest GetTestMsg() { - NProtobufJsonUt::TInlineTest msg; - msg.SetOptJson(R"({"a":1,"b":"000"})"); - msg.SetNotJson("12{}34"); - msg.AddRepJson("{}"); - msg.AddRepJson("[1,2]"); - msg.MutableInner()->AddNumber(100); - msg.MutableInner()->AddNumber(200); - msg.MutableInner()->SetInnerJson(R"({"xxx":[]})"); - return msg; -} - +static NProtobufJsonUt::TInlineTest GetTestMsg() { + NProtobufJsonUt::TInlineTest msg; + msg.SetOptJson(R"({"a":1,"b":"000"})"); + msg.SetNotJson("12{}34"); + msg.AddRepJson("{}"); + msg.AddRepJson("[1,2]"); + msg.MutableInner()->AddNumber(100); + msg.MutableInner()->AddNumber(200); + msg.MutableInner()->SetInnerJson(R"({"xxx":[]})"); + return msg; +} + Y_UNIT_TEST_SUITE(TProto2JsonInlineTest){ Y_UNIT_TEST(TestNormalPrint){ - NProtobufJsonUt::TInlineTest msg = GetTestMsg(); + NProtobufJsonUt::TInlineTest msg = GetTestMsg(); // normal print should output these fields as just string values TString expRaw = R"({"OptJson":"{\"a\":1,\"b\":\"000\"}","NotJson":"12{}34","RepJson":["{}","[1,2]"],)" R"("Inner":{"Number":[100,200],"InnerJson":"{\"xxx\":[]}"}})"; TString myRaw; Proto2Json(msg, myRaw); UNIT_ASSERT_STRINGS_EQUAL(myRaw, expRaw); - + myRaw = PrintInlined(msg, [](const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) { return false; }); UNIT_ASSERT_STRINGS_EQUAL(myRaw, expRaw); // result is the same } - + Y_UNIT_TEST(TestInliningPrinter) { NProtobufJsonUt::TInlineTest msg = GetTestMsg(); // inlined print should output these fields as inlined json sub-objects TString expInlined = R"({"OptJson":{"a":1,"b":"000"},"NotJson":"12{}34","RepJson":[{},[1,2]],)" R"("Inner":{"Number":[100,200],"InnerJson":{"xxx":[]}}})"; - + { TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test)); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); - } + } { auto functor = [](const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) { return field->name() == "OptJson" || field->name() == "RepJson" || field->name() == "InnerJson"; @@ -53,7 +53,7 @@ Y_UNIT_TEST(TestInliningPrinter) { UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } } - + Y_UNIT_TEST(TestNoValues) { // no values - no printing NProtobufJsonUt::TInlineTest msg; @@ -61,35 +61,35 @@ Y_UNIT_TEST(TestNoValues) { msg.MutableInner()->AddNumber(200); TString expInlined = R"({"Inner":{"Number":[100,200]}})"; - + TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test)); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } - + Y_UNIT_TEST(TestMissingKeyModeNull) { NProtobufJsonUt::TInlineTest msg; msg.MutableInner()->AddNumber(100); msg.MutableInner()->AddNumber(200); - + TString expInlined = R"({"OptJson":null,"NotJson":null,"RepJson":null,"Inner":{"Number":[100,200],"InnerJson":null}})"; - + TProto2JsonConfig cfg; cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyNull).SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeyNull); TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test), cfg); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } - + Y_UNIT_TEST(TestMissingKeyModeDefault) { NProtobufJsonUt::TInlineTestDefaultValues msg; - + TString expInlined = R"({"OptJson":{"default":1},"Number":0,"RepJson":[],"Inner":{"OptJson":{"default":2}}})"; - + TProto2JsonConfig cfg; cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault).SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeyDefault); TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test), cfg); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } - + Y_UNIT_TEST(NoUnnecessaryCopyFunctor) { size_t CopyCount = 0; struct TFunctorMock { @@ -98,22 +98,22 @@ Y_UNIT_TEST(NoUnnecessaryCopyFunctor) { { UNIT_ASSERT(*CopyCount <= 1); } - + TFunctorMock(const TFunctorMock& f) : CopyCount(f.CopyCount) { ++*CopyCount; } - + TFunctorMock(TFunctorMock&& f) = default; - + bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) const { return false; } - + size_t* CopyCount; }; - + TProto2JsonConfig cfg; TInliningPrinter<> printer(TFunctorMock(&CopyCount), cfg); UNIT_ASSERT(CopyCount <= 1); diff --git a/library/cpp/protobuf/json/ut/inline_ut.proto b/library/cpp/protobuf/json/ut/inline_ut.proto index 76bd10232d..147c3369dd 100644 --- a/library/cpp/protobuf/json/ut/inline_ut.proto +++ b/library/cpp/protobuf/json/ut/inline_ut.proto @@ -17,13 +17,13 @@ message TInlineTest { } optional TInner Inner = 4; } - -message TInlineTestDefaultValues { - optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":1}"]; - optional uint32 Number = 2; - repeated string RepJson = 3 [(inline_test) = true]; - message TInner { - optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":2}"]; - } - optional TInner Inner = 4; -} + +message TInlineTestDefaultValues { + optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":1}"]; + optional uint32 Number = 2; + repeated string RepJson = 3 [(inline_test) = true]; + message TInner { + optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":2}"]; + } + optional TInner Inner = 4; +} diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..9dbec774e2 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -20,8 +20,8 @@ #include <util/system/defaults.h> #include <util/system/yassert.h> -#include <limits> - +#include <limits> + using namespace NProtobufJson; using namespace NProtobufJsonTest; @@ -887,18 +887,18 @@ Y_UNIT_TEST(TestFieldNameMode) { /// TODO: test missing keys } // TestFieldNameMode - + Y_UNIT_TEST(TestNan) { TFlatOptional proto; proto.SetDouble(std::numeric_limits<double>::quiet_NaN()); - + UNIT_ASSERT_EXCEPTION(Proto2Json(proto, TProto2JsonConfig()), yexception); } // TestNan - + Y_UNIT_TEST(TestInf) { TFlatOptional proto; proto.SetFloat(std::numeric_limits<float>::infinity()); - + UNIT_ASSERT_EXCEPTION(Proto2Json(proto, TProto2JsonConfig()), yexception); } // TestInf diff --git a/library/cpp/protobuf/json/ut/ya.make b/library/cpp/protobuf/json/ut/ya.make index b60a6d3c17..ac6aebeab8 100644 --- a/library/cpp/protobuf/json/ut/ya.make +++ b/library/cpp/protobuf/json/ut/ya.make @@ -3,14 +3,14 @@ UNITTEST_FOR(library/cpp/protobuf/json) OWNER(avitella) SRCS( - filter_ut.cpp + filter_ut.cpp json2proto_ut.cpp proto2json_ut.cpp inline_ut.proto inline_ut.cpp string_transform_ut.cpp - filter_ut.proto - test.proto + filter_ut.proto + test.proto util_ut.cpp ) diff --git a/library/cpp/protobuf/json/ya.make b/library/cpp/protobuf/json/ya.make index 2f2c75cfdb..b8e3f1a33c 100644 --- a/library/cpp/protobuf/json/ya.make +++ b/library/cpp/protobuf/json/ya.make @@ -4,9 +4,9 @@ OWNER(avitella) SRCS( json2proto.cpp - json_output_create.cpp - json_value_output.cpp - json_writer_output.cpp + json_output_create.cpp + json_value_output.cpp + json_writer_output.cpp name_generator.cpp proto2json.cpp proto2json_printer.cpp diff --git a/library/cpp/protobuf/util/cast.h b/library/cpp/protobuf/util/cast.h index 83749dfcee..d368d13766 100644 --- a/library/cpp/protobuf/util/cast.h +++ b/library/cpp/protobuf/util/cast.h @@ -1,11 +1,11 @@ #pragma once -#include "traits.h" +#include "traits.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> -#include <util/generic/cast.h> +#include <util/generic/cast.h> namespace NProtoBuf { // C++ compatible conversions of FieldDescriptor::CppType's diff --git a/library/cpp/protobuf/util/path.cpp b/library/cpp/protobuf/util/path.cpp index efa2a42c8a..8a9c2ba7d7 100644 --- a/library/cpp/protobuf/util/path.cpp +++ b/library/cpp/protobuf/util/path.cpp @@ -1,20 +1,20 @@ -#include "path.h" - -#include <util/generic/yexception.h> - -namespace NProtoBuf { +#include "path.h" + +#include <util/generic/yexception.h> + +namespace NProtoBuf { TFieldPath::TFieldPath() { } - + TFieldPath::TFieldPath(const Descriptor* msgType, const TStringBuf& path) { Init(msgType, path); } - + TFieldPath::TFieldPath(const TVector<const FieldDescriptor*>& path) : Path(path) { } - + bool TFieldPath::InitUnsafe(const Descriptor* msgType, TStringBuf path) { Path.clear(); while (path) { @@ -23,10 +23,10 @@ namespace NProtoBuf { next = path.NextTok('/'); if (!next) return true; - + if (!msgType) // need field but no message type return false; - + TString nextStr(next); const FieldDescriptor* field = msgType->FindFieldByName(nextStr); if (!field) { @@ -41,21 +41,21 @@ namespace NProtoBuf { return false; // ambiguity field = ext; } - } - } - + } + } + if (!field) return false; - + Path.push_back(field); msgType = field->type() == FieldDescriptor::TYPE_MESSAGE ? field->message_type() : nullptr; } return true; - } - + } + void TFieldPath::Init(const Descriptor* msgType, const TStringBuf& path) { if (!InitUnsafe(msgType, path)) ythrow yexception() << "Failed to resolve path \"" << path << "\" relative to " << msgType->full_name(); } -} +} diff --git a/library/cpp/protobuf/util/path.h b/library/cpp/protobuf/util/path.h index 487f643a2d..4fbee86f26 100644 --- a/library/cpp/protobuf/util/path.h +++ b/library/cpp/protobuf/util/path.h @@ -1,11 +1,11 @@ -#pragma once - +#pragma once + #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> - -#include <util/generic/vector.h> - -namespace NProtoBuf { + +#include <util/generic/vector.h> + +namespace NProtoBuf { class TFieldPath { public: TFieldPath(); @@ -13,40 +13,40 @@ namespace NProtoBuf { TFieldPath(const TVector<const FieldDescriptor*>& path); TFieldPath(const TFieldPath&) = default; TFieldPath& operator=(const TFieldPath&) = default; - + bool InitUnsafe(const Descriptor* msgType, const TStringBuf path); // noexcept void Init(const Descriptor* msgType, const TStringBuf& path); // throws - + const TVector<const FieldDescriptor*>& Fields() const { return Path; } - + void AddField(const FieldDescriptor* field) { Path.push_back(field); } - + const Descriptor* ParentType() const { return Empty() ? nullptr : Path.front()->containing_type(); } - + const FieldDescriptor* FieldDescr() const { return Empty() ? nullptr : Path.back(); } - + bool Empty() const { return Path.empty(); } - + explicit operator bool() const { return !Empty(); } - + bool operator!() const { return Empty(); } - + private: TVector<const FieldDescriptor*> Path; }; - + } diff --git a/library/cpp/protobuf/util/proto/ya.make b/library/cpp/protobuf/util/proto/ya.make index 4d68047d8b..3a3d58e486 100644 --- a/library/cpp/protobuf/util/proto/ya.make +++ b/library/cpp/protobuf/util/proto/ya.make @@ -1,11 +1,11 @@ -PROTO_LIBRARY() - -OWNER(mowgli) - -SRCS( - merge.proto -) - +PROTO_LIBRARY() + +OWNER(mowgli) + +SRCS( + merge.proto +) + EXCLUDE_TAGS(GO_PROTO) -END() +END() diff --git a/library/cpp/protobuf/util/repeated_field_utils.h b/library/cpp/protobuf/util/repeated_field_utils.h index c07bd84647..8f7428b5dc 100644 --- a/library/cpp/protobuf/util/repeated_field_utils.h +++ b/library/cpp/protobuf/util/repeated_field_utils.h @@ -38,8 +38,8 @@ namespace NProtoBuf { T* ret = field->Add(); MoveRepeatedFieldItem(field, field->size() - 1, index); return ret; - } - + } + template <typename TRepeated> // suitable both for RepeatedField and RepeatedPtrField static void RemoveRepeatedFieldItem(TRepeated* field, size_t index) { if ((int)index >= field->size()) @@ -70,8 +70,8 @@ namespace NProtoBuf { for (int i = begIndex; i < endIndex; ++i, ++shiftIndex) field->SwapElements(shiftIndex, i); } - } - + } + // Remove several items at once, could be more efficient compared to calling RemoveRepeatedFieldItem several times template <typename TRepeated> static void RemoveRepeatedFieldItems(TRepeated* field, const TVector<size_t>& sortedIndices) { diff --git a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp index 58aaaa9e12..94a494e1a3 100644 --- a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp +++ b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp @@ -1,46 +1,46 @@ -#include "repeated_field_utils.h" +#include "repeated_field_utils.h" #include <library/cpp/protobuf/util/ut/common_ut.pb.h> - + #include <library/cpp/testing/unittest/registar.h> - -using namespace NProtoBuf; - + +using namespace NProtoBuf; + Y_UNIT_TEST_SUITE(RepeatedFieldUtils) { Y_UNIT_TEST(RemoveIf) { - { - NProtobufUtilUt::TWalkTest msg; - msg.AddRepInt(0); - msg.AddRepInt(1); - msg.AddRepInt(2); - msg.AddRepInt(3); - msg.AddRepInt(4); - msg.AddRepInt(5); - auto cond = [](ui32 val) { - return val % 2 == 0; - }; - RemoveRepeatedFieldItemIf(msg.MutableRepInt(), cond); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepIntSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepInt(1)); - UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepInt(2)); - } - - { - NProtobufUtilUt::TWalkTest msg; - msg.AddRepSub()->SetOptInt(0); - msg.AddRepSub()->SetOptInt(1); - msg.AddRepSub()->SetOptInt(2); - msg.AddRepSub()->SetOptInt(3); - msg.AddRepSub()->SetOptInt(4); - msg.AddRepSub()->SetOptInt(5); - auto cond = [](const NProtobufUtilUt::TWalkTest& val) { - return val.GetOptInt() % 2 == 0; - }; - RemoveRepeatedFieldItemIf(msg.MutableRepSub(), cond); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepSubSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepSub(0).GetOptInt()); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepSub(1).GetOptInt()); - UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepSub(2).GetOptInt()); - } - } -} + { + NProtobufUtilUt::TWalkTest msg; + msg.AddRepInt(0); + msg.AddRepInt(1); + msg.AddRepInt(2); + msg.AddRepInt(3); + msg.AddRepInt(4); + msg.AddRepInt(5); + auto cond = [](ui32 val) { + return val % 2 == 0; + }; + RemoveRepeatedFieldItemIf(msg.MutableRepInt(), cond); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepIntSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepInt(1)); + UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepInt(2)); + } + + { + NProtobufUtilUt::TWalkTest msg; + msg.AddRepSub()->SetOptInt(0); + msg.AddRepSub()->SetOptInt(1); + msg.AddRepSub()->SetOptInt(2); + msg.AddRepSub()->SetOptInt(3); + msg.AddRepSub()->SetOptInt(4); + msg.AddRepSub()->SetOptInt(5); + auto cond = [](const NProtobufUtilUt::TWalkTest& val) { + return val.GetOptInt() % 2 == 0; + }; + RemoveRepeatedFieldItemIf(msg.MutableRepSub(), cond); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepSubSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepSub(0).GetOptInt()); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepSub(1).GetOptInt()); + UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepSub(2).GetOptInt()); + } + } +} diff --git a/library/cpp/protobuf/util/simple_reflection.cpp b/library/cpp/protobuf/util/simple_reflection.cpp index d842e9ee44..83afa0ce0c 100644 --- a/library/cpp/protobuf/util/simple_reflection.cpp +++ b/library/cpp/protobuf/util/simple_reflection.cpp @@ -1,10 +1,10 @@ -#include "simple_reflection.h" - -namespace NProtoBuf { +#include "simple_reflection.h" + +namespace NProtoBuf { const Message* GetMessageHelper(const TConstField& curField, bool) { return curField.HasValue() && curField.IsMessage() ? curField.Get<Message>() : nullptr; } - + Message* GetMessageHelper(TMutableField& curField, bool createPath) { if (curField.IsMessage()) { if (!curField.HasValue()) { @@ -13,14 +13,14 @@ namespace NProtoBuf { } else { return curField.MutableMessage(); } - } + } return nullptr; - } - + } + template <class TField, class TMsg> TMaybe<TField> ByPathImpl(TMsg& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath) { if (fieldsPath.empty()) - return TMaybe<TField>(); + return TMaybe<TField>(); TMsg* curParent = &msg; for (size_t i = 0, size = fieldsPath.size(); i < size; ++i) { const FieldDescriptor* field = fieldsPath[i]; @@ -35,36 +35,36 @@ namespace NProtoBuf { return TField(*curParent, fieldsPath.back()); else return TMaybe<TField>(); - } - + } + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TVector<const FieldDescriptor*>& fieldsPath) { return ByPathImpl<TConstField, const Message>(msg, fieldsPath, false); } - + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TStringBuf& path) { TFieldPath fieldPath; if (!fieldPath.InitUnsafe(msg.GetDescriptor(), path)) return TMaybe<TConstField>(); return ByPathImpl<TConstField, const Message>(msg, fieldPath.Fields(), false); } - + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TFieldPath& path) { return ByPathImpl<TConstField, const Message>(msg, path.Fields(), false); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath) { return ByPathImpl<TMutableField, Message>(msg, fieldsPath, createPath); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TStringBuf& path, bool createPath) { TFieldPath fieldPath; if (!fieldPath.InitUnsafe(msg.GetDescriptor(), path)) return TMaybe<TMutableField>(); return ByPathImpl<TMutableField, Message>(msg, fieldPath.Fields(), createPath); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TFieldPath& path, bool createPath) { return ByPathImpl<TMutableField, Message>(msg, path.Fields(), createPath); } -} +} diff --git a/library/cpp/protobuf/util/simple_reflection.h b/library/cpp/protobuf/util/simple_reflection.h index 61e877a787..a5dd46ac79 100644 --- a/library/cpp/protobuf/util/simple_reflection.h +++ b/library/cpp/protobuf/util/simple_reflection.h @@ -1,17 +1,17 @@ #pragma once -#include "cast.h" -#include "path.h" +#include "cast.h" +#include "path.h" #include "traits.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> -#include <util/generic/maybe.h> -#include <util/generic/typetraits.h> -#include <util/generic/vector.h> -#include <util/system/defaults.h> - +#include <util/generic/maybe.h> +#include <util/generic/typetraits.h> +#include <util/generic/vector.h> +#include <util/system/defaults.h> + namespace NProtoBuf { class TConstField { public: @@ -29,7 +29,7 @@ namespace NProtoBuf { const Message& Parent() const { return Msg; } - + const FieldDescriptor* Field() const { return Fd; } @@ -80,7 +80,7 @@ namespace NProtoBuf { bool IsMessage() const { return CppType() == FieldDescriptor::CPPTYPE_MESSAGE; } - + bool HasSameType(const TConstField& other) const { if (CppType() != other.CppType()) return false; @@ -90,7 +90,7 @@ namespace NProtoBuf { return false; return true; } - + protected: bool IsRepeated() const { return Fd->is_repeated(); @@ -137,7 +137,7 @@ namespace NProtoBuf { template <typename T> inline void Add(T value); - + inline void MergeFrom(const TConstField& src); inline void Clear() { @@ -167,17 +167,17 @@ namespace NProtoBuf { return; Refl().SwapElements(Mut(), Fd, index1, index2); } - + inline void Remove(size_t index) { if (index >= Size()) return; - + // Move to the end for (size_t i = index, size = Size(); i < size - 1; ++i) SwapElements(i, i + 1); RemoveLast(); } - + Message* MutableMessage(size_t index = 0) { Y_ASSERT(IsMessage()); if (IsRepeated()) { @@ -193,12 +193,12 @@ namespace NProtoBuf { inline TMsg* AddMessage() { return CheckedCast<TMsg*>(AddMessage()); } - + inline Message* AddMessage() { Y_ASSERT(IsMessage() && IsRepeated()); return Refl().AddMessage(Mut(), Fd); } - + private: Message* Mut() { return const_cast<Message*>(&Msg); diff --git a/library/cpp/protobuf/util/simple_reflection_ut.cpp b/library/cpp/protobuf/util/simple_reflection_ut.cpp index 169d4703c9..e380991c02 100644 --- a/library/cpp/protobuf/util/simple_reflection_ut.cpp +++ b/library/cpp/protobuf/util/simple_reflection_ut.cpp @@ -7,8 +7,8 @@ using namespace NProtoBuf; Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { - static TSample GenSampleForMergeFrom() { - TSample smf; + static TSample GenSampleForMergeFrom() { + TSample smf; smf.SetOneStr("one str"); smf.MutableOneMsg()->AddRepInt(1); smf.AddRepMsg()->AddRepInt(2); @@ -20,8 +20,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromGeneric) { - const TSample src(GenSampleForMergeFrom()); - TSample dst; + const TSample src(GenSampleForMergeFrom()); + TSample dst; const Descriptor* descr = dst.GetDescriptor(); { @@ -52,8 +52,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromSelf) { - const TSample sample(GenSampleForMergeFrom()); - TSample msg(sample); + const TSample sample(GenSampleForMergeFrom()); + TSample msg(sample); const Descriptor* descr = msg.GetDescriptor(); TMutableField oneStr(msg, descr->FindFieldByName("OneStr")); @@ -66,8 +66,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromAnotherFD) { - const TSample sample(GenSampleForMergeFrom()); - TSample msg(GenSampleForMergeFrom()); + const TSample sample(GenSampleForMergeFrom()); + TSample msg(GenSampleForMergeFrom()); const Descriptor* descr = msg.GetDescriptor(); { // string @@ -95,205 +95,205 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { UNIT_ASSERT_VALUES_EQUAL(msg.RepMsgSize(), sample.RepMsgSize() + 1); } } - + Y_UNIT_TEST(RemoveByIndex) { - TSample msg; - - const Descriptor* descr = msg.GetDescriptor(); - { - TMutableField fld(msg, descr->FindFieldByName("RepMsg")); - msg.AddRepMsg()->AddRepInt(1); - msg.AddRepMsg()->AddRepInt(2); - msg.AddRepMsg()->AddRepInt(3); - - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 2, 3 + TSample msg; + + const Descriptor* descr = msg.GetDescriptor(); + { + TMutableField fld(msg, descr->FindFieldByName("RepMsg")); + msg.AddRepMsg()->AddRepInt(1); + msg.AddRepMsg()->AddRepInt(2); + msg.AddRepMsg()->AddRepInt(3); + + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 2, 3 fld.Remove(1); // from middle - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); - - msg.AddRepMsg()->AddRepInt(5); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 3, 5 + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); + + msg.AddRepMsg()->AddRepInt(5); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 3, 5 fld.Remove(2); // from end - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); - msg.ClearRepMsg(); - } - - { - TMutableField fld(msg, descr->FindFieldByName("RepStr")); - msg.AddRepStr("1"); - msg.AddRepStr("2"); - msg.AddRepStr("3"); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepStrSize()); // "1", "2", "3" + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); + msg.ClearRepMsg(); + } + + { + TMutableField fld(msg, descr->FindFieldByName("RepStr")); + msg.AddRepStr("1"); + msg.AddRepStr("2"); + msg.AddRepStr("3"); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepStrSize()); // "1", "2", "3" fld.Remove(0); // from begin - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepStrSize()); - UNIT_ASSERT_VALUES_EQUAL("2", msg.GetRepStr(0)); - UNIT_ASSERT_VALUES_EQUAL("3", msg.GetRepStr(1)); - } - - { - TMutableField fld(msg, descr->FindFieldByName("OneStr")); - msg.SetOneStr("1"); - UNIT_ASSERT(msg.HasOneStr()); - fld.Remove(0); // not repeated - UNIT_ASSERT(!msg.HasOneStr()); - } - } - + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepStrSize()); + UNIT_ASSERT_VALUES_EQUAL("2", msg.GetRepStr(0)); + UNIT_ASSERT_VALUES_EQUAL("3", msg.GetRepStr(1)); + } + + { + TMutableField fld(msg, descr->FindFieldByName("OneStr")); + msg.SetOneStr("1"); + UNIT_ASSERT(msg.HasOneStr()); + fld.Remove(0); // not repeated + UNIT_ASSERT(!msg.HasOneStr()); + } + } + Y_UNIT_TEST(GetFieldByPath) { - // Simple get by path - { - TSample msg; - msg.SetOneStr("1"); - msg.MutableOneMsg()->AddRepInt(2); - msg.MutableOneMsg()->AddRepInt(3); - msg.AddRepMsg()->AddRepInt(4); - msg.MutableRepMsg(0)->AddRepInt(5); - msg.AddRepMsg()->AddRepInt(6); - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + // Simple get by path + { + TSample msg; + msg.SetOneStr("1"); + msg.MutableOneMsg()->AddRepInt(2); + msg.MutableOneMsg()->AddRepInt(3); + msg.AddRepMsg()->AddRepInt(4); + msg.MutableRepMsg(0)->AddRepInt(5); + msg.AddRepMsg()->AddRepInt(6); + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); UNIT_ASSERT_VALUES_EQUAL("1", (field->Get<TString>())); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT(field->IsMessageInstance<TInnerSample>()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "/OneMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT(field->IsMessageInstance<TInnerSample>()); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "/OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>(0)); - UNIT_ASSERT_VALUES_EQUAL(3, field->Get<int>(1)); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); + UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>(0)); + UNIT_ASSERT_VALUES_EQUAL(3, field->Get<int>(1)); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); - UNIT_ASSERT_VALUES_EQUAL(4, field->Get<int>(0)); - UNIT_ASSERT_VALUES_EQUAL(5, field->Get<int>(1)); - } - } - - // get of unset fields - { - TSample msg; - msg.MutableOneMsg(); - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); + UNIT_ASSERT_VALUES_EQUAL(4, field->Get<int>(0)); + UNIT_ASSERT_VALUES_EQUAL(5, field->Get<int>(1)); + } + } + + // get of unset fields + { + TSample msg; + msg.MutableOneMsg(); + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg/RepInt"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(!field); - } - } - - // mutable - { - TSample msg; - msg.MutableOneMsg(); - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(!field); + } + } + + // mutable + { + TSample msg; + msg.MutableOneMsg(); + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); field->Set(TString("zz")); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL("zz", msg.GetOneStr()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL("zz", msg.GetOneStr()); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); field->Set(TString("dd")); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL("dd", msg.GetOneStr()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL("dd", msg.GetOneStr()); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - field->Add(10); - UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(!field); - } - } - - // mutable with path creation - { - TSample msg; - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr", true); - UNIT_ASSERT(field); + field->Add(10); + UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(!field); + } + } + + // mutable with path creation + { + TSample msg; + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr", true); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt", true); - UNIT_ASSERT(field); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt", true); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - UNIT_ASSERT(msg.HasOneMsg()); - field->Add(10); - UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt", true); - TMaybe<TMutableField> fieldCopy = TMutableField::ByPath(msg, "RepMsg/RepInt", true); + UNIT_ASSERT(msg.HasOneMsg()); + field->Add(10); + UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt", true); + TMaybe<TMutableField> fieldCopy = TMutableField::ByPath(msg, "RepMsg/RepInt", true); Y_UNUSED(fieldCopy); - UNIT_ASSERT(field); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.RepMsgSize()); - field->Add(12); - UNIT_ASSERT_VALUES_EQUAL(12, field->Get<int>()); - } - } - - // error - { + UNIT_ASSERT_VALUES_EQUAL(1, msg.RepMsgSize()); + field->Add(12); + UNIT_ASSERT_VALUES_EQUAL(12, field->Get<int>()); + } + } + + // error + { {TSample msg; UNIT_ASSERT(!TConstField::ByPath(msg, "SomeField")); } - + { TSample msg; UNIT_ASSERT(!TMutableField::ByPath(msg, "SomeField/FieldSome")); } - + { TSample msg; UNIT_ASSERT(!TMutableField::ByPath(msg, "SomeField/FieldSome", true)); } } - + // extension { TSample msg; @@ -303,13 +303,13 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { TInnerSample* subMsg = msg.MutableExtension(NExt::SubMsgExt); subMsg->AddRepInt(20); subMsg->SetExtension(NExt::Ext3Field, 54); - + { TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.TTestExt.ExtField"); UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); UNIT_ASSERT_VALUES_EQUAL("ext", field->Get<TString>()); - } + } { TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.ExtField"); UNIT_ASSERT(field); diff --git a/library/cpp/protobuf/util/traits.h b/library/cpp/protobuf/util/traits.h index 50f036d0ea..01b49e4184 100644 --- a/library/cpp/protobuf/util/traits.h +++ b/library/cpp/protobuf/util/traits.h @@ -177,7 +177,7 @@ namespace NProtoBuf { static inline T GetDefault(const FieldDescriptor* field) { return TBaseTraits::GetDefault(field); } - + static inline bool Has(const Message& msg, const FieldDescriptor* field) { return TBaseTraits::Has(msg, field); } diff --git a/library/cpp/protobuf/util/ut/extensions.proto b/library/cpp/protobuf/util/ut/extensions.proto index 4944f0f5ca..0ef6a6fec2 100644 --- a/library/cpp/protobuf/util/ut/extensions.proto +++ b/library/cpp/protobuf/util/ut/extensions.proto @@ -1,22 +1,22 @@ -package NExt; - +package NExt; + import "library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto"; - -message TTestExt { - extend TSample { - optional string ExtField = 100; - } -} - -extend TSample { - optional uint64 ExtField = 150; // the same name, but another full name -} - -extend TSample { - repeated uint64 Ext2Field = 105; - optional TInnerSample SubMsgExt = 111; -} - -extend TInnerSample { - optional uint64 Ext3Field = 100; -} + +message TTestExt { + extend TSample { + optional string ExtField = 100; + } +} + +extend TSample { + optional uint64 ExtField = 150; // the same name, but another full name +} + +extend TSample { + repeated uint64 Ext2Field = 105; + optional TInnerSample SubMsgExt = 111; +} + +extend TInnerSample { + optional uint64 Ext3Field = 100; +} diff --git a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto index cca1dd869a..88e4f0f877 100644 --- a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto +++ b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto @@ -1,16 +1,16 @@ -message TInnerSample { +message TInnerSample { repeated int32 RepInt = 1; - - extensions 100 to 199; + + extensions 100 to 199; } -message TSample { +message TSample { optional string OneStr = 1; - optional TInnerSample OneMsg = 2; - repeated TInnerSample RepMsg = 3; + optional TInnerSample OneMsg = 2; + repeated TInnerSample RepMsg = 3; repeated string RepStr = 4; optional string AnotherOneStr = 5; - + optional int32 OneInt = 6; repeated int32 RepInt = 7; @@ -21,5 +21,5 @@ message TSample { optional EEnum OneEnum = 8; repeated EEnum RepEnum = 9; - extensions 100 to 199; + extensions 100 to 199; } diff --git a/library/cpp/protobuf/util/ut/ya.make b/library/cpp/protobuf/util/ut/ya.make index 701ba9a8c8..dd850af6cb 100644 --- a/library/cpp/protobuf/util/ut/ya.make +++ b/library/cpp/protobuf/util/ut/ya.make @@ -3,16 +3,16 @@ OWNER(nga) UNITTEST_FOR(library/cpp/protobuf/util) SRCS( - extensions.proto + extensions.proto sample_for_is_equal.proto sample_for_simple_reflection.proto common_ut.proto pb_io_ut.cpp - is_equal_ut.cpp + is_equal_ut.cpp iterators_ut.cpp - simple_reflection_ut.cpp - repeated_field_utils_ut.cpp - walk_ut.cpp + simple_reflection_ut.cpp + repeated_field_utils_ut.cpp + walk_ut.cpp merge_ut.cpp ) diff --git a/library/cpp/protobuf/util/walk.h b/library/cpp/protobuf/util/walk.h index d15d76562d..f5559fd907 100644 --- a/library/cpp/protobuf/util/walk.h +++ b/library/cpp/protobuf/util/walk.h @@ -30,4 +30,4 @@ namespace NProtoBuf { // Returned bool defines if we should walk down deeper to current node children (true), or not (false) void WalkSchema(const Descriptor* descriptor, std::function<bool(const FieldDescriptor*)> onField); -} +} diff --git a/library/cpp/protobuf/util/ya.make b/library/cpp/protobuf/util/ya.make index b62028af58..6f0299b76b 100644 --- a/library/cpp/protobuf/util/ya.make +++ b/library/cpp/protobuf/util/ya.make @@ -10,14 +10,14 @@ PEERDIR( ) SRCS( - is_equal.cpp + is_equal.cpp iterators.h merge.cpp - path.cpp - pb_io.cpp + path.cpp + pb_io.cpp pb_utils.h repeated_field_utils.h - simple_reflection.cpp + simple_reflection.cpp walk.cpp ) |