Scott Baker | e7144bc | 2019-10-01 14:16:47 -0700 | [diff] [blame] | 1 | package reflect2 |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "unsafe" |
| 6 | ) |
| 7 | |
| 8 | type UnsafeStructType struct { |
| 9 | unsafeType |
| 10 | likePtr bool |
| 11 | } |
| 12 | |
| 13 | func newUnsafeStructType(cfg *frozenConfig, type1 reflect.Type) *UnsafeStructType { |
| 14 | return &UnsafeStructType{ |
| 15 | unsafeType: *newUnsafeType(cfg, type1), |
| 16 | likePtr: likePtrType(type1), |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func (type2 *UnsafeStructType) LikePtr() bool { |
| 21 | return type2.likePtr |
| 22 | } |
| 23 | |
| 24 | func (type2 *UnsafeStructType) Indirect(obj interface{}) interface{} { |
| 25 | objEFace := unpackEFace(obj) |
| 26 | assertType("Type.Indirect argument 1", type2.ptrRType, objEFace.rtype) |
| 27 | return type2.UnsafeIndirect(objEFace.data) |
| 28 | } |
| 29 | |
| 30 | func (type2 *UnsafeStructType) UnsafeIndirect(ptr unsafe.Pointer) interface{} { |
| 31 | if type2.likePtr { |
| 32 | return packEFace(type2.rtype, *(*unsafe.Pointer)(ptr)) |
| 33 | } |
| 34 | return packEFace(type2.rtype, ptr) |
| 35 | } |
| 36 | |
| 37 | func (type2 *UnsafeStructType) FieldByName(name string) StructField { |
| 38 | structField, found := type2.Type.FieldByName(name) |
| 39 | if !found { |
| 40 | return nil |
| 41 | } |
| 42 | return newUnsafeStructField(type2, structField) |
| 43 | } |
| 44 | |
| 45 | func (type2 *UnsafeStructType) Field(i int) StructField { |
| 46 | return newUnsafeStructField(type2, type2.Type.Field(i)) |
| 47 | } |
| 48 | |
| 49 | func (type2 *UnsafeStructType) FieldByIndex(index []int) StructField { |
| 50 | return newUnsafeStructField(type2, type2.Type.FieldByIndex(index)) |
| 51 | } |
| 52 | |
| 53 | func (type2 *UnsafeStructType) FieldByNameFunc(match func(string) bool) StructField { |
| 54 | structField, found := type2.Type.FieldByNameFunc(match) |
| 55 | if !found { |
| 56 | panic("field match condition not found in " + type2.Type.String()) |
| 57 | } |
| 58 | return newUnsafeStructField(type2, structField) |
| 59 | } |