diff options
author | zhvv117 <zhvv117@yandex-team.ru> | 2022-02-10 16:50:35 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:35 +0300 |
commit | 3889d4c01a589a555fbc33d66279be23e267ab3b (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library | |
parent | 6672234a0566803bfeb1d572c14de9222b9a5a49 (diff) | |
download | ydb-3889d4c01a589a555fbc33d66279be23e267ab3b.tar.gz |
Restoring authorship annotation for <zhvv117@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/protobuf/json/json2proto.cpp | 42 | ||||
-rw-r--r-- | library/cpp/protobuf/json/json2proto.h | 44 | ||||
-rw-r--r-- | library/cpp/protobuf/json/ut/json2proto_ut.cpp | 232 |
3 files changed, 159 insertions, 159 deletions
diff --git a/library/cpp/protobuf/json/json2proto.cpp b/library/cpp/protobuf/json/json2proto.cpp index 10d0e027c2..640c10f5a5 100644 --- a/library/cpp/protobuf/json/json2proto.cpp +++ b/library/cpp/protobuf/json/json2proto.cpp @@ -175,14 +175,14 @@ Json2SingleField(const NJson::TJsonValue& json, bool isMapValue = false) { using namespace google::protobuf; - const Reflection* reflection = proto.GetReflection(); - Y_ASSERT(!!reflection); - + const Reflection* reflection = proto.GetReflection(); + Y_ASSERT(!!reflection); + TString name; if (!isMapValue) { name = GetFieldName(field, config); if (!json.Has(name) || json[name].GetType() == NJson::JSON_UNDEFINED || json[name].GetType() == NJson::JSON_NULL) { - if (field.is_required() && !field.has_default_value() && !reflection->HasField(proto, &field) && config.CheckRequiredFields) { + if (field.is_required() && !field.has_default_value() && !reflection->HasField(proto, &field) && config.CheckRequiredFields) { ythrow yexception() << "JSON has no field for required field " << name << "."; } @@ -215,7 +215,7 @@ Json2SingleField(const NJson::TJsonValue& json, case FieldDescriptor::CPPTYPE_MESSAGE: { Message* innerProto = reflection->MutableMessage(&proto, &field); Y_ASSERT(!!innerProto); - NProtobufJson::MergeJson2Proto(fieldJson, *innerProto, config); + NProtobufJson::MergeJson2Proto(fieldJson, *innerProto, config); break; } @@ -299,7 +299,7 @@ Json2RepeatedFieldValue(const NJson::TJsonValue& jsonValue, Y_ENSURE(valueField, "Map entry value field not found."); Json2SingleField(jsonValue, *innerProto, *valueField, config, /*isMapValue=*/true); } else { - NProtobufJson::MergeJson2Proto(jsonValue, *innerProto, config); + NProtobufJson::MergeJson2Proto(jsonValue, *innerProto, config); } break; @@ -353,9 +353,9 @@ Json2RepeatedField(const NJson::TJsonValue& json, Json2RepeatedFieldValue(jsonValue, proto, field, config, reflection, key); } } else { - if (config.ReplaceRepeatedFields) { - reflection->ClearField(&proto, &field); - } + if (config.ReplaceRepeatedFields) { + reflection->ClearField(&proto, &field); + } if (fieldJson.GetType() == NJson::JSON_ARRAY) { const NJson::TJsonValue::TArray& jsonArray = fieldJson.GetArray(); for (const NJson::TJsonValue& jsonValue : jsonArray) { @@ -372,7 +372,7 @@ Json2RepeatedField(const NJson::TJsonValue& json, } namespace NProtobufJson { - void MergeJson2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { + void MergeJson2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { if (json.IsNull()) { return; } @@ -405,7 +405,7 @@ namespace NProtobufJson { } } - void MergeJson2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { + void MergeJson2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { NJson::TJsonReaderConfig jsonCfg; jsonCfg.DontValidateUtf8 = true; jsonCfg.AllowComments = config.AllowComments; @@ -413,16 +413,16 @@ namespace NProtobufJson { NJson::TJsonValue jsonValue; ReadJsonTree(json, &jsonCfg, &jsonValue, /* throwOnError = */ true); - MergeJson2Proto(jsonValue, proto, config); + MergeJson2Proto(jsonValue, proto, config); } - void Json2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { - proto.Clear(); - MergeJson2Proto(json, proto, config); - } - - void Json2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { - proto.Clear(); - MergeJson2Proto(json, proto, config); - } + void Json2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { + proto.Clear(); + MergeJson2Proto(json, proto, config); + } + + void Json2Proto(const TStringBuf& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config) { + proto.Clear(); + MergeJson2Proto(json, proto, config); + } } diff --git a/library/cpp/protobuf/json/json2proto.h b/library/cpp/protobuf/json/json2proto.h index 091b53730f..4c33498dfa 100644 --- a/library/cpp/protobuf/json/json2proto.h +++ b/library/cpp/protobuf/json/json2proto.h @@ -73,11 +73,11 @@ namespace NProtobufJson { return *this; } - TSelf& SetReplaceRepeatedFields(bool replaceRepeatedFields) { - ReplaceRepeatedFields = replaceRepeatedFields; - return *this; - } - + TSelf& SetReplaceRepeatedFields(bool replaceRepeatedFields) { + ReplaceRepeatedFields = replaceRepeatedFields; + return *this; + } + TSelf& SetNameGenerator(TNameGenerator callback) { NameGenerator = callback; return *this; @@ -127,9 +127,9 @@ 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; - + /// Replace repeated fields content during merging + bool ReplaceRepeatedFields = false; + /// Custom field names generator. TNameGenerator NameGenerator = {}; @@ -147,20 +147,20 @@ namespace NProtobufJson { }; /// @throw yexception - void MergeJson2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, - const TJson2ProtoConfig& config = TJson2ProtoConfig()); - - /// @throw yexception - void MergeJson2Proto(const TStringBuf& json, google::protobuf::Message& proto, - const TJson2ProtoConfig& config = TJson2ProtoConfig()); - - /// @throw yexception - inline void MergeJson2Proto(const TString& json, google::protobuf::Message& proto, - const TJson2ProtoConfig& config = TJson2ProtoConfig()) { - MergeJson2Proto(TStringBuf(json), proto, config); - } - - /// @throw yexception + void MergeJson2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, + const TJson2ProtoConfig& config = TJson2ProtoConfig()); + + /// @throw yexception + void MergeJson2Proto(const TStringBuf& json, google::protobuf::Message& proto, + const TJson2ProtoConfig& config = TJson2ProtoConfig()); + + /// @throw yexception + inline void MergeJson2Proto(const TString& json, google::protobuf::Message& proto, + const TJson2ProtoConfig& config = TJson2ProtoConfig()) { + MergeJson2Proto(TStringBuf(json), proto, config); + } + + /// @throw yexception void Json2Proto(const NJson::TJsonValue& json, google::protobuf::Message& proto, const TJson2ProtoConfig& config = TJson2ProtoConfig()); diff --git a/library/cpp/protobuf/json/ut/json2proto_ut.cpp b/library/cpp/protobuf/json/ut/json2proto_ut.cpp index ab1f8fcb8b..0dfe57bc7a 100644 --- a/library/cpp/protobuf/json/ut/json2proto_ut.cpp +++ b/library/cpp/protobuf/json/ut/json2proto_ut.cpp @@ -987,122 +987,122 @@ Y_UNIT_TEST(TestMapAsObjectConfigNotSet) { Json2Proto<TMapType>(modelStr, config), yexception, "Map as object representation is not allowed"); } // TestMapAsObjectNotSet - -Y_UNIT_TEST(TestMergeFlatOptional) { - const NJson::TJsonValue& json = CreateFlatJson(); - - NJson::TJsonValue patch; - patch["I32"] = 5; - patch["Bool"] = false; - patch["String"] = "abacaba"; - patch["Double"] = 0.123; - - TFlatOptional proto; - UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); - UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); - - TFlatRequired modelProto; - FillFlatProto(&modelProto); - modelProto.SetI32(5); - modelProto.SetBool(false); - modelProto.SetString("abacaba"); - modelProto.SetDouble(0.123); - - UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); -} // TestMergeFlatOptional - -Y_UNIT_TEST(TestMergeFlatRequired) { - const NJson::TJsonValue& json = CreateFlatJson(); - - NJson::TJsonValue patch; - patch["I32"] = 5; - patch["Bool"] = false; - patch["String"] = "abacaba"; - patch["Double"] = 0.123; - - TFlatRequired proto; - UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); - UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); - - TFlatRequired modelProto; - FillFlatProto(&modelProto); - modelProto.SetI32(5); - modelProto.SetBool(false); - modelProto.SetString("abacaba"); - modelProto.SetDouble(0.123); - - UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); -} // TestMergeFlatRequired - -Y_UNIT_TEST(TestMergeComposite) { - const NJson::TJsonValue& json = CreateCompositeJson(); - - NJson::TJsonValue patch; - patch["Part"]["I32"] = 5; - patch["Part"]["Bool"] = false; - patch["Part"]["String"] = "abacaba"; - patch["Part"]["Double"] = 0.123; - - TCompositeOptional proto; - UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); - UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); - - TCompositeOptional modelProto; - FillCompositeProto(&modelProto); - modelProto.MutablePart()->SetI32(5); - modelProto.MutablePart()->SetBool(false); - modelProto.MutablePart()->SetString("abacaba"); - modelProto.MutablePart()->SetDouble(0.123); - - UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); -} // TestMergeComposite - -Y_UNIT_TEST(TestMergeRepeatedReplace) { - const NJson::TJsonValue& json = CreateRepeatedFlatJson(); - - NJson::TJsonValue patch; - patch["I32"].AppendValue(5); - patch["I32"].AppendValue(6); - patch["String"].AppendValue("abacaba"); - - TFlatRepeated proto; - TJson2ProtoConfig config; - config.ReplaceRepeatedFields = true; - UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); - UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto, config)); - - TFlatRepeated modelProto; - FillRepeatedProto(&modelProto); - modelProto.ClearI32(); - modelProto.AddI32(5); - modelProto.AddI32(6); - modelProto.ClearString(); - modelProto.AddString("abacaba"); - - UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); -} // TestMergeRepeatedReplace - -Y_UNIT_TEST(TestMergeRepeatedAppend) { - const NJson::TJsonValue& json = CreateRepeatedFlatJson(); - - NJson::TJsonValue patch; - patch["I32"].AppendValue(5); - patch["I32"].AppendValue(6); - patch["String"].AppendValue("abacaba"); - - TFlatRepeated proto; - UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); - UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); - - TFlatRepeated modelProto; - FillRepeatedProto(&modelProto); - modelProto.AddI32(5); - modelProto.AddI32(6); - modelProto.AddString("abacaba"); - - UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); -} // TestMergeRepeatedAppend - + +Y_UNIT_TEST(TestMergeFlatOptional) { + const NJson::TJsonValue& json = CreateFlatJson(); + + NJson::TJsonValue patch; + patch["I32"] = 5; + patch["Bool"] = false; + patch["String"] = "abacaba"; + patch["Double"] = 0.123; + + TFlatOptional proto; + UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); + UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); + + TFlatRequired modelProto; + FillFlatProto(&modelProto); + modelProto.SetI32(5); + modelProto.SetBool(false); + modelProto.SetString("abacaba"); + modelProto.SetDouble(0.123); + + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); +} // TestMergeFlatOptional + +Y_UNIT_TEST(TestMergeFlatRequired) { + const NJson::TJsonValue& json = CreateFlatJson(); + + NJson::TJsonValue patch; + patch["I32"] = 5; + patch["Bool"] = false; + patch["String"] = "abacaba"; + patch["Double"] = 0.123; + + TFlatRequired proto; + UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); + UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); + + TFlatRequired modelProto; + FillFlatProto(&modelProto); + modelProto.SetI32(5); + modelProto.SetBool(false); + modelProto.SetString("abacaba"); + modelProto.SetDouble(0.123); + + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); +} // TestMergeFlatRequired + +Y_UNIT_TEST(TestMergeComposite) { + const NJson::TJsonValue& json = CreateCompositeJson(); + + NJson::TJsonValue patch; + patch["Part"]["I32"] = 5; + patch["Part"]["Bool"] = false; + patch["Part"]["String"] = "abacaba"; + patch["Part"]["Double"] = 0.123; + + TCompositeOptional proto; + UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); + UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); + + TCompositeOptional modelProto; + FillCompositeProto(&modelProto); + modelProto.MutablePart()->SetI32(5); + modelProto.MutablePart()->SetBool(false); + modelProto.MutablePart()->SetString("abacaba"); + modelProto.MutablePart()->SetDouble(0.123); + + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); +} // TestMergeComposite + +Y_UNIT_TEST(TestMergeRepeatedReplace) { + const NJson::TJsonValue& json = CreateRepeatedFlatJson(); + + NJson::TJsonValue patch; + patch["I32"].AppendValue(5); + patch["I32"].AppendValue(6); + patch["String"].AppendValue("abacaba"); + + TFlatRepeated proto; + TJson2ProtoConfig config; + config.ReplaceRepeatedFields = true; + UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); + UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto, config)); + + TFlatRepeated modelProto; + FillRepeatedProto(&modelProto); + modelProto.ClearI32(); + modelProto.AddI32(5); + modelProto.AddI32(6); + modelProto.ClearString(); + modelProto.AddString("abacaba"); + + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); +} // TestMergeRepeatedReplace + +Y_UNIT_TEST(TestMergeRepeatedAppend) { + const NJson::TJsonValue& json = CreateRepeatedFlatJson(); + + NJson::TJsonValue patch; + patch["I32"].AppendValue(5); + patch["I32"].AppendValue(6); + patch["String"].AppendValue("abacaba"); + + TFlatRepeated proto; + UNIT_ASSERT_NO_EXCEPTION(Json2Proto(json, proto)); + UNIT_ASSERT_NO_EXCEPTION(MergeJson2Proto(patch, proto)); + + TFlatRepeated modelProto; + FillRepeatedProto(&modelProto); + modelProto.AddI32(5); + modelProto.AddI32(6); + modelProto.AddString("abacaba"); + + UNIT_ASSERT_PROTOS_EQUAL(proto, modelProto); +} // TestMergeRepeatedAppend + Y_UNIT_TEST(TestEmptyStringForCastFromString) { NJson::TJsonValue json; json["I32"] = ""; |