diff options
| author | galtsev <[email protected]> | 2023-04-19 23:02:45 +0300 | 
|---|---|---|
| committer | galtsev <[email protected]> | 2023-04-19 23:02:45 +0300 | 
| commit | 4529cb782d6962d3a1e38d761cfa24a3c1bac8dd (patch) | |
| tree | 3973453b56dcb6503ed4d2a773eff6eaf1e45dbe /library/cpp/yt | |
| parent | 3937a38da8a5080fd0a470c50e91335bf66ce57e (diff) | |
YT-18920: Parse unknown enum values
Diffstat (limited to 'library/cpp/yt')
| -rw-r--r-- | library/cpp/yt/string/enum-inl.h | 26 | 
1 files changed, 25 insertions, 1 deletions
diff --git a/library/cpp/yt/string/enum-inl.h b/library/cpp/yt/string/enum-inl.h index 7e6785efb45..2fa138f52fb 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 <library/cpp/yt/exception/exception.h> +  #include <util/string/printf.h>  namespace NYT { @@ -28,7 +30,29 @@ template <class T>  std::optional<T> TryParseEnum(TStringBuf value)  {      auto tryFromString = [] (TStringBuf value) -> std::optional<T> { -        return TEnumTraits<T>::FindValueByLiteral(DecodeEnumValue(value)); +        try { +            return TEnumTraits<T>::FindValueByLiteral(DecodeEnumValue(value)); +        } catch (const TSimpleException&) { +            TStringBuf typeName; +            auto isTypeNameCorrect = value.NextTok('(', typeName) && typeName == TEnumTraits<T>::GetTypeName(); +            if (!isTypeNameCorrect) { +                throw; +            } + +            TStringBuf enumValue; +            std::underlying_type_t<T> underlyingValue; +            auto isEnumValueCorrect = value.NextTok(')', enumValue) && TryFromString(enumValue, underlyingValue); +            if (!isEnumValueCorrect) { +                throw; +            } + +            auto isParsingComplete = value.empty(); +            if (!isParsingComplete) { +                throw; +            } + +            return static_cast<T>(underlyingValue); +        }      };      if constexpr (TEnumTraits<T>::IsBitEnum) {  | 
