diff options
author | Nick Zolnierz <nicholas.zolnierz@mongodb.com> | 2019-02-08 10:24:36 -0500 |
---|---|---|
committer | Nick Zolnierz <nicholas.zolnierz@mongodb.com> | 2019-02-13 14:22:31 -0500 |
commit | 4a0d769e44e6a022e7c129c1beca6a7c86cdd818 (patch) | |
tree | 2b3bf6c7b6960a40b93953cd768b7715c95e6c8b | |
parent | 9f6ab44d229a40b65e9b0316fb3d72f494504768 (diff) | |
download | mongo-4a0d769e44e6a022e7c129c1beca6a7c86cdd818.tar.gz |
SERVER-39223 Expose type set parsing in JSON Schema parser
-rw-r--r-- | src/mongo/db/matcher/schema/json_schema_parser.cpp | 86 | ||||
-rw-r--r-- | src/mongo/db/matcher/schema/json_schema_parser.h | 7 |
2 files changed, 54 insertions, 39 deletions
diff --git a/src/mongo/db/matcher/schema/json_schema_parser.cpp b/src/mongo/db/matcher/schema/json_schema_parser.cpp index 336fb7f90cd..9f678e10a98 100644 --- a/src/mongo/db/matcher/schema/json_schema_parser.cpp +++ b/src/mongo/db/matcher/schema/json_schema_parser.cpp @@ -146,46 +146,8 @@ StatusWith<std::unique_ptr<InternalSchemaTypeExpression>> parseType( StringData keywordName, BSONElement typeElt, const StringMap<BSONType>& aliasMap) { - if (typeElt.type() != BSONType::String && typeElt.type() != BSONType::Array) { - return {Status(ErrorCodes::TypeMismatch, - str::stream() << "$jsonSchema keyword '" << keywordName - << "' must be either a string or an array of strings")}; - } - - std::set<StringData> aliases; - if (typeElt.type() == BSONType::String) { - if (typeElt.valueStringData() == JSONSchemaParser::kSchemaTypeInteger) { - return {ErrorCodes::FailedToParse, - str::stream() << "$jsonSchema type '" << JSONSchemaParser::kSchemaTypeInteger - << "' is not currently supported."}; - } - aliases.insert(typeElt.valueStringData()); - } else { - for (auto&& typeArrayEntry : typeElt.embeddedObject()) { - if (typeArrayEntry.type() != BSONType::String) { - return {Status(ErrorCodes::TypeMismatch, - str::stream() << "$jsonSchema keyword '" << keywordName - << "' array elements must be strings")}; - } - - if (typeArrayEntry.valueStringData() == JSONSchemaParser::kSchemaTypeInteger) { - return {ErrorCodes::FailedToParse, - str::stream() << "$jsonSchema type '" - << JSONSchemaParser::kSchemaTypeInteger - << "' is not currently supported."}; - } - auto insertionResult = aliases.insert(typeArrayEntry.valueStringData()); - if (!insertionResult.second) { - return {Status(ErrorCodes::FailedToParse, - str::stream() << "$jsonSchema keyword '" << keywordName - << "' has duplicate value: " - << typeArrayEntry.valueStringData())}; - } - } - } - - auto typeSet = MatcherTypeSet::fromStringAliases(std::move(aliases), aliasMap); + auto typeSet = JSONSchemaParser::parseTypeSet(typeElt, aliasMap); if (!typeSet.isOK()) { return typeSet.getStatus(); } @@ -1528,6 +1490,52 @@ StatusWithMatchExpression _parse(StringData path, BSONObj schema, bool ignoreUnk } } // namespace +StatusWith<MatcherTypeSet> JSONSchemaParser::parseTypeSet(BSONElement typeElt, + const StringMap<BSONType>& aliasMap) { + if (typeElt.type() != BSONType::String && typeElt.type() != BSONType::Array) { + return {Status(ErrorCodes::TypeMismatch, + str::stream() << "$jsonSchema keyword '" << typeElt.fieldNameStringData() + << "' must be either a string or an array of strings")}; + } + + std::set<StringData> aliases; + if (typeElt.type() == BSONType::String) { + if (typeElt.valueStringData() == JSONSchemaParser::kSchemaTypeInteger) { + return {ErrorCodes::FailedToParse, + str::stream() << "$jsonSchema type '" << JSONSchemaParser::kSchemaTypeInteger + << "' is not currently supported."}; + } + aliases.insert(typeElt.valueStringData()); + } else { + for (auto&& typeArrayEntry : typeElt.embeddedObject()) { + if (typeArrayEntry.type() != BSONType::String) { + return {Status(ErrorCodes::TypeMismatch, + str::stream() << "$jsonSchema keyword '" + << typeElt.fieldNameStringData() + << "' array elements must be strings")}; + } + + if (typeArrayEntry.valueStringData() == JSONSchemaParser::kSchemaTypeInteger) { + return {ErrorCodes::FailedToParse, + str::stream() << "$jsonSchema type '" + << JSONSchemaParser::kSchemaTypeInteger + << "' is not currently supported."}; + } + + auto insertionResult = aliases.insert(typeArrayEntry.valueStringData()); + if (!insertionResult.second) { + return {Status(ErrorCodes::FailedToParse, + str::stream() << "$jsonSchema keyword '" + << typeElt.fieldNameStringData() + << "' has duplicate value: " + << typeArrayEntry.valueStringData())}; + } + } + } + + return MatcherTypeSet::fromStringAliases(std::move(aliases), aliasMap); +} + StatusWithMatchExpression JSONSchemaParser::parse(BSONObj schema, bool ignoreUnknownKeywords) { LOG(5) << "Parsing JSON Schema: " << schema.jsonString(); try { diff --git a/src/mongo/db/matcher/schema/json_schema_parser.h b/src/mongo/db/matcher/schema/json_schema_parser.h index c4bf2958451..16fdfbb63af 100644 --- a/src/mongo/db/matcher/schema/json_schema_parser.h +++ b/src/mongo/db/matcher/schema/json_schema_parser.h @@ -87,6 +87,13 @@ public: * tree. Returns a non-OK status if the schema is invalid or cannot be parsed. */ static StatusWithMatchExpression parse(BSONObj schema, bool ignoreUnknownKeywords = false); + + /** + * Builds a set of type aliases from the given type element using 'aliasMap'. Returns a non-OK + * status if 'typeElt' is invalid or does not contain an entry in the 'aliasMap'. + */ + static StatusWith<MatcherTypeSet> parseTypeSet(BSONElement typeElt, + const StringMap<BSONType>& aliasMap); }; } // namespace mongo |