From fa0ad4e7ce8f4c19fe58bf06747bf8c62600581c Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Mon, 10 Dec 2018 16:56:51 -0500 Subject: RPCHelpMan: Check default values are given at compile-time --- src/rpc/util.h | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'src/rpc/util.h') diff --git a/src/rpc/util.h b/src/rpc/util.h index 4a9d4be787..e296ee68c9 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -12,6 +12,8 @@ #include #include +#include + class CKeyStore; class CPubKey; class CScript; @@ -39,11 +41,28 @@ struct RPCArg { AMOUNT, //!< Special type representing a floating point amount (can be either NUM or STR) STR_HEX, //!< Special type that is a STR with only hex chars }; + + enum class Optional { + /** Required arg */ + NO, + /** + * Optinal arg that is a named argument and has a default value of + * `null`. When possible, the default value should be specified. + */ + OMITTED_NAMED_ARG, + /** + * Optional argument with default value omitted because they are + * implicitly clear. That is, elements in an array or object may not + * exist by default. + * When possible, the default value should be specified. + */ + OMITTED, + }; + using Fallback = boost::variant; const std::string m_name; //!< The name of the arg (can be empty for inner args) const Type m_type; const std::vector m_inner; //!< Only used for arrays or dicts - const bool m_optional; - const std::string m_default_value; //!< Only used for optional args + const Fallback m_fallback; const std::string m_description; const std::string m_oneline_description; //!< Should be empty unless it is supposed to override the auto-generated summary line const std::vector m_type_str; //!< Should be empty unless it is supposed to override the auto-generated type strings. Vector length is either 0 or 2, m_type_str.at(0) will override the type of the value in a key-value pair, m_type_str.at(1) will override the type in the argument description. @@ -51,15 +70,13 @@ struct RPCArg { RPCArg( const std::string& name, const Type& type, - const bool opt, - const std::string& default_val, + const Fallback& fallback, const std::string& description, const std::string& oneline_description = "", const std::vector& type_str = {}) : m_name{name}, m_type{type}, - m_optional{opt}, - m_default_value{default_val}, + m_fallback{fallback}, m_description{description}, m_oneline_description{oneline_description}, m_type_str{type_str} @@ -70,8 +87,7 @@ struct RPCArg { RPCArg( const std::string& name, const Type& type, - const bool opt, - const std::string& default_val, + const Fallback& fallback, const std::string& description, const std::vector& inner, const std::string& oneline_description = "", @@ -79,8 +95,7 @@ struct RPCArg { : m_name{name}, m_type{type}, m_inner{inner}, - m_optional{opt}, - m_default_value{default_val}, + m_fallback{fallback}, m_description{description}, m_oneline_description{oneline_description}, m_type_str{type_str} @@ -101,9 +116,8 @@ struct RPCArg { /** * Return the description string, including the argument type and whether * the argument is required. - * implicitly_required is set for arguments in an array, which are neither optional nor required. */ - std::string ToDescriptionString(bool implicitly_required = false) const; + std::string ToDescriptionString() const; }; struct RPCResult { -- cgit v1.2.3