gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/schemax/fieldkind/field-kind.go (about)

     1  package fieldkind
     2  
     3  import "reflect"
     4  
     5  // 分类
     6  type Kind int
     7  
     8  const (
     9  	Unknown Kind = iota
    10  	Class
    11  	Enum
    12  	Map
    13  	Array
    14  	Basic
    15  	Any
    16  )
    17  
    18  func ReflectToFieldKind(ftyp reflect.Type) Kind {
    19  	switch ftyp.Kind() {
    20  	case reflect.Interface:
    21  		return Any
    22  	case reflect.Map:
    23  		return Map
    24  	case reflect.Slice:
    25  		fallthrough
    26  	case reflect.Array:
    27  		return Array
    28  	case reflect.Ptr:
    29  		ftyp = ftyp.Elem()
    30  		if ftyp.Kind() == reflect.Struct {
    31  			return Class
    32  		}
    33  	case reflect.Struct:
    34  		return Class
    35  	case reflect.String:
    36  		fallthrough
    37  	case reflect.Bool:
    38  		fallthrough
    39  	case reflect.Int8:
    40  		fallthrough
    41  	case reflect.Int16:
    42  		fallthrough
    43  	case reflect.Int32:
    44  		fallthrough
    45  	case reflect.Int64:
    46  		fallthrough
    47  	case reflect.Int:
    48  		fallthrough
    49  	case reflect.Uint8:
    50  		fallthrough
    51  	case reflect.Uint16:
    52  		fallthrough
    53  	case reflect.Uint32:
    54  		fallthrough
    55  	case reflect.Uint64:
    56  		fallthrough
    57  	case reflect.Uint:
    58  		fallthrough
    59  	case reflect.Float32:
    60  		fallthrough
    61  	case reflect.Float64:
    62  		if ftyp.String() == ftyp.Kind().String() {
    63  			// 基础类型
    64  			return Basic
    65  		} else {
    66  			// 枚举类型
    67  			return Enum
    68  		}
    69  
    70  	}
    71  	return Unknown
    72  }