DebugStringConvertibleOptions: Formating

Summary:
DebugStringConvertibleOptions allows pretty-format debug strings.
https://pxl.cl/ch0m

Reviewed By: fkgozali

Differential Revision: D7312622

fbshipit-source-id: 0ed62520bbc521790bedf5a6d18c796b42f85658
This commit is contained in:
Valentin Shergin 2018-03-18 19:04:32 -07:00 committed by Facebook Github Bot
parent 4cda0df2e5
commit 378da73201
2 changed files with 28 additions and 12 deletions

View File

@ -10,23 +10,31 @@
namespace facebook {
namespace react {
std::string DebugStringConvertible::getDebugChildrenDescription(int level) const {
std::string DebugStringConvertible::getDebugChildrenDescription(DebugStringConvertibleOptions options, int depth) const {
if (depth >= options.maximumDepth) {
return "";
}
std::string childrenString = "";
for (auto child : getDebugChildren()) {
childrenString += child->getDebugDescription(level + 1);
childrenString += child->getDebugDescription(options, depth + 1);
}
return childrenString;
}
std::string DebugStringConvertible::getDebugPropsDescription(int level) const {
std::string DebugStringConvertible::getDebugPropsDescription(DebugStringConvertibleOptions options, int depth) const {
if (depth >= options.maximumDepth) {
return "";
}
std::string propsString = "";
for (auto prop : getDebugProps()) {
auto name = prop->getDebugName();
auto value = prop->getDebugValue();
auto children = prop->getDebugPropsDescription(level + 1);
auto children = prop->getDebugPropsDescription(options, depth + 1);
auto valueAndChildren = value + (children.empty() ? "" : "(" + children + ")");
propsString += " " + name + (valueAndChildren.empty() ? "" : "=" + valueAndChildren);
}
@ -39,16 +47,19 @@ std::string DebugStringConvertible::getDebugPropsDescription(int level) const {
return propsString;
}
std::string DebugStringConvertible::getDebugDescription(int level) const {
std::string DebugStringConvertible::getDebugDescription(DebugStringConvertibleOptions options, int depth) const {
std::string nameString = getDebugName();
std::string valueString = getDebugValue();
std::string childrenString = getDebugChildrenDescription(level);
std::string propsString = getDebugPropsDescription(level);
std::string childrenString = getDebugChildrenDescription(options, depth + 1);
std::string propsString = getDebugPropsDescription(options, depth /* The first-level props are considered as same-depth things. */);
return "<" + nameString +
std::string leading = options.format ? std::string(depth, '\t') : "";
std::string trailing = options.format ? "\n" : "";
return leading + "<" + nameString +
(valueString.empty() ? "" : "=" + valueString) +
(propsString.empty() ? "" : " " + propsString) +
(childrenString.empty() ? "/>" : ">" + childrenString + "</" + nameString + ">");
(childrenString.empty() ? "/>" + trailing : ">" + trailing + childrenString + leading + "</" + nameString + ">" + trailing);
}
std::string DebugStringConvertible::getDebugName() const {

View File

@ -18,6 +18,11 @@ class DebugStringConvertible;
using SharedDebugStringConvertible = std::shared_ptr<const DebugStringConvertible>;
using SharedDebugStringConvertibleList = std::vector<const SharedDebugStringConvertible>;
struct DebugStringConvertibleOptions {
bool format {true};
int maximumDepth {INT_MAX};
};
// Abstract class describes conformance to DebugStringConvertible concept
// and implements basic recursive debug string assembly algorithm.
// Use this as a base class for providing a debugging textual representation
@ -47,12 +52,12 @@ public:
// Returns a string which represents the object in a human-readable way.
// Default implementation returns a description of the subtree
// rooted at this node, represented in XML-like format.
virtual std::string getDebugDescription(int level = 0) const;
virtual std::string getDebugDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const;
// Do same as `getDebugDescription` but return only *children* and
// *properties* parts (which are used in `getDebugDescription`).
virtual std::string getDebugPropsDescription(int level = 0) const;
virtual std::string getDebugChildrenDescription(int level = 0) const;
virtual std::string getDebugPropsDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const;
virtual std::string getDebugChildrenDescription(DebugStringConvertibleOptions options = {}, int depth = 0) const;
};
} // namespace react