forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeof.go
55 lines (50 loc) · 948 Bytes
/
sizeof.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
package kafka
import (
"fmt"
"reflect"
)
func sizeof(a interface{}) int32 {
switch v := a.(type) {
case int8:
return 1
case int16:
return 2
case int32:
return 4
case int64:
return 8
case string:
return 2 + int32(len(v))
case []byte:
return 4 + int32(len(v))
case messageSet:
return sizeofMessageSet(v)
}
switch v := reflect.ValueOf(a); v.Kind() {
case reflect.Struct:
return sizeofStruct(v)
case reflect.Slice:
return sizeofSlice(v)
default:
panic(fmt.Sprintf("unsupported type: %T", a))
}
}
func sizeofMessageSet(set messageSet) (size int32) {
for _, msg := range set {
size += sizeof(msg)
}
return
}
func sizeofStruct(v reflect.Value) (size int32) {
for i, n := 0, v.NumField(); i != n; i++ {
size += sizeof(v.Field(i).Interface())
}
return
}
func sizeofSlice(v reflect.Value) (size int32) {
size = 4
for i, n := 0, v.Len(); i != n; i++ {
size += sizeof(v.Index(i).Interface())
}
return
}