Thrift IDL、类型绑定、Binary/Compact 和分帧 RPC
///|
test "documented map container" {
let value = @thrift.MapValue(
Some(@thrift.BinaryKind),
Some(@thrift.I32Kind),
[(@thrift.Binary(b"answer"), @thrift.I32(42))],
)
let wire = @thrift.encode(value, @thrift.BinaryProtocol)
assert_eq(
@thrift.decode(wire, @thrift.MapKind, @thrift.BinaryProtocol),
value,
)
}///|
test "documented framed RPC exchange" {
let client = @thrift.Client::new(@thrift.CompactProtocol)
let request = client.call("ping", @thrift.Struct([]))
let frames = @thrift.FrameDecoder::new()
let message = @thrift.decode_message(
frames.feed(request)[0],
@thrift.CompactProtocol,
)
let reply : @thrift.Message = {
name: message.name,
sequence_id: message.sequence_id,
message_type: 2,
body: @thrift.Struct([(0, @thrift.I32(42))]),
}
assert_eq(
client.feed(@thrift.encode_framed_message(reply, @thrift.CompactProtocol)),
[reply],
)
client.finish()
}pub struct Client {
protocol : Protocol
strict_read : Bool
strict_write : Bool
decoder : FrameDecoder
pending : Map[Int, String]
next_sequence : UInt
failed : Bool
}fn Client::call(self : Client, name : String, body : Value, oneway? : Bool) -> Bytes raise CodecErrorfn Client::call_with_id(self : Client, name : String, body : Value, oneway? : Bool, response_name? : String?) -> (Int, Bytes) raise CodecErrorfn Client::new(protocol : Protocol, strict_read? : Bool, strict_write? : Bool) -> Client raise CodecErrorpub struct FrameDecoder {
max_frame : Int
header : UInt64
header_bytes : Int
expected : Int
buffer : Array[Byte]
failed : Bool
}pub(all) enum IdlDefinition {
Alias(String, IdlType, Map[String, String])
Enumeration(String, Array[(String, Int)], Map[String, String])
Record(String, String, Array[IdlField], Map[String, String])
Constant(String, IdlType, IdlConst)
Service(String, String?, Array[IdlMethod], Map[String, String])
} derive(Eq, Debug)pub struct Schema {
// private fields
}fn Schema::get_method(self : Schema, service : String, name : String) -> IdlMethod raise SchemaErrorfn Schema::make_call(self : Schema, service : String, name : String, arguments : Json, sequence_id : Int) -> Message raise SchemaErrorfn Schema::make_reply(self : Schema, service : String, name : String, sequence_id : Int, outcome : RpcOutcome) -> Message raise SchemaErrorfn Schema::read_reply(self : Schema, service : String, name : String, response : Message) -> RpcOutcome raise SchemaErrorfn compile_schema(root : String, sources : Map[String, String], include_paths? : Array[String]) -> Schema raise SchemaErrorfn decode_message(data : Bytes, protocol : Protocol, strict_read? : Bool) -> Message raise CodecErrorfn decode_selected(data : Bytes, fields : Array[(Int, Kind)], protocol : Protocol) -> Value raise CodecErrorfn encode_framed_message(message : Message, protocol : Protocol, strict_write? : Bool) -> Bytes raise CodecErrorfn encode_message(message : Message, protocol : Protocol, strict_write? : Bool) -> Bytes raise CodecErrorInstall
Download zipThrift IDL、类型绑定、Binary/Compact 和分帧 RPC