Skip to content

Commit 8c022e8

Browse files
author
MarcoFalke
committed
Merge #15746: rpc: RPCHelpMan: Always name dictionary keys
fa26eb5 rpc: RPCHelpMan: Always push_name when outer type is an object (MarcoFalke) fa652b2 rpc: Add some doxygen comments to utils (MarcoFalke) Pull request description: Fixes two issues reported in #15737: * > I am very perplexed as to how the code I'm looking at is generating the help text I'm seeing So add documentation * > This is a value for which a key is missing So always serialize the name of the dictionary key if the outer type is a dictionary ACKs for commit fa26eb: promag: Tested ACK fa26eb5. Tree-SHA512: b6f0cee1f1123d245d4902e8e113b5260cae7f2cb39c9bfb8893c5b0b33ffb6349ad05813d560d39a94ccf655399c05fcda15d9b0733e6bd696538fe0aca7021
2 parents 5392aee + fa26eb5 commit 8c022e8

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/rpc/util.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,21 @@ UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_s
165165
}
166166
}
167167

168+
/**
169+
* A pair of strings that can be aligned (through padding) with other Sections
170+
* later on
171+
*/
168172
struct Section {
169173
Section(const std::string& left, const std::string& right)
170174
: m_left{left}, m_right{right} {}
171175
const std::string m_left;
172176
const std::string m_right;
173177
};
174178

179+
/**
180+
* Keeps track of RPCArgs by transforming them into sections for the purpose
181+
* of serializing everything to a single string
182+
*/
175183
struct Sections {
176184
std::vector<Section> m_sections;
177185
size_t m_max_pad{0};
@@ -182,16 +190,26 @@ struct Sections {
182190
m_sections.push_back(s);
183191
}
184192

193+
/**
194+
* Serializing RPCArgs depends on the outer type. Only arrays and
195+
* dictionaries can be nested in json. The top-level outer type is "named
196+
* arguments", a mix between a dictionary and arrays.
197+
*/
185198
enum class OuterType {
186199
ARR,
187200
OBJ,
188201
NAMED_ARG, // Only set on first recursion
189202
};
190203

204+
/**
205+
* Recursive helper to translate an RPCArg into sections
206+
*/
191207
void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NAMED_ARG)
192208
{
193209
const auto indent = std::string(current_indent, ' ');
194210
const auto indent_next = std::string(current_indent + 2, ' ');
211+
const bool push_name{outer_type == OuterType::OBJ}; // Dictionary keys must have a name
212+
195213
switch (arg.m_type) {
196214
case RPCArg::Type::STR_HEX:
197215
case RPCArg::Type::STR:
@@ -201,10 +219,10 @@ struct Sections {
201219
case RPCArg::Type::BOOL: {
202220
if (outer_type == OuterType::NAMED_ARG) return; // Nothing more to do for non-recursive types on first recursion
203221
auto left = indent;
204-
if (arg.m_type_str.size() != 0 && outer_type == OuterType::OBJ) {
222+
if (arg.m_type_str.size() != 0 && push_name) {
205223
left += "\"" + arg.m_name + "\": " + arg.m_type_str.at(0);
206224
} else {
207-
left += outer_type == OuterType::OBJ ? arg.ToStringObj(/* oneline */ false) : arg.ToString(/* oneline */ false);
225+
left += push_name ? arg.ToStringObj(/* oneline */ false) : arg.ToString(/* oneline */ false);
208226
}
209227
left += ",";
210228
PushSection({left, arg.ToDescriptionString()});
@@ -213,7 +231,7 @@ struct Sections {
213231
case RPCArg::Type::OBJ:
214232
case RPCArg::Type::OBJ_USER_KEYS: {
215233
const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString();
216-
PushSection({indent + "{", right});
234+
PushSection({indent + (push_name ? "\"" + arg.m_name + "\": " : "") + "{", right});
217235
for (const auto& arg_inner : arg.m_inner) {
218236
Push(arg_inner, current_indent + 2, OuterType::OBJ);
219237
}
@@ -225,7 +243,7 @@ struct Sections {
225243
}
226244
case RPCArg::Type::ARR: {
227245
auto left = indent;
228-
left += outer_type == OuterType::OBJ ? "\"" + arg.m_name + "\": " : "";
246+
left += push_name ? "\"" + arg.m_name + "\": " : "";
229247
left += "[";
230248
const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString();
231249
PushSection({left, right});
@@ -241,6 +259,9 @@ struct Sections {
241259
}
242260
}
243261

262+
/**
263+
* Concatenate all sections with proper padding
264+
*/
244265
std::string ToString() const
245266
{
246267
std::string ret;

0 commit comments

Comments
 (0)