Thrift参数编号的目的是什么?

What is the purpose of Thrift parameter numbering?

每个参数(字段标识符)前的数字的用途是什么?为什么从5跳到16?

struct Tweet {
    1: required i32 userId;
    2: required string userName;
    3: required string text;
    4: optional Location loc;
    5: optional TweetType tweetType = TweetType.TWEET;
    16: optional string language = "english";
}

(摘自 http://diwakergupta.github.io/thrift-missing-guide/ 的片段)

一段时间以来我一直在努力寻找这个问题的答案,但在文档中没有找到任何内容。

也就是所谓的字段ID,一个32位的整数。除了数据本身,这是唯一通过网络让对方正确识别数据所属字段的东西。

在早期,系统会自动在内部提供这些 ID,这会导致不兼容:如果有人更改了字段的顺序,在其他人之间添加了一个字段或删除了字段。出于兼容性原因,您仍然可以省略数字并让系统自动分配负 ID1):

struct yeolde {
    i32 foo
    string bar
}

但现在您会收到一个很好的警告:

$ thrift -gen csharp test.thrift
[WARNING:test.thrift:3] No field key specified for foo, resulting protocol may have conflicts or not be backwards compatible!
[WARNING:test.thrift:4] No field key specified for bar, resulting protocol may have conflicts or not be backwards compatible!

Why does it jump from 5 to 16?

有人可能认为这是个好主意。除了数字必须是正的 32 位值 > 0.

之外没有太多限制

或者同时删除了一些字段。特别是对于后一种情况,建议注释过时的字段但将它们保留在 IDL 中以防止兼容性事故,因为有人 "reused" 已经使用和过时的旧字段编号用于新目的。


1)这就是为什么身份证号码不能为负数的原因。