diff options
| author | Vasily Gerasimov <[email protected]> | 2022-02-10 16:49:10 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:49:10 +0300 | 
| commit | 1eb755fbca92172a6aec2f57371b2b3a19dfab43 (patch) | |
| tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/protobuf/json | |
| parent | 6cdc8f140213c595e4ad38bc3d97fcef1146b8c3 (diff) | |
Restoring authorship annotation for Vasily Gerasimov <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/protobuf/json')
22 files changed, 358 insertions, 358 deletions
| diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index 3985b917bc5..dc84fb4d5db 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 3a2db53a922..c8a8bfbff5e 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 7033998e146..9a3ddb54fe3 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 03a4db81017..e2d7bb6ef04 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;  +//     Cout << NProtobufJson::PrintInlined(o, MakeFieldOptionFunctor(this_is_json)) << Endl; +// +// 5) Alternatively you can specify a some more abstract functor for defining raw json fields  // -// 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 2fc79341711..df143af57a7 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 0c1575d0ca0..378e4ea65a0 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 9f7b195cc75..ad3889f5e99 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 33a0f3d339a..d845cc1c74a 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 8d71d36af47..3fc6ff2ab0f 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 d6258eae1b9..288f645bab0 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 a634d9d5651..3d8a2daa56c 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 728b98d1f9b..3d76a916867 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 191c406e5ec..89a1781a40d 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 833a0d294b3..6123eab0f25 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 866fd8b5456..9dc5aa86c62 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 a5f17f02300..95c227666fb 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 c4866ed8134..29d630ade45 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 048e3fa2754..c29ad32e7d6 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 147c3369dd1..76bd10232dc 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 9dbec774e2f..07e52d7f2f5 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 ac6aebeab81..b60a6d3c17f 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 b8e3f1a33ce..2f2c75cfdb2 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 | 
