Пример #1
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;";
}
YGDisplay yogaStyleDisplayFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "flex") { return YGDisplayFlex; }
  if (stringValue == "none") { return YGDisplayNone; }

  abort();
}
Пример #3
0
// Parse type from a descriptive string, e.g. "int", "bool", etc...
static DataType kindOfFromDynamic(const folly::dynamic& t) {
  if (!t.isString()) {
    return KindOfInvalid;
  }

  // If you hit this assert, the IDL contains "type": "Array"; you need to
  // use one of {Int64,String,Variant}{Vec,Map} instead.
  //
  // These are still KindOfArray, not the HH types.
  assert(t.asString() != "Array");

  auto it = g_kindOfMap.find(t.asString());
  if (it == g_kindOfMap.end()) {
    return KindOfObject;
  }

  return it->second;
}
YGPositionType yogaStylePositionTypeFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "relative") { return YGPositionTypeRelative; }
  if (stringValue == "absolute") { return YGPositionTypeAbsolute; }

  abort();
}
YGDirection yogaStyleDirectionFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "inherit") { return YGDirectionInherit; }
  if (stringValue == "ltr") { return YGDirectionLTR; }
  if (stringValue == "rtl") { return YGDirectionRTL; }

  abort();
}
YGOverflow yogaStyleOverflowFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "visible") { return YGOverflowVisible; }
  if (stringValue == "hidden") { return YGOverflowHidden; }
  if (stringValue == "scroll") { return YGOverflowScroll; }

  abort();
}
YGWrap yogaStyleWrapFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "no-wrap") { return YGWrapNoWrap; }
  if (stringValue == "wrap") { return YGWrapWrap; }
  if (stringValue == "wrap-reverse") { return YGWrapWrapReverse; }

  abort();
}
YGFlexDirection yogaStyleFlexDirectionFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "column") { return YGFlexDirectionColumn; }
  if (stringValue == "column-reverse") { return YGFlexDirectionColumnReverse; }
  if (stringValue == "row") { return YGFlexDirectionRow; }
  if (stringValue == "row-reverse") { return YGFlexDirectionRowReverse; }

  abort();
}
YGFloatOptional yogaStyleOptionalFloatFromDynamic(const folly::dynamic &value) {
  if (value.isNumber()) {
    return YGFloatOptional(value.asDouble());
  } else if (value.isString()) {
    const auto stringValue = value.asString();
    if (stringValue == "auto") {
      return YGFloatOptional();
    }
  }

  abort();
}
YGJustify yogaStyleJustifyFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "flex-start") { return YGJustifyFlexStart; }
  if (stringValue == "center") { return YGJustifyCenter; }
  if (stringValue == "flex-end") { return YGJustifyFlexEnd; }
  if (stringValue == "space-between") { return YGJustifySpaceBetween; }
  if (stringValue == "space-around") { return YGJustifySpaceAround; }
  if (stringValue == "space-evenly") { return YGJustifySpaceEvenly; }

  abort();
}
YGAlign yogaStyleAlignFromDynamic(const folly::dynamic &value) {
  assert(value.isString());
  auto stringValue = value.asString();

  if (stringValue == "auto") { return YGAlignAuto; }
  if (stringValue == "flex-start") { return YGAlignFlexStart; }
  if (stringValue == "center") { return YGAlignCenter; }
  if (stringValue == "flex-end") { return YGAlignFlexEnd; }
  if (stringValue == "stretch") { return YGAlignStretch; }
  if (stringValue == "baseline") { return YGAlignBaseline; }
  if (stringValue == "between") { return YGAlignSpaceBetween; }
  if (stringValue == "space-around") { return YGAlignSpaceAround; }

  abort();
}
Пример #12
0
fbstring typeString(const folly::dynamic& typeNode, bool isReturnType) {
  if (!typeNode.isString()) {
    return "void";
  }

  auto typeIt = g_typeMap.find(typeNode.asString());
  if (typeIt == g_typeMap.end()) {
    return (isReturnType ? "HPHP::Object" : "HPHP::Object const&");
  }

  auto& type = typeIt->second;
  if (!isReturnType && isTypeCppIndirectPass(type)) {
    return type + " const&";
  } else {
    return type;
  }
}
YGValue yogaStyleValueFromDynamic(const folly::dynamic &value) {
  if (value.isNumber()) {
    float x = value.asDouble();
    return { x, YGUnitPoint };
  } else if (value.isString()) {
    const auto stringValue = value.asString();
    if (stringValue == "auto") {
      return { YGUndefined, YGUnitAuto };
    } else {
      if (stringValue.back() == '%') {
        return { folly::to<float>(stringValue.substr(stringValue.length() - 1)), YGUnitPercent };
      } else {
        return { folly::to<float>(stringValue), YGUnitPoint };
      }
    }
  }

  return YGValueUndefined;
}