blob: d4ba1f4f80e979f5157832f1f5014b9a63edbd9d [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001package reflect2
2
3import (
4 "reflect"
5 "unsafe"
6)
7
8type safeField struct {
9 reflect.StructField
10}
11
12func (field *safeField) Offset() uintptr {
13 return field.StructField.Offset
14}
15
16func (field *safeField) Name() string {
17 return field.StructField.Name
18}
19
20func (field *safeField) PkgPath() string {
21 return field.StructField.PkgPath
22}
23
24func (field *safeField) Type() Type {
25 panic("not implemented")
26}
27
28func (field *safeField) Tag() reflect.StructTag {
29 return field.StructField.Tag
30}
31
32func (field *safeField) Index() []int {
33 return field.StructField.Index
34}
35
36func (field *safeField) Anonymous() bool {
37 return field.StructField.Anonymous
38}
39
40func (field *safeField) Set(obj interface{}, value interface{}) {
41 val := reflect.ValueOf(obj).Elem()
42 val.FieldByIndex(field.Index()).Set(reflect.ValueOf(value).Elem())
43}
44
45func (field *safeField) UnsafeSet(obj unsafe.Pointer, value unsafe.Pointer) {
46 panic("unsafe operation is not supported")
47}
48
49func (field *safeField) Get(obj interface{}) interface{} {
50 val := reflect.ValueOf(obj).Elem().FieldByIndex(field.Index())
51 ptr := reflect.New(val.Type())
52 ptr.Elem().Set(val)
53 return ptr.Interface()
54}
55
56func (field *safeField) UnsafeGet(obj unsafe.Pointer) unsafe.Pointer {
57 panic("does not support unsafe operation")
58}