MongoDB  2.7.0
constraints.h
1 /* Copyright 2013 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "mongo/base/status.h"
19 #include "mongo/bson/util/builder.h"
20 #include "mongo/util/options_parser/environment.h"
21 
22 namespace mongo {
23 namespace optionenvironment {
24 
32  class Constraint {
33  public:
34  // Interface
35  Status operator()(const Environment& env) { return check(env); }
36  virtual ~Constraint() {}
37  private:
38  // Implementation
39  virtual Status check(const Environment&) = 0;
40  };
41 
46  class KeyConstraint : public Constraint {
47  public:
48  KeyConstraint(const Key& key) :
49  _key(key)
50  { }
51  virtual ~KeyConstraint() {}
52  protected:
53  Key _key;
54  };
55 
60  public:
61  NumericKeyConstraint(const Key& k, long min, long max) :
62  KeyConstraint(k),
63  _min(min),
64  _max(max)
65  { }
66  virtual ~NumericKeyConstraint() {}
67 
68  private:
69  virtual Status check(const Environment& env);
70  long _min;
71  long _max;
72  };
73 
79  public:
80  ImmutableKeyConstraint(const Key& k) : KeyConstraint(k)
81  { }
82  virtual ~ImmutableKeyConstraint() {}
83 
84  private:
85  virtual Status check(const Environment& env);
86  Value _value;
87  };
88 
93  public:
94  MutuallyExclusiveKeyConstraint(const Key& key, const Key& otherKey) : KeyConstraint(key),
95  _otherKey(otherKey)
96  { }
97  virtual ~MutuallyExclusiveKeyConstraint() {}
98  private:
99  virtual Status check(const Environment& env);
100  Key _otherKey;
101  };
102 
107  public:
108  RequiresOtherKeyConstraint(const Key& key, const Key& otherKey) : KeyConstraint(key),
109  _otherKey(otherKey)
110  { }
111  virtual ~RequiresOtherKeyConstraint() {}
112  private:
113  virtual Status check(const Environment& env);
114  Key _otherKey;
115  };
116 
121  public:
122  StringFormatKeyConstraint(const Key& key,
123  const std::string& regexFormat,
124  const std::string& displayFormat) : KeyConstraint(key),
125  _regexFormat(regexFormat),
126  _displayFormat(displayFormat)
127  { }
128  virtual ~StringFormatKeyConstraint() {}
129  private:
130  virtual Status check(const Environment& env);
131  std::string _regexFormat;
132  std::string _displayFormat;
133  };
134 
139  template <typename T>
141  public:
142  TypeKeyConstraint(const Key& k) :
143  KeyConstraint(k)
144  { }
145  virtual ~TypeKeyConstraint() {}
146 
147  private:
148  virtual Status check(const Environment& env) {
149  Value val;
150  Status s = env.get(_key, &val);
151  if (!s.isOK()) {
152  // Key not set, skipping type constraint check
153  return Status::OK();
154  }
155 
156  // The code that controls whether a type is "compatible" is contained in the Value
157  // class, so if that handles compatibility between numeric types then this will too.
158  T typedVal;
159  if (!val.get(&typedVal).isOK()) {
160  StringBuilder sb;
161  sb << "Error: value for key: " << _key << " was found as type: "
162  << val.typeToString() << " but is required to be type: " << typeid(typedVal).name();
163  return Status(ErrorCodes::InternalError, sb.str());
164  }
165  return Status::OK();
166  }
167  };
168 
169 } // namespace optionenvironment
170 } // namespace mongo
A Constraint validates an Environment.
Definition: constraints.h:32
std::string typeToString() const
Return the value's type as a string.
Definition: value.cpp:128
A simple container interface for storing various C++ values.
Definition: options_parser/value.h:54
Implementation of a Constraint that enforces a specific format on a string value. ...
Definition: constraints.h:120
Implementation of a Constraint that makes two keys mutually exclusive.
Definition: constraints.h:92
Implementation of a Constraint that makes one key require another.
Definition: constraints.h:106
Status get(const Key &key, Value *value) const
Populate the given Value with the Value stored for the given Key.
Definition: environment.cpp:42
Implementation of a Constraint on the range of a numeric Value.
Definition: constraints.h:59
Implementation of a Constraint on the type of a Value.
Definition: constraints.h:140
stringstream deals with locale so this is a lot faster than std::stringstream for UTF8 ...
Definition: builder.h:61
Implementation of a Constraint that makes a Value immutable.
Definition: constraints.h:78
A KeyConstraint is a Constraint on a specific Key.
Definition: constraints.h:46
An Environment is a map of values that can be validated according to a set of registered constraints...
Definition: environment.h:98