aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbulatman <bulatman@yandex-team.com>2024-07-23 19:30:08 +0300
committerbulatman <bulatman@yandex-team.com>2024-07-23 19:44:09 +0300
commitbe7891c3b3849f81c01b7f339fae2dd4a1b0c6a6 (patch)
tree2766d15e5c7e9ac4a8c4404ec33f4c69731fba49
parent99c13c34c671f7808aaf2c61c9552ae9d4c44fb4 (diff)
downloadydb-be7891c3b3849f81c01b7f339fae2dd4a1b0c6a6.tar.gz
Fix casting upper snake case to lower snake case in protobuf interop
eb272474ad75533b04b772af3caecf07d7df6c1c
-rw-r--r--yt/yt/core/yson/protobuf_interop.cpp11
-rw-r--r--yt/yt/core/yson/unittests/proto/protobuf_yson_casing_ut.proto7
-rw-r--r--yt/yt/core/yson/unittests/protobuf_yson_ut.cpp2
3 files changed, 15 insertions, 5 deletions
diff --git a/yt/yt/core/yson/protobuf_interop.cpp b/yt/yt/core/yson/protobuf_interop.cpp
index 5900fe24d2..59f6291587 100644
--- a/yt/yt/core/yson/protobuf_interop.cpp
+++ b/yt/yt/core/yson/protobuf_interop.cpp
@@ -132,14 +132,15 @@ bool IsMapKeyType(FieldDescriptor::Type type)
TString ToUnderscoreCase(const TString& protobufName)
{
TStringBuilder builder;
- for (auto ch : protobufName) {
- if (isupper(ch)) {
- if (builder.GetLength() > 0 && builder.GetBuffer()[builder.GetLength() - 1] != '_') {
+ for (size_t i = 0; i < protobufName.size(); ++i) {
+ if (isupper(protobufName[i])) {
+ size_t length = builder.GetLength();
+ if (length && builder.GetBuffer()[length - 1] != '_' && !isupper(protobufName[i - 1])) {
builder.AppendChar('_');
}
- builder.AppendChar(tolower(ch));
+ builder.AppendChar(tolower(protobufName[i]));
} else {
- builder.AppendChar(ch);
+ builder.AppendChar(protobufName[i]);
}
}
return builder.Flush();
diff --git a/yt/yt/core/yson/unittests/proto/protobuf_yson_casing_ut.proto b/yt/yt/core/yson/unittests/proto/protobuf_yson_casing_ut.proto
index 627d6e23ad..65fea7bc69 100644
--- a/yt/yt/core/yson/unittests/proto/protobuf_yson_casing_ut.proto
+++ b/yt/yt/core/yson/unittests/proto/protobuf_yson_casing_ut.proto
@@ -6,7 +6,14 @@ option (NYT.NYson.NProto.derive_underscore_case_names) = true;
message TCamelCaseStyleMessage
{
+ enum EEnum
+ {
+ VALUE_NONE = 0;
+ VALUE_FIRST = 1;
+ }
+
optional int32 SomeField = 1;
optional int32 AnotherField123 = 2;
optional int32 Crazy_Field = 3;
+ optional EEnum EnumField = 4;
}
diff --git a/yt/yt/core/yson/unittests/protobuf_yson_ut.cpp b/yt/yt/core/yson/unittests/protobuf_yson_ut.cpp
index ebc8afc360..d6d97d6c1f 100644
--- a/yt/yt/core/yson/unittests/protobuf_yson_ut.cpp
+++ b/yt/yt/core/yson/unittests/protobuf_yson_ut.cpp
@@ -1822,6 +1822,7 @@ TEST(TProtobufToYsonTest, Casing)
message.set_somefield(1);
message.set_anotherfield123(2);
message.set_crazy_field(3);
+ message.set_enumfield(NYT::NYson::NProto::TCamelCaseStyleMessage::VALUE_FIRST);
TEST_PROLOGUE()
Y_PROTOBUF_SUPPRESS_NODISCARD message.SerializeToCodedStream(&codedStream);
@@ -1833,6 +1834,7 @@ TEST(TProtobufToYsonTest, Casing)
.Item("some_field").Value(1)
.Item("another_field123").Value(2)
.Item("crazy_field").Value(3)
+ .Item("enum_field").Value("value_first")
.EndMap();
EXPECT_TRUE(AreNodesEqual(writtenNode, expectedNode));
}