-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.go
More file actions
98 lines (90 loc) · 2.95 KB
/
Copy pathinterface.go
File metadata and controls
98 lines (90 loc) · 2.95 KB
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
package types
import (
"github.com/HarryWang29/bytestruct/errors"
"github.com/HarryWang29/bytestruct/runtime"
"reflect"
"unsafe"
)
type interfaceType struct {
typ *runtime.Type
structName string
fieldName string
}
func newEmptyInterfaceDecoder(structName, fieldName string) *interfaceType {
ifaceDecoder := &interfaceType{
typ: emptyInterfaceType,
structName: structName,
fieldName: fieldName,
}
return ifaceDecoder
}
func newInterfaceDecoder(typ *runtime.Type, structName, fieldName string) *interfaceType {
return &interfaceType{
typ: typ,
structName: structName,
fieldName: fieldName,
}
}
var (
emptyInterfaceType = runtime.Type2RType(reflect.TypeOf((*interface{})(nil)).Elem())
EmptyInterfaceType = emptyInterfaceType
interfaceMapType = runtime.Type2RType(
reflect.TypeOf((*map[interface{}]interface{})(nil)).Elem(),
)
stringTyp = runtime.Type2RType(
reflect.TypeOf(""),
)
)
func (d *interfaceType) DecodeStream(s *Stream, p unsafe.Pointer) error {
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&emptyInterface{
typ: d.typ,
ptr: p,
}))
rv := reflect.ValueOf(runtimeInterfaceValue)
iface := rv.Interface()
ifaceHeader := (*emptyInterface)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ
//if ifaceHeader.ptr == nil || d.typ == typ || typ == nil {
// // concrete type is empty interface
// return d.DecodeStream(EmptyInterface(s, depth, p), depth)
//}
//if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
// return d.DecodeStream(EmptyInterface(s, depth, p), depth)
//}
decoder, err := CompileToGetDecoder(typ)
if err != nil {
return err
}
return decoder.DecodeStream(s, ifaceHeader.ptr)
}
func (d *interfaceType) errUnmarshalType(typ reflect.Type, offset int64) *errors.UnmarshalTypeError {
return &errors.UnmarshalTypeError{
Value: typ.String(),
Type: typ,
Offset: offset,
Struct: d.structName,
Field: d.fieldName,
}
}
func (d *interfaceType) EncodeStream(s *Stream, p unsafe.Pointer) error {
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&emptyInterface{
typ: d.typ,
ptr: p,
}))
rv := reflect.ValueOf(runtimeInterfaceValue)
iface := rv.Interface()
ifaceHeader := (*emptyInterface)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ
//if ifaceHeader.ptr == nil || d.typ == typ || typ == nil {
// // concrete type is empty interface
// return d.DecodeStream(EmptyInterface(s, depth, p), depth)
//}
//if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
// return d.DecodeStream(EmptyInterface(s, depth, p), depth)
//}
decoder, err := CompileToGetDecoder(typ)
if err != nil {
return err
}
return decoder.EncodeStream(s, ifaceHeader.ptr)
}