safe_field.go
1.2 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
package reflect2
import (
"reflect"
"unsafe"
)
type safeField struct {
reflect.StructField
}
func (field *safeField) Offset() uintptr {
return field.StructField.Offset
}
func (field *safeField) Name() string {
return field.StructField.Name
}
func (field *safeField) PkgPath() string {
return field.StructField.PkgPath
}
func (field *safeField) Type() Type {
panic("not implemented")
}
func (field *safeField) Tag() reflect.StructTag {
return field.StructField.Tag
}
func (field *safeField) Index() []int {
return field.StructField.Index
}
func (field *safeField) Anonymous() bool {
return field.StructField.Anonymous
}
func (field *safeField) Set(obj interface{}, value interface{}) {
val := reflect.ValueOf(obj).Elem()
val.FieldByIndex(field.Index()).Set(reflect.ValueOf(value).Elem())
}
func (field *safeField) UnsafeSet(obj unsafe.Pointer, value unsafe.Pointer) {
panic("unsafe operation is not supported")
}
func (field *safeField) Get(obj interface{}) interface{} {
val := reflect.ValueOf(obj).Elem().FieldByIndex(field.Index())
ptr := reflect.New(val.Type())
ptr.Elem().Set(val)
return ptr.Interface()
}
func (field *safeField) UnsafeGet(obj unsafe.Pointer) unsafe.Pointer {
panic("does not support unsafe operation")
}