aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/util/grpc_tool.cc
diff options
context:
space:
mode:
authorGravatar Yuchen Zeng <zyc@google.com>2016-08-26 16:23:29 -0700
committerGravatar Yuchen Zeng <zyc@google.com>2016-08-26 16:23:29 -0700
commit94d786fd4ca5e7898648a838ad1d23581077e1a4 (patch)
tree3196457e19b8c6e1cebd170e2fb8d141f5d0a8c6 /test/cpp/util/grpc_tool.cc
parentc931305c398e7e87013da52a5c2697d0fb14ad7f (diff)
Add type command
Diffstat (limited to 'test/cpp/util/grpc_tool.cc')
-rw-r--r--test/cpp/util/grpc_tool.cc35
1 files changed, 31 insertions, 4 deletions
diff --git a/test/cpp/util/grpc_tool.cc b/test/cpp/util/grpc_tool.cc
index f06053ca23..4feb2dcbaa 100644
--- a/test/cpp/util/grpc_tool.cc
+++ b/test/cpp/util/grpc_tool.cc
@@ -75,11 +75,11 @@ class GrpcTool {
GrpcToolOutputCallback callback);
bool CallMethod(int argc, const char** argv, CliCredentials cred,
GrpcToolOutputCallback callback);
+ bool PrintType(int argc, const char** argv, CliCredentials cred,
+ GrpcToolOutputCallback callback);
// TODO(zyc): implement the following methods
// bool ListServices(int argc, const char** argv, GrpcToolOutputCallback
// callback);
- // bool PrintType(int argc, const char** argv, GrpcToolOutputCallback
- // callback);
// bool PrintTypeId(int argc, const char** argv, GrpcToolOutputCallback
// callback);
// bool ParseMessage(int argc, const char** argv, GrpcToolOutputCallback
@@ -168,7 +168,7 @@ const Command ops[] = {
// {"ls", BindWith5Args(&GrpcTool::ListServices), 1, 3},
// {"list", BindWith5Args(&GrpcTool::ListServices), 1, 3},
{"call", BindWith5Args(&GrpcTool::CallMethod), 2, 3},
- // {"type", BindWith5Args(&GrpcTool::PrintType), 2, 2},
+ {"type", BindWith5Args(&GrpcTool::PrintType), 2, 2},
// {"parse", BindWith5Args(&GrpcTool::ParseMessage), 2, 3},
// {"totext", BindWith5Args(&GrpcTool::ToText), 2, 3},
// {"tobinary", BindWith5Args(&GrpcTool::ToBinary), 2, 3},
@@ -180,7 +180,7 @@ void Usage(const grpc::string& msg) {
"%s\n"
// " grpc_cli ls ... ; List services\n"
" grpc_cli call ... ; Call method\n"
- // " grpc_cli type ... ; Print type\n"
+ " grpc_cli type ... ; Print type\n"
// " grpc_cli parse ... ; Parse message\n"
// " grpc_cli totext ... ; Convert binary message to text\n"
// " grpc_cli tobinary ... ; Convert text message to binary\n"
@@ -257,6 +257,33 @@ bool GrpcTool::Help(int argc, const char** argv, const CliCredentials cred,
return true;
}
+bool GrpcTool::PrintType(int argc, const char** argv, CliCredentials cred,
+ GrpcToolOutputCallback callback) {
+ CommandUsage(
+ "Print type\n"
+ " grpc_cli type <address> <type>\n"
+ " <address> ; host:port\n"
+ " <type> ; Protocol buffer type name\n" +
+ cred.GetCredentialUsage());
+
+ grpc::string server_address(argv[0]);
+ std::shared_ptr<grpc::Channel> channel =
+ grpc::CreateChannel(server_address, cred.GetCredentials());
+ grpc::ProtoReflectionDescriptorDatabase desc_db(channel);
+ grpc::protobuf::DescriptorPool desc_pool(&desc_db);
+
+ grpc::string output;
+ const grpc::protobuf::Descriptor* descriptor =
+ desc_pool.FindMessageTypeByName(argv[1]);
+ if (descriptor != nullptr) {
+ output = descriptor->DebugString();
+ } else {
+ fprintf(stderr, "Type %s not found.\n", argv[1]);
+ return false;
+ }
+ return callback(output);
+}
+
bool GrpcTool::CallMethod(int argc, const char** argv,
const CliCredentials cred,
GrpcToolOutputCallback callback) {