Fabric: New nice way to deal with DebugStringConvertible items

Summary:
No macros in product code.
(Not all callsites are converted yet.)

Reviewed By: mdvacca

Differential Revision: D7797561

fbshipit-source-id: da899421bf99669a0e0a2b83df6004daf14355c2
This commit is contained in:
Valentin Shergin 2018-05-08 22:56:02 -07:00 committed by Facebook Github Bot
parent d9ff1769aa
commit 6e537b000b
11 changed files with 198 additions and 60 deletions

View File

@ -10,27 +10,21 @@
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include "debugStringConvertibleUtils.h"
namespace facebook {
namespace react {
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList ParagraphAttributes::getDebugProps() const {
ParagraphAttributes defaultParagraphAttributes = {};
SharedDebugStringConvertibleList list = {};
#define PARAGRAPH_ATTRIBUTE(stringName, propertyName, accessor, convertor) \
if (propertyName != defaultParagraphAttributes.propertyName) { \
list.push_back(std::make_shared<DebugStringConvertibleItem>(#stringName, convertor(propertyName accessor))); \
}
PARAGRAPH_ATTRIBUTE(maximumNumberOfLines, maximumNumberOfLines, , std::to_string)
PARAGRAPH_ATTRIBUTE(ellipsizeMode, ellipsizeMode, , stringFromEllipsizeMode)
PARAGRAPH_ATTRIBUTE(adjustsFontSizeToFit, adjustsFontSizeToFit, , std::to_string)
PARAGRAPH_ATTRIBUTE(minimumFontSize, minimumFontSize, , std::to_string)
PARAGRAPH_ATTRIBUTE(maximumFontSize, maximumFontSize, , std::to_string)
return list;
return {
debugStringConvertibleItem("maximumNumberOfLines", maximumNumberOfLines),
debugStringConvertibleItem("ellipsizeMode", ellipsizeMode),
debugStringConvertibleItem("adjustsFontSizeToFit", adjustsFontSizeToFit),
debugStringConvertibleItem("minimumFontSize", minimumFontSize),
debugStringConvertibleItem("maximumFontSize", maximumFontSize)
};
}
} // namespace react

View File

@ -7,8 +7,9 @@
#include "TextAttributes.h"
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/debugStringConvertibleUtils.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include "debugStringConvertibleUtils.h"
namespace facebook {
namespace react {
@ -53,26 +54,44 @@ void TextAttributes::apply(TextAttributes textAttributes) {
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList TextAttributes::getDebugProps() const {
TextAttributes defaultAttributes = {};
SharedDebugStringConvertibleList list = {};
#define PROPS_ADD_TO_SET(propertyName, accessor, convertor) \
if (propertyName != defaultAttributes.propertyName) { \
list.push_back(std::make_shared<DebugStringConvertibleItem>(#propertyName, convertor(propertyName accessor))); \
}
PROPS_ADD_TO_SET(backgroundColor, , colorNameFromColor)
PROPS_ADD_TO_SET(foregroundColor, , colorNameFromColor)
PROPS_ADD_TO_SET(opacity, , std::to_string)
PROPS_ADD_TO_SET(fontFamily, , )
PROPS_ADD_TO_SET(fontSize, , std::to_string)
PROPS_ADD_TO_SET(fontSizeMultiplier, , std::to_string)
// TODO: Implement all fields.
return list;
return {
// Color
debugStringConvertibleItem("backgroundColor", backgroundColor),
debugStringConvertibleItem("foregroundColor", foregroundColor),
debugStringConvertibleItem("opacity", opacity),
// Font
debugStringConvertibleItem("fontFamily", fontFamily),
debugStringConvertibleItem("fontSize", fontSize),
debugStringConvertibleItem("fontSizeMultiplier", fontSizeMultiplier),
debugStringConvertibleItem("fontWeight", fontWeight),
debugStringConvertibleItem("fontStyle", fontStyle),
//debugStringConvertibleItem("fontVariant", fontVariant),
debugStringConvertibleItem("allowFontScaling", allowFontScaling),
debugStringConvertibleItem("letterSpacing", letterSpacing),
// Paragraph Styles
debugStringConvertibleItem("lineHeight", lineHeight),
//debugStringConvertibleItem("alignment", alignment),
//debugStringConvertibleItem("baseWritingDirection", baseWritingDirection),
// Decoration
debugStringConvertibleItem("textDecorationColor", textDecorationColor),
//debugStringConvertibleItem("textDecorationLineType", textDecorationLineType),
//debugStringConvertibleItem("textDecorationLineStyle", textDecorationLineStyle),
//debugStringConvertibleItem("textDecorationLinePattern", textDecorationLinePattern),
// Shadow
debugStringConvertibleItem("textShadowOffset", textShadowOffset),
debugStringConvertibleItem("textShadowRadius", textShadowRadius),
debugStringConvertibleItem("textShadowColor", textShadowColor),
// Special
debugStringConvertibleItem("isHighlighted", isHighlighted),
//debugStringConvertibleItem("layoutDirection", layoutDirection),
};
}
} // namespace react

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/attributedstring/textValuesConversions.h>
namespace facebook {
namespace react {
DEBUG_STRING_CONVERTIBLE_TEMPLATE(EllipsizeMode, stringFromEllipsizeMode)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontWeight, stringFromFontWeight)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontStyle, stringFromFontStyle)
} // namespace react
} // namespace facebook

View File

@ -49,6 +49,10 @@ inline FontWeight fontWeightFromDynamic(const folly::dynamic &value) {
abort();
}
inline std::string stringFromFontWeight(const FontWeight &fontWeight) {
return std::to_string((int)fontWeight);
}
inline FontStyle fontStyleFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "normal") { return FontStyle::Normal; }
@ -57,6 +61,14 @@ inline FontStyle fontStyleFromDynamic(const folly::dynamic &value) {
abort();
}
inline std::string stringFromFontStyle(const FontStyle &fontStyle) {
switch (fontStyle) {
case FontStyle::Normal: return "normal";
case FontStyle::Italic: return "italic";
case FontStyle::Oblique: return "oblique";
}
}
inline FontVariant fontVariantFromDynamic(const folly::dynamic &value) {
assert(value.isArray());
FontVariant fontVariant = FontVariant::Default;

View File

@ -18,6 +18,10 @@ std::string DebugStringConvertible::getDebugChildrenDescription(DebugStringConve
std::string childrenString = "";
for (auto child : getDebugChildren()) {
if (!child) {
continue;
}
childrenString += child->getDebugDescription(options, depth + 1);
}
@ -32,6 +36,10 @@ std::string DebugStringConvertible::getDebugPropsDescription(DebugStringConverti
std::string propsString = "";
for (auto prop : getDebugProps()) {
if (!prop) {
continue;
}
auto name = prop->getDebugName();
auto value = prop->getDebugValue();
auto children = prop->getDebugPropsDescription(options, depth + 1);

View File

@ -0,0 +1,25 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "debugStringConvertibleUtils.h"
namespace facebook {
namespace react {
SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs) {
SharedDebugStringConvertibleList result = {};
std::move(lhs.begin(), lhs.end(), std::back_inserter(result));
std::move(rhs.begin(), rhs.end(), std::back_inserter(result));
return result;
}
SharedDebugStringConvertible debugStringConvertibleItem(std::string name, DebugStringConvertible value, std::string defaultValue) {
return debugStringConvertibleItem(name, value.getDebugDescription(), defaultValue);
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,53 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <string>
#include <limits>
#include <memory>
#include <vector>
#include <fabric/debug/DebugStringConvertible.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <folly/Conv.h>
namespace facebook {
namespace react {
SharedDebugStringConvertibleList operator+(const SharedDebugStringConvertibleList &lhs, const SharedDebugStringConvertibleList &rhs);
SharedDebugStringConvertible debugStringConvertibleItem(std::string name, DebugStringConvertible value, std::string defaultValue = "");
#define IS_EQUAL(a, b) ((a) == (b))
#define IS_EQUAL_FLOAT(a, b) ((isnan(a) == isnan(b)) || ((a) == (b)))
#define DEBUG_STRING_CONVERTIBLE_TEMPLATE(type, converter) \
DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(type, converter, {}, IS_EQUAL)
#define DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(type, converter, defaults, comparator) \
inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, type value, type defaultValue = defaults) { \
if (comparator(value, defaultValue)) { \
return nullptr; \
} \
return std::make_shared<DebugStringConvertibleItem>(name, converter(value)); \
} \
\
inline SharedDebugStringConvertible debugStringConvertibleItem(std::string name, folly::Optional<type> value, type defaultValue = defaults) { \
if (value.has_value()) { \
return nullptr; \
} \
return debugStringConvertibleItem(name, value.value_or(defaultValue), defaultValue); \
}
DEBUG_STRING_CONVERTIBLE_TEMPLATE(std::string, )
DEBUG_STRING_CONVERTIBLE_TEMPLATE(int, folly::to<std::string>)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(bool, folly::to<std::string>)
DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(float, folly::to<std::string>, std::numeric_limits<float>::quiet_NaN(), IS_EQUAL_FLOAT)
DEBUG_STRING_CONVERTIBLE_TEMPLATE_EX(double, folly::to<std::string>, std::numeric_limits<float>::quiet_NaN(), IS_EQUAL_FLOAT)
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,22 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/graphics/graphicValuesConversions.h>
namespace facebook {
namespace react {
DEBUG_STRING_CONVERTIBLE_TEMPLATE(Point, stringFromPoint)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(Size, stringFromSize)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(Rect, stringFromRect)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(SharedColor, colorNameFromColor)
} // namespace react
} // namespace facebook

View File

@ -61,12 +61,7 @@ TextAttributes BaseTextProps::getTextAttributes() const {
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList BaseTextProps::getDebugProps() const {
SharedDebugStringConvertibleList list = {};
auto textAttributesPropsList = textAttributes_.getDebugProps();
std::move(textAttributesPropsList.begin(), textAttributesPropsList.end(), std::back_inserter(list));
return list;
return textAttributes_.getDebugProps();
}
} // namespace react

View File

@ -7,7 +7,7 @@
#include "ParagraphLocalData.h"
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/debug/debugStringConvertibleUtils.h>
namespace facebook {
namespace react {
@ -37,9 +37,9 @@ std::string ParagraphLocalData::getDebugName() const {
}
SharedDebugStringConvertibleList ParagraphLocalData::getDebugProps() const {
SharedDebugStringConvertibleList list = {};
list.push_back(std::make_shared<DebugStringConvertibleItem>("attributedString", attributedString_.getDebugDescription()));
return list;
return {
debugStringConvertibleItem("attributedString", attributedString_)
};
}
} // namespace react

View File

@ -9,7 +9,7 @@
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/core/propsConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/text/propsConversions.h>
namespace facebook {
@ -43,21 +43,10 @@ bool ParagraphProps::getIsSelectable() const {
#pragma mark - DebugStringConvertible
SharedDebugStringConvertibleList ParagraphProps::getDebugProps() const {
SharedDebugStringConvertibleList list = {};
// View Props
auto &&viewPropsList = ViewProps::getDebugProps();
std::move(viewPropsList.begin(), viewPropsList.end(), std::back_inserter(list));
// Paragraph Props
auto &&paragraphAttributePropsList = paragraphAttributes_.getDebugProps();
std::move(paragraphAttributePropsList.begin(), paragraphAttributePropsList.end(), std::back_inserter(list));
// Base Text Props
auto &&baseTextPropsList = BaseTextProps::getDebugProps();
std::move(baseTextPropsList.begin(), baseTextPropsList.end(), std::back_inserter(list));
return list;
return
ViewProps::getDebugProps() +
paragraphAttributes_.getDebugProps() +
BaseTextProps::getDebugProps();
}
} // namespace react