diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/protobuf/json/inline.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf/json/inline.h')
-rw-r--r-- | library/cpp/protobuf/json/inline.h | 110 |
1 files changed, 55 insertions, 55 deletions
diff --git a/library/cpp/protobuf/json/inline.h b/library/cpp/protobuf/json/inline.h index e2d7bb6ef0..e5b0980227 100644 --- a/library/cpp/protobuf/json/inline.h +++ b/library/cpp/protobuf/json/inline.h @@ -48,68 +48,68 @@ #include <functional> namespace NProtobufJson { - template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant - class TInliningPrinter: public TBasePrinter { - public: - using TFieldPredicate = std::function<bool(const NProtoBuf::Message&, - const NProtoBuf::FieldDescriptor*)>; - - template <typename... TArgs> - TInliningPrinter(TFieldPredicate isInlined, TArgs&&... args) - : TBasePrinter(std::forward<TArgs>(args)...) - , IsInlined(std::move(isInlined)) - { - } - - virtual void PrintField(const NProtoBuf::Message& proto, - const NProtoBuf::FieldDescriptor& field, - IJsonOutput& json, - TStringBuf key) override { - const NProtoBuf::TConstField f(proto, &field); - if (!key && IsInlined(proto, &field) && ShouldPrint(f)) { - key = this->MakeKey(field); - json.WriteKey(key); - if (!field.is_repeated()) { - json.WriteRawJson(f.Get<TString>()); - } else { - json.BeginList(); - for (size_t i = 0, sz = f.Size(); i < sz; ++i) - json.WriteRawJson(f.Get<TString>(i)); - json.EndList(); - } + template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant + class TInliningPrinter: public TBasePrinter { + public: + using TFieldPredicate = std::function<bool(const NProtoBuf::Message&, + const NProtoBuf::FieldDescriptor*)>; + + template <typename... TArgs> + TInliningPrinter(TFieldPredicate isInlined, TArgs&&... args) + : TBasePrinter(std::forward<TArgs>(args)...) + , IsInlined(std::move(isInlined)) + { + } + + virtual void PrintField(const NProtoBuf::Message& proto, + const NProtoBuf::FieldDescriptor& field, + IJsonOutput& json, + TStringBuf key) override { + const NProtoBuf::TConstField f(proto, &field); + if (!key && IsInlined(proto, &field) && ShouldPrint(f)) { + key = this->MakeKey(field); + json.WriteKey(key); + if (!field.is_repeated()) { + json.WriteRawJson(f.Get<TString>()); + } else { + json.BeginList(); + for (size_t i = 0, sz = f.Size(); i < sz; ++i) + json.WriteRawJson(f.Get<TString>(i)); + json.EndList(); + } } else { - TBasePrinter::PrintField(proto, field, json, key); + TBasePrinter::PrintField(proto, field, json, key); } + } + + private: + bool ShouldPrint(const NProtoBuf::TConstField& f) const { + if (!f.IsString()) + ythrow yexception() << "TInliningPrinter: json field " + << f.Field()->name() << " should be a string"; + + if (f.HasValue()) + return true; + + // we may want write default value for given field in case of its absence + const auto& cfg = this->GetConfig(); + return (f.Field()->is_repeated() ? cfg.MissingRepeatedKeyMode : cfg.MissingSingleKeyMode) == TProto2JsonConfig::MissingKeyDefault; } - private: - bool ShouldPrint(const NProtoBuf::TConstField& f) const { - if (!f.IsString()) - ythrow yexception() << "TInliningPrinter: json field " - << f.Field()->name() << " should be a string"; - - if (f.HasValue()) - return true; - - // we may want write default value for given field in case of its absence - const auto& cfg = this->GetConfig(); - return (f.Field()->is_repeated() ? cfg.MissingRepeatedKeyMode : cfg.MissingSingleKeyMode) == TProto2JsonConfig::MissingKeyDefault; - } - - private: - TFieldPredicate IsInlined; - }; + 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); - } + 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); + } - inline TString PrintInlined(const NProtoBuf::Message& msg, TInliningPrinter<>::TFieldPredicate isInlined, const TProto2JsonConfig& config = TProto2JsonConfig()) { - TString ret; - PrintInlined(msg, std::move(isInlined), *CreateJsonMapOutput(ret, config), config); - return ret; + inline TString PrintInlined(const NProtoBuf::Message& msg, TInliningPrinter<>::TFieldPredicate isInlined, const TProto2JsonConfig& config = TProto2JsonConfig()) { + TString ret; + PrintInlined(msg, std::move(isInlined), *CreateJsonMapOutput(ret, config), config); + return ret; } } |