aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/error/error_attributes-inl.h
blob: 310ededd0250201ab261b8f21cf7fc158cc6b8cb (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
89
90
91
92
93
94
95
96
97
98
99
100
#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 CConvertibleFromAttributeValue<T>
T TErrorAttributes::Get(TStringBuf key) const
{
    auto value = GetValue(key);
    try {
        return NYT::FromErrorAttributeValue<T>(value);
    } catch (const std::exception& ex) {
        ThrowCannotParseAttributeException(key, ex);
    }
}

template <class T>
    requires CConvertibleFromAttributeValue<T>
typename TOptionalTraits<T>::TOptional TErrorAttributes::Find(TStringBuf key) const
{
    auto value = FindValue(key);
    if (!value) {
        return typename TOptionalTraits<T>::TOptional();
    }
    try {
        return NYT::FromErrorAttributeValue<T>(*value);
    } catch (const std::exception& ex) {
        ThrowCannotParseAttributeException(key, ex);
    }
}

template <class T>
    requires CConvertibleFromAttributeValue<T>
T TErrorAttributes::GetAndRemove(const TKey& key)
{
    auto result = Get<T>(key);
    Remove(key);
    return result;
}

template <class T>
    requires CConvertibleFromAttributeValue<T>
T TErrorAttributes::Get(TStringBuf key, const T& defaultValue) const
{
    return Find<T>(key).value_or(defaultValue);
}

template <class T>
    requires CConvertibleFromAttributeValue<T>
T TErrorAttributes::GetAndRemove(const TKey& key, const T& defaultValue)
{
    if (auto value = Find<T>(key)) {
        Remove(key);
        return *value;
    } else {
        return defaultValue;
    }
}

template <class T>
    requires CConvertibleFromAttributeValue<T>
typename TOptionalTraits<T>::TOptional TErrorAttributes::FindAndRemove(const TKey& key)
{
    auto value = Find<T>(key);
    if (value) {
        Remove(key);
    }
    return value;
}

template <CMergeableDictionary TDictionary>
void TErrorAttributes::MergeFrom(const TDictionary& dict)
{
    for (auto range = AsMergeableRange(dict); const auto& [key, value] : range) {
        SetValue(key, value);
    }
}

////////////////////////////////////////////////////////////////////////////////

namespace NMergeableRangeImpl {

inline TMergeableRange TagInvoke(TTagInvokeTag<AsMergeableRange>, const TErrorAttributes& attributes)
{
    return attributes.ListPairs();
}

} // namespace NMergeableRangeImpl

static_assert(CMergeableDictionary<TErrorAttributes>);

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT