-
Notifications
You must be signed in to change notification settings - Fork 296
Cache CLI input values #1496
Cache CLI input values #1496
Conversation
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use clang-format please
@@ -73,6 +77,8 @@ namespace iroha_cli { | |||
ParamsMap getCommonParamsMap(const std::string &default_ip, | |||
int default_port); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to add doc comment
// Description of parameters | ||
using ParamsDescription = std::vector<std::string>; | ||
using ParamsDescription = std::vector<std::shared_ptr<ParamData>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why shared_ptr
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I can use and update the cache. I tried passing ParamData by refs instead of a pointer but doesn't seem to work with boost::optional
(or I couldn't get it to work). I can give it another go to avoid using a pointer if that's preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean why not just std::vector<ParamData>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it gets copied and I lose the reference to the initial objection when trying to update the cache (interactive_common_cli.cpp:148).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it looks useless. Parameter in function is auto
, not auto &
, so modification of param
bring nothing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give another go at using std::vector<ParamData>
. IIRC I couldn't get it to compile when tried to return a reference in findInHandlerMap.
// commonParamsMap | ||
}; | ||
} | ||
|
||
ParamsDescription makeParamsDescription(std::vector<std::string> params) { | ||
ParamsDescription data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using:
return std::accumulate(
params.begin(),
params.end(),
ParamsDescription{},
[](auto &&acc, auto &el) {
acc.push_back(std::make_shared<ParamData>(std::make_pair(el, "")));
return std::forward<decltype(acc)>(acc);
});
@@ -62,11 +72,11 @@ namespace iroha_cli { | |||
} | |||
|
|||
void printCommandParameters(std::string &command, | |||
std::vector<std::string> parameters) { | |||
ParamsDescription parameters) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be const ParamsDescription&
@@ -87,12 +97,20 @@ namespace iroha_cli { | |||
return line; | |||
} | |||
|
|||
boost::optional<std::string> promptString(ParamData param) { | |||
std::string message = std::get<0>(param); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consider adding a new structure, because std::get
say nothing about the semantic
@@ -87,12 +97,20 @@ namespace iroha_cli { | |||
return line; | |||
} | |||
|
|||
boost::optional<std::string> promptString(ParamData param) { | |||
std::string message = std::get<0>(param); | |||
if (not std::get<1>(param).empty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::accumulate is also applicable here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think it is appropriate to use std::accumulate
here. param
is just a std::pair
(and changing to a struct).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, indeed. I though there was a loop, nvm then
auto val = promptString(param); | ||
if (val and not val.value().empty()) { | ||
params.push_back(val.value()); | ||
auto val = promptString(*param); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer using monadic bind operator
promptString(*param) | [&](auto &val) {
if (not val.empty()) {
...
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to use this feature, I get a compile error 😟.
../iroha-cli/interactive/impl/interactive_common_cli.cpp:151:28: error: invalid operands to binary expression ('boost::optional<std::string>' (aka 'optional<basic_string<char, char_traits<char>, allocator<char> > >') and '(lambda at ../iroha-cli/interactive/impl/interactive_common_cli.cpp:151:30)')
promptString(*param) | [&](auto &val) {
~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~
Help please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Do not forget to include "common/types.hpp"
- The operator itself is in the namespace, so you may use
using namespace iroha;
inside the function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was missing using namespace iroha;
. Thanks.
{GET_ROLES, makeParamsDescription({})}, | ||
{GET_AST_INFO, makeParamsDescription({ast_id})}, | ||
{GET_ROLE_PERM, makeParamsDescription({role_id})} | ||
// query_params_map_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please clarify, not sure what you meant here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the line with a comment. But you don't really have to remove it since the PR is about different things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this comment is needed for better formatting
@@ -46,8 +47,11 @@ namespace iroha_cli { | |||
RESULT | |||
}; | |||
|
|||
|
|||
// Parameter prompt and default/cache value pair | |||
using ParamData = std::pair<std::string, std::string>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider declaring struct ParamData
instead
// commonParamsMap | ||
}; | ||
} | ||
|
||
ParamsDescription makeParamsDescription(std::vector<std::string> params) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const std::vector<std::string> &
@l4l, if I use clang-format (e.g. |
@uditha-atukorala we have minor differences with google code style (in terms of clang-format) actually. They are listed in config, so prefer using |
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
// Description of parameters | ||
using ParamsDescription = std::vector<std::string>; | ||
using ParamsDescription = std::vector<std::shared_ptr<ParamData>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a continuation of the previous discussion
boost::optional can handle lvalue refs. Try this diff out: curl http://termbin.com/kzb6 | git apply
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
[](auto &&acc, auto &el) { | ||
acc.push_back(ParamData({el, {}})); | ||
return std::forward<decltype(acc)>(acc); | ||
; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove that ;
plz
// Parameter prompt and default/cache | ||
/** | ||
* Data structure for parameter data | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant line
Signed-off-by: Uditha Atukorala <[email protected]>
SonarQube analysis reported 2 issues
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job, thanks for your contribution
std::string line, std::string command_name, ParamsMap params_map) { | ||
auto params_description = | ||
findInHandlerMap(command_name, std::move(params_map)); | ||
std::string line, std::string command_name, ParamsMap ¶ms_map) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it should be const ParamsMap &
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually not, it is intentional it to be not const ParamsMap &
since it gets updated with user input values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Missed that
@@ -136,7 +170,7 @@ namespace iroha_cli { | |||
* @return vector with needed parameters | |||
*/ | |||
boost::optional<std::vector<std::string>> parseParams( | |||
std::string line, std::string command_name, ParamsMap params_map); | |||
std::string line, std::string command_name, ParamsMap ¶ms_map); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also const ParamsMap &
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, params_map gets updated.
boost::optional<V> findInHandlerMap(K command_name, | ||
std::unordered_map<K, V> params_map) { | ||
boost::optional<V &> findInHandlerMap( | ||
K command_name, std::unordered_map<K, V> ¶ms_map) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, params_map gets updated.
@@ -211,7 +245,7 @@ namespace iroha_cli { | |||
C class_pointer, | |||
std::string &line, | |||
std::unordered_map<std::string, V> &parsers_map, | |||
ParamsMap params_map) { | |||
ParamsMap ¶ms_map) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const ParamsMap &
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, params_map gets updated.
Signed-off-by: Uditha Atukorala <[email protected]>
Signed-off-by: Uditha Atukorala <[email protected]>
Description of the Change
Cache CLI input values to make it more convenient (#827).
There are two use cases.
iroha_cli::interactive::ParamsMap
- If a default value is provided (e.g. default host/port), that will be used when the user input is empty.Benefits
Get rid of annoying repeated host/port entries when attempting to send requests to Iroha (among other inputs) and make the CLI less frustrating to use.
Possible Drawbacks
There's no option to remove a cached value other than replacing it with a new value. However, the CLI doesn't allow empty inputs so this shouldn't be a problem.
Usage Examples or Tests
Scenario 1 - Using default values
There's no user input for
Peer port (50051)
, default is used (prompt displays the default values).Scenario 2 - Using cached values
There aren't any user inputs for
Requested account Id (admin@test)
,Peer address (localhost)
andPeer port (50051)
, cached valued are used. Note the prompt displays the cached values.