diff options
| author | galtsev <[email protected]> | 2023-05-04 15:39:00 +0300 | 
|---|---|---|
| committer | galtsev <[email protected]> | 2023-05-04 15:39:00 +0300 | 
| commit | 3b3c4be949271feb82f21388e31fb56cb7666062 (patch) | |
| tree | 64a97f249511579809def05223ab5c1c360bf393 /library/cpp/yt/string/enum.cpp | |
| parent | 69ceb35cb906509492a4b265c9872ae464c18952 (diff) | |
YT-18920: Add `TryDecodeEnumValue()`
Diffstat (limited to 'library/cpp/yt/string/enum.cpp')
| -rw-r--r-- | library/cpp/yt/string/enum.cpp | 25 | 
1 files changed, 21 insertions, 4 deletions
| diff --git a/library/cpp/yt/string/enum.cpp b/library/cpp/yt/string/enum.cpp index 7cb8e5c6b6a..e61b813fd21 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); | 
