rpc: Move OuterType enum to header

This is needed so that it can be used by RPCResult

Also,
* rename NAMED_ARG to NONE for generalization.
* change RPCArg constructors to initialize the members by moving values
This commit is contained in:
MarcoFalke 2019-12-26 03:26:59 +07:00
parent 31c0006a6c
commit fa7d0503d3
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 42 additions and 43 deletions

View file

@ -311,21 +311,10 @@ struct Sections {
m_sections.push_back(s); m_sections.push_back(s);
} }
/**
* Serializing RPCArgs depends on the outer type. Only arrays and
* dictionaries can be nested in json. The top-level outer type is "named
* arguments", a mix between a dictionary and arrays.
*/
enum class OuterType {
ARR,
OBJ,
NAMED_ARG, // Only set on first recursion
};
/** /**
* Recursive helper to translate an RPCArg into sections * Recursive helper to translate an RPCArg into sections
*/ */
void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NAMED_ARG) void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
{ {
const auto indent = std::string(current_indent, ' '); const auto indent = std::string(current_indent, ' ');
const auto indent_next = std::string(current_indent + 2, ' '); const auto indent_next = std::string(current_indent + 2, ' ');
@ -338,7 +327,7 @@ struct Sections {
case RPCArg::Type::AMOUNT: case RPCArg::Type::AMOUNT:
case RPCArg::Type::RANGE: case RPCArg::Type::RANGE:
case RPCArg::Type::BOOL: { case RPCArg::Type::BOOL: {
if (outer_type == OuterType::NAMED_ARG) return; // Nothing more to do for non-recursive types on first recursion if (outer_type == OuterType::NONE) return; // Nothing more to do for non-recursive types on first recursion
auto left = indent; auto left = indent;
if (arg.m_type_str.size() != 0 && push_name) { if (arg.m_type_str.size() != 0 && push_name) {
left += "\"" + arg.m_name + "\": " + arg.m_type_str.at(0); left += "\"" + arg.m_name + "\": " + arg.m_type_str.at(0);
@ -351,7 +340,7 @@ struct Sections {
} }
case RPCArg::Type::OBJ: case RPCArg::Type::OBJ:
case RPCArg::Type::OBJ_USER_KEYS: { case RPCArg::Type::OBJ_USER_KEYS: {
const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString(); const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
PushSection({indent + (push_name ? "\"" + arg.m_name + "\": " : "") + "{", right}); PushSection({indent + (push_name ? "\"" + arg.m_name + "\": " : "") + "{", right});
for (const auto& arg_inner : arg.m_inner) { for (const auto& arg_inner : arg.m_inner) {
Push(arg_inner, current_indent + 2, OuterType::OBJ); Push(arg_inner, current_indent + 2, OuterType::OBJ);
@ -359,20 +348,20 @@ struct Sections {
if (arg.m_type != RPCArg::Type::OBJ) { if (arg.m_type != RPCArg::Type::OBJ) {
PushSection({indent_next + "...", ""}); PushSection({indent_next + "...", ""});
} }
PushSection({indent + "}" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""}); PushSection({indent + "}" + (outer_type != OuterType::NONE ? "," : ""), ""});
break; break;
} }
case RPCArg::Type::ARR: { case RPCArg::Type::ARR: {
auto left = indent; auto left = indent;
left += push_name ? "\"" + arg.m_name + "\": " : ""; left += push_name ? "\"" + arg.m_name + "\": " : "";
left += "["; left += "[";
const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString(); const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
PushSection({left, right}); PushSection({left, right});
for (const auto& arg_inner : arg.m_inner) { for (const auto& arg_inner : arg.m_inner) {
Push(arg_inner, current_indent + 2, OuterType::ARR); Push(arg_inner, current_indent + 2, OuterType::ARR);
} }
PushSection({indent_next + "...", ""}); PushSection({indent_next + "...", ""});
PushSection({indent + "]" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""}); PushSection({indent + "]" + (outer_type != OuterType::NONE ? "," : ""), ""});
break; break;
} }

View file

@ -101,6 +101,16 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
/** Returns, given services flags, a list of humanly readable (known) network services */ /** Returns, given services flags, a list of humanly readable (known) network services */
UniValue GetServicesNames(ServiceFlags services); UniValue GetServicesNames(ServiceFlags services);
/**
* Serializing JSON objects depends on the outer type. Only arrays and
* dictionaries can be nested in json. The top-level outer type is "NONE".
*/
enum class OuterType {
ARR,
OBJ,
NONE, // Only set on first recursion
};
struct RPCArg { struct RPCArg {
enum class Type { enum class Type {
OBJ, OBJ,
@ -140,37 +150,37 @@ struct RPCArg {
const std::vector<std::string> 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. const std::vector<std::string> 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.
RPCArg( RPCArg(
const std::string& name, const std::string name,
const Type& type, const Type type,
const Fallback& fallback, const Fallback fallback,
const std::string& description, const std::string description,
const std::string& oneline_description = "", const std::string oneline_description = "",
const std::vector<std::string>& type_str = {}) const std::vector<std::string> type_str = {})
: m_name{name}, : m_name{std::move(name)},
m_type{type}, m_type{std::move(type)},
m_fallback{fallback}, m_fallback{std::move(fallback)},
m_description{description}, m_description{std::move(description)},
m_oneline_description{oneline_description}, m_oneline_description{std::move(oneline_description)},
m_type_str{type_str} m_type_str{std::move(type_str)}
{ {
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ); CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ);
} }
RPCArg( RPCArg(
const std::string& name, const std::string name,
const Type& type, const Type type,
const Fallback& fallback, const Fallback fallback,
const std::string& description, const std::string description,
const std::vector<RPCArg>& inner, const std::vector<RPCArg> inner,
const std::string& oneline_description = "", const std::string oneline_description = "",
const std::vector<std::string>& type_str = {}) const std::vector<std::string> type_str = {})
: m_name{name}, : m_name{std::move(name)},
m_type{type}, m_type{std::move(type)},
m_inner{inner}, m_inner{std::move(inner)},
m_fallback{fallback}, m_fallback{std::move(fallback)},
m_description{description}, m_description{std::move(description)},
m_oneline_description{oneline_description}, m_oneline_description{std::move(oneline_description)},
m_type_str{type_str} m_type_str{std::move(type_str)}
{ {
CHECK_NONFATAL(type == Type::ARR || type == Type::OBJ); CHECK_NONFATAL(type == Type::ARR || type == Type::OBJ);
} }