#pragma once #include "error_attribute.h" #include "mergeable_dictionary.h" #include namespace NYT { //////////////////////////////////////////////////////////////////////////////// // TODO(arkady-e1ppa): Try switching to TString/std::string eventually. // representing text-encoded yson string eventually (maybe). class TErrorAttributes { public: using TKey = TErrorAttribute::TKey; using TValue = TErrorAttribute::TValue; using TKeyValuePair = std::pair; //! Returns the list of all keys in the dictionary. std::vector ListKeys() const; //! Returns the list of all key-value pairs in the dictionary. std::vector ListPairs() const; //! Sets the value of the attribute. void SetAttribute(const TErrorAttribute& attribute); //! Removes the attribute. //! Returns |true| if the attribute was removed or |false| if there is no attribute with this key. bool Remove(const TKey& key); //! Removes all attributes. void Clear(); //! Returns |true| iff the given key is present. bool Contains(TStringBuf key) const; //! Finds the attribute and deserializes its value. //! Throws if no such value is found. template requires CConvertibleFromAttributeValue T Get(TStringBuf key) const; //! Same as #Get but removes the value. template requires CConvertibleFromAttributeValue T GetAndRemove(const TKey& key); //! Finds the attribute and deserializes its value. //! Uses default value if no such attribute is found. template requires CConvertibleFromAttributeValue T Get(TStringBuf key, const T& defaultValue) const; //! Same as #Get but removes the value if it exists. template requires CConvertibleFromAttributeValue T GetAndRemove(const TKey& key, const T& defaultValue); //! Finds the attribute and deserializes its value. //! Returns null if no such attribute is found. template requires CConvertibleFromAttributeValue typename TOptionalTraits::TOptional Find(TStringBuf key) const; //! Same as #Find but removes the value if it exists. template requires CConvertibleFromAttributeValue typename TOptionalTraits::TOptional FindAndRemove(const TKey& key); template void MergeFrom(const TDictionary& dict); private: THashMap, TEqualTo> Map_; friend class TErrorOr; TErrorAttributes() = default; TErrorAttributes(const TErrorAttributes& other) = default; TErrorAttributes& operator= (const TErrorAttributes& other) = default; TErrorAttributes(TErrorAttributes&& other) = default; TErrorAttributes& operator= (TErrorAttributes&& other) = default; //! Returns the value of the attribute (null indicates that the attribute is not found). std::optional FindValue(TStringBuf key) const; //! Returns the value of the attribute (throws an exception if the attribute is not found). TValue GetValue(TStringBuf key) const; //! Sets the value of the attribute. void SetValue(const TKey& key, const TValue& value); [[noreturn]] static void ThrowCannotParseAttributeException(TStringBuf key, const std::exception& ex); [[noreturn]] static void ThrowNoSuchAttributeException(TStringBuf key); }; bool operator == (const TErrorAttributes& lhs, const TErrorAttributes& rhs); //////////////////////////////////////////////////////////////////////////////// } // namespace NYT #define ERROR_ATTRIBUTES_INL_H_ #include "error_attributes-inl.h" #undef ERROR_ATTRIBUTES_INL_H_