summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorJustin Seyster <justin.seyster@mongodb.com>2018-02-15 12:25:37 -0500
committerJustin Seyster <justin.seyster@mongodb.com>2018-02-15 12:25:37 -0500
commit3fe0f3a0623972814edd101436d940182937223e (patch)
tree06a39021f817102059e74d0fba770cfd7b4968b3 /src/mongo/bson
parent482e6bedec49a7066b55c54e54797db76ac1dcda (diff)
downloadmongo-3fe0f3a0623972814edd101436d940182937223e.tar.gz
SERVER-32784 Add $convert with typecast conversions.
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/bsontypes.cpp31
-rw-r--r--src/mongo/bson/bsontypes.h13
2 files changed, 44 insertions, 0 deletions
diff --git a/src/mongo/bson/bsontypes.cpp b/src/mongo/bson/bsontypes.cpp
index a2dbcaaf334..a39470680d2 100644
--- a/src/mongo/bson/bsontypes.cpp
+++ b/src/mongo/bson/bsontypes.cpp
@@ -93,6 +93,37 @@ const char* typeName(BSONType type) {
}
}
+const StringMap<BSONType> kTypeAliasMap = {
+ {typeName(BSONType::NumberDouble), BSONType::NumberDouble},
+ {typeName(BSONType::String), BSONType::String},
+ {typeName(BSONType::Object), BSONType::Object},
+ {typeName(BSONType::Array), BSONType::Array},
+ {typeName(BSONType::BinData), BSONType::BinData},
+ {typeName(BSONType::Undefined), BSONType::Undefined},
+ {typeName(BSONType::jstOID), BSONType::jstOID},
+ {typeName(BSONType::Bool), BSONType::Bool},
+ {typeName(BSONType::Date), BSONType::Date},
+ {typeName(BSONType::jstNULL), BSONType::jstNULL},
+ {typeName(BSONType::RegEx), BSONType::RegEx},
+ {typeName(BSONType::DBRef), BSONType::DBRef},
+ {typeName(BSONType::Code), BSONType::Code},
+ {typeName(BSONType::Symbol), BSONType::Symbol},
+ {typeName(BSONType::CodeWScope), BSONType::CodeWScope},
+ {typeName(BSONType::NumberInt), BSONType::NumberInt},
+ {typeName(BSONType::bsonTimestamp), BSONType::bsonTimestamp},
+ {typeName(BSONType::NumberLong), BSONType::NumberLong},
+ {typeName(BSONType::NumberDecimal), BSONType::NumberDecimal},
+ {typeName(BSONType::MaxKey), BSONType::MaxKey},
+ {typeName(BSONType::MinKey), BSONType::MinKey}};
+
+BSONType typeFromName(StringData name) {
+ auto typeIt = kTypeAliasMap.find(name);
+ uassert(ErrorCodes::BadValue,
+ str::stream() << "Unknown type name: " << name,
+ typeIt != kTypeAliasMap.end());
+ return typeIt->second;
+}
+
std::ostream& operator<<(std::ostream& stream, BSONType type) {
return stream << typeName(type);
}
diff --git a/src/mongo/bson/bsontypes.h b/src/mongo/bson/bsontypes.h
index 115e2d93aee..5ec96545772 100644
--- a/src/mongo/bson/bsontypes.h
+++ b/src/mongo/bson/bsontypes.h
@@ -34,6 +34,7 @@
#include "mongo/config.h"
#include "mongo/platform/decimal128.h"
#include "mongo/util/assert_util.h"
+#include "mongo/util/string_map.h"
namespace mongo {
@@ -108,12 +109,24 @@ enum BSONType {
MaxKey = 127
};
+/*
+ * Maps from the set of type aliases accepted by the $type query operator to the corresponding BSON
+ * types. Excludes "number", since this alias maps to a set of BSON types.
+ */
+extern const StringMap<BSONType> kTypeAliasMap;
+
/**
* returns the name of the argument's type
*/
const char* typeName(BSONType type);
/**
+ * Reverse mapping of typeName(). Throws an exception with error code BadValue when passed in
+ * invalid type name.
+ */
+BSONType typeFromName(StringData name);
+
+/**
* Prints the name of the argument's type to the given stream.
*/
std::ostream& operator<<(std::ostream& stream, BSONType type);