diff options
author | Arslan Urtashev <urtashev@gmail.com> | 2022-02-10 16:48:55 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:55 +0300 |
commit | b97740540e7302cec9efa181e106ae1990a0cc1c (patch) | |
tree | 1a2a0bfe523c3272d207e2d75254a41d3945a3bb /library/cpp/protobuf/json | |
parent | 31789673dc3562bc7540e00fe031ba84ae282c24 (diff) | |
download | ydb-b97740540e7302cec9efa181e106ae1990a0cc1c.tar.gz |
Restoring authorship annotation for Arslan Urtashev <urtashev@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf/json')
-rw-r--r-- | library/cpp/protobuf/json/config.h | 14 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 14 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.h | 14 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json_output_create.cpp | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json_value_output.cpp | 12 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json_value_output.h | 12 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json_writer_output.h | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/json/name_generator.cpp | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/json/name_generator.h | 20 | ||||
-rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.cpp | 26 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/json2proto_ut.cpp | 34 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto.h | 4 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 44 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/test.proto | 24 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/ya.make | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ya.make | 8 |
16 files changed, 117 insertions, 117 deletions
diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index dc84fb4d5d..0ea37f1898 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -1,13 +1,13 @@ #pragma once #include "string_transform.h" -#include "name_generator.h" +#include "name_generator.h" #include <util/generic/vector.h> #include <util/generic/yexception.h> -#include <functional> - +#include <functional> + namespace NProtobufJson { struct TProto2JsonConfig { using TSelf = TProto2JsonConfig; @@ -85,14 +85,14 @@ namespace NProtobufJson { /// Custom enum values generator. TEnumValueGenerator EnumValueGenerator = {}; - + bool WriteNanAsString = false; TSelf& SetFormatOutput(bool format) { FormatOutput = format; return *this; } - + TSelf& SetMissingSingleKeyMode(MissingKeyMode mode) { MissingSingleKeyMode = mode; return *this; @@ -149,7 +149,7 @@ namespace NProtobufJson { NameGenerator = callback; return *this; } - + TSelf& SetEnumValueGenerator(TEnumValueGenerator callback) { EnumValueGenerator = callback; return *this; @@ -160,5 +160,5 @@ namespace NProtobufJson { return *this; } }; - + } diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index 640c10f5a5..5efa6d4b6c 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -36,10 +36,10 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config) { - if (config.NameGenerator) { - return config.NameGenerator(field); - } - + if (config.NameGenerator) { + return config.NameGenerator(field); + } + if (config.UseJsonName) { Y_ASSERT(!field.json_name().empty()); TString name = field.json_name(); @@ -409,10 +409,10 @@ namespace NProtobufJson { NJson::TJsonReaderConfig jsonCfg; jsonCfg.DontValidateUtf8 = true; jsonCfg.AllowComments = config.AllowComments; - + NJson::TJsonValue jsonValue; ReadJsonTree(json, &jsonCfg, &jsonValue, /* throwOnError = */ true); - + MergeJson2Proto(jsonValue, proto, config); } @@ -425,4 +425,4 @@ namespace NProtobufJson { proto.Clear(); MergeJson2Proto(json, proto, config); } -} +} diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index 4c33498dfa..5d35991cf3 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -1,7 +1,7 @@ #pragma once #include "string_transform.h" -#include "name_generator.h" +#include "name_generator.h" #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_value.h> @@ -105,7 +105,7 @@ namespace NProtobufJson { FldNameMode FieldNameMode = FieldNameOriginalCase; bool AllowUnknownFields = true; - + /// Use 'json_name' protobuf option for field name, mutually exclusive /// with FieldNameMode. bool UseJsonName = false; @@ -126,7 +126,7 @@ namespace NProtobufJson { /// Throw exception if there is no required fields in json object. bool CheckRequiredFields = true; - + /// Replace repeated fields content during merging bool ReplaceRepeatedFields = false; @@ -145,7 +145,7 @@ namespace NProtobufJson { /// Allow js-style comments (both // and /**/) bool AllowComments = false; }; - + /// @throw yexception void MergeJson2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()); @@ -173,7 +173,7 @@ namespace NProtobufJson { const TJson2ProtoConfig& config = TJson2ProtoConfig()) { Json2Proto(TStringBuf(json), proto, config); } - + /// @throw yexception inline void Json2Proto(IInputStream& in, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()) { @@ -190,7 +190,7 @@ namespace NProtobufJson { Json2Proto(jsonValue, protoValue, config); return protoValue; } - + /// @throw yexception template <typename T> T Json2Proto(IInputStream& in, const TJson2ProtoConfig& config = TJson2ProtoConfig()) { @@ -219,4 +219,4 @@ namespace NProtobufJson { return Json2Proto<T>(TStringBuf(ptr), config); } -} +} diff --git a/library/cpp/protobuf/json/json_output_create.cpp b/library/cpp/protobuf/json/json_output_create.cpp index 378e4ea65a..a0515b9ce7 100644 --- a/library/cpp/protobuf/json/json_output_create.cpp +++ b/library/cpp/protobuf/json/json_output_create.cpp @@ -16,7 +16,7 @@ namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(TString& str, const TProto2JsonConfig& config) { return MakeHolder<TJsonStringWriterOutput>(&str, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(TStringStream& out, const TProto2JsonConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } diff --git a/library/cpp/protobuf/json/json_value_output.cpp b/library/cpp/protobuf/json/json_value_output.cpp index d845cc1c74..88f52f8490 100644 --- a/library/cpp/protobuf/json/json_value_output.cpp +++ b/library/cpp/protobuf/json/json_value_output.cpp @@ -1,5 +1,5 @@ -#include "json_value_output.h" - +#include "json_value_output.h" + #include <library/cpp/json/json_reader.h> namespace NProtobufJson { @@ -17,7 +17,7 @@ namespace NProtobufJson { void TJsonValueOutput::DoWrite(const TStringBuf& s) { WriteImpl(s); - } + } void TJsonValueOutput::DoWrite(const TString& s) { WriteImpl(s); @@ -34,7 +34,7 @@ namespace NProtobufJson { void TJsonValueOutput::DoWrite(long long i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(unsigned long long i) { WriteImpl(i); } @@ -69,7 +69,7 @@ namespace NProtobufJson { 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); @@ -85,7 +85,7 @@ namespace NProtobufJson { 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); diff --git a/library/cpp/protobuf/json/json_value_output.h b/library/cpp/protobuf/json/json_value_output.h index 3fc6ff2ab0..d1127d0673 100644 --- a/library/cpp/protobuf/json/json_value_output.h +++ b/library/cpp/protobuf/json/json_value_output.h @@ -1,11 +1,11 @@ #pragma once -#include "json_output.h" - +#include "json_output.h" + #include <library/cpp/json/writer/json_value.h> -#include <util/generic/stack.h> - +#include <util/generic/stack.h> + namespace NProtobufJson { class TJsonValueOutput: public IJsonOutput { public: @@ -54,10 +54,10 @@ 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.h b/library/cpp/protobuf/json/json_writer_output.h index 3d8a2daa56..fbd3f10c6f 100644 --- a/library/cpp/protobuf/json/json_writer_output.h +++ b/library/cpp/protobuf/json/json_writer_output.h @@ -5,7 +5,7 @@ #include <library/cpp/json/json_writer.h> -#include <util/string/builder.h> +#include <util/string/builder.h> #include <util/generic/store_policy.h> namespace NProtobufJson { diff --git a/library/cpp/protobuf/json/name_generator.cpp b/library/cpp/protobuf/json/name_generator.cpp index c1fb421175..c692cb3120 100644 --- a/library/cpp/protobuf/json/name_generator.cpp +++ b/library/cpp/protobuf/json/name_generator.cpp @@ -1 +1 @@ -#include "name_generator.h" +#include "name_generator.h" diff --git a/library/cpp/protobuf/json/name_generator.h b/library/cpp/protobuf/json/name_generator.h index 2b5361bee2..e9ce067800 100644 --- a/library/cpp/protobuf/json/name_generator.h +++ b/library/cpp/protobuf/json/name_generator.h @@ -1,18 +1,18 @@ -#pragma once - -#include <util/generic/string.h> - -#include <functional> - -namespace google { +#pragma once + +#include <util/generic/string.h> + +#include <functional> + +namespace google { namespace protobuf { class FieldDescriptor; class EnumValueDescriptor; } } - -namespace NProtobufJson { + +namespace NProtobufJson { using TNameGenerator = std::function<TString(const google::protobuf::FieldDescriptor&)>; using TEnumValueGenerator = std::function<TString(const google::protobuf::EnumValueDescriptor&)>; - + } diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 6123eab0f2..31e7fe66d3 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -14,12 +14,12 @@ namespace NProtobufJson { TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf) : NewKeyStr(tmpBuf) { - if (config.NameGenerator) { - NewKeyStr = config.NameGenerator(field); - NewKeyBuf = NewKeyStr; - return; - } - + if (config.NameGenerator) { + NewKeyStr = config.NameGenerator(field); + NewKeyBuf = NewKeyStr; + return; + } + if (config.UseJsonName) { Y_ASSERT(!field.json_name().empty()); NewKeyStr = field.json_name(); @@ -136,11 +136,11 @@ namespace NProtobufJson { void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key, const EnumValueDescriptor* value, IJsonOutput& json) { - if (Config.EnumValueGenerator) { - WriteWithMaybeEmptyKey<InMapContext>(json, key, Config.EnumValueGenerator(*value)); - return; - } - + if (Config.EnumValueGenerator) { + WriteWithMaybeEmptyKey<InMapContext>(json, key, Config.EnumValueGenerator(*value)); + return; + } + switch (GetConfig().EnumMode) { case TProto2JsonConfig::EnumNumber: { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number()); @@ -333,7 +333,7 @@ namespace NProtobufJson { default: ythrow yexception() << "Unknown protobuf field type: " << static_cast<int>(field.cpp_type()) << "."; - } + } if (isMap) { json.EndObject(); @@ -462,7 +462,7 @@ namespace NProtobufJson { Y_ASSERT(field); PrintField(proto, *field, json); } - + // Check extensions via ListFields std::vector<const FieldDescriptor*> fields; auto* ref = proto.GetReflection(); diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp index 0dfe57bc7a..42a8ddf2c3 100644 --- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp +++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp @@ -1,6 +1,6 @@ #include "json.h" #include "proto.h" -#include "proto2json.h" +#include "proto2json.h" #include <library/cpp/protobuf/json/ut/test.pb.h> @@ -50,11 +50,11 @@ namespace { TString ConvertToString(double value) { return FloatToString(value); } - - TString JsonValueToString(const NJson::TJsonValue& json) { - NJsonWriter::TBuf buf(NJsonWriter::HEM_UNSAFE); - return buf.WriteJsonValue(&json).Str(); - } + + TString JsonValueToString(const NJson::TJsonValue& json) { + NJsonWriter::TBuf buf(NJsonWriter::HEM_UNSAFE); + return buf.WriteJsonValue(&json).Str(); + } void TestComplexMapAsObject(std::function<void(TComplexMapType&)>&& init, const TString& json, const TJson2ProtoConfig& config = TJson2ProtoConfig().SetMapAsObject(true)) { TComplexMapType modelProto; @@ -121,13 +121,13 @@ UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); Y_UNIT_TEST(TestNameGenerator) { TJson2ProtoConfig cfg; cfg.SetNameGenerator([](const NProtoBuf::FieldDescriptor&) { return "42"; }); - + TNameGeneratorType proto; Json2Proto(TStringBuf(R"({"42":42})"), proto, cfg); - + TNameGeneratorType expected; expected.SetField(42); - + UNIT_ASSERT_PROTOS_EQUAL(expected, proto); } @@ -139,11 +139,11 @@ Y_UNIT_TEST(TestFlatNoCheckRequired) { TFlatRequired modelProto; FillFlatProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); - } - + } + TJson2ProtoConfig cfg; cfg.CheckRequiredFields = false; - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -154,9 +154,9 @@ Y_UNIT_TEST(TestFlatNoCheckRequired) { UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto, cfg)); \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestFlatNoCheckRequired - + Y_UNIT_TEST(TestFlatRepeated){ {const NJson::TJsonValue& json = CreateRepeatedFlatJson(); TFlatRepeated proto; @@ -217,7 +217,7 @@ FillCompositeProto(&modelProto); modelProto.MutablePart()->SetDouble(42.5); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + // Try to skip each field #define DEFINE_FIELD(name, value) \ { \ @@ -237,9 +237,9 @@ UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD +#undef DEFINE_FIELD } // TestCompositeOptionalStringBuf - + Y_UNIT_TEST(TestCompositeRequired) { { const NJson::TJsonValue& json = CreateCompositeJson(); diff --git a/library/cpp/protobuf/json/ut/proto.h b/library/cpp/protobuf/json/ut/proto.h index 8183bfc8e1..db1e58557d 100644 --- a/library/cpp/protobuf/json/ut/proto.h +++ b/library/cpp/protobuf/json/ut/proto.h @@ -55,8 +55,8 @@ namespace NProtobufJsonTest { Cerr << lhs.DebugString() << Endl; \ Cerr << rhs.DebugString() << Endl; \ UNIT_ASSERT_STRINGS_EQUAL(lhs.DebugString(), rhs.DebugString()); \ - UNIT_ASSERT_STRINGS_EQUAL(lhs.SerializeAsString(), rhs.SerializeAsString()); \ + UNIT_ASSERT_STRINGS_EQUAL(lhs.SerializeAsString(), rhs.SerializeAsString()); \ } \ - } while (false); + } while (false); } diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..3e21b23371 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -75,31 +75,31 @@ Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { } Y_UNIT_TEST(TestNameGenerator) { - TNameGeneratorType proto; - proto.SetField(42); - - TProto2JsonConfig cfg; + TNameGeneratorType proto; + proto.SetField(42); + + TProto2JsonConfig cfg; cfg.SetNameGenerator([](const NProtoBuf::FieldDescriptor&) { return "42"; }); - - TStringStream str; - Proto2Json(proto, str, cfg); - - UNIT_ASSERT_STRINGS_EQUAL(R"({"42":42})", str.Str()); - } - + + TStringStream str; + Proto2Json(proto, str, cfg); + + UNIT_ASSERT_STRINGS_EQUAL(R"({"42":42})", str.Str()); + } + Y_UNIT_TEST(TestEnumValueGenerator) { - TEnumValueGeneratorType proto; - proto.SetEnum(TEnumValueGeneratorType::ENUM_42); - - TProto2JsonConfig cfg; + TEnumValueGeneratorType proto; + proto.SetEnum(TEnumValueGeneratorType::ENUM_42); + + TProto2JsonConfig cfg; cfg.SetEnumValueGenerator([](const NProtoBuf::EnumValueDescriptor&) { return "42"; }); - - TStringStream str; - Proto2Json(proto, str, cfg); - - UNIT_ASSERT_STRINGS_EQUAL(R"({"Enum":"42"})", str.Str()); - } - + + TStringStream str; + Proto2Json(proto, str, cfg); + + UNIT_ASSERT_STRINGS_EQUAL(R"({"Enum":"42"})", str.Str()); + } + Y_UNIT_TEST(TestFlatOptional){ {TFlatOptional proto; FillFlatProto(&proto); diff --git a/library/cpp/protobuf/json/ut/test.proto b/library/cpp/protobuf/json/ut/test.proto index 0fa996fd41..c8352b8c52 100644 --- a/library/cpp/protobuf/json/ut/test.proto +++ b/library/cpp/protobuf/json/ut/test.proto @@ -135,18 +135,18 @@ message TMapType { map<string, string> Items = 1; }; -message TNameGeneratorType { - optional int32 Field = 1; -}; - -message TEnumValueGeneratorType { - enum EEnum { - ENUM_42 = 1; - }; - - optional EEnum Enum = 1; -}; - +message TNameGeneratorType { + optional int32 Field = 1; +}; + +message TEnumValueGeneratorType { + enum EEnum { + ENUM_42 = 1; + }; + + optional EEnum Enum = 1; +}; + message TComplexMapType { map<int32, int32> I32 = 1; map<int64, int64> I64 = 2; diff --git a/library/cpp/protobuf/json/ut/ya.make b/library/cpp/protobuf/json/ut/ya.make index b60a6d3c17..4b8ed9c804 100644 --- a/library/cpp/protobuf/json/ut/ya.make +++ b/library/cpp/protobuf/json/ut/ya.make @@ -1,6 +1,6 @@ UNITTEST_FOR(library/cpp/protobuf/json) -OWNER(avitella) +OWNER(avitella) SRCS( filter_ut.cpp diff --git a/library/cpp/protobuf/json/ya.make b/library/cpp/protobuf/json/ya.make index 2f2c75cfdb..a9049e5655 100644 --- a/library/cpp/protobuf/json/ya.make +++ b/library/cpp/protobuf/json/ya.make @@ -1,15 +1,15 @@ LIBRARY() -OWNER(avitella) +OWNER(avitella) SRCS( json2proto.cpp json_output_create.cpp json_value_output.cpp json_writer_output.cpp - name_generator.cpp - proto2json.cpp - proto2json_printer.cpp + name_generator.cpp + proto2json.cpp + proto2json_printer.cpp string_transform.cpp util.h util.cpp |