gitee.com/quant1x/num@v0.3.2/type_generic.go (about)

     1  package num
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  // Type is a convenience alias that can be used for a more type safe way of
     8  // reason and use Series types.
     9  //type Type = reflect.Kind
    10  
    11  //var (
    12  //	sKindInvalid       = "invalid"
    13  //	sKindBool          = "bool"
    14  //	sKindInt           = "int"
    15  //	sKindInt8          = "int8"
    16  //	sKindInt16         = "int16"
    17  //	sKindInt32         = "int32"
    18  //	sKindInt64         = "int64"
    19  //	sKindUint          = "uint"
    20  //	sKindUint8         = "uint8"
    21  //	sKindUint16        = "uint16"
    22  //	sKindUint32        = "uint32"
    23  //	sKindUint64        = "uint64"
    24  //	sKindUintptr       = "uintptr"
    25  //	sKindFloat32       = "float32"
    26  //	sKindFloat64       = "float64"
    27  //	sKindComplex64     = "complex64"
    28  //	sKindComplex128    = "complex128"
    29  //	sKindArray         = "array"
    30  //	sKindChan          = "chan"
    31  //	sKindFunc          = "func"
    32  //	sKindInterface     = "interface"
    33  //	sKindMap           = "map"
    34  //	sKindPointer       = "ptr"
    35  //	sKindSlice         = "slice"
    36  //	sKindString        = "string"
    37  //	sKindUnsafePointer = "unsafe.Pointer"
    38  //	// 缓存Kind对应关系
    39  //	mapKind = map[string]reflect.Kind{
    40  //		sKindInvalid:       reflect.Invalid,
    41  //		sKindBool:          reflect.Bool,
    42  //		sKindInt:           reflect.Int,
    43  //		sKindInt8:          reflect.Int8,
    44  //		sKindInt16:         reflect.Int16,
    45  //		sKindInt32:         reflect.Int32,
    46  //		sKindInt64:         reflect.Int64,
    47  //		sKindUint:          reflect.Uint,
    48  //		sKindUint8:         reflect.Uint8,
    49  //		sKindUint16:        reflect.Uint16,
    50  //		sKindUint32:        reflect.Uint32,
    51  //		sKindUint64:        reflect.Uint64,
    52  //		sKindUintptr:       reflect.Uintptr,
    53  //		sKindFloat32:       reflect.Float32,
    54  //		sKindFloat64:       reflect.Float64,
    55  //		sKindComplex64:     reflect.Complex64,
    56  //		sKindComplex128:    reflect.Complex128,
    57  //		sKindArray:         reflect.Array,
    58  //		sKindChan:          reflect.Chan,
    59  //		sKindFunc:          reflect.Func,
    60  //		sKindInterface:     reflect.Interface,
    61  //		sKindMap:           reflect.Map,
    62  //		sKindPointer:       reflect.Pointer,
    63  //		sKindSlice:         reflect.Slice,
    64  //		sKindString:        reflect.String,
    65  //		sKindUnsafePointer: reflect.UnsafePointer,
    66  //	}
    67  //)
    68  //
    69  //// 初始化全局的私有变量
    70  //var (
    71  //	rawBool     bool    = true
    72  //	TypeBool            = reflect.TypeOf([]bool{})
    73  //	rawInt32    int32   = int32(0)
    74  //	typeInt32           = reflect.TypeOf([]int32{})
    75  //	rawInt64    int64   = int64(0)
    76  //	TypeInt64           = reflect.TypeOf([]int64{})
    77  //	rawFloat32  float32 = float32(0)
    78  //	TypeFloat32         = reflect.TypeOf([]float32{})
    79  //	rawFloat64  float64 = float64(0)
    80  //	TypeFloat64         = reflect.TypeOf([]float64{})
    81  //	TypeString          = reflect.TypeOf([]string{})
    82  //)
    83  //
    84  //// CheckoutRawType 从泛型检测出类型
    85  //func v1CheckoutRawType(frame any) reflect.Kind {
    86  //	ft := reflect.TypeOf(frame)
    87  //	strType := ft.String()
    88  //	pos := strings.LastIndexByte(strType, '[')
    89  //	if pos < 0 {
    90  //		return reflect.Invalid
    91  //	}
    92  //	strType = strType[pos+1:]
    93  //	pos = strings.LastIndexByte(strType, ']')
    94  //	if pos < 0 {
    95  //		return reflect.Invalid
    96  //	}
    97  //	rawType := strings.TrimSpace(strType[:pos])
    98  //	// 如果是0, 这个应该是个slice
    99  //	if len(rawType) < 1 {
   100  //		rawType = strings.TrimSpace(strType[pos+1:])
   101  //		if len(rawType) < 1 {
   102  //			return reflect.Invalid
   103  //		}
   104  //	}
   105  //	if t, ok := mapKind[rawType]; ok {
   106  //		return t
   107  //	}
   108  //	return reflect.Invalid
   109  //}
   110  
   111  // CheckoutRawType 从泛型检测出类型
   112  func CheckoutRawType(frame any) reflect.Kind {
   113  	typ := reflect.TypeOf(frame)
   114  	switch typ.Kind() {
   115  	case reflect.Slice, reflect.Array:
   116  		return typ.Elem().Kind()
   117  	}
   118  	return reflect.Invalid
   119  }