typename RouterInfo::RouteHandlePtr makeModifyKeyRoute()

in mcrouter/routes/ModifyKeyRoute.h [150:219]


typename RouterInfo::RouteHandlePtr makeModifyKeyRoute(
    RouteHandleFactory<typename RouterInfo::RouteHandleIf>& factory,
    const folly::dynamic& json) {
  auto jtarget = json.get_ptr("target");
  checkLogic(jtarget, "ModifyKeyRoute: no target");

  folly::Optional<std::string> routingPrefix;
  if (auto jroutingPrefix = json.get_ptr("set_routing_prefix")) {
    auto rp = jroutingPrefix->stringPiece();
    if (rp.empty()) {
      routingPrefix = "";
    } else {
      try {
        routingPrefix = RoutingPrefix(rp).str();
      } catch (const std::exception& e) {
        throw std::logic_error(
            "ModifyKeyRoute: set_routing_prefix: " + std::string(e.what()));
      }
    }
  }
  std::string keyPrefix;
  if (auto jkeyPrefix = json.get_ptr("ensure_key_prefix")) {
    keyPrefix = jkeyPrefix->getString();
    auto err = isKeyValid<true /* DoSpaceAndCtrlCheck */>(keyPrefix);
    checkLogic(
        keyPrefix.empty() || err == mc_req_err_valid,
        "ModifyKeyRoute: invalid key prefix '{}', {}",
        keyPrefix,
        mc_req_err_to_string(err));
  }

  folly::Optional<std::string> keyReplace;
  if (auto jkeyReplace = json.get_ptr("replace_key_prefix")) {
    keyReplace = jkeyReplace->getString();
    auto err = isKeyValid<true /* DoSpaceAndCtrlCheck */>(keyReplace.value());
    checkLogic(
        keyReplace.value().empty() || err == mc_req_err_valid,
        "ModifyKeyRoute: invalid key prefix '{}', {}",
        keyReplace.value(),
        mc_req_err_to_string(err));
  }

  std::string keySuffix;
  if (auto jkeySuffix = json.get_ptr("set_key_suffix")) {
    keySuffix = jkeySuffix->getString();
    auto err = isKeyValid<true /* DoSpaceAndCtrlCheck */>(keySuffix);
    checkLogic(
        keySuffix.empty() || err == mc_req_err_valid,
        "ModifyKeyRoute: invalid key suffix '{}', {}",
        keySuffix,
        mc_req_err_to_string(err));
  }

  bool modifyInplace = false;
  if (auto joverwrite = json.get_ptr("modify_inplace")) {
    checkLogic(
        !keyReplace.hasValue(),
        "replace_key_prefix and modify_inplace cannot be used together");
    checkLogic(
        joverwrite->isBool(), "ModifyKeyRoute: modify_inplace is not a bool");
    modifyInplace = joverwrite->asBool();
  }
  return makeRouteHandle<typename RouterInfo::RouteHandleIf, ModifyKeyRoute>(
      factory.create(*jtarget),
      std::move(routingPrefix),
      std::move(keyPrefix),
      modifyInplace,
      std::move(keyReplace),
      std::move(keySuffix));
}