thrift_dyn

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2022 License: MIT Imports: 10 Imported by: 0

README

go-thrift-dyn

Go Reference

Inspect and rebuild Apache Thrift wire data

Example
// testRpcMethod_args
var args RPCStruct
// testRpcMethod_results
var results RPCStruct
client := thrift.NewTStandardClient(iprot, oprot)

var meta thrift.ResponseMeta
var err error
// oneway
meta, err = client.Call(ctx, "testRpcMethod", &args, nil)
// duplex
meta, err = client.Call(ctx, "testRpcMethod", &args, &results)
_ = results.Fields
Benchmark

Benchmark write of simple message:

struct Model {
    1: string abc; 4: i64 sd; 9: double f64 // "hello", 0xcafe, 0.0
    10: optional list<i64> listI64    // {1, 2, 3}
    11: optional map<i64, i64> mapI64 // {1: 2}
    12: optional map<i32, i32> mapI32 // empty
}
TBinary:
BenchmarkModelRebuildOriginal-12     1971684	       611.0 ns/op
BenchmarkModelRebuildOriginal-12     1982988	       598.5 ns/op
BenchmarkModelRebuildOriginal-12     1901395	       597.0 ns/op
BenchmarkModelRebuildDyn-12    	     1000000	      1040 ns/op
BenchmarkModelRebuildDyn-12    	     1000000	      1063 ns/op
BenchmarkModelRebuildDyn-12    	     1000000	      1025 ns/op

TCompact:
BenchmarkModelRebuildOriginal-12     1681024	       748.5 ns/op
BenchmarkModelRebuildOriginal-12     1709762	       690.0 ns/op
BenchmarkModelRebuildOriginal-12     1666425	       769.9 ns/op
BenchmarkModelRebuildDyn-12          1124538	      1177 ns/op
BenchmarkModelRebuildDyn-12          1000000	      1091 ns/op
BenchmarkModelRebuildDyn-12          1063579	      1171 ns/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidContainerItem = errors.New("invalid container type item")
)
View Source
var (
	ErrSkipField = errors.New("skip field")
)
View Source
var (
	TTypeToReflectType [_BT_SIZE]reflect.Type
)

Functions

func ProtocolFactory

func ProtocolFactory(ptype ProtocolType, conf *thrift.TConfiguration) thrift.TProtocolFactory

func ReadData added in v0.2.0

func ReadData[T any](ctx context.Context, t TData[T]) (err error)

func ReadDataGeneric added in v0.2.0

func ReadDataGeneric(ctx context.Context, t TDataSpec, value *any) (err error)

func String2bs added in v0.2.0

func String2bs(s string) (r []byte)

func WriteData added in v0.2.0

func WriteData[T any](ctx context.Context, t TData[T]) (err error)

func WriteDataGeneric added in v0.2.0

func WriteDataGeneric(ctx context.Context, t TDataSpec, value any) (err error)

Types

type Container added in v0.2.0

type Container interface {
}

type Decoder added in v0.2.0

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder added in v0.2.0

func NewDecoder(pf thrift.TProtocolFactory) *Decoder

func (*Decoder) Decode added in v0.2.0

func (dec *Decoder) Decode(src []byte, valueDst any) (err error)

func (*Decoder) Init added in v0.2.0

func (dec *Decoder) Init(pf thrift.TProtocolFactory) *Decoder

func (*Decoder) ReadFrom added in v0.2.0

func (dec *Decoder) ReadFrom(reader io.Reader, valueDst any) (err error)

type Encoder added in v0.2.0

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder added in v0.2.0

func NewEncoder(pf thrift.TProtocolFactory) *Encoder

func (*Encoder) Encode added in v0.2.0

func (enc *Encoder) Encode(value any) (bb []byte, err error)

func (*Encoder) EncodeTo added in v0.2.0

func (enc *Encoder) EncodeTo(dst []byte, value any) (n int, err error)

func (*Encoder) Init added in v0.2.0

func (enc *Encoder) Init(pf thrift.TProtocolFactory) *Encoder

func (*Encoder) WriteTo added in v0.2.0

func (enc *Encoder) WriteTo(writer io.Writer, value any) (n int64, err error)

type ProtocolType

type ProtocolType = string
const (
	ProtocolType_Compact    ProtocolType = "tcompact"
	ProtocolType_Binary     ProtocolType = "tbinary"
	ProtocolType_SimpleJSON ProtocolType = "tsimplejson"
	ProtocolType_JSON       ProtocolType = "tjson"
)

type RPCStruct

type RPCStruct struct {
	Name   string
	Fields []*TField
}

func (*RPCStruct) AddField

