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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#ifndef ERROR_ATTRIBUTES_INL_H_
#error "Direct inclusion of this file is not allowed, include error_attributes.h"
// For the sake of sane code completion.
#include "error_attributes.h"
#endif
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <class T>
requires CConvertToWorks<T, TErrorAttributes::TValue>
T TErrorAttributes::Get(TStringBuf key) const
{
auto yson = GetYson(key);
try {
return NYT::ConvertTo<T>(yson);
} catch (const std::exception& ex) {
ThrowCannotParseAttributeException(key, ex);
}
}
template <class T>
requires CConvertToWorks<T, TErrorAttributes::TValue>
typename TOptionalTraits<T>::TOptional TErrorAttributes::Find(TStringBuf key) const
{
auto yson = FindYson(key);
if (!yson) {
return typename TOptionalTraits<T>::TOptional();
}
try {
return NYT::ConvertTo<T>(yson);
} catch (const std::exception& ex) {
ThrowCannotParseAttributeException(key, ex);
}
}
template <class T>
requires CConvertToWorks<T, TErrorAttributes::TValue>
T TErrorAttributes::GetAndRemove(const TString& key)
{
auto result = Get<T>(key);
Remove(key);
return result;
}
template <class T>
requires CConvertToWorks<T, TErrorAttributes::TValue>
T TErrorAttributes::Get(TStringBuf key, const T& defaultValue) const
{
return Find<T>(key).value_or(defaultValue);
}
template <class T>
requires CConvertToWorks<T, TErrorAttributes::TValue>
T TErrorAttributes::GetAndRemove(const TString& key, const T& defaultValue)
{
auto result = Find<T>(key);
if (result) {
Remove(key);
return *result;
} else {
return defaultValue;
}
}
template <class T>
requires CConvertToWorks<T, TErrorAttributes::TValue>
typename TOptionalTraits<T>::TOptional TErrorAttributes::FindAndRemove(const TString& key)
{
auto result = Find<T>(key);
if (result) {
Remove(key);
}
return result;
}
template <CMergeableDictionary TDictionary>
void TErrorAttributes::MergeFrom(const TDictionary& dict)
{
using TTraits = TMergeDictionariesTraits<TDictionary>;
for (const auto& [key, value] : TTraits::MakeIterableView(dict)) {
SetYson(key, value);
}
}
////////////////////////////////////////////////////////////////////////////////
template <>
struct TMergeDictionariesTraits<TErrorAttributes>
{
static auto MakeIterableView(const TErrorAttributes& attributes)
{
return attributes.ListPairs();
}
};
static_assert(CMergeableDictionary<TErrorAttributes>);
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|