json: fix indentation of last }

This commit is contained in:
Jake Hillion 2023-08-25 10:46:34 -07:00 committed by Jake Hillion
parent a9cffbe22d
commit 5b674fd74c

View File

@ -15,6 +15,7 @@
*/ */
#include <oi/exporters/Json.h> #include <oi/exporters/Json.h>
#include <algorithm>
#include <stdexcept> #include <stdexcept>
template <class> template <class>
@ -34,8 +35,9 @@ void printStringList(std::ostream& out, It it, It end, bool pretty) {
out << ']'; out << ']';
} }
std::string makeIndent(std::vector<std::string_view>& tp) { std::string makeIndent(size_t depth) {
return std::string((tp.size() - 1) * 4, ' '); depth = std::max(depth, 1UL);
return std::string((depth - 1) * 4, ' ');
} }
} // namespace } // namespace
@ -52,13 +54,14 @@ void Json::print(IntrospectionResult::const_iterator& it,
IntrospectionResult::const_iterator end) { IntrospectionResult::const_iterator end) {
std::vector<std::string_view> firstTypePath = it->type_path; std::vector<std::string_view> firstTypePath = it->type_path;
const auto indent = pretty_ ? makeIndent(firstTypePath) : ""; const auto indent = pretty_ ? makeIndent(firstTypePath.size()) : "";
const auto lastIndent =
pretty_ ? makeIndent(std::max(firstTypePath.size(), 1UL) - 1) : "";
const auto* tab = pretty_ ? " " : ""; const auto* tab = pretty_ ? " " : "";
const auto* space = pretty_ ? " " : ""; const auto* space = pretty_ ? " " : "";
const auto* endl = pretty_ ? "\n" : ""; const auto* endl = pretty_ ? "\n" : "";
out_ << '['; out_ << '[' << endl << indent;
out_ << endl << indent;
bool first = true; bool first = true;
while (it != end) { while (it != end) {
@ -114,12 +117,16 @@ void Json::print(IntrospectionResult::const_iterator& it,
if ((++it)->type_path.size() > firstTypePath.size()) { if ((++it)->type_path.size() > firstTypePath.size()) {
print(it, end); print(it, end);
} else { } else {
out_ << "[]" << endl << indent; out_ << "[]" << endl;
} }
out_ << "}"; out_ << indent << "}";
}
if (firstTypePath.size() == 1) {
out_ << endl << ']' << endl;
} else {
out_ << endl << lastIndent << tab << ']' << endl;
} }
out_ << endl << indent << ']' << endl;
} }
} // namespace oi::exporters } // namespace oi::exporters