error_attributes-inl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. auto value = Find<T>(key);
  52. if (value) {
  53. Remove(key);
  54. return *value;
  55. } else {
  56. return defaultValue;
  57. }
  58. }
  59. template <class T>
  60. requires CConvertibleFromAttributeValue<T>
  61. typename TOptionalTraits<T>::TOptional TErrorAttributes::FindAndRemove(const TKey& key)
  62. {
  63. auto value = Find<T>(key);
  64. if (value) {
  65. Remove(key);
  66. }
  67. return value;
  68. }
  69. template <CMergeableDictionary TDictionary>
  70. void TErrorAttributes::MergeFrom(const TDictionary& dict)
  71. {
  72. for (auto range = AsMergeableRange(dict); const auto& [key, value] : range) {
  73. SetValue(key, value);
  74. }
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. namespace NMergeableRangeImpl {
  78. inline TMergeableRange TagInvoke(TTagInvokeTag<AsMergeableRange>, const TErrorAttributes& attributes)
  79. {
  80. return attributes.ListPairs();
  81. }
  82. } // namespace NMergeableRangeImpl
  83. static_assert(CMergeableDictionary<TErrorAttributes>);
  84. ////////////////////////////////////////////////////////////////////////////////
  85. } // namespace NYT