diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2019-10-21 23:19:39 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-10-21 23:19:39 +0000 |
commit | 39bfc190446acd0b049c5cb29dfbbc6574a40ce1 (patch) | |
tree | 4dd24a8fe0a60a23e48b98d87929aafc2d3516a0 /src/mongo/idl | |
parent | 1b0927456ed31f6903cdeab64217ada8415f0c95 (diff) | |
download | mongo-39bfc190446acd0b049c5cb29dfbbc6574a40ce1.tar.gz |
SERVER-44119 Customize string coersion for IDL
Diffstat (limited to 'src/mongo/idl')
-rw-r--r-- | src/mongo/idl/server_parameter.cpp | 25 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter.h | 4 | ||||
-rw-r--r-- | src/mongo/idl/server_parameter_specialized_test.cpp | 2 |
3 files changed, 31 insertions, 0 deletions
diff --git a/src/mongo/idl/server_parameter.cpp b/src/mongo/idl/server_parameter.cpp index 5e0d3ef66fd..975f2b515e6 100644 --- a/src/mongo/idl/server_parameter.cpp +++ b/src/mongo/idl/server_parameter.cpp @@ -88,6 +88,31 @@ void ServerParameterSet::add(ServerParameter* sp) { x = sp; } +StatusWith<std::string> ServerParameter::coerceToString(const BSONElement& element, bool redact) { + switch (element.type()) { + case NumberDouble: + return std::to_string(element.Double()); + case String: + return element.String(); + case NumberInt: + return std::to_string(element.Int()); + case NumberLong: + return std::to_string(element.Long()); + case Date: + return dateToISOStringLocal(element.Date()); + default: + std::string diag; + if (redact) { + diag = "###"; + } else { + diag = element.toString(); + } + return {ErrorCodes::BadValue, + str::stream() << "Unsupported type " << typeName(element.type()) << " (value: '" + << diag << "') for setParameter: " << name()}; + } +} + IDLServerParameterDeprecatedAlias::IDLServerParameterDeprecatedAlias(StringData name, ServerParameter* sp) : ServerParameter(ServerParameterSet::getGlobal(), diff --git a/src/mongo/idl/server_parameter.h b/src/mongo/idl/server_parameter.h index f4f32265ce7..4fa5ba6b6f7 100644 --- a/src/mongo/idl/server_parameter.h +++ b/src/mongo/idl/server_parameter.h @@ -122,6 +122,10 @@ public: _testOnly = true; } +protected: + // Helper for translating setParameter values from BSON to string. + StatusWith<std::string> coerceToString(const BSONElement&, bool redact); + private: std::string _name; bool _allowedToChangeAtStartup; diff --git a/src/mongo/idl/server_parameter_specialized_test.cpp b/src/mongo/idl/server_parameter_specialized_test.cpp index 52a7e8af8b0..869effeeb2a 100644 --- a/src/mongo/idl/server_parameter_specialized_test.cpp +++ b/src/mongo/idl/server_parameter_specialized_test.cpp @@ -211,6 +211,8 @@ TEST(SpecializedServerParameter, withAtomicValue) { auto* wv = getServerParameter<cls>("specializedWithAtomicValue"); ASSERT_EQ(wv->_data.load(), cls::kDataDefault); ASSERT_APPENDED_INT(wv, cls::kDataDefault); + ASSERT_OK(wv->set(BSON("" << 99).firstElement())); + ASSERT_APPENDED_INT(wv, 99); ASSERT_OK(wv->setFromString("101")); ASSERT_APPENDED_INT(wv, 101); ASSERT_EQ(wv->_data.load(), 101); |