Skip to content

Commit fa7d050

Browse files
author
MarcoFalke
committed
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
1 parent 31c0006 commit fa7d050

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

src/rpc/util.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -311,21 +311,10 @@ struct Sections {
311311
m_sections.push_back(s);
312312
}
313313

314-
/**
315-
* Serializing RPCArgs depends on the outer type. Only arrays and
316-
* dictionaries can be nested in json. The top-level outer type is "named
317-
* arguments", a mix between a dictionary and arrays.
318-
*/
319-
enum class OuterType {
320-
ARR,
321-
OBJ,
322-
NAMED_ARG, // Only set on first recursion
323-
};
324-
325314
/**
326315
* Recursive helper to translate an RPCArg into sections
327316
*/
328-
void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NAMED_ARG)
317+
void Push(const RPCArg& arg, const size_t current_indent = 5, const OuterType outer_type = OuterType::NONE)
329318
{
330319
const auto indent = std::string(current_indent, ' ');
331320
const auto indent_next = std::string(current_indent + 2, ' ');
@@ -338,7 +327,7 @@ struct Sections {
338327
case RPCArg::Type::AMOUNT:
339328
case RPCArg::Type::RANGE:
340329
case RPCArg::Type::BOOL: {
341-
if (outer_type == OuterType::NAMED_ARG) return; // Nothing more to do for non-recursive types on first recursion
330+
if (outer_type == OuterType::NONE) return; // Nothing more to do for non-recursive types on first recursion
342331
auto left = indent;
343332
if (arg.m_type_str.size() != 0 && push_name) {
344333
left += "\"" + arg.m_name + "\": " + arg.m_type_str.at(0);
@@ -351,28 +340,28 @@ struct Sections {
351340
}
352341
case RPCArg::Type::OBJ:
353342
case RPCArg::Type::OBJ_USER_KEYS: {
354-
const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString();
343+
const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
355344
PushSection({indent + (push_name ? "\"" + arg.m_name + "\": " : "") + "{", right});
356345
for (const auto& arg_inner : arg.m_inner) {
357346
Push(arg_inner, current_indent + 2, OuterType::OBJ);
358347
}
359348
if (arg.m_type != RPCArg::Type::OBJ) {
360349
PushSection({indent_next + "...", ""});
361350
}
362-
PushSection({indent + "}" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""});
351+
PushSection({indent + "}" + (outer_type != OuterType::NONE ? "," : ""), ""});
363352
break;
364353
}
365354
case RPCArg::Type::ARR: {
366355
auto left = indent;
367356
left += push_name ? "\"" + arg.m_name + "\": " : "";
368357
left += "[";
369-
const auto right = outer_type == OuterType::NAMED_ARG ? "" : arg.ToDescriptionString();
358+
const auto right = outer_type == OuterType::NONE ? "" : arg.ToDescriptionString();
370359
PushSection({left, right});
371360
for (const auto& arg_inner : arg.m_inner) {
372361
Push(arg_inner, current_indent + 2, OuterType::ARR);
373362
}
374363
PushSection({indent_next + "...", ""});
375-
PushSection({indent + "]" + (outer_type != OuterType::NAMED_ARG ? "," : ""), ""});
364+
PushSection({indent + "]" + (outer_type != OuterType::NONE ? "," : ""), ""});
376365
break;
377366
}
378367

src/rpc/util.h

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, Fl
101101
/** Returns, given services flags, a list of humanly readable (known) network services */
102102
UniValue GetServicesNames(ServiceFlags services);
103103

104+
/**
105+
* Serializing JSON objects depends on the outer type. Only arrays and
106+
* dictionaries can be nested in json. The top-level outer type is "NONE".
107+
*/
108+
enum class OuterType {
109+
ARR,
110+
OBJ,
111+
NONE, // Only set on first recursion
112+
};
113+
104114
struct RPCArg {
105115
enum class Type {
106116
OBJ,
@@ -140,37 +150,37 @@ struct RPCArg {
140150
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.
141151

142152
RPCArg(
143-
const std::string& name,
144-
const Type& type,
145-
const Fallback& fallback,
146-
const std::string& description,
147-
const std::string& oneline_description = "",
148-
const std::vector<std::string>& type_str = {})
149-
: m_name{name},
150-
m_type{type},
151-
m_fallback{fallback},
152-
m_description{description},
153-
m_oneline_description{oneline_description},
154-
m_type_str{type_str}
153+
const std::string name,
154+
const Type type,
155+
const Fallback fallback,
156+
const std::string description,
157+
const std::string oneline_description = "",
158+
const std::vector<std::string> type_str = {})
159+
: m_name{std::move(name)},
160+
m_type{std::move(type)},
161+
m_fallback{std::move(fallback)},
162+
m_description{std::move(description)},
163+
m_oneline_description{std::move(oneline_description)},
164+
m_type_str{std::move(type_str)}
155165
{
156166
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ);
157167
}
158168

159169
RPCArg(
160-
const std::string& name,
161-
const Type& type,
162-
const Fallback& fallback,
163-
const std::string& description,
164-
const std::vector<RPCArg>& inner,
165-
const std::string& oneline_description = "",
166-
const std::vector<std::string>& type_str = {})
167-
: m_name{name},
168-
m_type{type},
169-
m_inner{inner},
170-
m_fallback{fallback},
171-
m_description{description},
172-
m_oneline_description{oneline_description},
173-
m_type_str{type_str}
170+
const std::string name,
171+
const Type type,
172+
const Fallback fallback,
173+
const std::string description,
174+
const std::vector<RPCArg> inner,
175+
const std::string oneline_description = "",
176+
const std::vector<std::string> type_str = {})
177+
: m_name{std::move(name)},
178+
m_type{std::move(type)},
179+
m_inner{std::move(inner)},
180+
m_fallback{std::move(fallback)},
181+
m_description{std::move(description)},
182+
m_oneline_description{std::move(oneline_description)},
183+
m_type_str{std::move(type_str)}
174184
{
175185
CHECK_NONFATAL(type == Type::ARR || type == Type::OBJ);
176186
}

0 commit comments

Comments
 (0)