diff options
author | stakanviski <stakanviski@yandex-team.ru> | 2022-02-10 16:50:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:01 +0300 |
commit | da8356a5535e6177965cba3bbae73c5fb28f6ccc (patch) | |
tree | 61a6bb4fa5e2c51ac92f621f5a9eba5658d8aba0 /library/cpp/protobuf | |
parent | c345445ed6cb1fc34136ef6f02ce07f5aff9e504 (diff) | |
download | ydb-da8356a5535e6177965cba3bbae73c5fb28f6ccc.tar.gz |
Restoring authorship annotation for <stakanviski@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf')
-rw-r--r-- | library/cpp/protobuf/json/config.h | 6 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 40 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.h | 8 | ||||
-rw-r--r-- | library/cpp/protobuf/json/proto2json_printer.cpp | 10 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/fields.incl | 4 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/json2proto_ut.cpp | 70 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 22 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/test.proto | 30 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/ya.make | 4 |
9 files changed, 97 insertions, 97 deletions
diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index dc84fb4d5d..7168b6b012 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -4,7 +4,7 @@ #include "name_generator.h" #include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <functional> @@ -68,7 +68,7 @@ namespace NProtobufJson { /// 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; @@ -134,7 +134,7 @@ namespace NProtobufJson { StringTransforms.push_back(transform); return *this; } - + TSelf& SetMapAsObject(bool value) { MapAsObject = value; return *this; diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index 640c10f5a5..8476cc83d7 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -8,30 +8,30 @@ #include <util/generic/hash.h> #include <util/generic/maybe.h> -#include <util/string/ascii.h> +#include <util/string/ascii.h> #include <util/string/cast.h> #define JSON_TO_FIELD(EProtoCppType, name, json, JsonCheckType, ProtoSet, JsonGet) \ - case FieldDescriptor::EProtoCppType: { \ + case FieldDescriptor::EProtoCppType: { \ if (config.CastRobust) { \ reflection->ProtoSet(&proto, &field, json.JsonGet##Robust()); \ break; \ } \ - if (!json.JsonCheckType()) { \ - if (config.CastFromString && json.IsString()) { \ + if (!json.JsonCheckType()) { \ + if (config.CastFromString && json.IsString()) { \ if (config.DoNotCastEmptyStrings && json.GetString().empty()) { \ /* Empty string is same as "no value" for scalar types.*/ \ break; \ } \ - reflection->ProtoSet(&proto, &field, FromString(json.GetString())); \ - break; \ - } \ + reflection->ProtoSet(&proto, &field, FromString(json.GetString())); \ + break; \ + } \ ythrow yexception() << "Invalid type of JSON field " << name << ": " \ - << #JsonCheckType << "() failed while " \ - << #EProtoCppType << " is expected."; \ - } \ - reflection->ProtoSet(&proto, &field, json.JsonGet()); \ - break; \ + << #JsonCheckType << "() failed while " \ + << #EProtoCppType << " is expected."; \ + } \ + reflection->ProtoSet(&proto, &field, json.JsonGet()); \ + break; \ } static TString GetFieldName(const google::protobuf::FieldDescriptor& field, @@ -40,16 +40,16 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field, return config.NameGenerator(field); } - if (config.UseJsonName) { - Y_ASSERT(!field.json_name().empty()); + if (config.UseJsonName) { + Y_ASSERT(!field.json_name().empty()); TString name = field.json_name(); if (!field.has_json_name() && !name.empty()) { // FIXME: https://st.yandex-team.ru/CONTRIB-139 name[0] = AsciiToLower(name[0]); } return name; - } - + } + TString name = field.name(); switch (config.FieldNameMode) { case NProtobufJson::TJson2ProtoConfig::FieldNameOriginalCase: @@ -60,11 +60,11 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field, case NProtobufJson::TJson2ProtoConfig::FieldNameUpperCase: name.to_upper(); break; - case NProtobufJson::TJson2ProtoConfig::FieldNameCamelCase: + case NProtobufJson::TJson2ProtoConfig::FieldNameCamelCase: if (!name.empty()) { - name[0] = AsciiToLower(name[0]); - } - break; + name[0] = AsciiToLower(name[0]); + } + break; case NProtobufJson::TJson2ProtoConfig::FieldNameSnakeCase: NProtobufJson::ToSnakeCase(&name); break; diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index 4c33498dfa..00e7a9accb 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -52,7 +52,7 @@ namespace NProtobufJson { StringTransforms.push_back(transform); return *this; } - + TSelf& SetCastFromString(bool cast) { CastFromString = cast; return *this; @@ -67,7 +67,7 @@ namespace NProtobufJson { CastRobust = cast; return *this; } - + TSelf& SetMapAsObject(bool mapAsObject) { MapAsObject = mapAsObject; return *this; @@ -112,7 +112,7 @@ namespace NProtobufJson { /// Transforms will be applied only to string values (== protobuf fields of string / bytes type). TVector<TStringTransformPtr> StringTransforms; - + /// Cast string json values to protobuf field type bool CastFromString = false; /// Skip empty strings, instead casting from string into scalar types. @@ -120,7 +120,7 @@ namespace NProtobufJson { bool DoNotCastEmptyStrings = false; /// Cast all json values to protobuf field types bool CastRobust = false; - + /// Consider map to be an object, otherwise consider it to be an array of key/value objects bool MapAsObject = false; diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 6123eab0f2..dd244978da 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -3,7 +3,7 @@ #include "util.h" #include <util/generic/yexception.h> -#include <util/string/ascii.h> +#include <util/string/ascii.h> #include <util/string/cast.h> namespace NProtobufJson { @@ -36,7 +36,7 @@ namespace NProtobufJson { NewKeyBuf = field.name(); break; } - + case TProto2JsonConfig::FieldNameLowerCase: { NewKeyStr = field.name(); NewKeyStr.to_lower(); @@ -76,9 +76,9 @@ namespace NProtobufJson { default: Y_VERIFY_DEBUG(false, "Unknown FieldNameMode."); - } - } - + } + } + const TStringBuf& GetKey() const { return NewKeyBuf; } diff --git a/library/cpp/protobuf/json/ut/fields.incl b/library/cpp/protobuf/json/ut/fields.incl index 4b22985836..d848f614cd 100644 --- a/library/cpp/protobuf/json/ut/fields.incl +++ b/library/cpp/protobuf/json/ut/fields.incl @@ -17,7 +17,7 @@ DEFINE_FIELD(Bytes, "מחשב") DEFINE_FIELD(Enum, E_1) DEFINE_FIELD(Float, 1.123f) DEFINE_FIELD(Double, 1.123456789012) -DEFINE_FIELD(OneString, "Lorem ipsum dolor") -DEFINE_FIELD(OneTwoString, "Lorem ipsum dolor sit") +DEFINE_FIELD(OneString, "Lorem ipsum dolor") +DEFINE_FIELD(OneTwoString, "Lorem ipsum dolor sit") DEFINE_FIELD(ABC, "abc") DEFINE_FIELD(UserID, "some_id")
\ No newline at end of file diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp index 0dfe57bc7a..55dd9f18cb 100644 --- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp +++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp @@ -16,7 +16,7 @@ #include <util/generic/string.h> #include <util/generic/ylimits.h> #include <util/stream/str.h> -#include <util/string/cast.h> +#include <util/string/cast.h> #include <util/system/defaults.h> #include <util/system/yassert.h> @@ -33,7 +33,7 @@ namespace google { } // namespace protobuf } -namespace { +namespace { class TInit { public: TInit() { @@ -41,15 +41,15 @@ namespace { } } Init; - template <typename T> - TString ConvertToString(T value) { - return ToString(value); - } - - // default ToString<double>() implementation loses precision - TString ConvertToString(double value) { - return FloatToString(value); - } + template <typename T> + TString ConvertToString(T value) { + return ToString(value); + } + + // default ToString<double>() implementation loses precision + TString ConvertToString(double value) { + return FloatToString(value); + } TString JsonValueToString(const NJson::TJsonValue& json) { NJsonWriter::TBuf buf(NJsonWriter::HEM_UNSAFE); @@ -68,8 +68,8 @@ namespace { UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } -} - +} + Y_UNIT_TEST_SUITE(TJson2ProtoTest) { Y_UNIT_TEST(TestFlatOptional){ {const NJson::TJsonValue& json = CreateFlatJson(); @@ -369,35 +369,35 @@ Y_UNIT_TEST(TestFieldNameMode) { // Camelcase { TString modelStr(R"_({"string":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameCamelCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "value"); } { TString modelStr(R"_({"oneString":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameCamelCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetOneString() == "value"); } { TString modelStr(R"_({"oneTwoString":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameCamelCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetOneTwoString() == "value"); } - + // snake_case { TString modelStr(R"_({"string":"value"})_"); @@ -457,7 +457,7 @@ Y_UNIT_TEST(TestFieldNameMode) { UNIT_ASSERT(proto.GetI32(0) == 1); UNIT_ASSERT(proto.GetI32(1) == 2); } - + // UseJsonName { // FIXME(CONTRIB-139): since protobuf 3.1, Def_upper json name is @@ -465,18 +465,18 @@ Y_UNIT_TEST(TestFieldNameMode) { // updated, library/cpp/protobuf/json preserves compatibility with // protobuf 3.0 by lowercasing default names, making it "defUpper". TString modelStr(R"_({"My-Upper":1,"my-lower":2,"defUpper":3,"defLower":4})_"); - + TWithJsonName proto; TJson2ProtoConfig config; config.SetUseJsonName(true); - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TWithJsonName>(modelStr, config)); UNIT_ASSERT_EQUAL(proto.Getmy_upper(), 1); UNIT_ASSERT_EQUAL(proto.GetMy_lower(), 2); UNIT_ASSERT_EQUAL(proto.GetDef_upper(), 3); UNIT_ASSERT_EQUAL(proto.Getdef_lower(), 4); } - + // FieldNameMode with UseJsonName { TJson2ProtoConfig config; @@ -589,23 +589,23 @@ Y_UNIT_TEST(TestCastFromString) { // single fields { NJson::TJsonValue json; -#define DEFINE_FIELD(name, value) \ +#define DEFINE_FIELD(name, value) \ json.InsertValue(#name, ConvertToString(value)); #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD - +#undef DEFINE_FIELD + TFlatOptional proto; UNIT_ASSERT_EXCEPTION_CONTAINS(Json2Proto(json, proto), yexception, "Invalid type"); - + TJson2ProtoConfig config; config.SetCastFromString(true); Json2Proto(json, proto, config); - + TFlatOptional modelProto; FillFlatProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } - + // repeated fields { NJson::TJsonValue json; @@ -619,21 +619,21 @@ Y_UNIT_TEST(TestCastFromString) { json.InsertValue(#name, array); \ } #include <library/cpp/protobuf/json/ut/repeated_fields.incl> -#undef DEFINE_REPEATED_FIELD - +#undef DEFINE_REPEATED_FIELD + TFlatRepeated proto; UNIT_ASSERT_EXCEPTION_CONTAINS(Json2Proto(json, proto), yexception, "Invalid type"); - + TJson2ProtoConfig config; config.SetCastFromString(true); Json2Proto(json, proto, config); - + TFlatRepeated modelProto; FillRepeatedProto(&modelProto); UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); } } // TestCastFromString - + Y_UNIT_TEST(TestMap) { TMapType modelProto; diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..7fc3a1e403 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -721,41 +721,41 @@ Y_UNIT_TEST(TestFieldNameMode) { // Camelcase { TString modelStr(R"_({"string":"value"})_"); - + TFlatOptional proto; proto.SetString("value"); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameCamelCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } { TString modelStr(R"_({"oneString":"value"})_"); - + TFlatOptional proto; proto.SetOneString("value"); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameCamelCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } { TString modelStr(R"_({"oneTwoString":"value"})_"); - + TFlatOptional proto; proto.SetOneTwoString("value"); TStringStream jsonStr; TProto2JsonConfig config; config.FieldNameMode = TProto2JsonConfig::FieldNameCamelCase; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // snake_case { TString modelStr(R"_({"string":"value"})_"); @@ -857,7 +857,7 @@ Y_UNIT_TEST(TestFieldNameMode) { // FIXME(CONTRIB-139): see the comment about UseJsonName in json2proto_ut.cpp: // Def_upper json name should be "DefUpper". TString modelStr(R"_({"My-Upper":1,"my-lower":2,"defUpper":3,"defLower":4})_"); - + TWithJsonName proto; proto.Setmy_upper(1); proto.SetMy_lower(2); @@ -866,11 +866,11 @@ Y_UNIT_TEST(TestFieldNameMode) { TStringStream jsonStr; TProto2JsonConfig config; config.SetUseJsonName(true); - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // FieldNameMode with UseJsonName { TProto2JsonConfig config; @@ -884,7 +884,7 @@ Y_UNIT_TEST(TestFieldNameMode) { UNIT_ASSERT_EXCEPTION_CONTAINS( config.SetFieldNameMode(TProto2JsonConfig::FieldNameLowerCase), yexception, "mutually exclusive"); } - + /// TODO: test missing keys } // TestFieldNameMode diff --git a/library/cpp/protobuf/json/ut/test.proto b/library/cpp/protobuf/json/ut/test.proto index 0fa996fd41..1285cb430d 100644 --- a/library/cpp/protobuf/json/ut/test.proto +++ b/library/cpp/protobuf/json/ut/test.proto @@ -28,9 +28,9 @@ message TFlatOptional { optional float Float = 15; optional double Double = 16; - - optional string OneString = 17; - optional string OneTwoString = 18; + + optional string OneString = 17; + optional string OneTwoString = 18; optional string ABC = 19; optional string UserID = 20; }; @@ -56,9 +56,9 @@ message TFlatRequired { required float Float = 15; required double Double = 16; - - required string OneString = 17; - required string OneTwoString = 18; + + required string OneString = 17; + required string OneTwoString = 18; required string ABC = 19; required string UserID = 20; }; @@ -84,9 +84,9 @@ message TFlatRepeated { repeated float Float = 15; repeated double Double = 16; - - repeated string OneString = 17; - repeated string OneTwoString = 18; + + repeated string OneString = 17; + repeated string OneTwoString = 18; repeated string ABC = 19; repeated string UserID = 20; }; @@ -112,9 +112,9 @@ message TFlatDefault { optional float Float = 15 [default = 0.123]; optional double Double = 16 [default = 0.456]; - - optional string OneString = 17 [default = "string"]; - optional string OneTwoString = 18 [default = "string"]; + + optional string OneString = 17 [default = "string"]; + optional string OneTwoString = 18 [default = "string"]; optional string ABC = 19 [default = "abc"]; optional string UserID = 20 [default = "some_id"]; }; @@ -134,7 +134,7 @@ message TCompositeRepeated { message TMapType { map<string, string> Items = 1; }; - + message TNameGeneratorType { optional int32 Field = 1; }; @@ -171,12 +171,12 @@ message TComplexMapType { map<string, TComplexMapType> Nested = 16; }; -message TWithJsonName { +message TWithJsonName { optional int32 my_upper = 1 [json_name = "My-Upper"]; optional int32 My_lower = 2 [json_name = "my-lower"]; optional int32 Def_upper = 3; // json_name = "DefUpper" optional int32 def_lower = 4; // json_name = "defLower" -} +} message TSingleRequiredString { required string String = 1; diff --git a/library/cpp/protobuf/json/ut/ya.make b/library/cpp/protobuf/json/ut/ya.make index b60a6d3c17..68a087d224 100644 --- a/library/cpp/protobuf/json/ut/ya.make +++ b/library/cpp/protobuf/json/ut/ya.make @@ -14,8 +14,8 @@ SRCS( util_ut.cpp ) -GENERATE_ENUM_SERIALIZATION(test.pb.h) - +GENERATE_ENUM_SERIALIZATION(test.pb.h) + PEERDIR( library/cpp/protobuf/json ) |