github.com/chain5j/chain5j-pkg@v1.0.7/util/reflectutil/reflect.go (about)

     1  // Package reflectutil
     2  //
     3  // @author: xwc1125
     4  package reflectutil
     5  
     6  import (
     7  	"log"
     8  	"reflect"
     9  
    10  	"github.com/chain5j/logger"
    11  )
    12  
    13  // ToPointer 将 interface{T} 转变为 interface{*T}
    14  // 如果是指针,直接返回
    15  func ToPointer(w interface{}) interface{} {
    16  	typeOf := reflect.TypeOf(w)
    17  	if typeOf.Kind() == reflect.Ptr {
    18  		return w
    19  	}
    20  	// reflect.PtrTo(typeOf)
    21  	valueOf := reflect.ValueOf(w)
    22  	pv := reflect.New(typeOf)
    23  	pv.Elem().Set(valueOf)
    24  	return pv.Interface()
    25  }
    26  
    27  func DelPointer(w interface{}) interface{} {
    28  	typeOf := reflect.TypeOf(w)
    29  	if typeOf.Kind() != reflect.Ptr {
    30  		return w
    31  	}
    32  	valueOf := reflect.ValueOf(w)
    33  	return valueOf.Elem().Interface()
    34  }
    35  
    36  // GetFieldName structName 的 type 不是结构体类型,就会报以下错误:panic: reflect: NumField of non-struct type,故需要在程序中加以判断。
    37  // 获取结构体中字段的名称
    38  func GetFieldName(structName interface{}) []string {
    39  	t := reflect.TypeOf(structName)
    40  	if t.Kind() == reflect.Ptr {
    41  		t = t.Elem()
    42  	}
    43  	if t.Kind() != reflect.Struct {
    44  		log.Println("Check type error not Struct")
    45  		return nil
    46  	}
    47  	fieldNum := t.NumField()
    48  	result := make([]string, 0, fieldNum)
    49  	for i := 0; i < fieldNum; i++ {
    50  		result = append(result, t.Field(i).Name)
    51  	}
    52  	return result
    53  }
    54  
    55  func GetValueByFieldName(structName interface{}, fieldName string) interface{} {
    56  	t := reflect.ValueOf(structName)
    57  	if t.Kind() == reflect.Ptr {
    58  		t = t.Elem()
    59  	}
    60  	if t.Kind() != reflect.Struct {
    61  		logger.Println("Check type error not Struct")
    62  		return nil
    63  	}
    64  	fieldByName := t.FieldByName(fieldName)
    65  	return fieldByName.Interface()
    66  }
    67  
    68  // 获取结构体中Tag的值,如果没有tag则返回字段值
    69  func GetTagName(structName interface{}, tagKey string) []FieldInfo {
    70  	t := reflect.TypeOf(structName)
    71  	if t.Kind() == reflect.Ptr {
    72  		t = t.Elem()
    73  	}
    74  	if t.Kind() != reflect.Struct {
    75  		log.Println("Check type error not Struct")
    76  		return nil
    77  	}
    78  	fieldNum := t.NumField()
    79  	fieldInfos := make([]FieldInfo, 0, fieldNum)
    80  	for i := 0; i < fieldNum; i++ {
    81  		fieldName := t.Field(i).Name
    82  		fieldType := t.Field(i).Type.Name() // 类型
    83  
    84  		fieldInfo := FieldInfo{
    85  			FieldName: fieldName,
    86  			FieldType: fieldType,
    87  		}
    88  		// 获取tag内容
    89  		tagValue := t.Field(i).Tag.Get(tagKey)
    90  		fieldInfo.TagValue = tagValue
    91  		fieldInfos = append(fieldInfos, fieldInfo)
    92  	}
    93  	return fieldInfos
    94  }
    95  
    96  type FieldInfo struct {
    97  	FieldName string // 字段名称
    98  	FieldType string // 字段类型
    99  	TagValue  string // tag的值
   100  }