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 14 : JsonInputFileFormatter::JsonInputFileFormatter() : _spaces(2), _level(0) {} 16 : 17 : std::string 18 14 : JsonInputFileFormatter::toString(const nlohmann::json & root) 19 : { 20 14 : _stream.clear(); 21 28 : _stream.str(""); 22 554 : for (auto && el : root["blocks"].items()) 23 554 : addBlock(el.key(), el.value(), true); 24 14 : return _stream.str(); 25 : } 26 : 27 : void 28 1129869 : JsonInputFileFormatter::addLine(const std::string & line, 29 : size_t max_line_len, 30 : const std::string & comment) 31 : { 32 1129869 : if (line.empty() && comment.empty()) 33 : { 34 20459 : _stream << "\n"; 35 61377 : return; 36 : } 37 : 38 2218820 : std::string indent(_level * _spaces, ' '); 39 1109410 : auto doc = MooseUtils::trim(comment); 40 1109410 : if (doc.empty()) // Not comment so just print out the line normally 41 : { 42 40918 : _stream << indent << line << "\n"; 43 40918 : 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 1068492 : _stream << indent << line; 49 1068492 : std::vector<std::string> elements; 50 : 51 : // if the line is empty we can just start the comment right away 52 1068492 : int extra = 1; 53 1068492 : if (line.empty()) 54 718588 : extra = 0; 55 : 56 : // be careful of really long lines. 57 1068492 : int len = 100 - max_line_len - indent.size(); 58 1068492 : if (len < 0) 59 3136 : len = 20; 60 : 61 2136984 : MooseUtils::tokenize(doc, elements, len, " \t"); 62 1068492 : std::string first(max_line_len - line.size() + extra, ' '); 63 1068492 : _stream << first << "# " << elements[0] << "\n"; 64 1068492 : std::string cindent(max_line_len + indent.size() + extra, ' '); 65 1991607 : for (size_t i = 1; i < elements.size(); ++i) 66 923115 : _stream << cindent << "# " << elements[i] << "\n"; 67 : 68 1068492 : _stream << std::flush; 69 1150328 : } 70 : 71 : void 72 19968 : JsonInputFileFormatter::addBlock(const std::string & name, 73 : const nlohmann::json & block, 74 : bool toplevel) 75 : { 76 59904 : addLine(""); 77 19968 : if (toplevel) 78 1080 : addLine("[" + name + "]"); 79 : else 80 38856 : addLine("[./" + name + "]"); 81 : 82 19968 : _level++; 83 21156 : std::string desc = block.contains("description") ? nlohmann::to_string(block["description"]) : ""; 84 19968 : if (!desc.empty()) 85 37560 : addLine("", 0, desc); 86 : 87 19968 : if (block.contains("parameters")) 88 18780 : addParameters(block["parameters"]); 89 : 90 19968 : if (block.contains("actions")) 91 : { 92 : // there could be duplicate parameters across actions, last one wins 93 736 : nlohmann::json all_params; 94 736 : auto & actions = block["actions"]; 95 1563 : for (auto && el : actions.items()) 96 : { 97 827 : auto & params = el.value()["parameters"]; 98 6368 : for (auto && param_el : params.items()) 99 6368 : all_params[param_el.key()] = param_el.value(); 100 736 : } 101 736 : addParameters(all_params); 102 736 : } 103 : 104 19968 : if (block.contains("star")) 105 1440 : addBlock("*", block["star"]); 106 : 107 39936 : addTypes("subblock_types", block); 108 19968 : addTypes("types", block); 109 : 110 19968 : if (block.contains("subblocks")) 111 : { 112 77 : auto & subblocks = block["subblocks"]; 113 77 : if (!subblocks.is_null()) 114 245 : for (auto && el : subblocks.items()) 115 245 : addBlock(el.key(), el.value()); 116 : } 117 : 118 19968 : _level--; 119 19968 : if (toplevel) 120 2160 : addLine("[]"); 121 : else 122 77712 : addLine("[../]"); 123 19968 : } 124 : 125 : void 126 39936 : JsonInputFileFormatter::addTypes(const std::string & key, const nlohmann::json & block) 127 : { 128 39936 : if (!block.contains(key)) 129 39200 : return; 130 736 : auto & types = block[key]; 131 736 : if (types.is_null()) 132 245 : return; 133 : 134 1964 : addLine(""); 135 1473 : addLine("[./<types>]"); 136 491 : _level++; 137 19271 : for (auto && el : types.items()) 138 19271 : addBlock("<" + el.key() + ">", el.value()); 139 491 : _level--; 140 1964 : addLine("[../]"); 141 : } 142 : 143 : void 144 19516 : JsonInputFileFormatter::addParameters(const nlohmann::json & params) 145 : { 146 19516 : size_t max_name = 0; 147 369420 : for (auto & el : params.items()) 148 349904 : if (el.key().size() > max_name) 149 65159 : max_name = el.key().size(); 150 : 151 19516 : size_t max_len = 0; 152 19516 : std::map<std::string, std::string> lines; 153 369420 : for (auto && el : params.items()) 154 : { 155 349904 : auto & name = el.key(); 156 349904 : auto & param = el.value(); 157 : auto def = 158 811967 : param.contains("default") ? MooseUtils::trim(nlohmann::to_string(param["default"])) : ""; 159 349904 : if (!def.empty()) 160 237745 : def = def.substr(1, def.size() - 2); 161 349904 : if (def.find(' ') != std::string::npos) 162 7370 : def = "'" + def + "'"; 163 349904 : std::string indent(max_name - name.size(), ' '); 164 349904 : std::string required; 165 349904 : if (param["required"]) 166 33052 : required = "(required)"; 167 349904 : if (def.size() == 0 && required.size() == 0) 168 114199 : def = "(no_default)"; 169 349904 : std::string l = name + indent + " = " + def + required; 170 349904 : if (l.size() > max_len) 171 52102 : max_len = l.size(); 172 349904 : lines[name] = l; 173 369420 : } 174 369420 : for (auto & el : params.items()) 175 : { 176 349904 : auto & name = el.key(); 177 349904 : auto & param = el.value(); 178 349904 : auto & l = lines[name]; 179 349904 : auto desc = nlohmann::to_string(param["description"]); 180 349904 : addLine(l, max_len, desc); 181 : 182 349904 : const auto doc_unit = nlohmann::to_string(param["doc_unit"]); 183 349904 : if (!doc_unit.empty()) 184 699808 : addLine("", max_len + 1, 185 699808 : "Unit: " + doc_unit); // a +1 to account for an empty line 186 : 187 349904 : const auto group = nlohmann::to_string(param["group_name"]); 188 349904 : if (!group.empty()) 189 699808 : addLine("", max_len + 1, 190 699808 : "Group: " + group); // a +1 to account for an empty line 191 369420 : } 192 19516 : }