Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "JsonInputFileFormatter.h" 11 : #include "MooseUtils.h" 12 : 13 : #include <vector> 14 : 15 6 : JsonInputFileFormatter::JsonInputFileFormatter() : _spaces(2), _level(0) {} 16 : 17 : std::string 18 6 : JsonInputFileFormatter::toString(const nlohmann::json & root) 19 : { 20 6 : _stream.clear(); 21 12 : _stream.str(""); 22 233 : for (auto && el : root["blocks"].items()) 23 233 : addBlock(el.key(), el.value(), true); 24 6 : return _stream.str(); 25 : } 26 : 27 : void 28 475640 : JsonInputFileFormatter::addLine(const std::string & line, 29 : size_t max_line_len, 30 : const std::string & comment) 31 : { 32 475640 : if (line.empty() && comment.empty()) 33 : { 34 8794 : _stream << "\n"; 35 26382 : return; 36 : } 37 : 38 933692 : std::string indent(_level * _spaces, ' '); 39 466846 : auto doc = MooseUtils::trim(comment); 40 466846 : if (doc.empty()) // Not comment so just print out the line normally 41 : { 42 17588 : _stream << indent << line << "\n"; 43 17588 : return; 44 : } 45 : 46 : // We have a comment so we need to break it up over multiple lines if necessary 47 : // and make sure that they all start at the same spot. 48 449258 : _stream << indent << line; 49 449258 : std::vector<std::string> elements; 50 : 51 : // if the line is empty we can just start the comment right away 52 449258 : int extra = 1; 53 449258 : if (line.empty()) 54 302197 : extra = 0; 55 : 56 : // be careful of really long lines. 57 449258 : int len = 100 - max_line_len - indent.size(); 58 449258 : if (len < 0) 59 2565 : len = 20; 60 : 61 898516 : MooseUtils::tokenize(doc, elements, len, " \t"); 62 449258 : std::string first(max_line_len - line.size() + extra, ' '); 63 449258 : _stream << first << "# " << elements[0] << "\n"; 64 449258 : std::string cindent(max_line_len + indent.size() + extra, ' '); 65 830219 : for (size_t i = 1; i < elements.size(); ++i) 66 380961 : _stream << cindent << "# " << elements[i] << "\n"; 67 : 68 449258 : _stream << std::flush; 69 484434 : } 70 : 71 : void 72 8581 : JsonInputFileFormatter::addBlock(const std::string & name, 73 : const nlohmann::json & block, 74 : bool toplevel) 75 : { 76 25743 : addLine(""); 77 8581 : if (toplevel) 78 454 : addLine("[" + name + "]"); 79 : else 80 16708 : addLine("[./" + name + "]"); 81 : 82 8581 : _level++; 83 9087 : std::string desc = block.contains("description") ? nlohmann::to_string(block["description"]) : ""; 84 8581 : if (!desc.empty()) 85 16150 : addLine("", 0, desc); 86 : 87 8581 : if (block.contains("parameters")) 88 8075 : addParameters(block["parameters"]); 89 : 90 8581 : if (block.contains("actions")) 91 : { 92 : // there could be duplicate parameters across actions, last one wins 93 315 : nlohmann::json all_params; 94 315 : auto & actions = block["actions"]; 95 669 : for (auto && el : actions.items()) 96 : { 97 354 : auto & params = el.value()["parameters"]; 98 2706 : for (auto && param_el : params.items()) 99 2706 : all_params[param_el.key()] = param_el.value(); 100 315 : } 101 315 : addParameters(all_params); 102 315 : } 103 : 104 8581 : if (block.contains("star")) 105 621 : addBlock("*", block["star"]); 106 : 107 17162 : addTypes("subblock_types", block); 108 8581 : addTypes("types", block); 109 : 110 8581 : if (block.contains("subblocks")) 111 : { 112 33 : auto & subblocks = block["subblocks"]; 113 33 : if (!subblocks.is_null()) 114 105 : for (auto && el : subblocks.items()) 115 105 : addBlock(el.key(), el.value()); 116 : } 117 : 118 8581 : _level--; 119 8581 : if (toplevel) 120 908 : addLine("[]"); 121 : else 122 33416 : addLine("[../]"); 123 8581 : } 124 : 125 : void 126 17162 : JsonInputFileFormatter::addTypes(const std::string & key, const nlohmann::json & block) 127 : { 128 17162 : if (!block.contains(key)) 129 16847 : return; 130 315 : auto & types = block[key]; 131 315 : if (types.is_null()) 132 102 : return; 133 : 134 852 : addLine(""); 135 639 : addLine("[./<types>]"); 136 213 : _level++; 137 8288 : for (auto && el : types.items()) 138 8288 : addBlock("<" + el.key() + ">", el.value()); 139 213 : _level--; 140 852 : addLine("[../]"); 141 : } 142 : 143 : void 144 8390 : JsonInputFileFormatter::addParameters(const nlohmann::json & params) 145 : { 146 8390 : size_t max_name = 0; 147 155451 : for (auto & el : params.items()) 148 147061 : if (el.key().size() > max_name) 149 26987 : max_name = el.key().size(); 150 : 151 8390 : size_t max_len = 0; 152 8390 : std::map<std::string, std::string> lines; 153 155451 : for (auto && el : params.items()) 154 : { 155 147061 : auto & name = el.key(); 156 147061 : auto & param = el.value(); 157 : auto def = 158 341624 : param.contains("default") ? MooseUtils::trim(nlohmann::to_string(param["default"])) : ""; 159 147061 : if (!def.empty()) 160 99559 : def = def.substr(1, def.size() - 2); 161 147061 : if (def.find(' ') != std::string::npos) 162 2892 : def = "'" + def + "'"; 163 147061 : std::string indent(max_name - name.size(), ' '); 164 147061 : std::string required; 165 147061 : if (param["required"]) 166 14076 : required = "(required)"; 167 147061 : if (def.size() == 0 && required.size() == 0) 168 47612 : def = "(no_default)"; 169 147061 : std::string l = name + indent + " = " + def + required; 170 147061 : if (l.size() > max_len) 171 22157 : max_len = l.size(); 172 147061 : lines[name] = l; 173 155451 : } 174 155451 : for (auto & el : params.items()) 175 : { 176 147061 : auto & name = el.key(); 177 147061 : auto & param = el.value(); 178 147061 : auto & l = lines[name]; 179 147061 : auto desc = nlohmann::to_string(param["description"]); 180 147061 : addLine(l, max_len, desc); 181 : 182 147061 : const auto doc_unit = nlohmann::to_string(param["doc_unit"]); 183 147061 : if (!doc_unit.empty()) 184 294122 : addLine("", max_len + 1, 185 294122 : "Unit: " + doc_unit); // a +1 to account for an empty line 186 : 187 147061 : const auto group = nlohmann::to_string(param["group_name"]); 188 147061 : if (!group.empty()) 189 294122 : addLine("", max_len + 1, 190 294122 : "Group: " + group); // a +1 to account for an empty line 191 155451 : } 192 8390 : }