解析文本格式的 protobuf 消息时如何忽略错误的字段

How to ignore wrong fields when parsing a text-format protobuf message

我用c++模拟了一个文本格式的文件解析错误的字段。

我的简单测试.proto文件:

$ cat settings.proto
package settings;
message Settings {
   optional int32  param1 = 1;
   optional string param2 = 2;
   optional bytes  param3 = 3;
}

我的文本格式文件:

$ cat settings.txt
param1: 123
param: "some string"
param3: "another string"

我正在用 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;
}

我将解析器的 AllowPartialMessage 设置为 true。所有字段都是可选的。 但目前 Parse 在第一个错误字段后停止解析。解析后 "settings" 仅包含一个第一个字段。

有没有办法通知失败并继续解析另一个正确的字段?

text-format 解析器不允许未知字段。 Text-format 用于与人类交流,而人类会出现拼写错误。重要的是要发现这些拼写错误,而不是默默地忽略它们。

通常,忽略未知字段的原因是 forwards-compatibility:这样您的程序就可以(部分)理解针对具有新字段的协议的未来版本编写的消息。我经常看到两个特定的用例:

  • 以文本格式进行 machine-to-machine 通信的系统。我建议不要这样做。相反,使用二进制格式,或者如果您真的希望 machine-to-machine 通信是文本的,请使用 JSON.

  • 人类编写 text-format 配置文件然后将其分发到生产中的 possibly-old 服务器的系统。在这种情况下,我建议在人类桌面上使用工具 运行 "pre-compiling" 将 text-format protobuf 转换为二进制,然后只发送 binary 消息到生产服务器。本地工具可以很容易地保留 up-to-date 并且能够在人类用户拼错字段名称时告诉他们。