This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new dc4c249b Make config enum error more clear for users (#1639)
dc4c249b is described below

commit dc4c249b7286066da7302e33ecee842182292306
Author: Twice <[email protected]>
AuthorDate: Sun Aug 6 17:12:35 2023 +0800

    Make config enum error more clear for users (#1639)
---
 src/config/config.cc     | 15 ++++++++++-----
 src/config/config_type.h | 15 +++++++++++----
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/config/config.cc b/src/config/config.cc
index 08ab4c96..39d8d352 100644
--- a/src/config/config.cc
+++ b/src/config/config.cc
@@ -24,6 +24,7 @@
 #include <rocksdb/env.h>
 #include <strings.h>
 
+#include <algorithm>
 #include <cstring>
 #include <fstream>
 #include <iostream>
@@ -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);
@@ -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_, "")},
diff --git a/src/config/config_type.h b/src/config/config_type.h
index 5c11af65..54add0f5 100644
--- a/src/config/config_type.h
+++ b/src/config/config_type.h
@@ -25,6 +25,7 @@
 #include <climits>
 #include <functional>
 #include <limits>
+#include <numeric>
 #include <string>
 #include <utility>
 
@@ -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'"};
@@ -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:

Reply via email to