diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2018-11-07 14:31:23 +0000 |
---|---|---|
committer | Sara Golemon <sara.golemon@mongodb.com> | 2018-11-24 18:36:50 +0000 |
commit | da22c83c0e2d4d9b1e014b183a21898443dbc128 (patch) | |
tree | 2e092cba3d1cbfa7d4d19e1421797fa726df0d27 /src | |
parent | ef9dc1e966374196badef69b88fa87f119feac16 (diff) | |
download | mongo-da22c83c0e2d4d9b1e014b183a21898443dbc128.tar.gz |
SERVER-37093 Implement code-gen for IDL config options
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/idl/SConscript | 12 | ||||
-rw-r--r-- | src/mongo/idl/config_option_test.cpp | 371 | ||||
-rw-r--r-- | src/mongo/idl/config_option_test.idl | 154 | ||||
-rw-r--r-- | src/mongo/util/options_parser/constraints.h | 81 | ||||
-rw-r--r-- | src/mongo/util/options_parser/option_description.h | 44 |
5 files changed, 662 insertions, 0 deletions
diff --git a/src/mongo/idl/SConscript b/src/mongo/idl/SConscript index 14d58001fba..78c54ed60d0 100644 --- a/src/mongo/idl/SConscript +++ b/src/mongo/idl/SConscript @@ -53,3 +53,15 @@ env.CppUnitTest( 'idl_parser', ], ) + +env.CppUnitTest( + target='idl_config_option_test', + source=[ + 'config_option_test.cpp', + env.Idlc('config_option_test.idl')[0], + ], + LIBDEPS=[ + '$BUILD_DIR/mongo/base', + '$BUILD_DIR/mongo/util/options_parser/options_parser', + ], +) diff --git a/src/mongo/idl/config_option_test.cpp b/src/mongo/idl/config_option_test.cpp new file mode 100644 index 00000000000..31824b893bf --- /dev/null +++ b/src/mongo/idl/config_option_test.cpp @@ -0,0 +1,371 @@ +/** + * Copyright (C) 2018-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kDefault + +#include "mongo/platform/basic.h" + +#include "mongo/idl/config_option_test_gen.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/log.h" +#include "mongo/util/options_parser/options_parser.h" +#include "mongo/util/options_parser/startup_option_init.h" +#include "mongo/util/options_parser/startup_options.h" + +namespace mongo { +namespace test { + +namespace moe = ::mongo::optionenvironment; + +namespace { + +Status parseArgv(const std::vector<std::string>& argv, moe::Environment* parsed) { + auto status = moe::OptionsParser().run(moe::startupOptions, argv, {}, parsed); + if (!status.isOK()) { + return status; + } + return parsed->validate(); +} + +Status parseConfig(const std::string& config, moe::Environment* parsed) { + auto status = moe::OptionsParser().runConfigFile(moe::startupOptions, config, {}, parsed); + if (!status.isOK()) { + return status; + } + return parsed->validate(); +} + +Status parseMixed(const std::vector<std::string>& argv, + const std::string& config, + moe::Environment* env) try { + moe::OptionsParser mixedParser; + + moe::Environment conf; + uassertStatusOK(mixedParser.runConfigFile(moe::startupOptions, config, {}, &conf)); + uassertStatusOK(env->setAll(conf)); + + moe::Environment cli; + uassertStatusOK(mixedParser.run(moe::startupOptions, argv, {}, &cli)); + uassertStatusOK(env->setAll(cli)); + + return env->validate(); +} catch (const DBException& ex) { + return ex.toStatus(); +} + +MONGO_STARTUP_OPTIONS_PARSE(ConfigOption)(InitializerContext*) { + // Fake argv for default arg parsing. + const std::vector<std::string> argv = { + "mongo", + "--testConfigOpt2", + "true", + "--testConfigOpt8", + "8", + "--testConfigOpt12", + "command-line option", + }; + return parseArgv(argv, &moe::startupOptionsParsed); +} + +template <typename T> +void ASSERT_OPTION_SET(const moe::Environment& env, const moe::Key& name, const T& exp) { + ASSERT_TRUE(env.count(name)); + ASSERT_EQ(env[name].as<T>(), exp); +} + +// ASSERT_EQ can't handle vectors, so take slightly more pains. +template <typename T> +void ASSERT_VECTOR_OPTION_SET(const moe::Environment& env, + const moe::Key& name, + const std::vector<T>& exp) { + ASSERT_TRUE(env.count(name)); + auto value = env[name].as<std::vector<T>>(); + ASSERT_EQ(exp.size(), value.size()); + for (size_t i = 0; i < exp.size(); ++i) { + ASSERT_EQ(exp[i], value[i]); + } +} + +template <typename T> +void ASSERT_OPTION_NOT_SET(const moe::Environment& env, const moe::Key& name) { + ASSERT_FALSE(env.count(name)); + ASSERT_THROWS(env[name].as<T>(), AssertionException); +} + +TEST(ConfigOption, Opt1) { + ASSERT_OPTION_NOT_SET<bool>(moe::startupOptionsParsed, "test.config.opt1"); + + moe::Environment parsed; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt1"}, &parsed)); + ASSERT_OPTION_SET<bool>(parsed, "test.config.opt1", true); + + moe::Environment parsedYAML; + ASSERT_OK(parseConfig("test: { config: { opt1: true } }", &parsedYAML)); + ASSERT_OPTION_SET<bool>(parsedYAML, "test.config.opt1", true); + + moe::Environment parsedINI; + ASSERT_OK(parseConfig("testConfigOpt1=true", &parsedINI)); + ASSERT_OPTION_SET<bool>(parsedINI, "test.config.opt1", true); +} + +TEST(ConfigOption, Opt2) { + ASSERT_OPTION_SET<bool>(moe::startupOptionsParsed, "test.config.opt2", true); + + moe::Environment parsedAbsent; + ASSERT_OK(parseArgv({"mongod"}, &parsedAbsent)); + ASSERT_OPTION_NOT_SET<bool>(parsedAbsent, "test.config.opt2"); + + moe::Environment parsedTrue; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt2", "true"}, &parsedTrue)); + ASSERT_OPTION_SET<bool>(parsedTrue, "test.config.opt2", true); + + moe::Environment parsedFalse; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt2", "false"}, &parsedFalse)); + ASSERT_OPTION_SET<bool>(parsedFalse, "test.config.opt2", false); + + moe::Environment parsedFail; + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt2"}, &parsedFail)); + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt2", "banana"}, &parsedFail)); + ASSERT_NOT_OK(parseConfig("test: { config: { opt2: true } }", &parsedFail)); + ASSERT_NOT_OK(parseConfig("testConfigOpt2=true", &parsedFail)); +} + +TEST(ConfigOption, Opt3) { + ASSERT_OPTION_NOT_SET<bool>(moe::startupOptionsParsed, "test.config.opt3"); + + moe::Environment parsedAbsent; + ASSERT_OK(parseArgv({"mongod"}, &parsedAbsent)); + ASSERT_OPTION_NOT_SET<bool>(parsedAbsent, "test.config.opt3"); + + moe::Environment parsedTrue; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt3", "true"}, &parsedTrue)); + ASSERT_OPTION_SET<bool>(parsedTrue, "test.config.opt3", true); + + moe::Environment parsedFalse; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt3", "false"}, &parsedFalse)); + ASSERT_OPTION_SET<bool>(parsedFalse, "test.config.opt3", false); + + moe::Environment parsedImplicit; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt3"}, &parsedImplicit)); + ASSERT_OPTION_SET<bool>(parsedImplicit, "test.config.opt3", true); +} + +TEST(ConfigOption, Opt4) { + ASSERT_OPTION_SET<std::string>(moe::startupOptionsParsed, "test.config.opt4", "Default Value"); + + moe::Environment parsedDefault; + ASSERT_OK(parseArgv({"mongod"}, &parsedDefault)); + ASSERT_OPTION_SET<std::string>(parsedDefault, "test.config.opt4", "Default Value"); + + moe::Environment parsedHello; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt4", "Hello"}, &parsedHello)); + ASSERT_OPTION_SET<std::string>(parsedHello, "test.config.opt4", "Hello"); + + moe::Environment parsedFail; + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt4"}, &parsedFail)); +} + +TEST(ConfigOption, Opt5) { + ASSERT_OPTION_NOT_SET<int>(moe::startupOptionsParsed, "test.config.opt5"); + + moe::Environment parsedFail; + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt5"}, &parsedFail)); + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt5", "123"}, &parsedFail)); + ASSERT_NOT_OK(parseConfig("test: { config: { opt5: 123 } }", &parsedFail)); + + moe::Environment parsedINI; + ASSERT_OK(parseConfig("testConfigOpt5=123", &parsedINI)); + ASSERT_OPTION_SET<int>(parsedINI, "test.config.opt5", 123); +} + +TEST(ConfigOption, Opt6) { + ASSERT_OPTION_NOT_SET<std::string>(moe::startupOptionsParsed, "testConfigOpt6"); + + moe::Environment parsed; + ASSERT_OK(parseArgv({"mongod", "some value"}, &parsed)); + ASSERT_OPTION_SET<std::string>(parsed, "testConfigOpt6", "some value"); + + moe::Environment parsedINI; + ASSERT_OK(parseConfig("testConfigOpt6=other thing", &parsedINI)); + ASSERT_OPTION_SET<std::string>(parsedINI, "testConfigOpt6", "other thing"); +} + +TEST(ConfigOption, Opt7) { + ASSERT_OPTION_NOT_SET<std::vector<std::string>>(moe::startupOptionsParsed, "testConfigOpt7"); + + // Single arg goes to opt6 per positioning. + moe::Environment parsedSingleArg; + ASSERT_OK(parseArgv({"mongod", "value1"}, &parsedSingleArg)); + ASSERT_OPTION_SET<std::string>(parsedSingleArg, "testConfigOpt6", "value1"); + ASSERT_OPTION_NOT_SET<std::vector<std::string>>(parsedSingleArg, "testConfigOpt7"); + + moe::Environment parsedMultiArg; + ASSERT_OK(parseArgv({"mongod", "value1", "value2", "value3"}, &parsedMultiArg)); + ASSERT_OPTION_SET<std::string>(parsedMultiArg, "testConfigOpt6", "value1"); + + // ASSERT macros can't deal with vector<string>, so break out the test manually. + ASSERT_VECTOR_OPTION_SET<std::string>(parsedMultiArg, "testConfigOpt7", {"value2", "value3"}); +} + +TEST(ConfigOption, Opt8) { + ASSERT_OPTION_SET<long>(moe::startupOptionsParsed, "test.config.opt8", 8); + + moe::Environment parsed; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt8", "42"}, &parsed)); + ASSERT_OPTION_SET<long>(parsed, "test.config.opt8", 42); + + moe::Environment parsedDeprShort; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt8a", "43"}, &parsedDeprShort)); + ASSERT_OPTION_SET<long>(parsedDeprShort, "test.config.opt8", 43); + + moe::Environment parsedDeprDotted; + ASSERT_OK(parseConfig("test: { config: { opt8b: 44 } }", &parsedDeprDotted)); + ASSERT_OPTION_SET<long>(parsedDeprDotted, "test.config.opt8", 44); +} + +TEST(ConfigOption, Opt9) { + ASSERT_OPTION_NOT_SET<unsigned>(moe::startupOptionsParsed, "test.config.opt9"); + ASSERT_OPTION_NOT_SET<long>(moe::startupOptionsParsed, "test.config.opt9a"); + ASSERT_OPTION_NOT_SET<unsigned long long>(moe::startupOptionsParsed, "test.config.opt9b"); + + moe::Environment parsedCLI; + ASSERT_OK( + parseArgv({"mongod", "--testConfigOpt9", "42", "--testConfigOpt9a", "43"}, &parsedCLI)); + ASSERT_OPTION_SET<unsigned>(parsedCLI, "test.config.opt9", 42); + ASSERT_OPTION_SET<long>(parsedCLI, "test.config.opt9a", 43); + ASSERT_OPTION_NOT_SET<unsigned long long>(parsedCLI, "test.config.opt9b"); + + moe::Environment parsedINI; + ASSERT_OK(parseConfig("testConfigOpt9=42\ntestConfigOpt9a=43", &parsedINI)); + ASSERT_OPTION_SET<unsigned>(parsedINI, "test.config.opt9", 42); + ASSERT_OPTION_SET<long>(parsedINI, "test.config.opt9a", 43); + ASSERT_OPTION_NOT_SET<unsigned long long>(parsedINI, "test.config.opt9b"); + + moe::Environment parsedYAML; + ASSERT_OK(parseConfig("test: { config: { opt9: 42, opt9a: 43 } }", &parsedYAML)); + ASSERT_OPTION_SET<unsigned>(parsedYAML, "test.config.opt9", 42); + ASSERT_OPTION_SET<long>(parsedYAML, "test.config.opt9a", 43); + ASSERT_OPTION_NOT_SET<unsigned long long>(parsedYAML, "test.config.opt9b"); + + moe::Environment parsedMixed; + ASSERT_OK(parseMixed( + {"mongod", "--testConfigOpt9", "42"}, "test: { config: { opt9a: 43 } }", &parsedMixed)); + ASSERT_OPTION_SET<unsigned>(parsedMixed, "test.config.opt9", 42); + ASSERT_OPTION_SET<long>(parsedMixed, "test.config.opt9a", 43); + ASSERT_OPTION_NOT_SET<unsigned long long>(parsedMixed, "test.config.opt9b"); + + moe::Environment parsedFail; + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt9", "42"}, &parsedFail)); + ASSERT_NOT_OK( + parseArgv({"mongod", "--testConfigOpt9", "42", "--testConfigOpt9b", "44"}, &parsedFail)); + ASSERT_NOT_OK(parseArgv( + {"mongod", "--testConfigOpt9", "42", "--testConfigOpt9a", "43", "--testConfigOpt9b", "44"}, + &parsedFail)); + ASSERT_NOT_OK(parseConfig("testConfigOpt9=42", &parsedFail)); + ASSERT_NOT_OK(parseConfig("testConfigOpt9=42\ntestConfigOpt9b=44", &parsedFail)); + ASSERT_NOT_OK( + parseConfig("testConfigOpt9=42\ntestConfigOpt9a=43\ntestConfigOpt9b=44", &parsedFail)); + ASSERT_NOT_OK(parseConfig("test: { config: { opt9: 42 } }", &parsedFail)); + ASSERT_NOT_OK(parseConfig("test: { config: { opt9: 42, opt9b: 44 } }", &parsedFail)); + ASSERT_NOT_OK(parseConfig("test: { config: { opt9: 42, opt9a: 43, opt9b: 44 } }", &parsedFail)); +} + +TEST(ConfigOption, Opt10) { + ASSERT_OPTION_NOT_SET<int>(moe::startupOptionsParsed, "test.config.opt10a"); + ASSERT_OPTION_NOT_SET<int>(moe::startupOptionsParsed, "test.config.opt10b"); + + const auto tryParse = [](int a, int b) { + moe::Environment parsed; + ASSERT_OK(parseArgv({"mongod", + "--testConfigOpt10a", + std::to_string(a), + "--testConfigOpt10b", + std::to_string(b)}, + &parsed)); + ASSERT_OPTION_SET<int>(parsed, "test.config.opt10a", a); + ASSERT_OPTION_SET<int>(parsed, "test.config.opt10b", b); + }; + const auto failParse = [](int a, int b) { + moe::Environment parsedFail; + ASSERT_NOT_OK(parseArgv({"mongod", + "--testConfigOpt10a", + std::to_string(a), + "--testConfigOpt10b", + std::to_string(b)}, + &parsedFail)); + }; + tryParse(1, 1); + tryParse(99, 99); + tryParse(1, 0); + tryParse(99, 100); + failParse(0, 0); + failParse(100, 100); +} + +TEST(ConfigOption, Opt11) { + ASSERT_OPTION_NOT_SET<int>(moe::startupOptionsParsed, "test.config.opt11"); + + const auto tryParse = [](int val) { + moe::Environment parsed; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt11", std::to_string(val)}, &parsed)); + ASSERT_OPTION_SET<int>(parsed, "test.config.opt11", val); + }; + const auto failParse = [](int val) { + moe::Environment parsed; + ASSERT_NOT_OK(parseArgv({"mongod", "--testConfigOpt11", std::to_string(val)}, &parsed)); + }; + tryParse(1); + tryParse(123456789); + failParse(0); + failParse(2); + failParse(123456780); +} + +TEST(ConfigOption, Opt12) { + ASSERT_OPTION_SET<std::string>( + moe::startupOptionsParsed, "test.config.opt12", "command-line option"); + ASSERT_EQ(gTestConfigOpt12, "command-line option"); +} + +TEST(ConfigOption, Opt13) { + ASSERT_OPTION_NOT_SET<std::string>(moe::startupOptionsParsed, "test.config.opt13"); + + moe::Environment parsedSingle; + ASSERT_OK(parseArgv({"mongod", "-o", "single"}, &parsedSingle)); + ASSERT_OPTION_SET<std::string>(parsedSingle, "test.config.opt13", "single"); + + moe::Environment parsedShort; + ASSERT_OK(parseArgv({"mongod", "--testConfigOpt13", "short"}, &parsedShort)); + ASSERT_OPTION_SET<std::string>(parsedShort, "test.config.opt13", "short"); +} + +} // namespace + +} // namespace test +} // namespace mongo diff --git a/src/mongo/idl/config_option_test.idl b/src/mongo/idl/config_option_test.idl new file mode 100644 index 00000000000..6c85b939421 --- /dev/null +++ b/src/mongo/idl/config_option_test.idl @@ -0,0 +1,154 @@ +# Copyright (C) 2018-present MongoDB, Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Server Side Public License, version 1, +# as published by MongoDB, Inc. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Server Side Public License for more details. +# +# You should have received a copy of the Server Side Public License +# along with this program. If not, see +# <http://www.mongodb.com/licensing/server-side-public-license>. +# +# As a special exception, the copyright holders give permission to link the +# code of portions of this program with the OpenSSL library under certain +# conditions as described in each individual source file and distribute +# linked combinations including the program with the OpenSSL library. You +# must comply with the Server Side Public License in all respects for +# all of the code used other than as permitted herein. If you modify file(s) +# with this exception, you may extend this exception to your version of the +# file(s), but you are not obligated to do so. If you do not wish to do so, +# delete this exception statement from your version. If you delete this +# exception statement from all source files in the program, then also delete +# it in the license file. +# + +global: + cpp_namespace: "mongo::test" + cpp_includes: + - "mongo/idl/server_parameter_with_storage_test.h" + configs: + initializer_name: TestConfigs + +imports: + - "mongo/idl/basic_types.idl" + +configs: + "test.config.opt1": + short_name: testConfigOpt1 + description: "Basic switch" + arg_vartype: Switch + source: [ cli, yaml, ini ] + + "test.config.opt2": + short_name: testConfigOpt2 + description: "Boolean option without implicit value" + arg_vartype: Bool + source: cli + + "test.config.opt3": + short_name: testConfigOpt3 + description: "Boolean option with implicit value" + arg_vartype: Bool + source: cli + implicit: true + + "test.config.opt4": + short_name: testConfigOpt4 + description: "String option with a default value" + arg_vartype: String + source: cli + default: "Default Value" + + "test.config.opt5": + short_name: testConfigOpt5 + description: "Int option only settable from INI" + arg_vartype: Int + source: ini + + # Positional options must be configured with a "short name" only. + "testConfigOpt6": + description: "Positional string argument" + arg_vartype: String + source: [ cli, ini ] + positional: 1 + hidden: true + + "testConfigOpt7": + description: "Muilti-value positional string arguments" + arg_vartype: StringVector + source: cli + positional: 2- + hidden: true + + "test.config.opt8": + short_name: testConfigOpt8 + deprecated_name: [ "test.config.opt8a", "test.config.opt8b" ] + deprecated_short_name: [ testConfigOpt8a, testConfigOpt8b ] + description: "Option with deprecated names" + source: [ cli, yaml ] + arg_vartype: Long + + "test.config.opt9": + short_name: testConfigOpt9 + description: "Option with dependencies" + arg_vartype: Unsigned + source: [ cli, ini, yaml ] + requires: "test.config.opt9a" + conflicts: "test.config.opt9b" + + "test.config.opt9a": + short_name: testConfigOpt9a + description: "Required with opt9" + arg_vartype: Long + source: [ cli, ini, yaml ] + + "test.config.opt9b": + short_name: testConfigOpt9b + description: "Conflicts with opt9" + arg_vartype: UnsignedLongLong + source: [ cli, ini, yaml ] + + "test.config.opt10a": + short_name: testConfigOpt10a + description: "Integer from 0 to 100 exclusive" + arg_vartype: Int + source: cli + validator: + gt: 0 + lt: 100 + + "test.config.opt10b": + short_name: testConfigOpt10b + description: "Integer from 0 to 100 inclusive" + arg_vartype: Int + source: cli + validator: + gte: 0 + lte: 100 + + "test.config.opt11": + short_name: testConfigOpt11 + description: "Odd integer (callback test)" + arg_vartype: Int + source: cli + validator: + callback: "validateOdd" + + "test.config.opt12": + short_name: testConfigOpt12 + description: "Test declared storage" + arg_vartype: String + source: cli + cpp_vartype: std::string + cpp_varname: gTestConfigOpt12 + + "test.config.opt13": + description: "Test with single name" + short_name: testConfigOpt13 + single_name: o + arg_vartype: String + source: cli 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 diff --git a/src/mongo/util/options_parser/option_description.h b/src/mongo/util/options_parser/option_description.h index 3869ad630fd..45979bc27ee 100644 --- a/src/mongo/util/options_parser/option_description.h +++ b/src/mongo/util/options_parser/option_description.h @@ -244,5 +244,49 @@ public: Canonicalize_t _canonicalize; }; +template <OptionType T> +struct OptionTypeMap; + +template <> +struct OptionTypeMap<StringVector> { + using type = std::vector<std::string>; +}; +template <> +struct OptionTypeMap<StringMap> { + using type = std::vector<std::string>; +}; +template <> +struct OptionTypeMap<Bool> { + using type = bool; +}; +template <> +struct OptionTypeMap<Double> { + using type = double; +}; +template <> +struct OptionTypeMap<Int> { + using type = int; +}; +template <> +struct OptionTypeMap<Long> { + using type = long; +}; +template <> +struct OptionTypeMap<String> { + using type = std::string; +}; +template <> +struct OptionTypeMap<UnsignedLongLong> { + using type = unsigned long long; +}; +template <> +struct OptionTypeMap<Unsigned> { + using type = unsigned; +}; +template <> +struct OptionTypeMap<Switch> { + using type = bool; +}; + } // namespace optionenvironment } // namespace mongo |