blob: beeba753dafa39cb1bd3855b0f0681315e005d7a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
#include "enum.h"
#include "format.h"
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
////////////////////////////////////////////////////////////////////////////////
#if defined(_MSC_VER)
extern "C" TEnumSuggestionsCalculator TryGetEnumSuggestionsCalculatorWeak()
{
return nullptr;
}
__pragma(comment(linker, "/alternatename:TryGetEnumSuggestionsCalculator=TryGetEnumSuggestionsCalculatorWeak"))
#else
extern "C" Y_WEAK TEnumSuggestionsCalculator TryGetEnumSuggestionsCalculator()
{
return nullptr;
}
#endif
////////////////////////////////////////////////////////////////////////////////
void ThrowMalformedEnumValueException(
TStringBuf typeName,
TStringBuf value,
const std::span<const TStringBuf>& domainNames)
{
auto errorMessage = Format("Error parsing %v value %Qv", typeName, value);
auto suggestionsCalculator = TryGetEnumSuggestionsCalculator();
if (!domainNames.empty() && suggestionsCalculator) {
errorMessage += Format("; closest possible values are %v", suggestionsCalculator(value, domainNames));
}
throw TSimpleException(errorMessage);
}
template <bool ThrowOnError>
std::optional<std::string> DecodeEnumValueImpl(TStringBuf value)
{
auto camelValue = UnderscoreCaseToCamelCase(value);
auto underscoreValue = CamelCaseToUnderscoreCase(camelValue);
if (value != underscoreValue) {
if constexpr (ThrowOnError) {
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;
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NDetail
////////////////////////////////////////////////////////////////////////////////
std::optional<std::string> TryDecodeEnumValue(TStringBuf value)
{
return NDetail::DecodeEnumValueImpl<false>(value);
}
std::string DecodeEnumValue(TStringBuf value)
{
auto decodedValue = NDetail::DecodeEnumValueImpl<true>(value);
YT_VERIFY(decodedValue);
return *decodedValue;
}
std::string EncodeEnumValue(TStringBuf value)
{
return CamelCaseToUnderscoreCase(value);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|