diff options
Diffstat (limited to 'src/mongo/util/options_parser/constraints.cpp')
-rw-r--r-- | src/mongo/util/options_parser/constraints.cpp | 161 |
1 files changed, 79 insertions, 82 deletions
diff --git a/src/mongo/util/options_parser/constraints.cpp b/src/mongo/util/options_parser/constraints.cpp index 8c79138a44d..1ed195e19c6 100644 --- a/src/mongo/util/options_parser/constraints.cpp +++ b/src/mongo/util/options_parser/constraints.cpp @@ -35,107 +35,104 @@ namespace mongo { namespace optionenvironment { - Status NumericKeyConstraint::check(const Environment& env) { - Value val; - Status s = env.get(_key, &val); +Status NumericKeyConstraint::check(const Environment& env) { + Value val; + Status s = env.get(_key, &val); - if (s == ErrorCodes::NoSuchKey) { - // Key not set, skipping numeric constraint check - return Status::OK(); - } - - // The code that controls whether a type is "compatible" is contained in the Value - // class, so if that handles compatibility between numeric types then this will too. - long intVal; - if (val.get(&intVal).isOK()) { - if (intVal < _min || intVal > _max) { - StringBuilder sb; - sb << "Error: Attempting to set " << _key << " to value: " << - intVal << " which is out of range: (" << - _min << "," << _max << ")"; - return Status(ErrorCodes::BadValue, sb.str()); - } - } - else { - StringBuilder sb; - sb << "Error: " << _key << " is of type: " << val.typeToString() << - " but must be of a numeric type."; - return Status(ErrorCodes::BadValue, sb.str()); - } + if (s == ErrorCodes::NoSuchKey) { + // Key not set, skipping numeric constraint check return Status::OK(); } - Status ImmutableKeyConstraint::check(const Environment& env) { - Value env_value; - Status ret = env.get(_key, &env_value); - if (ret.isOK()) { - if (_value.isEmpty()) { - _value = env_value; - } - else { - if (!_value.equal(env_value)) { - StringBuilder sb; - sb << "Error: " << _key << " is immutable once set"; - return Status(ErrorCodes::BadValue, sb.str()); - } - } + // The code that controls whether a type is "compatible" is contained in the Value + // class, so if that handles compatibility between numeric types then this will too. + long intVal; + if (val.get(&intVal).isOK()) { + if (intVal < _min || intVal > _max) { + StringBuilder sb; + sb << "Error: Attempting to set " << _key << " to value: " << intVal + << " which is out of range: (" << _min << "," << _max << ")"; + return Status(ErrorCodes::BadValue, sb.str()); } - - return ret; + } else { + StringBuilder sb; + sb << "Error: " << _key << " is of type: " << val.typeToString() + << " but must be of a numeric type."; + return Status(ErrorCodes::BadValue, sb.str()); } - - Status MutuallyExclusiveKeyConstraint::check(const Environment& env) { - Value env_value; - Status ret = env.get(_key, &env_value); - if (ret.isOK()) { - ret = env.get(_otherKey, &env_value); - if (ret.isOK()) { + return Status::OK(); +} + +Status ImmutableKeyConstraint::check(const Environment& env) { + Value env_value; + Status ret = env.get(_key, &env_value); + if (ret.isOK()) { + if (_value.isEmpty()) { + _value = env_value; + } else { + if (!_value.equal(env_value)) { StringBuilder sb; - sb << _otherKey << " is not allowed when " << _key << " is specified"; + sb << "Error: " << _key << " is immutable once set"; return Status(ErrorCodes::BadValue, sb.str()); } } - - return Status::OK(); } - Status RequiresOtherKeyConstraint::check(const Environment& env) { - Value env_value; - Status ret = env.get(_key, &env_value); + return ret; +} + +Status MutuallyExclusiveKeyConstraint::check(const Environment& env) { + Value env_value; + Status ret = env.get(_key, &env_value); + if (ret.isOK()) { + ret = env.get(_otherKey, &env_value); if (ret.isOK()) { - ret = env.get(_otherKey, &env_value); - if (!ret.isOK()) { - StringBuilder sb; - sb << _otherKey << " is required when " << _key << " is specified"; - return Status(ErrorCodes::BadValue, sb.str()); - } + StringBuilder sb; + sb << _otherKey << " is not allowed when " << _key << " is specified"; + return Status(ErrorCodes::BadValue, sb.str()); } + } - return Status::OK(); + return Status::OK(); +} + +Status RequiresOtherKeyConstraint::check(const Environment& env) { + Value env_value; + Status ret = env.get(_key, &env_value); + if (ret.isOK()) { + ret = env.get(_otherKey, &env_value); + if (!ret.isOK()) { + StringBuilder sb; + sb << _otherKey << " is required when " << _key << " is specified"; + return Status(ErrorCodes::BadValue, sb.str()); + } } - Status StringFormatKeyConstraint::check(const Environment& env) { - Value value; - Status ret = env.get(_key, &value); - if (ret.isOK()) { - std::string stringVal; - ret = value.get(&stringVal); - if (!ret.isOK()) { - StringBuilder sb; - sb << _key << " could not be read as a string: " << ret.reason(); - return Status(ErrorCodes::BadValue, sb.str()); - } + return Status::OK(); +} - pcrecpp::RE re(_regexFormat); - if (!re.FullMatch(stringVal)) { - StringBuilder sb; - sb << _key << " must be a string of the format: " << _displayFormat; - return Status(ErrorCodes::BadValue, sb.str()); - } +Status StringFormatKeyConstraint::check(const Environment& env) { + Value value; + Status ret = env.get(_key, &value); + if (ret.isOK()) { + std::string stringVal; + ret = value.get(&stringVal); + if (!ret.isOK()) { + StringBuilder sb; + sb << _key << " could not be read as a string: " << ret.reason(); + return Status(ErrorCodes::BadValue, sb.str()); } - return Status::OK(); + pcrecpp::RE re(_regexFormat); + if (!re.FullMatch(stringVal)) { + StringBuilder sb; + sb << _key << " must be a string of the format: " << _displayFormat; + return Status(ErrorCodes::BadValue, sb.str()); + } } -} // namespace optionenvironment -} // namespace mongo + return Status::OK(); +} + +} // namespace optionenvironment +} // namespace mongo |