error_attributes-inl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef ERROR_ATTRIBUTES_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include error_attributes.h"
  3. // For the sake of sane code completion.
  4. #include "error_attributes.h"
  5. #endif
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <class T>
  9. requires CConvertibleFromAttributeValue<T>
  10. T TErrorAttributes::Get(TStringBuf key) const
  11. {
  12. auto value = GetValue(key);
  13. try {
  14. return NYT::FromErrorAttributeValue<T>(value);
  15. } catch (const std::exception& ex) {
  16. ThrowCannotParseAttributeException(key, ex);
  17. }
  18. }
  19. template <class T>
  20. requires CConvertibleFromAttributeValue<T>
  21. typename TOptionalTraits<T>::TOptional TErrorAttributes::Find(TStringBuf key) const
  22. {
  23. auto value = FindValue(key);
  24. if (!value) {
  25. return typename TOptionalTraits<T>::TOptional();
  26. }
  27. try {
  28. return NYT::FromErrorAttributeValue<T>(*value);
  29. } catch (const std::exception& ex) {
  30. ThrowCannotParseAttributeException(key, ex);
  31. }
  32. }
  33. template <class T>
  34. requires CConvertibleFromAttributeValue<T>
  35. T TErrorAttributes::GetAndRemove(const TKey& key)
  36. {
  37. auto result = Get<T>(key);
  38. Remove(key);
  39. return result;
  40. }
  41. template <class T>
  42. requires CConvertibleFromAttributeValue<T>
  43. T TErrorAttributes::Get(TStringBuf key, const T& defaultValue) const
  44. {
  45. return Find<T>(key).value_or(defaultValue);
  46. }
  47. template <class T>
  48. requires CConvertibleFromAttributeValue<T>
  49. T TErrorAttributes::GetAndRemove(const TKey& key, const T& defaultValue)
  50. {
  51. if (auto value = Find<T>(key)) {
  52. Remove(key);
  53. return *value;
  54. } else {
  55. return defaultValue;
  56. }
  57. }
  58. template <class T>
  59. requires CConvertibleFromAttributeValue<T>
  60. typename TOptionalTraits<T>::TOptional TErrorAttributes::FindAndRemove(const TKey& key)
  61. {
  62. auto value = Find<T>(key);
  63. if (value) {
  64. Remove(key);
  65. }
  66. return value;
  67. }
  68. template <CMergeableDictionary TDictionary>
  69. void TErrorAttributes::MergeFrom(const TDictionary& dict)
  70. {
  71. for (auto range = AsMergeableRange(dict); const auto& [key, value] : range) {
  72. SetValue(key, value);
  73. }
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////
  76. namespace NMergeableRangeImpl {
  77. inline TMergeableRange TagInvoke(TTagInvokeTag<AsMergeableRange>, const TErrorAttributes& attributes)
  78. {
  79. return attributes.ListPairs();
  80. }
  81. } // namespace NMergeableRangeImpl
  82. static_assert(CMergeableDictionary<TErrorAttributes>);
  83. ////////////////////////////////////////////////////////////////////////////////
  84. } // namespace NYT