summaryrefslogtreecommitdiff
path: root/src/mongo/bson/util/bson_extract.cpp
diff options
context:
space:
mode:
authorAndy Schwerin <schwerin@10gen.com>2012-11-06 13:03:56 -0500
committerAndy Schwerin <schwerin@10gen.com>2012-11-06 14:40:06 -0500
commit449f8502943557fab1f13e4add93d808e74c68dd (patch)
treee6c5dd29154853d558a8da1c2f657b8dd06b139d /src/mongo/bson/util/bson_extract.cpp
parent32f2b9b4967af5efef63ca8183ee3be5da5b1ee0 (diff)
downloadmongo-449f8502943557fab1f13e4add93d808e74c68dd.tar.gz
Utility methods for extracting elements from BSON objects, that return Status.
Code that works with BSON that return Status objects intead of throwing exceptions generate a lot of boilerplate when using the regular BSON accessor operations. These functions consolidate that boilerplate in one place, bson_extract.h/cpp. It also provides a convenient location for dropping other generic field extraction methods that return Statuses.
Diffstat (limited to 'src/mongo/bson/util/bson_extract.cpp')
-rw-r--r--src/mongo/bson/util/bson_extract.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/mongo/bson/util/bson_extract.cpp b/src/mongo/bson/util/bson_extract.cpp
new file mode 100644
index 00000000000..68843fd445e
--- /dev/null
+++ b/src/mongo/bson/util/bson_extract.cpp
@@ -0,0 +1,87 @@
+/* Copyright 2012 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "mongo/bson/util/bson_extract.h"
+
+#include "mongo/db/jsobj.h"
+
+namespace mongo {
+
+ Status bsonExtractField(const BSONObj& object,
+ const StringData& fieldName,
+ BSONElement* outElement) {
+ BSONElement element = object[fieldName.data()];
+ if (element.eoo())
+ return Status(ErrorCodes::NoSuchKey, std::string(fieldName.data(), fieldName.size()));
+ *outElement = element;
+ return Status::OK();
+ }
+
+ Status bsonExtractTypedField(const BSONObj& object,
+ const StringData& fieldName,
+ BSONType type,
+ BSONElement* outElement) {
+ Status status = bsonExtractField(object, fieldName, outElement);
+ if (!status.isOK())
+ return status;
+ if (type != outElement->type()) {
+ return Status(ErrorCodes::TypeMismatch,
+ std::string("Expected ") + typeName(type) +
+ " found " + typeName(outElement->type()));
+ }
+ return Status::OK();
+ }
+
+ Status bsonExtractBooleanFieldWithDefault(const BSONObj& object,
+ const StringData& fieldName,
+ bool defaultValue,
+ bool* out) {
+ BSONElement value;
+ Status status = bsonExtractField(object, fieldName, &value);
+ if (!status.isOK())
+ return status;
+ if (!value.isNumber() && !value.isBoolean())
+ return Status(ErrorCodes::TypeMismatch, "Expected boolean or number type");
+
+ *out = value.trueValue();
+ return Status::OK();
+ }
+
+ Status bsonExtractStringField(const BSONObj& object,
+ const StringData& fieldName,
+ std::string* out) {
+ BSONElement element;
+ Status status = bsonExtractTypedField(object, fieldName, String, &element);
+ if (!status.isOK())
+ return status;
+ *out = element.str();
+ return Status::OK();
+ }
+
+ Status bsonExtractStringFieldWithDefault(const BSONObj& object,
+ const StringData& fieldName,
+ const StringData& defaultValue,
+ std::string* out) {
+ Status status = bsonExtractStringField(object, fieldName, out);
+ if (status == ErrorCodes::NoSuchKey) {
+ *out = std::string(defaultValue.data(), defaultValue.size());
+ }
+ else if (!status.isOK()) {
+ return status;
+ }
+ return Status::OK();
+ }
+
+} // namespace mongo