在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:thrift-iterator/go开源软件地址:https://github.com/thrift-iterator/go开源编程语言:Go 100.0%开源软件介绍:thrifterdecode/encode thrift message without IDL Why?
works like encoding/json
import "github.com/thrift-iterator/go"
// marshal to thrift
thriftEncodedBytes, err := thrifter.Marshal([]int{1, 2, 3})
// unmarshal back
var val []int
err = thrifter.Unmarshal(thriftEncodedBytes, &val) even struct data binding is supported import "github.com/thrift-iterator/go"
type NewOrderRequest struct {
Lines []NewOrderLine `thrift:",1"`
}
type NewOrderLine struct {
ProductId string `thrift:",1"`
Quantity int `thrift:",2"`
}
// marshal to thrift
thriftEncodedBytes, err := thrifter.Marshal(NewOrderRequest{
Lines: []NewOrderLine{
{"apple", 1},
{"orange", 2},
}
})
// unmarshal back
var val NewOrderRequest
err = thrifter.Unmarshal(thriftEncodedBytes, &val) without IDLyou do not need to define IDL. you do not need to use static code generation. you do not event need to define struct. import "github.com/thrift-iterator/go"
import "github.com/thrift-iterator/go/general"
var msg general.Message
err := thrifter.Unmarshal(thriftEncodedBytes, &msg)
// the RPC call method name, type is string
fmt.Println(msg.MessageName)
// the RPC call arguments, type is general.Struct
fmt.Println(msg.Arguments) what is type FieldId int16
type Struct map[FieldId]interface{} we can extract out specific argument from deeply nested arguments using one line productId := msg.MessageArgs.Get(
protocol.FieldId(1), // lines of request
0, // the first line
protocol.FieldId(1), // product id
).(string) You can unmarshal any thrift bytes into general objects. And you can marshal them back. Partial decodingfully decoding into a go struct consumes substantial resources.
thrifter provide option to do partial decoding. You can modify part of the
message, with untouched parts in import "github.com/thrift-iterator/go"
import "github.com/thrift-iterator/go/protocol"
import "github.com/thrift-iterator/go/raw"
// partial decoding
decoder := thrifter.NewDecoder(reader)
var msgHeader protocol.MessageHeader
decoder.Decode(&msgHeader)
var msgArgs raw.Struct
decoder.Decode(&msgArgs)
// modify...
// encode back
encoder := thrifter.NewEncoder(writer)
encoder.Encode(msgHeader)
encoder.Encode(msgArgs) the definition of type StructField struct {
Buffer []byte
Type protocol.TType
}
type Struct map[protocol.FieldId]StructField Performancethrifter does not compromise performance. gogoprotobuf
thrift
thrifter by static codegen
thrifter by reflection
You can see the reflection implementation is not bad, much faster than the static code generated by thrift original implementation. To have best performance, you can choose to use static code generation. The api is unchanged, just need to add extra static codegen in your build steps, and include the generated code in your package. The runtime will automatically use the generated encoder/decoder instead of reflection. For example of static codegen, checkout https://github.com/thrift-iterator/go/blob/master/test/api/init.go Sync IDL and Go StructKeep IDL and your object model is challenging. We do not always like the code generated from thrift IDL. But manually keeping the IDL and model in sync is tedious and error prone. A separate toolchain to manipulate thrift IDL file, and keeping them bidirectionally in sync will be provided in another project. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论