diff options
author | isiv <isiv@yandex-team.ru> | 2022-02-10 16:47:54 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:54 +0300 |
commit | 40aa65b121746185908e51e32dca11ccee70140b (patch) | |
tree | c0748b5dcbade83af788c0abfa89c0383d6b779c /library/cpp | |
parent | f20e3f1949626df2ef9d81d7618ec367b0c34f6b (diff) | |
download | ydb-40aa65b121746185908e51e32dca11ccee70140b.tar.gz |
Restoring authorship annotation for <isiv@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/scheme/scheme.h | 2 | ||||
-rw-r--r-- | library/cpp/scheme/scimpl_protobuf.cpp | 90 | ||||
-rw-r--r-- | library/cpp/scheme/tests/ut/scheme_proto_ut.cpp | 90 | ||||
-rw-r--r-- | library/cpp/scheme/tests/ut/scheme_ut.proto | 8 |
4 files changed, 95 insertions, 95 deletions
diff --git a/library/cpp/scheme/scheme.h b/library/cpp/scheme/scheme.h index dc82028a89..3d7c59f3c9 100644 --- a/library/cpp/scheme/scheme.h +++ b/library/cpp/scheme/scheme.h @@ -385,7 +385,7 @@ namespace NSc { public: // very specific methods useful in very specific corner cases - static TValue From(const ::google::protobuf::Message&, bool mapAsDict = false); + static TValue From(const ::google::protobuf::Message&, bool mapAsDict = false); void To(::google::protobuf::Message&, const TProtoOpts& opts = {}) const; diff --git a/library/cpp/scheme/scimpl_protobuf.cpp b/library/cpp/scheme/scimpl_protobuf.cpp index ff2e7e0d59..0c99122c69 100644 --- a/library/cpp/scheme/scimpl_protobuf.cpp +++ b/library/cpp/scheme/scimpl_protobuf.cpp @@ -10,7 +10,7 @@ using namespace google::protobuf; namespace NSc { - TValue TValue::From(const Message& msg, bool mapAsDict) { + TValue TValue::From(const Message& msg, bool mapAsDict) { TValue v; const Reflection* r = msg.GetReflection(); TVector<const FieldDescriptor*> fields; @@ -22,18 +22,18 @@ namespace NSc { const FieldDescriptor* field = *it; try { if (field->is_repeated()) { - if (field->is_map() && mapAsDict) { + if (field->is_map() && mapAsDict) { auto& elem = v[field->name()]; - for (int i2 = 0; i2 < r->FieldSize(msg, field); ++i2) { - auto val = FromRepeatedField(msg, field, i2); - if (val.IsDict()) { - elem[TStringBuf(val["key"])] = val["value"]; - } - } - } else { - for (int i2 = 0; i2 < r->FieldSize(msg, field); ++i2) - v[field->name()][i2] = FromRepeatedField(msg, field, i2); - } + for (int i2 = 0; i2 < r->FieldSize(msg, field); ++i2) { + auto val = FromRepeatedField(msg, field, i2); + if (val.IsDict()) { + elem[TStringBuf(val["key"])] = val["value"]; + } + } + } else { + for (int i2 = 0; i2 < r->FieldSize(msg, field); ++i2) + v[field->name()][i2] = FromRepeatedField(msg, field, i2); + } } else { v[field->name()] = FromField(msg, field); } @@ -143,9 +143,9 @@ namespace NSc { const Descriptor* descriptor = msg.GetDescriptor(); for (int i = 0, count = descriptor->field_count(); i < count; ++i) { const FieldDescriptor* field = descriptor->field(i); - if (field->is_map()) { + if (field->is_map()) { ToMapField(msg, field, opts); - } else if (field->is_repeated()) { + } else if (field->is_repeated()) { ToRepeatedField(msg, field, opts); } else { ToField(msg, field, opts); @@ -203,11 +203,11 @@ namespace NSc { } void TValue::ToField(Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const { - const TString& name = field->name(); - const TValue& value = Get(name); + const TString& name = field->name(); + const TValue& value = Get(name); ValueToField(value, msg, field, opts); - } - + } + void TValue::ToEnumField(Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const { const EnumDescriptor* enumField = field->enum_type(); @@ -291,39 +291,39 @@ namespace NSc { } void TValue::ToMapField(Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const { - const TString& name = field->name(); - - const TValue& fieldValue = Get(name); - if (fieldValue.IsNull()) { - return; - } - - if (fieldValue.IsArray()) { - // read dict from key, value array + const TString& name = field->name(); + + const TValue& fieldValue = Get(name); + if (fieldValue.IsNull()) { + return; + } + + if (fieldValue.IsArray()) { + // read dict from key, value array ToRepeatedField(msg, field, opts); - return; - } - - if (!fieldValue.IsDict()) { + return; + } + + if (!fieldValue.IsDict()) { if (opts.SkipTypeMismatch) { return; // leave map field empty } else { ythrow TSchemeException() << "invalid type of map field " << name << ": not dict or array"; } - } - - const Reflection* reflection = msg.GetReflection(); - - auto mutableField = reflection->GetMutableRepeatedFieldRef<Message>(&msg, field); - for (const auto& value : fieldValue.GetDict()) { + } + + const Reflection* reflection = msg.GetReflection(); + + auto mutableField = reflection->GetMutableRepeatedFieldRef<Message>(&msg, field); + for (const auto& value : fieldValue.GetDict()) { THolder<Message> entry(mutableField.NewMessage()); - auto entryDesc = entry->GetDescriptor(); - auto keyField = entryDesc->FindFieldByNumber(1); - auto valueField = entryDesc->FindFieldByNumber(2); - auto entryReflection = entry->GetReflection(); - entryReflection->SetString(entry.Get(), keyField, TString(value.first)); + auto entryDesc = entry->GetDescriptor(); + auto keyField = entryDesc->FindFieldByNumber(1); + auto valueField = entryDesc->FindFieldByNumber(2); + auto entryReflection = entry->GetReflection(); + entryReflection->SetString(entry.Get(), keyField, TString(value.first)); ValueToField(value.second, *entry, valueField, opts); - mutableField.Add(*entry); - } - } + mutableField.Add(*entry); + } + } } diff --git a/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp b/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp index ae670640ef..e711a0d092 100644 --- a/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp @@ -16,23 +16,23 @@ Y_UNIT_TEST_SUITE(TSchemeProtoTest) { Y_UNIT_TEST(TestFromProtobufWithDict) { DoTestProtobuf(true, true); - } - + } + Y_UNIT_TEST(TestToProtobufWithDict) { DoTestProtobuf(false, true); - } - - template <class T> - void AddMapPair(NSc::TValue& v, TStringBuf key, T value, bool mapAsDict) { - if (mapAsDict) { - v[key] = value; - } else { + } + + template <class T> + void AddMapPair(NSc::TValue& v, TStringBuf key, T value, bool mapAsDict) { + if (mapAsDict) { + v[key] = value; + } else { auto& newElement = v.Push(); - newElement["key"] = key; - newElement["value"] = value; - } - } - + newElement["key"] = key; + newElement["value"] = value; + } + } + void DoTestProtobuf(bool fromProto, bool mapAsDict) { NSc::TMessage m; NSc::TValue v; @@ -53,36 +53,36 @@ Y_UNIT_TEST_SUITE(TSchemeProtoTest) { m.SetBytes("Bytes"), v["Bytes"] = "Bytes"; m.SetEnum(NSc::VALUE1), v["Enum"] = "VALUE1"; - auto& mapDoublesP = *m.mutable_mapdoubles(); - auto& mapDoublesV = v["MapDoubles"]; - mapDoublesP["pi"] = 3.14; - AddMapPair(mapDoublesV, "pi", 3.14, mapAsDict); - if (fromProto && mapAsDict) { - // can not add two entries in dict, as it represent over array with unstable order - mapDoublesP["back_e"] = 1 / 2.7; - AddMapPair(mapDoublesV, "back_e", 1 / 2.7, mapAsDict); - } - - auto& mapInt32sP = *m.mutable_mapint32s(); - auto& mapInt32sV = v["MapInt32s"]; - mapInt32sP["pi"] = -7; - AddMapPair(mapInt32sV, "pi", -7, mapAsDict); - if (fromProto && mapAsDict) { - // can not add two entries in dict, as it represent over array with unstable order - mapInt32sP["back_e"] = 42; - AddMapPair(mapInt32sV, "back_e", 42, mapAsDict); - } - - auto& mapStringP = *m.mutable_mapstring(); - auto& mapStringV = v["MapString"]; - mapStringP["intro"] = "body"; - AddMapPair(mapStringV, "intro", "body", mapAsDict); - if (fromProto && mapAsDict) { - // can not add two entries in dict, as it represent over array with unstable order - mapStringP["deep"] = "blue"; - AddMapPair(mapStringV, "deep", "blue", mapAsDict); - } - + auto& mapDoublesP = *m.mutable_mapdoubles(); + auto& mapDoublesV = v["MapDoubles"]; + mapDoublesP["pi"] = 3.14; + AddMapPair(mapDoublesV, "pi", 3.14, mapAsDict); + if (fromProto && mapAsDict) { + // can not add two entries in dict, as it represent over array with unstable order + mapDoublesP["back_e"] = 1 / 2.7; + AddMapPair(mapDoublesV, "back_e", 1 / 2.7, mapAsDict); + } + + auto& mapInt32sP = *m.mutable_mapint32s(); + auto& mapInt32sV = v["MapInt32s"]; + mapInt32sP["pi"] = -7; + AddMapPair(mapInt32sV, "pi", -7, mapAsDict); + if (fromProto && mapAsDict) { + // can not add two entries in dict, as it represent over array with unstable order + mapInt32sP["back_e"] = 42; + AddMapPair(mapInt32sV, "back_e", 42, mapAsDict); + } + + auto& mapStringP = *m.mutable_mapstring(); + auto& mapStringV = v["MapString"]; + mapStringP["intro"] = "body"; + AddMapPair(mapStringV, "intro", "body", mapAsDict); + if (fromProto && mapAsDict) { + // can not add two entries in dict, as it represent over array with unstable order + mapStringP["deep"] = "blue"; + AddMapPair(mapStringV, "deep", "blue", mapAsDict); + } + m.AddDoubles((double)1 / 3), v["Doubles"][0] = (double)1 / 3; m.AddDoubles((double)1 / 4), v["Doubles"][1] = (double)1 / 4; @@ -208,7 +208,7 @@ Y_UNIT_TEST_SUITE(TSchemeProtoTest) { *(m.AddMessages()) = m2, v["Messages"][1] = v2; if (fromProto) { - UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::From(m, mapAsDict))); + UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::From(m, mapAsDict))); } else { NSc::TMessage proto; v.To(proto); diff --git a/library/cpp/scheme/tests/ut/scheme_ut.proto b/library/cpp/scheme/tests/ut/scheme_ut.proto index 1d89d990b3..7981af7eae 100644 --- a/library/cpp/scheme/tests/ut/scheme_ut.proto +++ b/library/cpp/scheme/tests/ut/scheme_ut.proto @@ -36,10 +36,10 @@ message TMessage { repeated bytes Bytess = 32; repeated EEnum Enums = 33; repeated TMessage2 Messages = 34; - - map<string, double> MapDoubles = 35; - map<string, int32> MapInt32s = 36; - map<string, string> MapString = 37; + + map<string, double> MapDoubles = 35; + map<string, int32> MapInt32s = 36; + map<string, string> MapString = 37; } enum EEnum { |