diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2019-10-24 17:58:47 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-10-24 17:58:47 +0000 |
commit | bc3412923e5747c67261cf31d28e54028127447d (patch) | |
tree | 9f8539de63302fd98cbdfc0edc0fc67230f52098 /src/mongo/idl | |
parent | f794fe87500b5d9d0e22dce40674528114bb52a3 (diff) | |
download | mongo-bc3412923e5747c67261cf31d28e54028127447d.tar.gz |
SERVER-44119 Customize string coersion for IDL
(cherry picked from commit 39bfc190446acd0b049c5cb29dfbbc6574a40ce1)
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 5587e29fad9..80a3d02b5a5 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); |