Skip to content

Commit dfc4818

Browse files
committed
Fix YsonStruct comparison
commit_hash:eac657d72f016fe36e37cdee3dc3f9299fed9171
1 parent ab2faef commit dfc4818

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

yt/yt/core/ytree/unittests/yson_struct_ut.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,6 +2943,7 @@ struct TComparableYsonStruct
29432943

29442944
std::vector<int> Values;
29452945
TIntrusivePtr<TSimpleYsonStruct> SimpleSubStruct;
2946+
THashMap<int, TIntrusivePtr<TSimpleYsonStruct>> Mapping;
29462947

29472948
bool UnregisteredValue = false;
29482949

@@ -2958,6 +2959,13 @@ struct TComparableYsonStruct
29582959
ptr->IntValue = 77;
29592960
return ptr;
29602961
});
2962+
registrar.Parameter("mapping", &TThis::Mapping)
2963+
.DefaultCtor([] {
2964+
THashMap<int, TIntrusivePtr<TSimpleYsonStruct>> mapping = {};
2965+
mapping[42] = New<TSimpleYsonStruct>();
2966+
return mapping;
2967+
});
2968+
29612969
registrar.Parameter("values", &TThis::Values)
29622970
.Default({1, 2, 3});
29632971
}

yt/yt/core/ytree/yson_struct_detail-inl.h

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,29 @@ inline void ResetOnLoad(TMap& parameter)
593593

594594
////////////////////////////////////////////////////////////////////////////////
595595

596+
// Any T.
597+
template <class T>
598+
bool CompareValue(const T& lhs, const T& rhs);
599+
600+
// TIntrusivePtr.
601+
template <class T>
602+
bool CompareValues(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs);
603+
604+
// std::optional.
605+
template <class T>
606+
bool CompareValues(const std::optional<T>& lhs, const std::optional<T>& rhs);
607+
608+
// std::vector.
609+
template <CStdVector T>
610+
bool CompareValues(const T& lhs, const T& rhs);
611+
612+
// any map.
613+
template <CAnyMap T>
614+
bool CompareValues(const T& lhs, const T& rhs);
615+
616+
////////////////////////////////////////////////////////////////////////////////
617+
618+
// Any T.
596619
template <class T>
597620
bool CompareValues(const T& lhs, const T& rhs)
598621
{
@@ -603,6 +626,7 @@ bool CompareValues(const T& lhs, const T& rhs)
603626
}
604627
}
605628

629+
// TIntrusivePtr.
606630
template <class T>
607631
bool CompareValues(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
608632
{
@@ -611,12 +635,70 @@ bool CompareValues(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
611635
return rhs == lhs;
612636
}
613637

614-
return *lhs == *rhs;
638+
return CompareValues(*lhs, *rhs);
615639
} else {
616640
return false;
617641
}
618642
}
619643

644+
// std::optional.
645+
template <class T>
646+
bool CompareValues(const std::optional<T>& lhs, const std::optional<T>& rhs)
647+
{
648+
if (lhs.has_value() != rhs.has_value()) {
649+
return false;
650+
}
651+
652+
if (!lhs.has_value()) {
653+
return true;
654+
}
655+
656+
return CompareValues(*lhs, *rhs);
657+
}
658+
659+
// std::vector.
660+
template <CStdVector T>
661+
bool CompareValues(const T& lhs, const T& rhs)
662+
{
663+
if (std::ssize(lhs) != std::ssize(rhs)) {
664+
return false;
665+
}
666+
667+
for (int idx = 0; idx < std::ssize(lhs); ++idx) {
668+
if (!CompareValues(lhs[idx], rhs[idx])) {
669+
return false;
670+
}
671+
}
672+
673+
return true;
674+
}
675+
676+
// any map.
677+
template <CAnyMap T>
678+
bool CompareValues(const T& lhs, const T& rhs)
679+
{
680+
if (std::ssize(lhs) != std::ssize(rhs)) {
681+
return false;
682+
}
683+
684+
for (const auto& [key, value] : lhs) {
685+
auto rhsIt = rhs.find(key);
686+
if (rhsIt == std::end(rhs)) {
687+
return false;
688+
}
689+
690+
if (!CompareValues(key, rhsIt->first)) {
691+
return false;
692+
}
693+
694+
if (!CompareValues(value, rhsIt->second)) {
695+
return false;
696+
}
697+
}
698+
699+
return true;
700+
}
701+
620702
} // namespace NPrivate
621703

622704
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)