diff options
author | galtsev <galtsev@yandex-team.com> | 2023-05-04 15:39:00 +0300 |
---|---|---|
committer | galtsev <galtsev@yandex-team.com> | 2023-05-04 15:39:00 +0300 |
commit | 3b3c4be949271feb82f21388e31fb56cb7666062 (patch) | |
tree | 64a97f249511579809def05223ab5c1c360bf393 /library/cpp | |
parent | 69ceb35cb906509492a4b265c9872ae464c18952 (diff) | |
download | ydb-3b3c4be949271feb82f21388e31fb56cb7666062.tar.gz |
YT-18920: Add `TryDecodeEnumValue()`
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/yt/string/enum-inl.h | 46 | ||||
-rw-r--r-- | library/cpp/yt/string/enum.cpp | 25 | ||||
-rw-r--r-- | library/cpp/yt/string/enum.h | 1 |
3 files changed, 49 insertions, 23 deletions
diff --git a/library/cpp/yt/string/enum-inl.h b/library/cpp/yt/string/enum-inl.h index 2fa138f52f..205ececae3 100644 --- a/library/cpp/yt/string/enum-inl.h +++ b/library/cpp/yt/string/enum-inl.h @@ -4,6 +4,8 @@ #include "enum.h" #endif +#include "format.h" + #include <library/cpp/yt/exception/exception.h> #include <util/string/printf.h> @@ -30,29 +32,35 @@ template <class T> std::optional<T> TryParseEnum(TStringBuf value) { auto tryFromString = [] (TStringBuf value) -> std::optional<T> { - try { - return TEnumTraits<T>::FindValueByLiteral(DecodeEnumValue(value)); - } catch (const TSimpleException&) { - TStringBuf typeName; - auto isTypeNameCorrect = value.NextTok('(', typeName) && typeName == TEnumTraits<T>::GetTypeName(); - if (!isTypeNameCorrect) { - throw; - } + if (auto decodedValue = TryDecodeEnumValue(value)) { + return TEnumTraits<T>::FindValueByLiteral(*decodedValue); + } - TStringBuf enumValue; - std::underlying_type_t<T> underlyingValue; - auto isEnumValueCorrect = value.NextTok(')', enumValue) && TryFromString(enumValue, underlyingValue); - if (!isEnumValueCorrect) { - throw; - } + auto reportError = [value] () { + throw TSimpleException(Format("Enum value %Qv is neither in a proper underscore case nor in a format \"%v(123)\"", + value, + TEnumTraits<T>::GetTypeName())); + }; - auto isParsingComplete = value.empty(); - if (!isParsingComplete) { - throw; - } + TStringBuf typeName; + auto isTypeNameCorrect = value.NextTok('(', typeName) && typeName == TEnumTraits<T>::GetTypeName(); + if (!isTypeNameCorrect) { + reportError(); + } + + TStringBuf enumValue; + std::underlying_type_t<T> underlyingValue = 0; + auto isEnumValueCorrect = value.NextTok(')', enumValue) && TryFromString(enumValue, underlyingValue); + if (!isEnumValueCorrect) { + reportError(); + } - return static_cast<T>(underlyingValue); + auto isParsingComplete = value.empty(); + if (!isParsingComplete) { + reportError(); } + + return static_cast<T>(underlyingValue); }; if constexpr (TEnumTraits<T>::IsBitEnum) { diff --git a/library/cpp/yt/string/enum.cpp b/library/cpp/yt/string/enum.cpp index 7cb8e5c6b6..e61b813fd2 100644 --- a/library/cpp/yt/string/enum.cpp +++ b/library/cpp/yt/string/enum.cpp @@ -6,18 +6,35 @@ namespace NYT { //////////////////////////////////////////////////////////////////////////////// -TString DecodeEnumValue(TStringBuf value) +template <bool failOnError> +std::optional<TString> DecodeEnumValueImpl(TStringBuf value) { auto camelValue = UnderscoreCaseToCamelCase(value); auto underscoreValue = CamelCaseToUnderscoreCase(camelValue); if (value != underscoreValue) { - throw TSimpleException(Format("Enum value %Qv is not in a proper underscore case; did you mean %Qv?", - value, - underscoreValue)); + if constexpr (failOnError) { + throw TSimpleException(Format("Enum value %Qv is not in a proper underscore case; did you mean %Qv?", + value, + underscoreValue)); + } else { + return std::nullopt; + } } return camelValue; } +std::optional<TString> TryDecodeEnumValue(TStringBuf value) +{ + return DecodeEnumValueImpl<false>(value); +} + +TString DecodeEnumValue(TStringBuf value) +{ + auto decodedValue = DecodeEnumValueImpl<true>(value); + YT_VERIFY(decodedValue); + return *decodedValue; +} + TString EncodeEnumValue(TStringBuf value) { return CamelCaseToUnderscoreCase(value); diff --git a/library/cpp/yt/string/enum.h b/library/cpp/yt/string/enum.h index caad6f2064..b4b8d59171 100644 --- a/library/cpp/yt/string/enum.h +++ b/library/cpp/yt/string/enum.h @@ -10,6 +10,7 @@ namespace NYT { //////////////////////////////////////////////////////////////////////////////// +std::optional<TString> TryDecodeEnumValue(TStringBuf value); TString DecodeEnumValue(TStringBuf value); TString EncodeEnumValue(TStringBuf value); |