-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypes.go
71 lines (54 loc) · 1.33 KB
/
types.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
package ikea
import (
"fmt"
"io"
"reflect"
)
type readWriter interface {
isFixed() bool
}
type fixedReadWriter interface {
readWriter
length() int
readFixed([]byte, reflect.Value)
writeFixed([]byte, reflect.Value)
}
type variableReadWriter interface {
readWriter
vLength(reflect.Value) int
readVariable(io.Reader, reflect.Value) error
writeVariable(io.Writer, reflect.Value) error
}
func getTypeHandler(typ reflect.Type) readWriter {
kind := typ.Kind()
if primitive, ok := primitiveIndex[kind]; ok {
return primitive
}
switch kind {
case reflect.Ptr:
return getPointerHandlerFromType(typ)
case reflect.String:
return stringTypeHandler
case reflect.Struct:
return getStructHandlerFromType(typ)
case reflect.Slice:
return getSliceHandlerFromType(typ)
case reflect.Map:
return getMapHandlerFromType(typ)
case reflect.Uint:
fallthrough
case reflect.Int:
panic("types uint and int are not supported, as their actual size is dependant on compiler architecture and" +
" could cause data inconsistencies. use uint32/uint64/int32/int64 instead")
default:
panic(fmt.Sprintf("Cannot build type handler for type \"%s\" with kind nr. %d", typ.String(), typ.Kind()))
}
}
type fixed struct{}
func (fixed) isFixed() bool {
return true
}
type variable struct{}
func (variable) isFixed() bool {
return false
}