github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/reflectx/reflect.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  func IndirectType(tpe reflect.Type) reflect.Type {
     8  	for {
     9  		if tpe.Kind() == reflect.Interface {
    10  			e := tpe.Elem()
    11  			if e.Kind() == reflect.Ptr {
    12  				tpe = e
    13  				continue
    14  			}
    15  		}
    16  		if tpe.Kind() != reflect.Ptr {
    17  			break
    18  		}
    19  		tpe = tpe.Elem()
    20  	}
    21  	return tpe
    22  }
    23  
    24  func Indirect(v reflect.Value) reflect.Value {
    25  	for {
    26  		if v.Kind() == reflect.Interface {
    27  			e := v.Elem()
    28  			if e.Kind() == reflect.Ptr {
    29  				v = e
    30  				continue
    31  			}
    32  		}
    33  		if v.Kind() != reflect.Ptr {
    34  			break
    35  		}
    36  		v = v.Elem()
    37  	}
    38  	return v
    39  }