aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-11-13 12:05:10 +0300
committerarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-11-13 12:51:37 +0300
commitdfc4818ec613d6908c1acb5450e7814d07a11dcd (patch)
tree4d6f33c1ea98027b9289545000b87e63133066d1
parentab2faef909ba8ab6a961784263f54a81056b38c9 (diff)
downloadydb-dfc4818ec613d6908c1acb5450e7814d07a11dcd.tar.gz
Fix YsonStruct comparison
commit_hash:eac657d72f016fe36e37cdee3dc3f9299fed9171
-rw-r--r--yt/yt/core/ytree/unittests/yson_struct_ut.cpp8
-rw-r--r--yt/yt/core/ytree/yson_struct_detail-inl.h84
2 files changed, 91 insertions, 1 deletions
diff --git a/yt/yt/core/ytree/unittests/yson_struct_ut.cpp b/yt/yt/core/ytree/unittests/yson_struct_ut.cpp
index 187b067c82..eb66de318f 100644
--- a/yt/yt/core/ytree/unittests/yson_struct_ut.cpp
+++ b/yt/yt/core/ytree/unittests/yson_struct_ut.cpp
@@ -2943,6 +2943,7 @@ struct TComparableYsonStruct
std::vector<int> Values;
TIntrusivePtr<TSimpleYsonStruct> SimpleSubStruct;
+ THashMap<int, TIntrusivePtr<TSimpleYsonStruct>> Mapping;
bool UnregisteredValue = false;
@@ -2958,6 +2959,13 @@ struct TComparableYsonStruct
ptr->IntValue = 77;
return ptr;
});
+ registrar.Parameter("mapping", &TThis::Mapping)
+ .DefaultCtor([] {
+ THashMap<int, TIntrusivePtr<TSimpleYsonStruct>> mapping = {};
+ mapping[42] = New<TSimpleYsonStruct>();
+ return mapping;
+ });
+
registrar.Parameter("values", &TThis::Values)
.Default({1, 2, 3});
}
diff --git a/yt/yt/core/ytree/yson_struct_detail-inl.h b/yt/yt/core/ytree/yson_struct_detail-inl.h
index 71e7157d9c..3db366c328 100644
--- a/yt/yt/core/ytree/yson_struct_detail-inl.h
+++ b/yt/yt/core/ytree/yson_struct_detail-inl.h
@@ -593,6 +593,29 @@ inline void ResetOnLoad(TMap& parameter)
////////////////////////////////////////////////////////////////////////////////
+// Any T.
+template <class T>
+bool CompareValue(const T& lhs, const T& rhs);
+
+// TIntrusivePtr.
+template <class T>
+bool CompareValues(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs);
+
+// std::optional.
+template <class T>
+bool CompareValues(const std::optional<T>& lhs, const std::optional<T>& rhs);
+
+// std::vector.
+template <CStdVector T>
+bool CompareValues(const T& lhs, const T& rhs);
+
+// any map.
+template <CAnyMap T>
+bool CompareValues(const T& lhs, const T& rhs);
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Any T.
template <class T>
bool CompareValues(const T& lhs, const T& rhs)
{
@@ -603,6 +626,7 @@ bool CompareValues(const T& lhs, const T& rhs)
}
}
+// TIntrusivePtr.
template <class T>
bool CompareValues(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
{
@@ -611,12 +635,70 @@ bool CompareValues(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
return rhs == lhs;
}
- return *lhs == *rhs;
+ return CompareValues(*lhs, *rhs);
} else {
return false;
}
}
+// std::optional.
+template <class T>
+bool CompareValues(const std::optional<T>& lhs, const std::optional<T>& rhs)
+{
+ if (lhs.has_value() != rhs.has_value()) {
+ return false;
+ }
+
+ if (!lhs.has_value()) {
+ return true;
+ }
+
+ return CompareValues(*lhs, *rhs);
+}
+
+// std::vector.
+template <CStdVector T>
+bool CompareValues(const T& lhs, const T& rhs)
+{
+ if (std::ssize(lhs) != std::ssize(rhs)) {
+ return false;
+ }
+
+ for (int idx = 0; idx < std::ssize(lhs); ++idx) {
+ if (!CompareValues(lhs[idx], rhs[idx])) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+// any map.
+template <CAnyMap T>
+bool CompareValues(const T& lhs, const T& rhs)
+{
+ if (std::ssize(lhs) != std::ssize(rhs)) {
+ return false;
+ }
+
+ for (const auto& [key, value] : lhs) {
+ auto rhsIt = rhs.find(key);
+ if (rhsIt == std::end(rhs)) {
+ return false;
+ }
+
+ if (!CompareValues(key, rhsIt->first)) {
+ return false;
+ }
+
+ if (!CompareValues(value, rhsIt->second)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
} // namespace NPrivate
////////////////////////////////////////////////////////////////////////////////