Skip to content

Commit 5eb6126

Browse files
committed
Support GetStringReference in SimpleProtobufReflection
commit_hash:f4cebea4a1e97ab52be1c0d3c91ec4a1a0de6a39
1 parent 11a87b4 commit 5eb6126

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

library/cpp/protobuf/util/simple_reflection.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ namespace NProtoBuf {
5252
template <typename T>
5353
inline typename TSelectCppType<T>::T Get(size_t index = 0) const;
5454

55+
inline TStringBuf GetStringReference(TString* scratch) const {
56+
return GetStringReference(0, scratch);
57+
}
58+
59+
inline TStringBuf GetStringReference(size_t index, TString* scratch) const {
60+
Y_ASSERT(IsString());
61+
return IsRepeated() ? Refl().GetRepeatedStringReference(Msg, Fd, index, scratch)
62+
: Refl().GetStringReference(Msg, Fd, scratch);
63+
}
64+
5565
template <typename TMsg>
5666
inline const TMsg* GetAs(size_t index = 0) const {
5767
// casting version of Get

library/cpp/protobuf/util/simple_reflection_ut.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) {
1919
return smf;
2020
}
2121

22+
Y_UNIT_TEST(GetStringReference) {
23+
TSample src(GenSampleForMergeFrom());
24+
const Descriptor* descr = src.GetDescriptor();
25+
TString scratch;
26+
27+
TConstField srcOneStr(src, descr->FindFieldByName("OneStr"));
28+
UNIT_ASSERT_VALUES_EQUAL("one str"_sb, srcOneStr.GetStringReference(&scratch));
29+
30+
src.ClearOneStr();
31+
UNIT_ASSERT_VALUES_EQUAL(TStringBuf(), srcOneStr.GetStringReference(&scratch));
32+
33+
TConstField repStr(src, descr->FindFieldByName("RepStr"));
34+
UNIT_ASSERT_VALUES_EQUAL("two rep str"_sb, repStr.GetStringReference(1, &scratch));
35+
}
36+
2237
Y_UNIT_TEST(MergeFromGeneric) {
2338
const TSample src(GenSampleForMergeFrom());
2439
TSample dst;

0 commit comments

Comments
 (0)