/
type_container_list.go
101 lines (93 loc) · 2.4 KB
/
type_container_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package thrift_dyn
import (
"context"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
)
type TypeContainerList[T Sliceable] struct {
Required bool
Desc TypeContainerDesc
Size int
Value []T
}
func NewTypeContainerList[T Sliceable](desc TypeContainerDesc, required bool) *TypeContainerList[T] {
typ := &TypeContainerList[T]{
Required: required,
Desc: desc,
}
return typ
}
func (t *TypeContainerList[T]) Add(vs ...T) {
t.Value = append(t.Value, vs...)
}
func (t *TypeContainerList[T]) Write(ctx context.Context, p thrift.TProtocol) (err error) {
size := t.GetSize()
if err = p.WriteListBegin(ctx, t.Desc.Value, size); err != nil {
return
}
for i := 0; i < size; i++ {
if err = WriteData[T](ctx, TData[T]{
TDataSpec: TDataSpec{
Type: t.Desc.Value,
Required: t.Required,
Protocol: p,
},
Value: &t.Value[i],
}); err != nil {
return
}
}
if err = p.WriteListEnd(ctx); err != nil {
return
}
return
}
func (t *TypeContainerList[T]) Read(ctx context.Context, p thrift.TProtocol) (err error) {
vv := t.Value[:0]
for i := 0; i < t.Size; i++ {
var value T
data := TData[T]{
TDataSpec: TDataSpec{
Type: t.Desc.Value,
Required: t.Required,
Protocol: p,
},
Value: &value,
}
if err = ReadData(ctx, data); err != nil {
return
}
vv = append(vv, value)
}
t.Value = vv
return
}
func (t *TypeContainerList[T]) SetSize(v int) {
t.Size = v
}
func (t *TypeContainerList[T]) GetSize() int {
return len(t.Value)
}
func NewTypeContainerListOfTType(desc TypeContainerDesc, required bool) (TypeContainerImplementer, error) {
switch desc.Value {
case thrift.BOOL:
return NewTypeContainerList[bool](desc, required), nil
case thrift.BYTE:
return NewTypeContainerList[int8](desc, required), nil
case thrift.I16:
return NewTypeContainerList[int16](desc, required), nil
case thrift.I32:
return NewTypeContainerList[int32](desc, required), nil
case thrift.I64:
return NewTypeContainerList[int64](desc, required), nil
case thrift.DOUBLE:
return NewTypeContainerList[float64](desc, required), nil
case thrift.STRING:
return NewTypeContainerList[string](desc, required), nil
case thrift.STRUCT:
return NewTypeContainerList[thrift.TStruct](desc, required), nil
case thrift.MAP, thrift.LIST, thrift.SET:
return NewTypeContainerList[TypeContainerImplementer](desc, required), nil
}
return nil, fmt.Errorf("unhandled type %T", desc.Value)
}