func (s *RPCStruct) AddField(fs ...*TField) *RPCStruct

func (*RPCStruct) Read

func (s *RPCStruct) Read(ctx context.Context, p thrift.TProtocol) (err error)

Read reads fields from wire

func (*RPCStruct) Write

func (s *RPCStruct) Write(ctx context.Context, p thrift.TProtocol) (err error)

Write writes fields to the wire

type Sliceable added in v0.2.0

type Sliceable interface {
	bool | int8 | int16 | int32 | int64 |
		float64 | string | *RPCStruct | Container
}

type TData added in v0.2.0

type TData[T any] struct {
	TDataSpec
	Value *T
}

type TDataSpec added in v0.2.0

type TDataSpec struct {
	Type     thrift.TType
	Required bool
	Protocol thrift.TProtocol
}

type TField

type TField struct {
	ID       TFieldID     // i16
	Type     thrift.TType // i8
	Required bool         // i8
	Name     string

	Value any
}

func NewTField

func NewTField(id TFieldID, type_ thrift.TType, name string, required bool) *TField

NewTField create new TField.

func (TField) GetID

func (f TField) GetID() TFieldID

GetID get field ID.

func (*TField) GetValue

func (f *TField) GetValue() any

GetValue get field value.

func (*TField) Read

func (f *TField) Read(ctx context.Context, p thrift.TProtocol) (err error)

Read read struct field with its value.

func (*TField) SetValue

func (f *TField) SetValue(value any) *TField

SetValue set value of field, or value of KV container (map)

func (*TField) Write

func (f *TField) Write(ctx context.Context, p thrift.TProtocol) (err error)

Write write struct field with its value.

func (*TField) WriteData

func (f *TField) WriteData(ctx context.Context, p thrift.TProtocol) (err error)

WriteData write data to wire, without writing struct field.

type TFieldID

type TFieldID = int16

type TFieldImplementerx

type TFieldImplementerx interface {
	GetID() TFieldID
	GetValue() any

	WriteDataThunk(ctx context.Context, p thrift.TProtocol) (doFunc func() error)
	Write(ctx context.Context, p thrift.TProtocol) (err error)
	Read(ctx context.Context, p thrift.TProtocol) (err error)
}

type TypeContainer

type TypeContainer struct {
	Type  thrift.TType
	Desc  TypeContainerDesc
	Size  int
	Value reflect.Value
}

func (*TypeContainer) GetSize

func (t *TypeContainer) GetSize() int

func (*TypeContainer) GetValue

func (t *TypeContainer) GetValue() any

func (*TypeContainer) Init added in v0.2.0

func (t *TypeContainer) Init() *TypeContainer

func (*TypeContainer) SetSize

func (t *TypeContainer) SetSize(sz int)

type TypeContainerDesc

type TypeContainerDesc struct {
	Key   thrift.TType
	Value thrift.TType
}

type TypeContainerImplementer

type TypeContainerImplementer interface {
	SetSize(v int)
	GetSize() int

	Read(ctx context.Context, p thrift.TProtocol) (err error)
	Write(ctx context.Context, p thrift.TProtocol) (err error)
}

func NewTypeContainerListOfTType added in v0.2.0

func NewTypeContainerListOfTType(desc TypeContainerDesc, required bool) (TypeContainerImplementer, error)

func NewTypeContainerMapOfTType added in v0.2.0

func NewTypeContainerMapOfTType(desc TypeContainerDesc, required bool) (TypeContainerImplementer, error)

func NewTypeContainerMapUnorderedOfTType added in v0.2.1

func NewTypeContainerMapUnorderedOfTType(desc TypeContainerDesc, required bool) (TypeContainerImplementer, error)

func NewTypeContainerSetOfTType added in v0.2.0

func NewTypeContainerSetOfTType(desc TypeContainerDesc, required bool) (TypeContainerImplementer, error)

type TypeContainerList

type TypeContainerList[T Sliceable] struct {
	Required bool
	Desc     TypeContainerDesc
	Size     int
	Value    []T
}

func NewTypeContainerList

func NewTypeContainerList[T Sliceable](desc TypeContainerDesc, required bool) *TypeContainerList[T]

func (*TypeContainerList[T]) Add added in v0.2.0

func (t *TypeContainerList[T]) Add(vs ...T)

func (*TypeContainerList[T]) GetSize added in v0.2.0

func (t *TypeContainerList[T]) GetSize() int

func (*TypeContainerList[T]) Read

func (t *TypeContainerList[T]) Read(ctx context.Context, p thrift.TProtocol) (err error)

func (*TypeContainerList[T]) SetSize added in v0.2.0

func (t *TypeContainerList[T]) SetSize(v int)

func (*TypeContainerList[T]) Write

func (t *TypeContainerList[T]) Write(ctx context.Context, p thrift.TProtocol) (err error)

type TypeContainerMap

type TypeContainerMap[K comparable, V any] struct {
	Required bool
	Desc     TypeContainerDesc
	Size     int
	Value    []TypeContainerMapItem[K, V]
}

func NewTypeContainerMap

func NewTypeContainerMap[K comparable, V any](desc TypeContainerDesc, required bool) *TypeContainerMap[K, V]

func (*TypeContainerMap[K, V]) Add added in v0.2.0

func (t *TypeContainerMap[K, V]) Add(vs ...TypeContainerMapItem[K, V])

func (*TypeContainerMap[K, V]) AddKV added in v0.2.0

func (t *TypeContainerMap[K, V]) AddKV(key K, value V)

func (*TypeContainerMap[K, V]) FromMap added in v0.2.0

func (t *TypeContainerMap[K, V]) FromMap(m map[K]V)

FromMap *copy* map to value

func (*TypeContainerMap[K, V]) FromMapOrdered added in v0.2.0

func (t *TypeContainerMap[K, V]) FromMapOrdered(m map[K]V)

FromMapOrdered *copy* map to value

func (*TypeContainerMap[K, V]) GetSize added in v0.2.0

func (t *TypeContainerMap[K, V]) GetSize() int

func (*TypeContainerMap[K, V]) Read

func (t *TypeContainerMap[K, V]) Read(ctx context.Context, p thrift.TProtocol) (err error)

func (*TypeContainerMap[K, V]) SetSize added in v0.2.0

func (t *TypeContainerMap[K, V]) SetSize(v int)

func (*TypeContainerMap[K, V]) ToMap added in v0.2.0

func (t *TypeContainerMap[K, V]) ToMap() map[K]V

func (*TypeContainerMap[K, V]) ToMapPtr added in v0.2.0

func (t *TypeContainerMap[K, V]) ToMapPtr() map[K]*V

func (*TypeContainerMap[K, V]) Write

func (t *TypeContainerMap[K, V]) Write(ctx context.Context, p thrift.TProtocol) (err error)

type TypeContainerMapItem added in v0.2.0

type TypeContainerMapItem[K comparable, V any] struct {
	Key   K
	Value V
}

type TypeContainerMapUnordered added in v0.2.1

type TypeContainerMapUnordered[K comparable, V any] struct {
	Required bool
	Desc     TypeContainerDesc
	Size     int
	Value    map[K]V
}

func NewTypeContainerMapUnordered added in v0.2.1

func NewTypeContainerMapUnordered[K comparable, V any](desc TypeContainerDesc, required bool) *TypeContainerMapUnordered[K, V]

func (*TypeContainerMapUnordered[K, V]) AddKV added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) AddKV(key K, value V)

func (*TypeContainerMapUnordered[K, V]) FromMap added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) FromMap(m map[K]V)

func (*TypeContainerMapUnordered[K, V]) GetSize added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) GetSize() int

func (*TypeContainerMapUnordered[K, V]) Read added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) Read(ctx context.Context, p thrift.TProtocol) (err error)

func (*TypeContainerMapUnordered[K, V]) SetSize added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) SetSize(v int)

func (*TypeContainerMapUnordered[K, V]) ToMap added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) ToMap() map[K]V

func (*TypeContainerMapUnordered[K, V]) Write added in v0.2.1

func (t *TypeContainerMapUnordered[K, V]) Write(ctx context.Context, p thrift.TProtocol) (err error)

type TypeContainerSet

type TypeContainerSet[T Sliceable] struct {
	Required bool
	Desc     TypeContainerDesc
	Size     int
	Value    []T
}

func NewTypeContainerSet

func NewTypeContainerSet[T Sliceable](desc TypeContainerDesc, required bool) *TypeContainerSet[T]

func (*TypeContainerSet[T]) Add added in v0.2.0

func (t *TypeContainerSet[T]) Add(vs ...T)

func (*TypeContainerSet[T]) GetSize added in v0.2.0

func (t *TypeContainerSet[T]) GetSize() int

func (*TypeContainerSet[T]) Read

func (t *TypeContainerSet[T]) Read(ctx context.Context, p thrift.TProtocol) (err error)

func (*TypeContainerSet[T]) SetSize added in v0.2.0

func (t *TypeContainerSet[T]) SetSize(v int)

func (*TypeContainerSet[T]) Write

func (t *TypeContainerSet[T]) Write(ctx context.Context, p thrift.TProtocol) (err error)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL