diff options
author | dtorilov <dtorilov@yandex-team.com> | 2024-08-08 12:23:20 +0300 |
---|---|---|
committer | dtorilov <dtorilov@yandex-team.com> | 2024-08-08 12:33:33 +0300 |
commit | ed77962244e7018d6b7bd14af51dd890de3ea13f (patch) | |
tree | 34462833024cbfd59be31f7c1fdc282aa0fe0cc7 | |
parent | 093af125622cb9955164ea27efff424204bfac2b (diff) | |
download | ydb-ed77962244e7018d6b7bd14af51dd890de3ea13f.tar.gz |
YT-21306: Add EnumHasDefaultValue
d9b67f1778da2d15dd94f7285afe4e3490a233ab
-rw-r--r-- | library/cpp/yt/misc/cast-inl.h | 4 | ||||
-rw-r--r-- | library/cpp/yt/misc/enum.h | 14 | ||||
-rw-r--r-- | library/cpp/yt/string/enum-inl.h | 5 | ||||
-rw-r--r-- | yt/yt/core/bus/public.cpp | 8 | ||||
-rw-r--r-- | yt/yt/core/bus/public.h | 4 | ||||
-rw-r--r-- | yt/yt/core/misc/protobuf_helpers-inl.h | 12 | ||||
-rw-r--r-- | yt/yt_proto/yt/client/api/rpc_proxy/proto/api_service.proto | 2 |
7 files changed, 45 insertions, 4 deletions
diff --git a/library/cpp/yt/misc/cast-inl.h b/library/cpp/yt/misc/cast-inl.h index a694394f88..795682864f 100644 --- a/library/cpp/yt/misc/cast-inl.h +++ b/library/cpp/yt/misc/cast-inl.h @@ -124,6 +124,10 @@ T CheckedEnumCast(S value) { T result; if (!TryEnumCast<T>(value, &result)) { + if constexpr (TEnumHasDefaultValue<T>::value) { + return GetDefaultValue(T{}); + } + throw TSimpleException(Sprintf("Error casting %s value \"%d\" to enum %s", TypeName<S>().c_str(), static_cast<int>(value), diff --git a/library/cpp/yt/misc/enum.h b/library/cpp/yt/misc/enum.h index 954b63cbc0..11e168948f 100644 --- a/library/cpp/yt/misc/enum.h +++ b/library/cpp/yt/misc/enum.h @@ -197,6 +197,20 @@ constexpr bool None(E value) noexcept; //////////////////////////////////////////////////////////////////////////////// +template <typename E, typename = void> + requires TEnumTraits<E>::IsEnum +struct TEnumHasDefaultValue + : std::false_type +{ }; + +template <typename E> + requires TEnumTraits<E>::IsEnum +struct TEnumHasDefaultValue<E, std::void_t<decltype(GetDefaultValue(std::declval<E>()))>> + : std::is_same<decltype(GetDefaultValue(std::declval<E>())), E> +{ }; + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT #define ENUM_INL_H_ diff --git a/library/cpp/yt/string/enum-inl.h b/library/cpp/yt/string/enum-inl.h index 41f7197d15..19ba1f7fe5 100644 --- a/library/cpp/yt/string/enum-inl.h +++ b/library/cpp/yt/string/enum-inl.h @@ -87,6 +87,11 @@ T ParseEnum(TStringBuf value) if (auto optionalResult = TryParseEnum<T>(value)) { return *optionalResult; } + + if constexpr (TEnumHasDefaultValue<T>::value) { + return GetDefaultValue(T{}); + } + NYT::NDetail::ThrowMalformedEnumValueException(TEnumTraits<T>::GetTypeName(), value); } diff --git a/yt/yt/core/bus/public.cpp b/yt/yt/core/bus/public.cpp index 6cff5f25eb..0487d79fd6 100644 --- a/yt/yt/core/bus/public.cpp +++ b/yt/yt/core/bus/public.cpp @@ -9,5 +9,11 @@ const TString LocalNetworkName("local"); //////////////////////////////////////////////////////////////////////////////// -} // namespace NYT::NBus +EMultiplexingBand GetDefaultValue(EMultiplexingBand) +{ + return EMultiplexingBand::Default; +} + +//////////////////////////////////////////////////////////////////////////////// +} // namespace NYT::NBus diff --git a/yt/yt/core/bus/public.h b/yt/yt/core/bus/public.h index f5359d51b7..5a07031874 100644 --- a/yt/yt/core/bus/public.h +++ b/yt/yt/core/bus/public.h @@ -34,8 +34,11 @@ DEFINE_ENUM(EMultiplexingBand, ((Heavy) (2)) ((Interactive) (3)) ((RealTime) (4)) + ((Journal) (5)) ); +EMultiplexingBand GetDefaultValue(EMultiplexingBand); + YT_DEFINE_ERROR_ENUM( ((TransportError) (100)) ((SslError) (119)) @@ -59,4 +62,3 @@ extern const TString LocalNetworkName; //////////////////////////////////////////////////////////////////////////////// } // namespace NYT::NBus - diff --git a/yt/yt/core/misc/protobuf_helpers-inl.h b/yt/yt/core/misc/protobuf_helpers-inl.h index adf5a30851..00ae1906ff 100644 --- a/yt/yt/core/misc/protobuf_helpers-inl.h +++ b/yt/yt/core/misc/protobuf_helpers-inl.h @@ -181,7 +181,11 @@ template <class T> requires TEnumTraits<T>::IsEnum && (!TEnumTraits<T>::IsBitEnum) void FromProto(T* original, int serialized) { - *original = static_cast<T>(serialized); + if constexpr (TEnumHasDefaultValue<T>::value) { + *original = CheckedEnumCast<T>(serialized); + } else { + *original = static_cast<T>(serialized); + } } template <class T> @@ -195,7 +199,11 @@ template <class T> requires TEnumTraits<T>::IsBitEnum void FromProto(T* original, ui64 serialized) { - *original = static_cast<T>(serialized); + if constexpr (TEnumHasDefaultValue<T>::value) { + *original = CheckedEnumCast<T>(serialized); + } else { + *original = static_cast<T>(serialized); + } } //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt_proto/yt/client/api/rpc_proxy/proto/api_service.proto b/yt/yt_proto/yt/client/api/rpc_proxy/proto/api_service.proto index 39ef2f681b..0333b8d4f0 100644 --- a/yt/yt_proto/yt/client/api/rpc_proxy/proto/api_service.proto +++ b/yt/yt_proto/yt/client/api/rpc_proxy/proto/api_service.proto @@ -224,6 +224,8 @@ enum EMultiplexingBand MB_CONTROL = 1; MB_HEAVY = 2; MB_INTERACTIVE = 3; + MB_REAL_TIME = 4; + MB_JOURNAL = 5; } enum ETableSchemaModification |