Skip to content

Instantly share code, notes, and snippets.

@vo
Last active July 26, 2018 16:08
Show Gist options
  • Save vo/a2a5b9177d0049513ad2d1f047220543 to your computer and use it in GitHub Desktop.
Save vo/a2a5b9177d0049513ad2d1f047220543 to your computer and use it in GitHub Desktop.
Protobuf serialize/deserialize test
syntax = "proto3";
package request1;
message Request {
Type type = 1;
AmazingCommand amazingCommand = 2;
BeautifulCommand beautifulCommand = 3;
CharmingCommand charmingCommand = 4;
DashingCommand dashingCommand = 5;
enum Type {
AMAZING = 0;
BEAUTIFUL = 1;
CHARMING = 2;
DASHING = 3;
}
message AmazingCommand {
bool amazing = 1;
}
message BeautifulCommand {
string beautiful = 1;
}
message CharmingCommand {
int32 charming = 1;
}
message DashingCommand {
double dashing = 1;
}
}
message Response {
bool success = 1;
}
syntax = "proto3";
package request2;
message Request {
Type type = 1;
AmazingCommand amazingCommand = 2;
BeautifulCommand beautifulCommand = 3;
CharmingCommand charmingCommand = 4;
DashingCommand dashingCommand = 5;
ExcellentCommand excellentCommand = 6;
enum Type {
AMAZING = 0;
BEAUTIFUL = 1;
CHARMING = 2;
DASHING = 3;
EXCELLENT = 4;
}
message AmazingCommand {
bool amazing = 1;
}
message BeautifulCommand {
string beautiful = 1;
}
message CharmingCommand {
int32 charming = 1;
}
message DashingCommand {
double dashing = 1;
}
message ExcellentCommand {
string excellent = 1;
}
}
message Response {
bool success = 1;
}
// Protobuf serialize/deserialize test
// Christopher Vo ([email protected])
#include <iostream>
#include "request1.pb.cc"
#include "request2.pb.cc"
void printRequest1(const request1::Request & request)
{
switch(request.type()) {
case request1::Request_Type_AMAZING:
std::cout << "Parsed AMAZING command via API1" << std::endl;
break;
case request1::Request_Type_BEAUTIFUL:
std::cout << "Parsed BEAUTIFUL command via API1" << std::endl;
break;
case request1::Request_Type_CHARMING:
std::cout << "Parsed CHARMING command via API1" << std::endl;
break;
case request1::Request_Type_DASHING:
std::cout << "Parsed DASHING command via API1" << std::endl;
break;
default:
std::cout << "Parsed UNKNOWN command via API1" << std::endl;
break;
}
}
void printRequest2(const request2::Request & request)
{
switch(request.type()) {
case request2::Request_Type_AMAZING:
std::cout << "Parsed AMAZING command via API2" << std::endl;
break;
case request2::Request_Type_BEAUTIFUL:
std::cout << "Parsed BEAUTIFUL command via API2" << std::endl;
break;
case request2::Request_Type_CHARMING:
std::cout << "Parsed CHARMING command via API2" << std::endl;
break;
case request2::Request_Type_DASHING:
std::cout << "Parsed DASHING command via API2" << std::endl;
break;
case request2::Request_Type_EXCELLENT:
std::cout << "Parsed EXCELLENT command via API2" << std::endl;
break;
default:
std::cout << "Parsed UNKNOWN command via API2" << std::endl;
break;
}
}
int main(int argc, char *argv[])
{
// case 1: send API1, receive API1
{
std::cout << "Case 1: API1 -> API1" << std::endl;
// serialize a request
request1::Request r;
r.set_type(request1::Request::AMAZING);
auto amazing = r.mutable_amazingcommand();
amazing->set_amazing(true);
std::string serialized = r.SerializeAsString();
std::cout << " [Case 1]: Serialize AMAZING command via API1" << std::endl;
// deserialize the request
request1::Request d;
if(d.ParseFromString(serialized)) {
std::cout << " [Case 1]: ";
printRequest1(d);
} else {
std::cerr << " [Case 1]: Could not parse via API1" << std::endl;
}
}
// case 2: send API1, receive API2
{
std::cout << "Case 2: API1 -> API2" << std::endl;
// serialize a request
request1::Request r;
r.set_type(request1::Request::AMAZING);
auto amazing = r.mutable_amazingcommand();
amazing->set_amazing(true);
std::string serialized = r.SerializeAsString();
std::cout << " [Case 2]: Serialize AMAZING command via API1" << std::endl;
// deserialize the request
request2::Request d;
if(d.ParseFromString(serialized)) {
std::cout << " [Case 2]: ";
printRequest2(d);
} else {
std::cerr << " [Case 2]: Could not parse via API2" << std::endl;
}
}
// case 3: send API2, receive API1
{
std::cout << "Case 3: API2 -> API1" << std::endl;
// serialize a request
request2::Request r;
r.set_type(request2::Request::EXCELLENT);
auto excellent = r.mutable_excellentcommand();
excellent->set_excellent("YES, SO EXCELLENT");
std::string serialized = r.SerializeAsString();
std::cout << " [Case 3]: Serialize EXCELLENT command via API2" << std::endl;
// deserialize the request
request1::Request d;
if(d.ParseFromString(serialized)) {
std::cout << " [Case 3]: ";
printRequest1(d);
} else {
std::cerr << " [Case 3]: Could not parse via API1" << std::endl;
}
}
// case 4: send API2, receive API2
{
std::cout << "Case 4: API2 -> API2" << std::endl;
// serialize a request
request2::Request r;
r.set_type(request2::Request::EXCELLENT);
auto excellent = r.mutable_excellentcommand();
excellent->set_excellent("YES, SO EXCELLENT");
std::string serialized = r.SerializeAsString();
std::cout << " [Case 4]: Serialize EXCELLENT command via API2" << std::endl;
// deserialize the request
request2::Request d;
if(d.ParseFromString(serialized)) {
std::cout << " [Case 4]: ";
printRequest2(d);
} else {
std::cerr << " [Case 4]: Could not parse via API2" << std::endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment