Esempio n. 1
0
FailoverErrorsSettings::FailoverErrorsSettings(const folly::dynamic& json) {
  checkLogic(json.isObject() || json.isArray(),
      "Failover errors must be either an array or an object.");

  if (json.isObject()) {
    if (auto jsonGets = json.get_ptr("gets")) {
      gets_ = FailoverErrorsSettings::List(*jsonGets);
    }
    if (auto jsonUpdates = json.get_ptr("updates")) {
      updates_ = FailoverErrorsSettings::List(*jsonUpdates);
    }
    if (auto jsonDeletes = json.get_ptr("deletes")) {
      deletes_ = FailoverErrorsSettings::List(*jsonDeletes);
    }
  } else if (json.isArray()) {
    gets_ = FailoverErrorsSettings::List(json);
    updates_ = FailoverErrorsSettings::List(json);
    deletes_ = FailoverErrorsSettings::List(json);
  }
}
Esempio n. 2
0
fbstring phpSerialize(const folly::dynamic& d) {
  if (d.isNull()) {
    return "N;";
  }
  if (d.isBool()) {
    return d.asBool() ? "b:1;" : "b:0;";
  }
  if (d.isInt()) {
    return "i:" + d.asString() + ";";
  }
  if (d.isDouble()) {
    return "d:" + d.asString() + ";";
  }
  if (d.isString()) {
    auto str = d.asString();
    return folly::to<fbstring>("s:", str.size(), ":\"", str, "\";");
  }
  if (d.isArray()) {
    fbstring ret = folly::to<fbstring>("a:", d.size(), ":{");
    int i = 0;
    for (auto &v : d) {
      ret += folly::to<fbstring>("i:", i, ";", phpSerialize(v));
    }
    return ret + "};";
  }
  if (d.isObject()) {
    fbstring ret = folly::to<fbstring>("a:", d.size(), ":{");
    int nextindex = 0;
    for (auto &k : d.keys()) {
      if (k.isNull()) {
        ret += "i:0;";
        if (nextindex <= 0) {
          nextindex = 1;
        }
      } else if (k.isInt() || k.isDouble()) {
        int i = k.asInt();
        ret += folly::to<fbstring>("i:", i, ";");
        if (nextindex <= i) {
          nextindex = i + 1;
        }
      } else if (k.isString()) {
        ret += folly::to<fbstring>("s:", k.size(), ":\"",
                                   escapeCpp(k.asString()), "\";");
      } else {
        /* Should never be reached, but cover it to be safe */
        ret += folly::to<fbstring>("i:", nextindex++, ";");
      }
      ret += phpSerialize(d[k]);
    }
    return ret + "};";
  }
  throw std::logic_error("Unhandled dynamic type in php serialization");
  return "N;";
}
Esempio n. 3
0
FailoverErrorsSettings::List::List(const folly::dynamic& json) {
  checkLogic(json.isArray(), "List of failover errors is not an array.");

  std::vector<std::string> errors;
  errors.reserve(json.size());
  for (const auto& elem : json) {
    checkLogic(elem.isString(), "Failover error {} is not a string", elem);
    errors.push_back(elem.getString());
  }

  init(std::move(errors));
}
Esempio n. 4
0
bool anyFlags(std::function<bool(const fbstring&)> pred,
              const folly::dynamic& flagsArray) {
  if (!flagsArray.isArray()) {
    return false;
  }
  for (auto const& strDyn : flagsArray) {
    auto str = strDyn.asString();
    if (pred(str)) {
      return true;
    }
  }
  return false;
}
Esempio n. 5
0
static unsigned long parseFlags(const folly::dynamic &flags) {
  if (flags.isNull()) {
    return 0;
  }
  if (!flags.isArray()) {
    throw std::logic_error("'flags' field must be an array");
  }

  unsigned long ret = 0;
  for (auto &flag : flags) {
    auto f = g_flagsMap.find(flag.asString());
    if (f == g_flagsMap.end()) {
      throw std::logic_error(
        folly::format("Unknown flag '{0}' specified", flag.asString()).str()
      );
    }
    ret |= f->second;
  }
  return ret;
}
Esempio n. 6
0
jni::local_ref<NativeArray::jhybridobject>
createReadableNativeArrayWithContents(folly::dynamic array) {
  if (array.isNull()) {
    return jni::local_ref<NativeArray::jhybridobject>();
  }

  if (!array.isArray()) {
    jni::throwNewJavaException("com/facebook/react/bridge/UnexpectedNativeTypeException",
                               "expected Array, got a %s", array.typeName());
  }

  static auto readableNativeArrayClass =
    jni::findClassStatic("com/facebook/react/bridge/ReadableNativeArray");
  static auto readableNativeArrayCtor =
    readableNativeArrayClass->getConstructor<NativeArray::jhybridobject()>();

  auto jnewArray = readableNativeArrayClass->newObject(readableNativeArrayCtor);
  jni::cthis(jnewArray)->array = std::move(array);
  return jnewArray;
}
Esempio n. 7
0
// Infer type from an actual value, e.g. 123, "foo", null, true, etc...
static DataType kindOfFromValue(const folly::dynamic& v) {
  if (v.isNull()) {
    return KindOfNull;
  }
  if (v.isBool()) {
    return KindOfBoolean;
  }
  if (v.isInt()) {
    return KindOfInt64;
  }
  if (v.isDouble()) {
    return KindOfDouble;
  }
  if (v.isString()) {
    return KindOfString;
  }
  if (v.isArray()) {
    return KindOfArray;
  }
  if (v.isObject()) {
    return KindOfObject;
  }
  return KindOfInvalid;
}