Remove some magic_enum from headers

This commit is contained in:
Raúl Marín 2024-03-19 17:40:27 +01:00
parent 018316c78a
commit c6f0a434c0
4 changed files with 59 additions and 26 deletions

View File

@ -1,6 +1,8 @@
#include <Common/IntervalKind.h> #include <Common/IntervalKind.h>
#include <Common/Exception.h> #include <Common/Exception.h>
#include <base/EnumReflection.h>
namespace DB namespace DB
{ {
@ -10,6 +12,11 @@ namespace ErrorCodes
extern const int BAD_ARGUMENTS; extern const int BAD_ARGUMENTS;
} }
std::string_view IntervalKind::toString() const
{
return magic_enum::enum_name(kind);
}
Int64 IntervalKind::toAvgNanoseconds() const Int64 IntervalKind::toAvgNanoseconds() const
{ {
static constexpr Int64 NANOSECONDS_PER_MICROSECOND = 1000; static constexpr Int64 NANOSECONDS_PER_MICROSECOND = 1000;

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <base/types.h> #include <base/types.h>
#include <base/EnumReflection.h>
namespace DB namespace DB
{ {
@ -27,7 +26,7 @@ struct IntervalKind
IntervalKind(Kind kind_ = Kind::Second) : kind(kind_) {} /// NOLINT IntervalKind(Kind kind_ = Kind::Second) : kind(kind_) {} /// NOLINT
operator Kind() const { return kind; } /// NOLINT operator Kind() const { return kind; } /// NOLINT
constexpr std::string_view toString() const { return magic_enum::enum_name(kind); } std::string_view toString() const;
/// Returns number of nanoseconds in one interval. /// Returns number of nanoseconds in one interval.
/// For `Month`, `Quarter` and `Year` the function returns an average number of nanoseconds. /// For `Month`, `Quarter` and `Year` the function returns an average number of nanoseconds.

View File

@ -0,0 +1,47 @@
#include <base/types.h>
#include <Common/Exception.h>
#include <magic_enum.hpp>
#include <Storages/MergeTree/MergeTreeDataPartType.h>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
template <typename E>
requires std::is_enum_v<E>
static E parseEnum(const String & str)
{
auto value = magic_enum::enum_cast<E>(str);
if (!value || *value == E::Unknown)
throw DB::Exception(ErrorCodes::BAD_ARGUMENTS, "Unexpected string {} for enum {}", str, magic_enum::enum_type_name<E>());
return *value;
}
String MergeTreeDataPartType::toString() const
{
return String(magic_enum::enum_name(value));
}
void MergeTreeDataPartType::fromString(const String & str)
{
value = parseEnum<Value>(str);
}
String MergeTreeDataPartStorageType::toString() const
{
return String(magic_enum::enum_name(value));
}
void MergeTreeDataPartStorageType::fromString(const String & str)
{
value = parseEnum<Value>(str);
}
}

View File

@ -1,29 +1,9 @@
#pragma once #pragma once
#include <Common/Exception.h>
#include <base/types.h> #include <base/types.h>
#include <magic_enum.hpp>
namespace DB namespace DB
{ {
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
template <typename E>
requires std::is_enum_v<E>
static E parseEnum(const String & str)
{
auto value = magic_enum::enum_cast<E>(str);
if (!value || *value == E::Unknown)
throw DB::Exception(ErrorCodes::BAD_ARGUMENTS,
"Unexpected string {} for enum {}", str, magic_enum::enum_type_name<E>());
return *value;
}
/// It's a bug in clang with three-way comparison operator /// It's a bug in clang with three-way comparison operator
/// https://github.com/llvm/llvm-project/issues/55919 /// https://github.com/llvm/llvm-project/issues/55919
#pragma clang diagnostic push #pragma clang diagnostic push
@ -51,8 +31,8 @@ public:
auto operator<=>(const MergeTreeDataPartType &) const = default; auto operator<=>(const MergeTreeDataPartType &) const = default;
Value getValue() const { return value; } Value getValue() const { return value; }
String toString() const { return String(magic_enum::enum_name(value)); } String toString() const;
void fromString(const String & str) { value = parseEnum<Value>(str); } void fromString(const String & str);
private: private:
Value value; Value value;
@ -74,8 +54,8 @@ public:
auto operator<=>(const MergeTreeDataPartStorageType &) const = default; auto operator<=>(const MergeTreeDataPartStorageType &) const = default;
Value getValue() const { return value; } Value getValue() const { return value; }
String toString() const { return String(magic_enum::enum_name(value)); } String toString() const;
void fromString(const String & str) { value = parseEnum<Value>(str); } void fromString(const String & str);
private: private:
Value value; Value value;