error_attributes.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include "error_attribute.h"
  3. #include "mergeable_dictionary.h"
  4. #include <library/cpp/yt/misc/optional.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. // TODO(arkady-e1ppa): Try switching to TString/std::string eventually.
  8. // representing text-encoded yson string eventually (maybe).
  9. class TErrorAttributes
  10. {
  11. public:
  12. using TKey = TErrorAttribute::TKey;
  13. using TValue = TErrorAttribute::TValue;
  14. using TKeyValuePair = std::pair<TKey, TValue>;
  15. //! Returns the list of all keys in the dictionary.
  16. std::vector<TKey> ListKeys() const;
  17. //! Returns the list of all key-value pairs in the dictionary.
  18. std::vector<TKeyValuePair> ListPairs() const;
  19. //! Sets the value of the attribute.
  20. void SetAttribute(const TErrorAttribute& attribute);
  21. //! Removes the attribute.
  22. //! Returns |true| if the attribute was removed or |false| if there is no attribute with this key.
  23. bool Remove(const TKey& key);
  24. //! Removes all attributes.
  25. void Clear();
  26. //! Returns |true| iff the given key is present.
  27. bool Contains(TStringBuf key) const;
  28. //! Finds the attribute and deserializes its value.
  29. //! Throws if no such value is found.
  30. template <class T>
  31. requires CConvertibleFromAttributeValue<T>
  32. T Get(TStringBuf key) const;
  33. //! Same as #Get but removes the value.
  34. template <class T>
  35. requires CConvertibleFromAttributeValue<T>
  36. T GetAndRemove(const TKey& key);
  37. //! Finds the attribute and deserializes its value.
  38. //! Uses default value if no such attribute is found.
  39. template <class T>
  40. requires CConvertibleFromAttributeValue<T>
  41. T Get(TStringBuf key, const T& defaultValue) const;
  42. //! Same as #Get but removes the value if it exists.
  43. template <class T>
  44. requires CConvertibleFromAttributeValue<T>
  45. T GetAndRemove(const TKey& key, const T& defaultValue);
  46. //! Finds the attribute and deserializes its value.
  47. //! Returns null if no such attribute is found.
  48. template <class T>
  49. requires CConvertibleFromAttributeValue<T>
  50. typename TOptionalTraits<T>::TOptional Find(TStringBuf key) const;
  51. //! Same as #Find but removes the value if it exists.
  52. template <class T>
  53. requires CConvertibleFromAttributeValue<T>
  54. typename TOptionalTraits<T>::TOptional FindAndRemove(const TKey& key);
  55. template <CMergeableDictionary TDictionary>
  56. void MergeFrom(const TDictionary& dict);
  57. private:
  58. THashMap<TKey, TValue, THash<TStringBuf>, TEqualTo<TStringBuf>> Map_;
  59. friend class TErrorOr<void>;
  60. TErrorAttributes() = default;
  61. TErrorAttributes(const TErrorAttributes& other) = default;
  62. TErrorAttributes& operator= (const TErrorAttributes& other) = default;
  63. TErrorAttributes(TErrorAttributes&& other) = default;
  64. TErrorAttributes& operator= (TErrorAttributes&& other) = default;
  65. //! Returns the value of the attribute (null indicates that the attribute is not found).
  66. std::optional<TValue> FindValue(TStringBuf key) const;
  67. //! Returns the value of the attribute (throws an exception if the attribute is not found).
  68. TValue GetValue(TStringBuf key) const;
  69. //! Sets the value of the attribute.
  70. void SetValue(const TKey& key, const TValue& value);
  71. [[noreturn]] static void ThrowCannotParseAttributeException(TStringBuf key, const std::exception& ex);
  72. [[noreturn]] static void ThrowNoSuchAttributeException(TStringBuf key);
  73. };
  74. bool operator == (const TErrorAttributes& lhs, const TErrorAttributes& rhs);
  75. ////////////////////////////////////////////////////////////////////////////////
  76. } // namespace NYT
  77. #define ERROR_ATTRIBUTES_INL_H_
  78. #include "error_attributes-inl.h"
  79. #undef ERROR_ATTRIBUTES_INL_H_