Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | package reflect2 |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type safeField struct { |
| 9 | reflect.StructField |
| 10 | } |
| 11 | |
| 12 | func (field *safeField) Offset() uintptr { |
| 13 | return field.StructField.Offset |
| 14 | } |
| 15 | |
| 16 | func (field *safeField) Name() string { |
| 17 | return field.StructField.Name |
| 18 | } |
| 19 | |
| 20 | func (field *safeField) PkgPath() string { |
| 21 | return field.StructField.PkgPath |
| 22 | } |
| 23 | |
| 24 | func (field *safeField) Type() Type { |
| 25 | panic("not implemented") |
| 26 | } |
| 27 | |
| 28 | func (field *safeField) Tag() reflect.StructTag { |
| 29 | return field.StructField.Tag |
| 30 | } |
| 31 | |
| 32 | func (field *safeField) Index() []int { |
| 33 | return field.StructField.Index |
| 34 | } |
| 35 | |
| 36 | func (field *safeField) Anonymous() bool { |
| 37 | return field.StructField.Anonymous |
| 38 | } |
| 39 | |
| 40 | func (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 | |
| 45 | func (field *safeField) UnsafeSet(obj unsafe.Pointer, value unsafe.Pointer) { |
| 46 | panic("unsafe operation is not supported") |
| 47 | } |
| 48 | |
| 49 | func (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 | |
| 56 | func (field *safeField) UnsafeGet(obj unsafe.Pointer) unsafe.Pointer { |
| 57 | panic("does not support unsafe operation") |
| 58 | } |