gitee.com/h79/goutils@v1.22.10/common/integer.go (about)

     1  package common
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  )
     7  
     8  type IntegerType interface {
     9  	~int8 | ~int16 | ~int | ~int32 | ~int64 | ~uint8 | ~uint16 | ~uint | ~uint32 | ~uint64 | ~float32 | ~float64
    10  }
    11  
    12  func SliceToInt(arr []any) []int {
    13  	return ToInt[int](arr)
    14  }
    15  
    16  func SliceToInt64(arr []any) []int64 {
    17  	return ToInt[int64](arr)
    18  }
    19  
    20  func ToInt[T IntegerType](arr []any) []T {
    21  	Int := make([]T, 0, len(arr))
    22  	for i := range arr {
    23  		Int = append(Int, T(ToInt64(arr[i])))
    24  	}
    25  	return Int
    26  }
    27  
    28  func WhereInt[T IntegerType](arr []any, where func(a any) (T, bool)) []T {
    29  	Int := make([]T, len(arr))
    30  	for i := range arr {
    31  		if ret, ok := where(arr[i]); ok {
    32  			Int = append(Int, ret)
    33  		}
    34  	}
    35  	return Int
    36  }
    37  
    38  func ToInt64(value interface{}) int64 {
    39  	reflectValue := reflect.Indirect(reflect.ValueOf(value))
    40  	return ToInt64V2(reflectValue)
    41  }
    42  
    43  func ToInt64V2(reflectValue reflect.Value) int64 {
    44  	switch reflectValue.Kind() {
    45  	case reflect.String:
    46  		if i, err := strconv.ParseInt(reflectValue.String(), 10, 64); err == nil {
    47  			return i
    48  		}
    49  		return 0
    50  	case reflect.Int:
    51  		fallthrough
    52  	case reflect.Int8:
    53  		fallthrough
    54  	case reflect.Int16:
    55  		fallthrough
    56  	case reflect.Int32:
    57  		fallthrough
    58  	case reflect.Int64:
    59  		return reflectValue.Int()
    60  	case reflect.Float32:
    61  		fallthrough
    62  	case reflect.Float64:
    63  		return int64(reflectValue.Float())
    64  	default:
    65  		panic("unhandled default case")
    66  	}
    67  	return 0
    68  }
    69  
    70  func ToFloat64(value interface{}) float64 {
    71  	reflectValue := reflect.Indirect(reflect.ValueOf(value))
    72  	return ToFloat64V2(reflectValue)
    73  }
    74  
    75  func ToFloat64V2(reflectValue reflect.Value) float64 {
    76  	switch reflectValue.Kind() {
    77  	case reflect.String:
    78  		f, _ := strconv.ParseFloat(reflectValue.String(), 4)
    79  		return f
    80  	case reflect.Int:
    81  		fallthrough
    82  	case reflect.Int8:
    83  		fallthrough
    84  	case reflect.Int16:
    85  		fallthrough
    86  	case reflect.Int32:
    87  		fallthrough
    88  	case reflect.Int64:
    89  		return float64(reflectValue.Int())
    90  	case reflect.Float32:
    91  		fallthrough
    92  	case reflect.Float64:
    93  		return reflectValue.Float()
    94  	default:
    95  		panic("unhandled default case")
    96  	}
    97  	return 0.0
    98  }
    99  
   100  func ToUInt64(value interface{}) uint64 {
   101  	reflectValue := reflect.Indirect(reflect.ValueOf(value))
   102  	return ToUInt64V2(reflectValue)
   103  }
   104  
   105  func ToUInt64V2(reflectValue reflect.Value) uint64 {
   106  	switch reflectValue.Kind() {
   107  	case reflect.String:
   108  		if i, err := strconv.ParseUint(reflectValue.String(), 10, 64); err == nil {
   109  			return i
   110  		}
   111  		return 0
   112  	case reflect.Uint:
   113  		fallthrough
   114  	case reflect.Uint8:
   115  		fallthrough
   116  	case reflect.Uint16:
   117  		fallthrough
   118  	case reflect.Uint32:
   119  		fallthrough
   120  	case reflect.Uint64:
   121  		return reflectValue.Uint()
   122  	default:
   123  		panic("unhandled default case")
   124  	}
   125  	return 0
   126  }