forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscard.go
99 lines (85 loc) · 2.01 KB
/
discard.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
package kafka
import (
"bufio"
"fmt"
"reflect"
)
func discardN(r *bufio.Reader, sz int, n int) (int, error) {
n, err := r.Discard(n)
return sz - n, err
}
func discardInt8(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 1)
}
func discardInt16(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 2)
}
func discardInt32(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 4)
}
func discardInt64(r *bufio.Reader, sz int) (int, error) {
return discardN(r, sz, 8)
}
func discardString(r *bufio.Reader, sz int) (int, error) {
return readStringWith(r, sz, func(r *bufio.Reader, sz int, n int) (int, error) {
if n < 0 {
return sz, nil
}
return discardN(r, sz, n)
})
}
func discardBytes(r *bufio.Reader, sz int) (int, error) {
return readBytesWith(r, sz, func(r *bufio.Reader, sz int, n int) (int, error) {
if n < 0 {
return sz, nil
}
return discardN(r, sz, n)
})
}
func discard(r *bufio.Reader, sz int, a interface{}) (int, error) {
switch a.(type) {
case int8:
return discardInt8(r, sz)
case int16:
return discardInt16(r, sz)
case int32:
return discardInt32(r, sz)
case int64:
return discardInt64(r, sz)
case string:
return discardString(r, sz)
case []byte:
return discardBytes(r, sz)
}
switch v := reflect.ValueOf(a); v.Kind() {
case reflect.Struct:
return discardStruct(r, sz, v)
case reflect.Slice:
return discardSlice(r, sz, v)
default:
panic(fmt.Sprintf("unsupported type: %T", a))
}
}
func discardStruct(r *bufio.Reader, sz int, v reflect.Value) (int, error) {
var err error
for i, n := 0, v.NumField(); i != n; i++ {
if sz, err = discard(r, sz, v.Field(i)); err != nil {
break
}
}
return sz, err
}
func discardSlice(r *bufio.Reader, sz int, v reflect.Value) (int, error) {
var zero = reflect.Zero(v.Type().Elem())
var err error
var len int32
if sz, err = readInt32(r, sz, &len); err != nil {
return sz, err
}
for n := int(len); n > 0; n-- {
if sz, err = discard(r, sz, zero); err != nil {
break
}
}
return sz, err
}