conversions.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <better/map.h>
  9. #include <folly/dynamic.h>
  10. #include <react/graphics/conversions.h>
  11. #include <react/imagemanager/primitives.h>
  12. namespace facebook {
  13. namespace react {
  14. inline void fromRawValue(const RawValue &value, ImageSource &result) {
  15. if (value.hasType<std::string>()) {
  16. result = {
  17. /* .type = */ ImageSource::Type::Remote,
  18. /* .uri = */ (std::string)value,
  19. };
  20. return;
  21. }
  22. if (value.hasType<better::map<std::string, RawValue>>()) {
  23. auto items = (better::map<std::string, RawValue>)value;
  24. result = {};
  25. result.type = ImageSource::Type::Remote;
  26. if (items.find("__packager_asset") != items.end()) {
  27. result.type = ImageSource::Type::Local;
  28. }
  29. if (items.find("width") != items.end() &&
  30. items.find("height") != items.end() &&
  31. // The following checks have to be removed after codegen is shipped.
  32. // See T45151459.
  33. items.at("width").hasType<Float>() &&
  34. items.at("height").hasType<Float>()) {
  35. result.size = {(Float)items.at("width"), (Float)items.at("height")};
  36. }
  37. if (items.find("scale") != items.end() &&
  38. // The following checks have to be removed after codegen is shipped.
  39. // See T45151459.
  40. items.at("scale").hasType<Float>()) {
  41. result.scale = (Float)items.at("scale");
  42. } else {
  43. result.scale = items.find("deprecated") != items.end() ? 0.0 : 1.0;
  44. }
  45. if (items.find("url") != items.end() &&
  46. // The following should be removed after codegen is shipped.
  47. // See T45151459.
  48. items.at("url").hasType<std::string>()) {
  49. result.uri = (std::string)items.at("url");
  50. }
  51. if (items.find("uri") != items.end() &&
  52. // The following should be removed after codegen is shipped.
  53. // See T45151459.
  54. items.at("uri").hasType<std::string>()) {
  55. result.uri = (std::string)items.at("uri");
  56. }
  57. if (items.find("bundle") != items.end() &&
  58. // The following should be removed after codegen is shipped.
  59. // See T45151459.
  60. items.at("bundle").hasType<std::string>()) {
  61. result.bundle = (std::string)items.at("bundle");
  62. result.type = ImageSource::Type::Local;
  63. }
  64. return;
  65. }
  66. // The following should be removed after codegen is shipped.
  67. // See T45151459.
  68. result = {};
  69. result.type = ImageSource::Type::Invalid;
  70. }
  71. inline std::string toString(const ImageSource &value) {
  72. return "{uri: " + value.uri + "}";
  73. }
  74. inline void fromRawValue(const RawValue &value, ImageResizeMode &result) {
  75. assert(value.hasType<std::string>());
  76. auto stringValue = (std::string)value;
  77. if (stringValue == "cover") {
  78. result = ImageResizeMode::Cover;
  79. return;
  80. }
  81. if (stringValue == "contain") {
  82. result = ImageResizeMode::Contain;
  83. return;
  84. }
  85. if (stringValue == "stretch") {
  86. result = ImageResizeMode::Stretch;
  87. return;
  88. }
  89. if (stringValue == "center") {
  90. result = ImageResizeMode::Center;
  91. return;
  92. }
  93. if (stringValue == "repeat") {
  94. result = ImageResizeMode::Repeat;
  95. return;
  96. }
  97. abort();
  98. }
  99. inline std::string toString(const ImageResizeMode &value) {
  100. switch (value) {
  101. case ImageResizeMode::Cover:
  102. return "cover";
  103. case ImageResizeMode::Contain:
  104. return "contain";
  105. case ImageResizeMode::Stretch:
  106. return "stretch";
  107. case ImageResizeMode::Center:
  108. return "center";
  109. case ImageResizeMode::Repeat:
  110. return "repeat";
  111. }
  112. }
  113. } // namespace react
  114. } // namespace facebook