feat update
This commit is contained in:
parent
84688ebd24
commit
8b1ea6f873
@ -10,6 +10,9 @@
|
|||||||
#include "sled/filesystem/path.h"
|
#include "sled/filesystem/path.h"
|
||||||
#include "sled/filesystem/temporary_file.h"
|
#include "sled/filesystem/temporary_file.h"
|
||||||
|
|
||||||
|
// lang
|
||||||
|
#include "lang/attributes.h"
|
||||||
|
|
||||||
// log
|
// log
|
||||||
#include "sled/log/log.h"
|
#include "sled/log/log.h"
|
||||||
|
|
||||||
|
@ -49,9 +49,7 @@ class ErrorInfo {
|
|||||||
public:
|
public:
|
||||||
ErrorInfo() = default;
|
ErrorInfo() = default;
|
||||||
|
|
||||||
explicit ErrorInfo(std::string reason,
|
explicit ErrorInfo(std::string reason, std::string domain, std::unordered_map<std::string, std::string> metadata)
|
||||||
std::string domain,
|
|
||||||
std::unordered_map<std::string, std::string> metadata)
|
|
||||||
: reason_(std::move(reason)),
|
: reason_(std::move(reason)),
|
||||||
domain_(std::move(domain)),
|
domain_(std::move(domain)),
|
||||||
metadata_(std::move(metadata))
|
metadata_(std::move(metadata))
|
||||||
@ -61,18 +59,13 @@ public:
|
|||||||
|
|
||||||
std::string const &domain() const { return domain_; }
|
std::string const &domain() const { return domain_; }
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> const &metadata() const
|
std::unordered_map<std::string, std::string> const &metadata() const { return metadata_; }
|
||||||
{
|
|
||||||
return metadata_;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend bool operator==(ErrorInfo const &, ErrorInfo const &);
|
friend bool operator==(ErrorInfo const &, ErrorInfo const &);
|
||||||
friend bool operator!=(ErrorInfo const &, ErrorInfo const &);
|
friend bool operator!=(ErrorInfo const &, ErrorInfo const &);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend void internal::AddMetadata(ErrorInfo &,
|
friend void internal::AddMetadata(ErrorInfo &, std::string const &key, std::string value);
|
||||||
std::string const &key,
|
|
||||||
std::string value);
|
|
||||||
std::string reason_;
|
std::string reason_;
|
||||||
std::string domain_;
|
std::string domain_;
|
||||||
std::unordered_map<std::string, std::string> metadata_;
|
std::unordered_map<std::string, std::string> metadata_;
|
||||||
|
@ -17,8 +17,7 @@ namespace sled {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
class StatusOr final {
|
class StatusOr final {
|
||||||
public:
|
public:
|
||||||
static_assert(!std::is_reference<T>::value,
|
static_assert(!std::is_reference<T>::value, "StatusOr<T> requires T to **not** be a reference type");
|
||||||
"StatusOr<T> requires T to **not** be a reference type");
|
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
|
||||||
StatusOr() : StatusOr(MakeDefaultStatus()) {}
|
StatusOr() : StatusOr(MakeDefaultStatus()) {}
|
||||||
@ -26,9 +25,7 @@ public:
|
|||||||
StatusOr(StatusOr const &) = default;
|
StatusOr(StatusOr const &) = default;
|
||||||
StatusOr &operator=(StatusOr const &) = default;
|
StatusOr &operator=(StatusOr const &) = default;
|
||||||
|
|
||||||
StatusOr(StatusOr &&other)
|
StatusOr(StatusOr &&other) : status_(std::move(other.status_)), value_(std::move(other.value_))
|
||||||
: status_(std::move(other.status_)),
|
|
||||||
value_(std::move(other.value_))
|
|
||||||
{
|
{
|
||||||
other.status_ = MakeDefaultStatus();
|
other.status_ = MakeDefaultStatus();
|
||||||
}
|
}
|
||||||
@ -43,10 +40,7 @@ public:
|
|||||||
|
|
||||||
StatusOr(Status rhs) : status_(std::move(rhs))
|
StatusOr(Status rhs) : status_(std::move(rhs))
|
||||||
{
|
{
|
||||||
if (status_.ok()) {
|
if (status_.ok()) { throw std::invalid_argument("Status::OK is not a valid argument to StatusOr<T>"); }
|
||||||
throw std::invalid_argument(
|
|
||||||
"Status::OK is not a valid argument to StatusOr<T>");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StatusOr &operator=(Status status)
|
StatusOr &operator=(Status status)
|
||||||
@ -57,9 +51,7 @@ public:
|
|||||||
|
|
||||||
template<typename U = T,
|
template<typename U = T,
|
||||||
/// @code implementation detail
|
/// @code implementation detail
|
||||||
typename std::enable_if<
|
typename std::enable_if<!std::is_same<StatusOr, typename std::decay<U>::type>::value, int>::type = 0
|
||||||
!std::is_same<StatusOr, typename std::decay<U>::type>::value,
|
|
||||||
int>::type = 0
|
|
||||||
/// @code end
|
/// @code end
|
||||||
>
|
>
|
||||||
StatusOr &operator=(U &&rhs)
|
StatusOr &operator=(U &&rhs)
|
||||||
@ -138,10 +130,7 @@ public:
|
|||||||
Status &&status() && { return std::move(status_); }
|
Status &&status() && { return std::move(status_); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Status MakeDefaultStatus()
|
static Status MakeDefaultStatus() { return Status{StatusCode::kUnknown, "default"}; }
|
||||||
{
|
|
||||||
return Status{StatusCode::kUnknown, "default"};
|
|
||||||
}
|
|
||||||
|
|
||||||
void CheckHasValue() const &
|
void CheckHasValue() const &
|
||||||
{
|
{
|
||||||
|
@ -44,8 +44,7 @@ StatusCodeToString(StatusCode code)
|
|||||||
case StatusCode::kDataLoss:
|
case StatusCode::kDataLoss:
|
||||||
return "DATA_LOSS";
|
return "DATA_LOSS";
|
||||||
default:
|
default:
|
||||||
return "UNEXPECTED_STATUS_CODE="
|
return "UNEXPECTED_STATUS_CODE=" + std::to_string(static_cast<int>(code));
|
||||||
+ std::to_string(static_cast<int>(code));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}// namespace internal
|
}// namespace internal
|
||||||
@ -59,8 +58,7 @@ operator<<(std::ostream &os, StatusCode code)
|
|||||||
bool
|
bool
|
||||||
operator==(ErrorInfo const &a, ErrorInfo const &b)
|
operator==(ErrorInfo const &a, ErrorInfo const &b)
|
||||||
{
|
{
|
||||||
return a.reason_ == b.reason_ && a.domain_ == b.domain_
|
return a.reason_ == b.reason_ && a.domain_ == b.domain_ && a.metadata_ == b.metadata_;
|
||||||
&& a.metadata_ == b.metadata_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -73,10 +71,7 @@ class Status::Impl {
|
|||||||
public:
|
public:
|
||||||
using PayloadType = std::unordered_map<std::string, std::string>;
|
using PayloadType = std::unordered_map<std::string, std::string>;
|
||||||
|
|
||||||
explicit Impl(StatusCode code,
|
explicit Impl(StatusCode code, std::string message, ErrorInfo error_info, PayloadType payload)
|
||||||
std::string message,
|
|
||||||
ErrorInfo error_info,
|
|
||||||
PayloadType payload)
|
|
||||||
: code_(code),
|
: code_(code),
|
||||||
message_(std::move(message)),
|
message_(std::move(message)),
|
||||||
error_info_(std::move(error_info)),
|
error_info_(std::move(error_info)),
|
||||||
@ -95,14 +90,11 @@ public:
|
|||||||
|
|
||||||
friend inline bool operator==(Impl const &a, Impl const &b)
|
friend inline bool operator==(Impl const &a, Impl const &b)
|
||||||
{
|
{
|
||||||
return a.code_ == b.code_ && a.message_ == b.message_
|
return a.code_ == b.code_ && a.message_ == b.message_ && a.error_info_ == b.error_info_
|
||||||
&& a.error_info_ == b.error_info_ && a.payload_ == b.payload_;
|
&& a.payload_ == b.payload_;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator!=(Impl const &a, Impl const &b)
|
friend inline bool operator!=(Impl const &a, Impl const &b) { return !(a == b); }
|
||||||
{
|
|
||||||
return !(a == b);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StatusCode code_;
|
StatusCode code_;
|
||||||
@ -118,9 +110,7 @@ Status::Status(Status &&) noexcept = default;
|
|||||||
Status &Status::operator=(Status &&) noexcept = default;
|
Status &Status::operator=(Status &&) noexcept = default;
|
||||||
|
|
||||||
// Deep copy
|
// Deep copy
|
||||||
Status::Status(Status const &other)
|
Status::Status(Status const &other) : impl_(other.ok() ? nullptr : new auto(*other.impl_)) {}
|
||||||
: impl_(other.ok() ? nullptr : new auto(*other.impl_))
|
|
||||||
{}
|
|
||||||
|
|
||||||
// Deep copy
|
// Deep copy
|
||||||
Status &
|
Status &
|
||||||
@ -131,11 +121,7 @@ Status::operator=(Status const &other)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Status::Status(StatusCode code, std::string message, ErrorInfo error_info)
|
Status::Status(StatusCode code, std::string message, ErrorInfo error_info)
|
||||||
: impl_(code == StatusCode::kOk ? nullptr
|
: impl_(code == StatusCode::kOk ? nullptr : new Status::Impl(code, std::move(message), std::move(error_info), {}))
|
||||||
: new Status::Impl(code,
|
|
||||||
std::move(message),
|
|
||||||
std::move(error_info),
|
|
||||||
{}))
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
StatusCode
|
StatusCode
|
||||||
@ -170,18 +156,30 @@ operator<<(std::ostream &os, const Status &s)
|
|||||||
if (s.ok()) return os << StatusCode::kOk;
|
if (s.ok()) return os << StatusCode::kOk;
|
||||||
os << s.code() << ": " << s.message();
|
os << s.code() << ": " << s.message();
|
||||||
auto const &e = s.error_info();
|
auto const &e = s.error_info();
|
||||||
if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) {
|
if (e.reason().empty() && e.domain().empty() && e.metadata().empty()) { return os; }
|
||||||
return os;
|
|
||||||
|
os << "error_info={";
|
||||||
|
bool first_field = true;
|
||||||
|
if (!e.reason().empty()) {
|
||||||
|
os << "reason=" << e.reason();
|
||||||
|
first_field = false;
|
||||||
}
|
}
|
||||||
os << "error_info={reason" << e.reason();
|
if (!e.domain().empty()) {
|
||||||
|
if (first_field) os << ", ";
|
||||||
os << ", domain=" << e.domain();
|
os << ", domain=" << e.domain();
|
||||||
os << ", metadata={";
|
first_field = false;
|
||||||
|
}
|
||||||
|
if (!e.metadata().empty()) {
|
||||||
|
if (first_field) os << ", ";
|
||||||
|
os << "metadata={";
|
||||||
char const *sep = "";
|
char const *sep = "";
|
||||||
for (auto const &item : e.metadata()) {
|
for (auto const &item : e.metadata()) {
|
||||||
os << sep << item.first << "=" << item.second;
|
os << item.first << "=" << item.second << ", ";
|
||||||
sep = ", ";
|
sep = ", ";
|
||||||
}
|
}
|
||||||
return os << "}}";
|
os << "}";
|
||||||
|
}
|
||||||
|
return os << "}";
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
Loading…
Reference in New Issue
Block a user