diff options
| author | vskipin <[email protected]> | 2022-02-10 16:46:00 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:46:00 +0300 | 
| commit | 4d8b546b89b5afc08cf3667e176271c7ba935f33 (patch) | |
| tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /library/cpp/scheme | |
| parent | 4e4b78bd7b67e2533da4dbb9696374a6d6068e32 (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/scheme')
| -rw-r--r-- | library/cpp/scheme/scheme.h | 4 | ||||
| -rw-r--r-- | library/cpp/scheme/scimpl_protobuf.cpp | 126 | ||||
| -rw-r--r-- | library/cpp/scheme/tests/ut/scheme_proto_ut.cpp | 22 | 
3 files changed, 76 insertions, 76 deletions
diff --git a/library/cpp/scheme/scheme.h b/library/cpp/scheme/scheme.h index 857a7fb9e0b..3d7c59f3c97 100644 --- a/library/cpp/scheme/scheme.h +++ b/library/cpp/scheme/scheme.h @@ -388,7 +388,7 @@ namespace NSc {          static TValue From(const ::google::protobuf::Message&, bool mapAsDict = false);          void To(::google::protobuf::Message&, const TProtoOpts& opts = {}) const; -  +      public:          inline explicit TValue(TPoolPtr&); @@ -420,7 +420,7 @@ namespace NSc {          static TValue FromField(const ::google::protobuf::Message&, const ::google::protobuf::FieldDescriptor*);          static TValue FromRepeatedField(const ::google::protobuf::Message&, const ::google::protobuf::FieldDescriptor*, int index); -  +          void ValueToField(const TValue& value, ::google::protobuf::Message&, const ::google::protobuf::FieldDescriptor*, const TProtoOpts& opts) const;          void ToField(::google::protobuf::Message&, const ::google::protobuf::FieldDescriptor*, const TProtoOpts& opts) const;          void ToEnumField(::google::protobuf::Message&, const ::google::protobuf::FieldDescriptor*, const TProtoOpts& opts) const; diff --git a/library/cpp/scheme/scimpl_protobuf.cpp b/library/cpp/scheme/scimpl_protobuf.cpp index e24be0aad3b..0c99122c69a 100644 --- a/library/cpp/scheme/scimpl_protobuf.cpp +++ b/library/cpp/scheme/scimpl_protobuf.cpp @@ -130,41 +130,41 @@ namespace NSc {      }      void TValue::To(Message& msg, const TProtoOpts& opts) const { -        msg.Clear();  -  -        if (IsNull()) {  -            return;  -        }  -  -        if (!IsDict()) {  -            ythrow TSchemeException() << "expected dictionary";  -        }  -  -        const Descriptor* descriptor = msg.GetDescriptor();  -        for (int i = 0, count = descriptor->field_count(); i < count; ++i) {  -            const FieldDescriptor* field = descriptor->field(i);  +        msg.Clear(); + +        if (IsNull()) { +            return; +        } + +        if (!IsDict()) { +            ythrow TSchemeException() << "expected dictionary"; +        } + +        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()) {                  ToMapField(msg, field, opts);              } else if (field->is_repeated()) {                  ToRepeatedField(msg, field, opts); -            } else {  +            } else {                  ToField(msg, field, opts); -            }  -        }  -    }  -  +            } +        } +    } +      void TValue::ValueToField(const TValue& value, Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const {          const TString& name = field->name(); -        if (value.IsNull()) {  -            if (field->is_required() && !field->has_default_value()) {  -                ythrow TSchemeException() << "has no value for required field " << name;  -            }  -            return;  -        }  -  -        const Reflection* reflection = msg.GetReflection();  -  -        switch (field->cpp_type()) {  +        if (value.IsNull()) { +            if (field->is_required() && !field->has_default_value()) { +                ythrow TSchemeException() << "has no value for required field " << name; +            } +            return; +        } + +        const Reflection* reflection = msg.GetReflection(); + +        switch (field->cpp_type()) {              case FieldDescriptor::CPPTYPE_INT32:                  reflection->SetInt32(&msg, field, value.ForceIntNumber());                  break; @@ -199,9 +199,9 @@ namespace NSc {                  ythrow TSchemeException()                      << "field " << field->full_name()                      << " unexpected type " << (int)field->cpp_type(); -        }  -    }  -  +        } +    } +      void TValue::ToField(Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const {          const TString& name = field->name();          const TValue& value = Get(name); @@ -209,49 +209,49 @@ namespace NSc {      }      void TValue::ToEnumField(Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const { -        const EnumDescriptor* enumField = field->enum_type();  -  -        const EnumValueDescriptor* enumFieldValue = IsString()  +        const EnumDescriptor* enumField = field->enum_type(); + +        const EnumValueDescriptor* enumFieldValue = IsString()                                                          ? enumField->FindValueByName(ForceString())                                                          : enumField->FindValueByNumber(ForceIntNumber()); -  -        if (!enumFieldValue) {  + +        if (!enumFieldValue) {              if (opts.UnknownEnumValueIsDefault) {                  enumFieldValue = field->default_value_enum();              } else {                  ythrow TSchemeException() << "invalid value of enum field " << field->name();              } -        }  -  -        const Reflection* reflection = msg.GetReflection();  -  -        if (field->is_repeated()) {  -            reflection->AddEnum(&msg, field, enumFieldValue);  -        } else {  -            reflection->SetEnum(&msg, field, enumFieldValue);  -        }  -    }  -  +        } + +        const Reflection* reflection = msg.GetReflection(); + +        if (field->is_repeated()) { +            reflection->AddEnum(&msg, field, enumFieldValue); +        } else { +            reflection->SetEnum(&msg, field, enumFieldValue); +        } +    } +      void TValue::ToRepeatedField(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()) {  + +        const TValue& fieldValue = Get(name); +        if (fieldValue.IsNull()) { +            return; +        } + +        if (!fieldValue.IsArray()) {              if (opts.SkipTypeMismatch) {                  return; // leave repeated field empty              } else {                  ythrow TSchemeException() << "invalid type of repeated field " << name << ": not an array";              } -        }  -  -        const Reflection* reflection = msg.GetReflection();  -  +        } + +        const Reflection* reflection = msg.GetReflection(); +          for (const TValue& value : fieldValue.GetArray()) { -            switch (field->cpp_type()) {  +            switch (field->cpp_type()) {                  case FieldDescriptor::CPPTYPE_INT32:                      reflection->AddInt32(&msg, field, value.ForceIntNumber());                      break; @@ -286,10 +286,10 @@ namespace NSc {                      ythrow TSchemeException()                          << "field " << field->full_name()                          << " unexpected type " << (int)field->cpp_type(); -            }  -        }  -    }  -  +            } +        } +    } +      void TValue::ToMapField(Message& msg, const FieldDescriptor* field, const TProtoOpts& opts) const {          const TString& name = field->name(); diff --git a/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp b/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp index 24c2addac9d..e711a0d0925 100644 --- a/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_proto_ut.cpp @@ -8,12 +8,12 @@ Y_UNIT_TEST_SUITE(TSchemeProtoTest) {      Y_UNIT_TEST(TestFromProtobuf) {          DoTestProtobuf(true, false); -    }  -  +    } +      Y_UNIT_TEST(TestToProtobuf) {          DoTestProtobuf(false, false); -    }  -  +    } +      Y_UNIT_TEST(TestFromProtobufWithDict) {          DoTestProtobuf(true, true);      } @@ -207,14 +207,14 @@ Y_UNIT_TEST_SUITE(TSchemeProtoTest) {          *(m.AddMessages()) = m2, v["Messages"][0] = v2;          *(m.AddMessages()) = m2, v["Messages"][1] = v2; -        if (fromProto) {  +        if (fromProto) {              UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::From(m, mapAsDict))); -        } else {  -            NSc::TMessage proto;  -            v.To(proto);  -  +        } else { +            NSc::TMessage proto; +            v.To(proto); +              TString differentPath; -            UNIT_ASSERT_C(NProtoBuf::IsEqual(m, proto, &differentPath), differentPath);  -        }  +            UNIT_ASSERT_C(NProtoBuf::IsEqual(m, proto, &differentPath), differentPath); +        }      }  };  | 
