Reformatted code as per clang-format

pull/16766/head
Feso 7 years ago
parent 29c131f790
commit 00bb2a035e
  1. 7
      test/cpp/util/grpc_tool.cc
  2. 80
      test/cpp/util/grpc_tool_test.cc
  3. 17
      test/cpp/util/proto_file_parser.cc
  4. 38
      test/cpp/util/proto_file_parser.h

@ -679,10 +679,9 @@ bool GrpcTool::CallMethod(int argc, const char** argv,
break;
}
} else {
grpc::string response_text = parser->GetFormattedStringFromMethod(method_name,
serialized_response_proto,
false /* is_request */,
FLAGS_json_output);
grpc::string response_text = parser->GetFormattedStringFromMethod(
method_name, serialized_response_proto,
false /* is_request */, FLAGS_json_output);
if (parser->HasError() && print_mode) {
fprintf(stderr, "Failed to parse response.\n");

@ -75,19 +75,19 @@ using grpc::testing::EchoResponse;
"{}\n"
#define ECHO_RESPONSE_MESSAGE_TEXT_FORMAT \
"message: \"echo\"\n" \
"param {\n" \
" host: \"localhost\"\n" \
" peer: \"peer\"\n" \
"message: \"echo\"\n" \
"param {\n" \
" host: \"localhost\"\n" \
" peer: \"peer\"\n" \
"}\n\n"
#define ECHO_RESPONSE_MESSAGE_JSON_FORMAT \
"{\n" \
" \"message\": \"echo\",\n" \
" \"param\": {\n" \
" \"host\": \"localhost\",\n" \
" \"peer\": \"peer\"\n" \
" }\n" \
"{\n" \
" \"message\": \"echo\",\n" \
" \"param\": {\n" \
" \"host\": \"localhost\",\n" \
" \"peer\": \"peer\"\n" \
" }\n" \
"}\n\n"
DECLARE_string(channel_creds_type);
@ -452,8 +452,8 @@ TEST_F(GrpcToolTest, CallCommand) {
// {
// "message": "Hello"
// }
EXPECT_TRUE(nullptr !=
strstr(output_stream.str().c_str(), "{\n \"message\": \"Hello\"\n}"));
EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
"{\n \"message\": \"Hello\"\n}"));
ShutdownServer();
}
@ -489,8 +489,8 @@ TEST_F(GrpcToolTest, CallCommandJsonInput) {
// {
// "message": "Hello"
// }
EXPECT_TRUE(nullptr !=
strstr(output_stream.str().c_str(), "{\n \"message\": \"Hello\"\n}"));
EXPECT_TRUE(nullptr != strstr(output_stream.str().c_str(),
"{\n \"message\": \"Hello\"\n}"));
ShutdownServer();
}
@ -527,11 +527,11 @@ TEST_F(GrpcToolTest, CallCommandBatch) {
std::cin.rdbuf(ss.rdbuf());
FLAGS_batch = true;
FLAGS_json_output= true;
FLAGS_json_output = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
FLAGS_json_output= false;
FLAGS_json_output = false;
FLAGS_batch = false;
// Expected output:
@ -565,7 +565,8 @@ TEST_F(GrpcToolTest, CallCommandBatchJsonInput) {
// Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
std::streambuf* orig = std::cin.rdbuf();
std::istringstream ss("{\"message\": \"Hello1\"}\n\n{\"message\": \"Hello2\" }\n\n");
std::istringstream ss(
"{\"message\": \"Hello1\"}\n\n{\"message\": \"Hello2\" }\n\n");
std::cin.rdbuf(ss.rdbuf());
FLAGS_json_input = true;
@ -588,11 +589,11 @@ TEST_F(GrpcToolTest, CallCommandBatchJsonInput) {
std::cin.rdbuf(ss.rdbuf());
FLAGS_batch = true;
FLAGS_json_output= true;
FLAGS_json_output = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
FLAGS_json_output= false;
FLAGS_json_output = false;
FLAGS_batch = false;
FLAGS_json_input = false;
@ -613,7 +614,6 @@ TEST_F(GrpcToolTest, CallCommandBatchJsonInput) {
"{\n \"message\": \"Hello1\"\n}\n"
"{\n \"message\": \"Hello2\"\n}\n"));
std::cin.rdbuf(orig);
ShutdownServer();
}
@ -649,11 +649,11 @@ TEST_F(GrpcToolTest, CallCommandBatchWithBadRequest) {
std::cin.rdbuf(ss.rdbuf());
FLAGS_batch = true;
FLAGS_json_output= true;
FLAGS_json_output = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
FLAGS_json_output= false;
FLAGS_json_output = false;
FLAGS_batch = false;
// Expected output:
@ -683,7 +683,8 @@ TEST_F(GrpcToolTest, CallCommandBatchJsonInputWithBadRequest) {
// Mock std::cin input "message: 1\n\n message: 'Hello2'\n\n"
std::streambuf* orig = std::cin.rdbuf();
std::istringstream ss("{ \"message\": 1 }\n\n { \"message\": \"Hello2\" }\n\n");
std::istringstream ss(
"{ \"message\": 1 }\n\n { \"message\": \"Hello2\" }\n\n");
std::cin.rdbuf(ss.rdbuf());
FLAGS_batch = true;
@ -707,11 +708,11 @@ TEST_F(GrpcToolTest, CallCommandBatchJsonInputWithBadRequest) {
FLAGS_batch = true;
FLAGS_json_input = true;
FLAGS_json_output= true;
FLAGS_json_output = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
FLAGS_json_output= false;
FLAGS_json_output = false;
FLAGS_json_input = false;
FLAGS_batch = false;
@ -768,7 +769,8 @@ TEST_F(GrpcToolTest, CallCommandRequestStreamJsonInput) {
// Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
std::streambuf* orig = std::cin.rdbuf();
std::istringstream ss("{ \"message\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
std::istringstream ss(
"{ \"message\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
std::cin.rdbuf(ss.rdbuf());
FLAGS_json_input = true;
@ -820,7 +822,8 @@ TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequestJsonInput) {
// Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"
std::streambuf* orig = std::cin.rdbuf();
std::istringstream ss("{ \"bad_field\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
std::istringstream ss(
"{ \"bad_field\": \"Hello1\" }\n\n{ \"message\": \"Hello2\" }\n\n");
std::cin.rdbuf(ss.rdbuf());
FLAGS_json_input = true;
@ -861,7 +864,7 @@ TEST_F(GrpcToolTest, CallCommandResponseStream) {
output_stream.str(grpc::string());
output_stream.clear();
FLAGS_json_output= true;
FLAGS_json_output = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
@ -938,7 +941,8 @@ TEST_F(GrpcToolTest, ParseCommand) {
const grpc::string server_address = SetUpServer();
const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
"grpc.testing.EchoResponse", ECHO_RESPONSE_MESSAGE_TEXT_FORMAT};
"grpc.testing.EchoResponse",
ECHO_RESPONSE_MESSAGE_TEXT_FORMAT};
FLAGS_binary_input = false;
FLAGS_binary_output = false;
@ -946,7 +950,8 @@ TEST_F(GrpcToolTest, ParseCommand) {
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
// Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
// with json_output
output_stream.str(grpc::string());
@ -959,7 +964,8 @@ TEST_F(GrpcToolTest, ParseCommand) {
FLAGS_json_output = false;
// Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
// Parse text message to binary message and then parse it back to text message
output_stream.str(grpc::string());
@ -979,7 +985,8 @@ TEST_F(GrpcToolTest, ParseCommand) {
std::placeholders::_1)));
// Expected output: ECHO_RESPONSE_MESSAGE
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
FLAGS_binary_input = false;
FLAGS_binary_output = false;
@ -994,7 +1001,8 @@ TEST_F(GrpcToolTest, ParseCommandJsonFormat) {
const grpc::string server_address = SetUpServer();
const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
"grpc.testing.EchoResponse", ECHO_RESPONSE_MESSAGE_JSON_FORMAT};
"grpc.testing.EchoResponse",
ECHO_RESPONSE_MESSAGE_JSON_FORMAT};
FLAGS_json_input = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
@ -1002,7 +1010,8 @@ TEST_F(GrpcToolTest, ParseCommandJsonFormat) {
std::placeholders::_1)));
// Expected output: ECHO_RESPONSE_MESSAGE_TEXT_FORMAT
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
ECHO_RESPONSE_MESSAGE_TEXT_FORMAT));
// with json_output
output_stream.str(grpc::string());
@ -1016,7 +1025,8 @@ TEST_F(GrpcToolTest, ParseCommandJsonFormat) {
FLAGS_json_input = false;
// Expected output: ECHO_RESPONSE_MESSAGE_JSON_FORMAT
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(),
ECHO_RESPONSE_MESSAGE_JSON_FORMAT));
ShutdownServer();
}

@ -236,12 +236,12 @@ grpc::string ProtoFileParser::GetFormattedStringFromMethod(
if (has_error_) {
return "";
}
return GetFormattedStringFromMessageType(message_type_name, serialized_proto, is_json_format);
return GetFormattedStringFromMessageType(message_type_name, serialized_proto,
is_json_format);
}
grpc::string ProtoFileParser::GetSerializedProtoFromMessageType(
const grpc::string& message_type_name,
const grpc::string& formatted_proto,
const grpc::string& message_type_name, const grpc::string& formatted_proto,
bool is_json_format) {
has_error_ = false;
grpc::string serialized;
@ -257,7 +257,7 @@ grpc::string ProtoFileParser::GetSerializedProtoFromMessageType(
bool ok;
if (is_json_format) {
ok = grpc::protobuf::json::JsonStringToMessage(formatted_proto, msg.get())
.ok();
.ok();
if (!ok) {
LogError("Failed to convert json format to proto.");
return "";
@ -279,8 +279,7 @@ grpc::string ProtoFileParser::GetSerializedProtoFromMessageType(
}
grpc::string ProtoFileParser::GetFormattedStringFromMessageType(
const grpc::string& message_type_name,
const grpc::string& serialized_proto,
const grpc::string& message_type_name, const grpc::string& serialized_proto,
bool is_json_format) {
has_error_ = false;
const protobuf::Descriptor* desc =
@ -300,9 +299,9 @@ grpc::string ProtoFileParser::GetFormattedStringFromMessageType(
if (is_json_format) {
grpc::protobuf::json::JsonPrintOptions jsonPrintOptions;
jsonPrintOptions.add_whitespace = true;
if (!grpc::protobuf::json::MessageToJsonString(*msg.get(),
&formatted_string,
jsonPrintOptions).ok()) {
if (!grpc::protobuf::json::MessageToJsonString(
*msg.get(), &formatted_string, jsonPrintOptions)
.ok()) {
LogError("Failed to print proto message to json format");
return "";
}

@ -55,16 +55,19 @@ class ProtoFileParser {
/// Converts a text or json string to its binary proto representation for the
/// given method's input or return type.
/// \param method the name of the method (does not need to be fully qualified name)
/// \param method the name of the method (does not need to be fully qualified
/// name)
/// \param formatted_proto the text- or json-formatted proto string
/// \param is_request if \c true the resolved type is that of the input parameter of
/// the method, otherwise it is the output type
/// \param is_request if \c true the resolved type is that of the input
/// parameter of the method, otherwise it is the output type
/// \param is_json_format if \c true the \c formatted_proto is treated as a
/// json-formatted proto, otherwise it is treated as a text-formatted proto
/// json-formatted proto, otherwise it is treated as a text-formatted
/// proto
/// \return the serialised binary proto represenation of \c formatted_proto
grpc::string GetSerializedProtoFromMethod(
const grpc::string& method, const grpc::string& formatted_proto,
bool is_request, bool is_json_format);
grpc::string GetSerializedProtoFromMethod(const grpc::string& method,
const grpc::string& formatted_proto,
bool is_request,
bool is_json_format);
/// Converts a text or json string to its proto representation for the given
/// message type.
@ -72,26 +75,27 @@ class ProtoFileParser {
/// \return the serialised binary proto represenation of \c formatted_proto
grpc::string GetSerializedProtoFromMessageType(
const grpc::string& message_type_name,
const grpc::string& formatted_proto,
bool is_json_format);
const grpc::string& formatted_proto, bool is_json_format);
/// Converts a binary proto string to its text or json string representation
/// for the given method's input or return type.
/// \param method the name of the method (does not need to be a fully qualified name)
/// \param the serialised binary proto representation of type \c message_type_name
/// \param method the name of the method (does not need to be a fully
/// qualified name)
/// \param the serialised binary proto representation of type
/// \c message_type_name
/// \return the text- or json-formatted proto string of \c serialized_proto
grpc::string GetFormattedStringFromMethod(const grpc::string& method,
const grpc::string& serialized_proto,
bool is_request, bool is_json_format);
grpc::string GetFormattedStringFromMethod(
const grpc::string& method, const grpc::string& serialized_proto,
bool is_request, bool is_json_format);
/// Converts a binary proto string to its text or json string representation
/// for the given message type.
/// \param the serialised binary proto representation of type \c message_type_name
/// \param the serialised binary proto representation of type
/// \c message_type_name
/// \return the text- or json-formatted proto string of \c serialized_proto
grpc::string GetFormattedStringFromMessageType(
const grpc::string& message_type_name,
const grpc::string& serialized_proto,
bool is_json_format);
const grpc::string& serialized_proto, bool is_json_format);
bool IsStreaming(const grpc::string& method, bool is_request);

Loading…
Cancel
Save