Skip to content

Commit

Permalink
Make config enum error more clear for users (#1639)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Aug 6, 2023
1 parent dbd5f8d commit dc4c249
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <rocksdb/env.h>
#include <strings.h>

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
Expand Down Expand Up @@ -59,6 +60,15 @@ std::vector<ConfigEnum> log_levels{
{"fatal", google::FATAL},
};

std::vector<ConfigEnum> compression_types{[] {
std::vector<ConfigEnum> res;
res.reserve(engine::CompressionOptions.size());
for (const auto &e : engine::CompressionOptions) {
res.push_back({e.name, e.type});
}
return res;
}()};

std::string TrimRocksDbPrefix(std::string s) {
if (strncasecmp(s.data(), "rocksdb.", 8) != 0) return s;
return s.substr(8, s.size() - 8);
Expand All @@ -74,11 +84,6 @@ Config::Config() {
: name(std::move(name)), readonly(readonly), field(field) {}
};

std::vector<ConfigEnum> compression_types;
compression_types.reserve(engine::CompressionOptions.size());
for (const auto &e : engine::CompressionOptions) {
compression_types.push_back({e.name, e.type});
}
FieldWrapper fields[] = {
{"daemonize", true, new YesNoField(&daemonize, false)},
{"bind", true, new StringField(&binds_str_, "")},
Expand Down
15 changes: 11 additions & 4 deletions src/config/config_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <climits>
#include <functional>
#include <limits>
#include <numeric>
#include <string>
#include <utility>

Expand Down Expand Up @@ -167,9 +168,9 @@ class YesNoField : public ConfigField {
return Status::OK();
}
Status Set(const std::string &v) override {
if (strcasecmp(v.data(), "yes") == 0) {
if (util::EqualICase(v, "yes")) {
*receiver_ = true;
} else if (strcasecmp(v.data(), "no") == 0) {
} else if (util::EqualICase(v, "no")) {
*receiver_ = false;
} else {
return {Status::NotOK, "argument must be 'yes' or 'no'"};
Expand Down Expand Up @@ -202,12 +203,18 @@ class EnumField : public ConfigField {

Status Set(const std::string &v) override {
for (const auto &e : enums_) {
if (strcasecmp(e.name.c_str(), v.c_str()) == 0) {
if (util::EqualICase(e.name, v)) {
*receiver_ = e.val;
return Status::OK();
}
}
return {Status::NotOK, "invalid enum option"};
return {Status::NotOK, fmt::format("invalid enum option, acceptable values are {}",
std::accumulate(enums_.begin(), enums_.end(), std::string{},
[this](const std::string &res, const ConfigEnum &e) {
if (&e != &enums_.back()) return res + "'" + e.name + "', ";

return res + "'" + e.name + "'";
}))};
}

private:
Expand Down

0 comments on commit dc4c249

Please sign in to comment.