11. Web APIでThriftを使うExample
def index = Action {
val indexResponse = new IndexResponse
indexResponse.success = true
val protocol = new TCompactProtocol.Factory()
val serializer = new TSerializer(protocol)
Ok(serializer.serialize(indexResponse))
}
サーバでシリアライズ(Scala)
var www = new WWW("http://localhost/index");
yield return www;
var stream = new MemoryStream(www.bytes);
var tProtocol = new TCompactProtocol(new
TStreamTransport(stream, stream));
var indexResponse = new IndexResponse();
indexResponse.Read(tProtocol);
クライアントでデシリアライズ(C#)
struct IndexResponse {
1: bool success;
}
IDLでの定義
12. ThriftのIDLを使う上で注意する点
• バイナリ上は、番号と型が紐づいている
• そのため、⼀度定義したフィールドの型を変更するとバイナリ
の互換性がなくなってしまう
• 型変更をする場合、新しいフィールドとして定義し直す⽅が良い
• 同様の理由で、すでに消してしまっていても、⼀度使った番号
を再利⽤せずに新しい番号を使うようにする
struct User (
1: i32 id,
2: string name
)
struct User (
1: i64 id, // 互換性がなくなる!
2: string name
)
idの型を変更
struct User (
1: i32 _id, // 互換性のために残す
2: string name,
3: i64 id
)
新しくフィールドを追加
(古いidはそのうち消す)