I am modeling parsing a text format with an invalid field in C ++.
My simple test .proto file:
$ cat settings.proto package settings; message Settings { optional int32 param1 = 1; optional string param2 = 2; optional bytes param3 = 3; }
My text file is:
$ cat settings.txt param1: 123 param: "some string" param3: "another string"
I am parsing a file using google :: protobuf :: TextFormat :: Parser:
#include <iostream> #include <fcntl.h> #include <unistd.h> #include <fstream> #include <google/protobuf/text_format.h> #include <google/protobuf/io/zero_copy_stream_impl.h> #include <settings.pb.h> using namespace std; int main( int argc, char* argv[] ) { GOOGLE_PROTOBUF_VERIFY_VERSION; settings::Settings settings; int fd = open( argv[1], O_RDONLY ); if( fd < 0 ) { cerr << " Error opening the file " << endl; return false; } google::protobuf::io::finputStream finput( fd ); finput.SetCloseOnDelete( true ); google::protobuf::TextFormat::Parser parser; parser.AllowPartialMessage( true ); if ( !parser.Parce( &finput, &settings ) ) { cerr << "Failed to parse file!" << endl; } cout << settings.DebugString() << endl; google::protobuf::ShutdownProtobufLibrary(); std::cout << "Exit" << std::endl; return true; }
I set AllowPartialMessage to true for the parser. All fields are optional. But Parse is currently stopping parsing after the first invalid field. And after parsing the "settings" it contains only one first field.
Is there a way to report an error and continue parsing other valid fields?
source share