Fix getCurrentThreadName() on OSX
[folly.git] / folly / experimental / JSONSchema.cpp
index 6978cf696f9f6610711ff055ccedef9b7dcf781d..f559a545d5985e6d886a6827c3b2b0bf58787ffb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
 #include <folly/Memory.h>
 #include <folly/Optional.h>
 #include <folly/String.h>
-#include <folly/experimental/Singleton.h>
+#include <folly/Singleton.h>
 #include <folly/json.h>
 
 namespace folly {
@@ -59,10 +59,10 @@ Optional<SchemaError> makeError(Args&&... args) {
 struct ValidationContext;
 
 struct IValidator {
-  virtual ~IValidator() {}
+  virtual ~IValidator() = default;
 
  private:
-  friend class ValidationContext;
+  friend struct ValidationContext;
 
   virtual Optional<SchemaError> validate(ValidationContext&,
                                          const dynamic& value) const = 0;
@@ -95,14 +95,14 @@ struct SchemaValidatorContext final {
   explicit SchemaValidatorContext(const dynamic& s) : schema(s) {}
 
   const dynamic& schema;
-  std::unordered_map<fbstring, IValidator*> refs;
+  std::unordered_map<std::string, IValidator*> refs;
 };
 
 /**
  * Root validator for a schema.
  */
 struct SchemaValidator final : IValidator, public Validator {
-  SchemaValidator() {}
+  SchemaValidator() = default;
   void loadSchema(SchemaValidatorContext& context, const dynamic& schema);
 
   Optional<SchemaError> validate(ValidationContext&,
@@ -164,21 +164,21 @@ struct ComparisonValidator final : IValidator {
     if (type_ == Type::MIN) {
       if (exclusive_) {
         if (v <= s) {
-          return makeError("greater than", schema_, value);
+          return makeError("greater than ", schema_, value);
         }
       } else {
         if (v < s) {
-          return makeError("greater than or equal to", schema_, value);
+          return makeError("greater than or equal to ", schema_, value);
         }
       }
     } else if (type_ == Type::MAX) {
       if (exclusive_) {
         if (v >= s) {
-          return makeError("less than", schema_, value);
+          return makeError("less than ", schema_, value);
         }
       } else {
         if (v > s) {
-          return makeError("less than or equal to", schema_, value);
+          return makeError("less than or equal to ", schema_, value);
         }
       }
     }
@@ -219,7 +219,7 @@ struct SizeValidator final : IValidator {
     if (value.type() != type_) {
       return none;
     }
-    if (!Comparison()(length_, value.size())) {
+    if (!Comparison()(length_, int64_t(value.size()))) {
       return makeError("different length string/array/object", value);
     }
     return none;
@@ -231,7 +231,7 @@ struct SizeValidator final : IValidator {
 struct StringPatternValidator final : IValidator {
   explicit StringPatternValidator(const dynamic& schema) {
     if (schema.isString()) {
-      regex_ = boost::regex(schema.getString().toStdString());
+      regex_ = boost::regex(schema.getString());
     }
   }
 
@@ -240,7 +240,7 @@ struct StringPatternValidator final : IValidator {
     if (!value.isString() || regex_.empty()) {
       return none;
     }
-    if (!boost::regex_search(value.getString().toStdString(), regex_)) {
+    if (!boost::regex_search(value.getString(), regex_)) {
       return makeError("string matching regex", value);
     }
     return none;
@@ -352,7 +352,7 @@ struct RequiredValidator final : IValidator {
     if (value.isObject()) {
       for (const auto& prop : properties_) {
         if (!value.get_ptr(prop)) {
-          return makeError("to have property", prop, value);
+          return makeError("property ", prop, value);
         }
       }
     }
@@ -360,7 +360,7 @@ struct RequiredValidator final : IValidator {
   }
 
  private:
-  std::vector<fbstring> properties_;
+  std::vector<std::string> properties_;
 };
 
 struct PropertiesValidator final : IValidator {
@@ -381,8 +381,8 @@ struct PropertiesValidator final : IValidator {
       for (const auto& pair : patternProperties->items()) {
         if (pair.first.isString()) {
           patternPropertyValidators_.emplace_back(
-              make_pair(boost::regex(pair.first.getString().toStdString()),
-                        SchemaValidator::make(context, pair.second)));
+              boost::regex(pair.first.getString()),
+              SchemaValidator::make(context, pair.second));
         }
       }
     }
@@ -405,7 +405,7 @@ struct PropertiesValidator final : IValidator {
       if (!pair.first.isString()) {
         continue;
       }
-      const fbstring& key = pair.first.getString();
+      const std::string& key = pair.first.getString();
       auto it = propertyValidators_.find(key);
       bool matched = false;
       if (it != propertyValidators_.end()) {
@@ -415,7 +415,7 @@ struct PropertiesValidator final : IValidator {
         matched = true;
       }
 
-      const std::string& strkey = key.toStdString();
+      const std::string& strkey = key;
       for (const auto& ppv : patternPropertyValidators_) {
         if (boost::regex_search(strkey, ppv.first)) {
           if (auto se = vc.validate(ppv.second.get(), pair.second)) {
@@ -440,7 +440,8 @@ struct PropertiesValidator final : IValidator {
     return none;
   }
 
-  std::unordered_map<fbstring, std::unique_ptr<IValidator>> propertyValidators_;
+  std::unordered_map<std::string, std::unique_ptr<IValidator>>
+      propertyValidators_;
   std::vector<std::pair<boost::regex, std::unique_ptr<IValidator>>>
       patternPropertyValidators_;
   std::unique_ptr<IValidator> additionalPropertyValidator_;
@@ -457,7 +458,7 @@ struct DependencyValidator final : IValidator {
         continue;
       }
       if (pair.second.isArray()) {
-        auto p = make_pair(pair.first.getString(), std::vector<fbstring>());
+        auto p = make_pair(pair.first.getString(), std::vector<std::string>());
         for (const auto& item : pair.second) {
           if (item.isString()) {
             p.second.push_back(item.getString());
@@ -466,9 +467,8 @@ struct DependencyValidator final : IValidator {
         propertyDep_.emplace_back(std::move(p));
       }
       if (pair.second.isObject()) {
-        schemaDep_.emplace_back(
-            make_pair(pair.first.getString(),
-                      SchemaValidator::make(context, pair.second)));
+        schemaDep_.emplace_back(pair.first.getString(),
+                                SchemaValidator::make(context, pair.second));
       }
     }
   }
@@ -482,7 +482,7 @@ struct DependencyValidator final : IValidator {
       if (value.count(pair.first)) {
         for (const auto& prop : pair.second) {
           if (!value.count(prop)) {
-            return makeError("property", prop, value);
+            return makeError("property ", prop, value);
           }
         }
       }
@@ -497,8 +497,8 @@ struct DependencyValidator final : IValidator {
     return none;
   }
 
-  std::vector<std::pair<fbstring, std::vector<fbstring>>> propertyDep_;
-  std::vector<std::pair<fbstring, std::unique_ptr<IValidator>>> schemaDep_;
+  std::vector<std::pair<std::string, std::vector<std::string>>> propertyDep_;
+  std::vector<std::pair<std::string, std::unique_ptr<IValidator>>> schemaDep_;
 };
 
 struct EnumValidator final : IValidator {
@@ -616,7 +616,7 @@ struct AnyOfValidator final : IValidator {
         errors.emplace_back(*se);
       }
     }
-    const int success = validators_.size() - errors.size();
+    const auto success = validators_.size() - errors.size();
     if (success == 0) {
       return makeError("at least one valid schema", value);
     } else if (success > 1 && type_ == Type::EXACTLY_ONE) {
@@ -691,7 +691,7 @@ void SchemaValidator::loadSchema(SchemaValidatorContext& context,
               s = s->get_ptr(pos);
               continue;
             }
-          } catch (const std::range_error& e) {
+          } catch (const std::range_error&) {
             // ignore
           }
         }
@@ -1011,7 +1011,7 @@ folly::Singleton<Validator> schemaValidator([]() {
 });
 }
 
-Validator::~Validator() {}
+Validator::~Validator() = default;
 
 std::unique_ptr<Validator> makeValidator(const dynamic& schema) {
   auto v = make_unique<SchemaValidator>();
@@ -1021,6 +1021,8 @@ std::unique_ptr<Validator> makeValidator(const dynamic& schema) {
   return std::move(v);
 }
 
-Validator* makeSchemaValidator() { return schemaValidator.get(); }
+std::shared_ptr<Validator> makeSchemaValidator() {
+  return schemaValidator.try_get();
+}
 }
 }