summaryrefslogtreecommitdiff
path: root/src/mongo/util/options_parser/constraints.h
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2018-11-07 14:31:23 +0000
committerSara Golemon <sara.golemon@mongodb.com>2018-11-24 18:36:50 +0000
commitda22c83c0e2d4d9b1e014b183a21898443dbc128 (patch)
tree2e092cba3d1cbfa7d4d19e1421797fa726df0d27 /src/mongo/util/options_parser/constraints.h
parentef9dc1e966374196badef69b88fa87f119feac16 (diff)
downloadmongo-da22c83c0e2d4d9b1e014b183a21898443dbc128.tar.gz
SERVER-37093 Implement code-gen for IDL config options
Diffstat (limited to 'src/mongo/util/options_parser/constraints.h')
-rw-r--r--src/mongo/util/options_parser/constraints.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/mongo/util/options_parser/constraints.h b/src/mongo/util/options_parser/constraints.h
index 9c4f425e920..5537adfe937 100644
--- a/src/mongo/util/options_parser/constraints.h
+++ b/src/mongo/util/options_parser/constraints.h
@@ -30,6 +30,8 @@
#pragma once
+#include <boost/optional.hpp>
+
#include "mongo/base/status.h"
#include "mongo/bson/util/builder.h"
#include "mongo/util/options_parser/environment.h"
@@ -176,5 +178,84 @@ private:
}
};
+/**
+ * Proxy constraint for callbacks used by IDL based config options with a key.
+ * Callback may take either the entire environment, or just the value being validated.
+ */
+template <typename T>
+class CallbackKeyConstraint : public KeyConstraint {
+public:
+ using Callback = std::function<Status(const Environment&, const Key&)>;
+ using ValueCallback = std::function<Status(const T&)>;
+
+ CallbackKeyConstraint(const Key& k, ValueCallback callback)
+ : KeyConstraint(k), _valueCallback(std::move(callback)) {}
+ CallbackKeyConstraint(const Key& k, Callback callback)
+ : KeyConstraint(k), _callback(std::move(callback)) {}
+
+private:
+ Status check(const Environment& env) override {
+ if (_callback) {
+ return _callback(env, _key);
+ }
+
+ if (!_valueCallback) {
+ return Status::OK();
+ }
+
+ Value val;
+ auto status = env.get(_key, &val);
+ if (!status.isOK()) {
+ // Key not set, skipping callback constraint check.
+ return Status::OK();
+ }
+
+ T typedVal;
+ if (!val.get(&typedVal).isOK()) {
+ return {ErrorCodes::InternalError,
+ str::stream() << "Error: value for key: " << _key << " was found as type: "
+ << val.typeToString()
+ << " but is required to be type: "
+ << typeid(typedVal).name()};
+ }
+
+ return _valueCallback(typedVal);
+ }
+
+ Callback _callback;
+ ValueCallback _valueCallback;
+};
+
+/**
+ * General boundary constraint for numeric type values.
+ */
+template <typename T>
+class BoundaryKeyConstraint : public CallbackKeyConstraint<T> {
+public:
+ BoundaryKeyConstraint(const Key& k,
+ const boost::optional<T>& gt,
+ const boost::optional<T>& lt,
+ const boost::optional<T>& gte,
+ const boost::optional<T>& lte)
+ : CallbackKeyConstraint<T>(k, [=](const T& val) -> Status {
+ if (gt && !(val > *gt)) {
+ return {ErrorCodes::BadValue,
+ str::stream() << k << " must be greater than " << *gt};
+ }
+ if (lt && !(val < *lt)) {
+ return {ErrorCodes::BadValue, str::stream() << k << " must be less than " << *lt};
+ }
+ if (gte && !(val >= *gte)) {
+ return {ErrorCodes::BadValue,
+ str::stream() << k << " must be greater than or equal to " << *gte};
+ }
+ if (lte && !(val <= *lte)) {
+ return {ErrorCodes::BadValue,
+ str::stream() << k << " must be less than or equal to " << *lte};
+ }
+ return Status::OK();
+ }) {}
+};
+
} // namespace optionenvironment
} // namespace mongo