【问题标题】:Changing protobuff optional field to oneof将 protobuf 可选字段更改为 oneof
【发布时间】:2020-02-03 15:16:07
【问题描述】:

我收到以下消息:

message Message {
    int64 id = 1;
    google.protobuf.FloatValue weight = 2;
    google.protobuf.FloatValue override_weight = 3;
}

我希望将重量和 override_weight(可选字段)的类型更改为 google.protobuf.DoubleValue,所以我所做的是以下内容:

message Message {
    int64 id = 1;
    oneof weight_oneof {
        google.protobuf.FloatValue weight = 2 [deprecated=true];
        google.protobuf.DoubleValue double_weight = 4;
    }
    oneof override_weight_oneof {
        google.protobuf.FloatValue override_weight = 3 [deprecated=true];
        google.protobuf.DoubleValue double_override_weight = 5;
    }
}

我的问题是,假设我有旧消息是由以前的 protobuff 消息编译器为旧消息编译的,我能否将它们解析为新消息? 文档对此非常模糊:

“将可选字段移入或移出 oneof:在消息被序列化和解析后,您可能会丢失一些信息(某些字段将被清除)。但是,您可以安全地将单个字段移入新的 oneof 和如果知道只设置了一个字段,则可以移动多个字段。”

以前有人试过吗?这种情况的最佳做法是什么?

【问题讨论】:

    标签: python-3.x protocol-buffers proto


    【解决方案1】:

    据我所知,oneof 中的字段只是使用它们的标签号进行序列化。序列化的数据不指示字段是否是 oneof 的一部分。这一切都由序列化器和反序列化器处理。因此,只要标签号不冲突,就可以假设它将在两个方向上工作,旧消息到新序列化程序和新消息到旧序列化程序。

    您可以使用 online protobuf deserializer 对此进行测试。

    验证: 该代码确实产生了相同的字节串。您将在下面找到我使用的消息定义和 python 代码。 python代码会输出一个字节串,你可以在Marc Gravell的解码器中复制使用。

    syntax = "proto3";
    
    message MessageA {
        int64 id = 1;
        float weight = 2;
        float override_weight = 3;
    }
    
    message MessageB {
        int64 id = 1;
        oneof weight_oneof {
            float weight = 2 [deprecated=true];
            double double_weight = 4;
        }
        oneof override_weight_oneof {
            float override_weight = 3 [deprecated=true];
            double double_override_weight = 5;
        }
    }
    
    import Example_pb2
    
    # Set some data in the original message
    msgA = Example_pb2.MessageA()
    msgA.id = 1234
    msgA.weight = 3.21
    msgA.override_weight = 5.43
    
    # Output the serialized bytes in a pretty format
    str = 'msgA   = '
    for x in msgA.SerializeToString():
        str += "{:02x} ".format(x)
    print(str)
    
    # Next set the original fields in the new message
    msgB = Example_pb2.MessageB()
    msgB.id = 1234
    msgB.weight = 3.21
    msgB.override_weight = 5.43
    
    # Output the serialized bytes in a pretty format
    str = 'msgB 1 = '
    for x in msgB.SerializeToString():
        str += "{:02x} ".format(x)
    print(str)
    
    # And finally set the new fields in msgB
    msgB.double_weight = 3.21
    msgB.double_override_weight = 5.43
    
    # Output the serialized bytes in a pretty format
    str = 'msgB 2 = '
    for x in msgB.SerializeToString():
        str += "{:02x} ".format(x)
    print(str)
    

    python 脚本的输出是:

    msgA   = 08 d2 09 15 a4 70 4d 40 1d 8f c2 ad 40
    msgB 1 = 08 d2 09 15 a4 70 4d 40 1d 8f c2 ad 40
    msgB 2 = 08 d2 09 21 ae 47 e1 7a 14 ae 09 40 29 b8 1e 85 eb 51 b8 15 40
    

    如您所见,在设置原始字段时,消息 A 和消息 B 产生相同的字节字符串。只有当您设置新字段时,您才会得到不同的字符串。

    【讨论】:

    • +1 不适用于第一段:尽管我认为您是对的,但我无法检查它;相反,+1 链接到在线 protobuf 反序列化器!我从没想过要寻找这个有用的工具,并且因为没有想到它而自责,因为我过去本可以使用它! (链接上还有其他工具。)(实际上,我可以按照您自己的建议使用在线 protobuf 反序列化程序检查您的声明,但现在没有时间......也许其他人会报告?)
    • @davidbak 关于薄弱的第一段的好点。我已经添加了一些带有证明的代码。
    • 我会给你一个 +1 的改进,但我不能为相同的答案投票两次,对不起!
    • @davidbak 没问题我会把它添加到我的私人荣誉帐户;-)
    猜你喜欢
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多