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

Dan*_*Que 9 thrift

每个参数(字段标识符)之前的数字的目的是什么?它为什么从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";
}
Run Code Online (Sandbox Code Playgroud)

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

我一直试图找到答案,但在文档中没有找到任何内容.

Jen*_*nsG 7

这就是所谓的字段ID,一个32位整数.除了数据本身之外,这是唯一能够让对方正确识别数据所属字段的内容.

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

struct yeolde {
    i32 foo
    string bar
}
Run Code Online (Sandbox Code Playgroud)

但现在你得到一个很好的警告:

$ 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!
Run Code Online (Sandbox Code Playgroud)

它为什么从5跳到16?

人们可能已经决定这是一个好主意.除了数字必须是正32位值之外,没有那么多限制> 0.

或者在此期间已经删除了字段.特别是对于后一种情况,建议对过时的字段进行注释,但将它们保留在IDL中以防止出现兼容性事故,因为有人为了新的目的"重复使用"已经使用过时的旧字段数.


1)这就是为什么不允许负ID号码的原因.