diff options
author | alexcrush <alexcrush@yandex-team.ru> | 2022-02-10 16:50:33 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:33 +0300 |
commit | 34f0683c2d7731f2f9f59966c12e602f405fa0d6 (patch) | |
tree | 9af7dd74efcfa3d8aa7b40bd757ad8f2d89f6902 /library/cpp/protobuf | |
parent | a1d67d6a31f789aa011250c3edce5751c0cadad2 (diff) | |
download | ydb-34f0683c2d7731f2f9f59966c12e602f405fa0d6.tar.gz |
Restoring authorship annotation for <alexcrush@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/protobuf')
-rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 94 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.h | 18 | ||||
-rw-r--r-- | library/cpp/protobuf/json/string_transform.h | 2 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/json2proto_ut.cpp | 64 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/proto2json_ut.cpp | 16 |
5 files changed, 97 insertions, 97 deletions
diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index 640c10f5a5..dbbd07e002 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -51,15 +51,15 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field, } TString name = field.name(); - switch (config.FieldNameMode) { - case NProtobufJson::TJson2ProtoConfig::FieldNameOriginalCase: - break; - case NProtobufJson::TJson2ProtoConfig::FieldNameLowerCase: - name.to_lower(); - break; - case NProtobufJson::TJson2ProtoConfig::FieldNameUpperCase: - name.to_upper(); - break; + switch (config.FieldNameMode) { + case NProtobufJson::TJson2ProtoConfig::FieldNameOriginalCase: + break; + case NProtobufJson::TJson2ProtoConfig::FieldNameLowerCase: + name.to_lower(); + break; + case NProtobufJson::TJson2ProtoConfig::FieldNameUpperCase: + name.to_upper(); + break; case NProtobufJson::TJson2ProtoConfig::FieldNameCamelCase: if (!name.empty()) { name[0] = AsciiToLower(name[0]); @@ -73,43 +73,43 @@ static TString GetFieldName(const google::protobuf::FieldDescriptor& field, break; default: Y_VERIFY_DEBUG(false, "Unknown FieldNameMode."); - } - return name; -} - + } + return name; +} + static void -JsonString2Field(const NJson::TJsonValue& json, - google::protobuf::Message& proto, - const google::protobuf::FieldDescriptor& field, +JsonString2Field(const NJson::TJsonValue& json, + google::protobuf::Message& proto, + const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config) { - using namespace google::protobuf; - - const Reflection* reflection = proto.GetReflection(); + using namespace google::protobuf; + + const Reflection* reflection = proto.GetReflection(); Y_ASSERT(!!reflection); - + if (!json.IsString() && !config.CastRobust) { ythrow yexception() << "Invalid type of JSON field '" << field.name() << "': " - << "IsString() failed while " - << "CPPTYPE_STRING is expected."; - } + << "IsString() failed while " + << "CPPTYPE_STRING is expected."; + } TString value = json.GetStringRobust(); - for (size_t i = 0, endI = config.StringTransforms.size(); i < endI; ++i) { + for (size_t i = 0, endI = config.StringTransforms.size(); i < endI; ++i) { Y_ASSERT(!!config.StringTransforms[i]); - if (!!config.StringTransforms[i]) { - if (field.type() == google::protobuf::FieldDescriptor::TYPE_BYTES) { - config.StringTransforms[i]->TransformBytes(value); - } else { - config.StringTransforms[i]->Transform(value); - } - } - } - - if (field.is_repeated()) - reflection->AddString(&proto, &field, value); - else - reflection->SetString(&proto, &field, value); -} - + if (!!config.StringTransforms[i]) { + if (field.type() == google::protobuf::FieldDescriptor::TYPE_BYTES) { + config.StringTransforms[i]->TransformBytes(value); + } else { + config.StringTransforms[i]->Transform(value); + } + } + } + + if (field.is_repeated()) + reflection->AddString(&proto, &field, value); + else + reflection->SetString(&proto, &field, value); +} + static const NProtoBuf::EnumValueDescriptor* FindEnumValue(const NProtoBuf::EnumDescriptor* enumField, TStringBuf target, bool (*equals)(TStringBuf, TStringBuf)) { @@ -122,7 +122,7 @@ FindEnumValue(const NProtoBuf::EnumDescriptor* enumField, return nullptr; } -static void +static void JsonEnum2Field(const NJson::TJsonValue& json, google::protobuf::Message& proto, const google::protobuf::FieldDescriptor& field, @@ -170,7 +170,7 @@ JsonEnum2Field(const NJson::TJsonValue& json, static void Json2SingleField(const NJson::TJsonValue& json, google::protobuf::Message& proto, - const google::protobuf::FieldDescriptor& field, + const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config, bool isMapValue = false) { using namespace google::protobuf; @@ -206,7 +206,7 @@ Json2SingleField(const NJson::TJsonValue& json, JsonString2Field(fieldJson, proto, field, config); break; } - + case FieldDescriptor::CPPTYPE_ENUM: { JsonEnum2Field(fieldJson, proto, field, config); break; @@ -314,15 +314,15 @@ Json2RepeatedFieldValue(const NJson::TJsonValue& jsonValue, static void Json2RepeatedField(const NJson::TJsonValue& json, google::protobuf::Message& proto, - const google::protobuf::FieldDescriptor& field, + const google::protobuf::FieldDescriptor& field, const NProtobufJson::TJson2ProtoConfig& config) { using namespace google::protobuf; TString name = GetFieldName(field, config); - if (!json.Has(name)) + if (!json.Has(name)) return; - const NJson::TJsonValue& fieldJson = json[name]; + const NJson::TJsonValue& fieldJson = json[name]; if (fieldJson.GetType() == NJson::JSON_UNDEFINED || fieldJson.GetType() == NJson::JSON_NULL) return; @@ -337,7 +337,7 @@ Json2RepeatedField(const NJson::TJsonValue& json, if (fieldJson.GetType() != NJson::JSON_ARRAY && !config.MapAsObject && !config.VectorizeScalars && !config.ValueVectorizer) { ythrow yexception() << "JSON field doesn't represent an array for " - << name + << name << "(actual type is " << static_cast<int>(fieldJson.GetType()) << ")."; } @@ -351,7 +351,7 @@ Json2RepeatedField(const NJson::TJsonValue& json, const TString& key = x.first; const NJson::TJsonValue& jsonValue = x.second; Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection, key); - } + } } else { if (config.ReplaceRepeatedFields) { reflection->ClearField(&proto, &field); diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index 4c33498dfa..ac4e941fae 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -1,8 +1,8 @@ #pragma once -#include "string_transform.h" +#include "string_transform.h" #include "name_generator.h" - + #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_value.h> @@ -29,25 +29,25 @@ namespace NProtobufJson { FieldNameSnakeCase, // ABC -> a_b_c, UserID -> user_i_d FieldNameSnakeCaseDense // ABC -> abc, UserID -> user_id }; - + enum EnumValueMode { EnumCaseSensetive = 0, // default EnumCaseInsensetive, EnumSnakeCaseInsensitive }; - + 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& AddStringTransform(TStringTransformPtr transform) { StringTransforms.push_back(transform); return *this; @@ -57,7 +57,7 @@ namespace NProtobufJson { CastFromString = cast; return *this; } - + TSelf& SetDoNotCastEmptyStrings(bool cast) { DoNotCastEmptyStrings = cast; return *this; @@ -109,7 +109,7 @@ 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). TVector<TStringTransformPtr> StringTransforms; @@ -163,7 +163,7 @@ namespace NProtobufJson { /// @throw yexception void Json2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()); - + /// @throw yexception void Json2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()); diff --git a/library/cpp/protobuf/json/string_transform.h b/library/cpp/protobuf/json/string_transform.h index e4b296bc01..1f1859514b 100644 --- a/library/cpp/protobuf/json/string_transform.h +++ b/library/cpp/protobuf/json/string_transform.h @@ -27,7 +27,7 @@ namespace NProtobufJson { return Transform(str); } }; - + using TStringTransformPtr = TIntrusivePtr<IStringTransform>; template <bool quote, bool tounicode> diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp index 0dfe57bc7a..c659cfcc50 100644 --- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp +++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp @@ -317,55 +317,55 @@ Y_UNIT_TEST(TestInvalidEnum) { UNIT_ASSERT_EXCEPTION(Json2Proto(json, proto), yexception); } } - + Y_UNIT_TEST(TestFieldNameMode) { // Original case 1 { TString modelStr(R"_({"String":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "value"); } - + // Original case 2 { TString modelStr(R"_({"String":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameOriginalCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "value"); } - + // Lowercase { TString modelStr(R"_({"string":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameLowerCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "value"); } - + // Uppercase { TString modelStr(R"_({"STRING":"value"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameUpperCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "value"); } - + // Camelcase { TString modelStr(R"_({"string":"value"})_"); @@ -433,25 +433,25 @@ Y_UNIT_TEST(TestFieldNameMode) { // Original case, repeated { TString modelStr(R"_({"I32":[1,2]})_"); - + TFlatRepeated proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameOriginalCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatRepeated>(modelStr, config)); UNIT_ASSERT(proto.I32Size() == 2); UNIT_ASSERT(proto.GetI32(0) == 1); UNIT_ASSERT(proto.GetI32(1) == 2); } - + // Lower case, repeated { TString modelStr(R"_({"i32":[1,2]})_"); - + TFlatRepeated proto; TJson2ProtoConfig config; config.FieldNameMode = TJson2ProtoConfig::FieldNameLowerCase; - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatRepeated>(modelStr, config)); UNIT_ASSERT(proto.I32Size() == 2); UNIT_ASSERT(proto.GetI32(0) == 1); @@ -491,7 +491,7 @@ Y_UNIT_TEST(TestFieldNameMode) { config.SetFieldNameMode(TJson2ProtoConfig::FieldNameLowerCase), yexception, "mutually exclusive"); } } // TestFieldNameMode - + class TStringTransform: public IStringTransform { public: int GetType() const override { @@ -501,7 +501,7 @@ public: str = "transformed_any"; } }; - + class TBytesTransform: public IStringTransform { public: int GetType() const override { @@ -519,7 +519,7 @@ Y_UNIT_TEST(TestInvalidJson) { TFlatOptional proto; UNIT_ASSERT_EXCEPTION(Json2Proto(val, proto), yexception); } - + Y_UNIT_TEST(TestInvalidRepeatedFieldWithMapAsObject) { TCompositeRepeated proto; TJson2ProtoConfig config; @@ -531,37 +531,37 @@ Y_UNIT_TEST(TestStringTransforms) { // Check that strings and bytes are transformed { TString modelStr(R"_({"String":"value_str", "Bytes": "value_bytes"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.AddStringTransform(new TStringTransform); - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "transformed_any"); UNIT_ASSERT(proto.GetBytes() == "transformed_any"); } - + // Check that bytes are transformed, strings are left intact { TString modelStr(R"_({"String":"value_str", "Bytes": "value_bytes"})_"); - + TFlatOptional proto; TJson2ProtoConfig config; config.AddStringTransform(new TBytesTransform); - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatOptional>(modelStr, config)); UNIT_ASSERT(proto.GetString() == "value_str"); UNIT_ASSERT(proto.GetBytes() == "transformed_bytes"); } - + // Check that repeated bytes are transformed, repeated strings are left intact { TString modelStr(R"_({"String":["value_str", "str2"], "Bytes": ["value_bytes", "bytes2"]})_"); - + TFlatRepeated proto; TJson2ProtoConfig config; config.AddStringTransform(new TBytesTransform); - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TFlatRepeated>(modelStr, config)); UNIT_ASSERT(proto.StringSize() == 2); UNIT_ASSERT(proto.GetString(0) == "value_str"); @@ -570,15 +570,15 @@ Y_UNIT_TEST(TestStringTransforms) { UNIT_ASSERT(proto.GetBytes(0) == "transformed_bytes"); UNIT_ASSERT(proto.GetBytes(1) == "transformed_bytes"); } - + // Check that bytes are transformed, strings are left intact in composed messages { TString modelStr(R"_({"Part": {"String":"value_str", "Bytes": "value_bytes"}})_"); - + TCompositeOptional proto; TJson2ProtoConfig config; config.AddStringTransform(new TBytesTransform); - + UNIT_ASSERT_NO_EXCEPTION(proto = Json2Proto<TCompositeOptional>(modelStr, config)); UNIT_ASSERT(proto.GetPart().GetString() == "value_str"); UNIT_ASSERT(proto.GetPart().GetBytes() == "transformed_bytes"); diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..31264e4a74 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -631,37 +631,37 @@ public: str = "bytes"; } }; - + Y_UNIT_TEST(TestBytesTransform) { // Test that string field is not changed { TString modelStr(R"_({"String":"value"})_"); - + TFlatOptional proto; proto.SetString(R"_(value)_"); TProto2JsonConfig config; config.StringTransforms.push_back(new TBytesTransform()); TStringStream jsonStr; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); } - + // Test that bytes field is changed { TString modelStr(R"_({"Bytes":"bytes"})_"); - + TFlatOptional proto; proto.SetBytes(R"_(value)_"); TProto2JsonConfig config; config.StringTransforms.push_back(new TBytesTransform()); TStringStream jsonStr; - + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, jsonStr, config)); UNIT_ASSERT_JSON_STRINGS_EQUAL(jsonStr.Str(), modelStr); - } + } } - + Y_UNIT_TEST(TestFieldNameMode) { // Original case 1 { |