Embed returns the embedded struct (in first field only) of given type within given struct
(stru interface{}, embed reflect.Type)
| 69 | |
| 70 | // Embed returns the embedded struct (in first field only) of given type within given struct |
| 71 | func Embed(stru interface{}, embed reflect.Type) interface{} { |
| 72 | if IfaceIsNil(stru) { |
| 73 | return nil |
| 74 | } |
| 75 | v := NonPtrValue(reflect.ValueOf(stru)) |
| 76 | typ := v.Type() |
| 77 | if typ == embed { |
| 78 | return PtrValue(v).Interface() |
| 79 | } |
| 80 | if typ.NumField() == 0 { |
| 81 | return nil |
| 82 | } |
| 83 | f := typ.Field(0) |
| 84 | if f.Type.Kind() == reflect.Struct && f.Anonymous { // anon only avail on StructField fm typ |
| 85 | vf := v.Field(0) |
| 86 | vfpi := PtrValue(vf).Interface() |
| 87 | if f.Type == embed { |
| 88 | return vfpi |
| 89 | } |
| 90 | rv := Embed(vfpi, embed) |
| 91 | if rv != nil { |
| 92 | return rv |
| 93 | } |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | var ( |
| 99 | trace = false |
nothing calls this directly
no test coverage detected