Skip to content

Commit fad56f7

Browse files
author
MarcoFalke
committed
doc: Properly report optional RPC args
1 parent fa09cb6 commit fad56f7

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/rpc/util.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ struct Sections {
405405
left += push_name ? arg.ToStringObj(/*oneline=*/false) : arg.ToString(/*oneline=*/false);
406406
}
407407
left += ",";
408-
PushSection({left, arg.ToDescriptionString()});
408+
PushSection({left, arg.ToDescriptionString(/*is_named_arg=*/push_name)});
409409
break;
410410
}
411411
case RPCArg::Type::OBJ:
412412
case RPCArg::Type::OBJ_USER_KEYS: {
413-
const auto right = is_top_level_arg ? "" : arg.ToDescriptionString();
413+
const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
414414
PushSection({indent + (push_name ? "\"" + arg.GetName() + "\": " : "") + "{", right});
415415
for (const auto& arg_inner : arg.m_inner) {
416416
Push(arg_inner, current_indent + 2, OuterType::OBJ);
@@ -425,7 +425,7 @@ struct Sections {
425425
auto left = indent;
426426
left += push_name ? "\"" + arg.GetName() + "\": " : "";
427427
left += "[";
428-
const auto right = is_top_level_arg ? "" : arg.ToDescriptionString();
428+
const auto right = is_top_level_arg ? "" : arg.ToDescriptionString(/*is_named_arg=*/push_name);
429429
PushSection({left, right});
430430
for (const auto& arg_inner : arg.m_inner) {
431431
Push(arg_inner, current_indent + 2, OuterType::ARR);
@@ -628,7 +628,7 @@ std::string RPCHelpMan::ToString() const
628628
if (i == 0) ret += "\nArguments:\n";
629629

630630
// Push named argument name and description
631-
sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString());
631+
sections.m_sections.emplace_back(::ToString(i + 1) + ". " + arg.GetFirstName(), arg.ToDescriptionString(/*is_named_arg=*/true));
632632
sections.m_max_pad = std::max(sections.m_max_pad, sections.m_sections.back().m_left.size());
633633

634634
// Recursively push nested args
@@ -722,7 +722,7 @@ bool RPCArg::IsOptional() const
722722
}
723723
}
724724

725-
std::string RPCArg::ToDescriptionString() const
725+
std::string RPCArg::ToDescriptionString(bool is_named_arg) const
726726
{
727727
std::string ret;
728728
ret += "(";
@@ -768,14 +768,12 @@ std::string RPCArg::ToDescriptionString() const
768768
ret += ", optional, default=" + std::get<RPCArg::Default>(m_fallback).write();
769769
} else {
770770
switch (std::get<RPCArg::Optional>(m_fallback)) {
771+
case RPCArg::Optional::OMITTED_NAMED_ARG: // Deprecated alias for OMITTED, can be removed
771772
case RPCArg::Optional::OMITTED: {
773+
if (is_named_arg) ret += ", optional"; // Default value is "null" in dicts. Otherwise,
772774
// nothing to do. Element is treated as if not present and has no default value
773775
break;
774776
}
775-
case RPCArg::Optional::OMITTED_NAMED_ARG: {
776-
ret += ", optional"; // Default value is "null"
777-
break;
778-
}
779777
case RPCArg::Optional::NO: {
780778
ret += ", required";
781779
break;

src/rpc/util.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,24 @@ struct RPCArg {
154154
/** Required arg */
155155
NO,
156156
/**
157+
* The arg is optional for one of two reasons:
158+
*
157159
* Optional arg that is a named argument and has a default value of
158-
* `null`. When possible, the default value should be specified.
159-
*/
160-
OMITTED_NAMED_ARG,
161-
/**
160+
* `null`.
161+
*
162162
* Optional argument with default value omitted because they are
163-
* implicitly clear. That is, elements in an array or object may not
163+
* implicitly clear. That is, elements in an array may not
164164
* exist by default.
165165
* When possible, the default value should be specified.
166166
*/
167167
OMITTED,
168+
OMITTED_NAMED_ARG, // Deprecated alias for OMITTED, can be removed
168169
};
170+
/** Hint for default value */
169171
using DefaultHint = std::string;
172+
/** Default constant value */
170173
using Default = UniValue;
171-
using Fallback = std::variant<Optional, /* hint for default value */ DefaultHint, /* default constant value */ Default>;
174+
using Fallback = std::variant<Optional, DefaultHint, Default>;
172175

173176
const std::string m_names; //!< The name of the arg (can be empty for inner args, can contain multiple aliases separated by | for named request arguments)
174177
const Type m_type;
@@ -234,7 +237,7 @@ struct RPCArg {
234237
* Return the description string, including the argument type and whether
235238
* the argument is required.
236239
*/
237-
std::string ToDescriptionString() const;
240+
std::string ToDescriptionString(bool is_named_arg) const;
238241
};
239242

240243
struct RPCResult {

0 commit comments

Comments
 (0)