github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_slice_float.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/wangyougui/gf/v2/internal/json"
    13  	"github.com/wangyougui/gf/v2/internal/reflection"
    14  )
    15  
    16  // SliceFloat is alias of Floats.
    17  func SliceFloat(any interface{}) []float64 {
    18  	return Floats(any)
    19  }
    20  
    21  // SliceFloat32 is alias of Float32s.
    22  func SliceFloat32(any interface{}) []float32 {
    23  	return Float32s(any)
    24  }
    25  
    26  // SliceFloat64 is alias of Float64s.
    27  func SliceFloat64(any interface{}) []float64 {
    28  	return Floats(any)
    29  }
    30  
    31  // Floats converts `any` to []float64.
    32  func Floats(any interface{}) []float64 {
    33  	return Float64s(any)
    34  }
    35  
    36  // Float32s converts `any` to []float32.
    37  func Float32s(any interface{}) []float32 {
    38  	if any == nil {
    39  		return nil
    40  	}
    41  	var (
    42  		array []float32 = nil
    43  	)
    44  	switch value := any.(type) {
    45  	case string:
    46  		if value == "" {
    47  			return []float32{}
    48  		}
    49  		return []float32{Float32(value)}
    50  	case []string:
    51  		array = make([]float32, len(value))
    52  		for k, v := range value {
    53  			array[k] = Float32(v)
    54  		}
    55  	case []int:
    56  		array = make([]float32, len(value))
    57  		for k, v := range value {
    58  			array[k] = Float32(v)
    59  		}
    60  	case []int8:
    61  		array = make([]float32, len(value))
    62  		for k, v := range value {
    63  			array[k] = Float32(v)
    64  		}
    65  	case []int16:
    66  		array = make([]float32, len(value))
    67  		for k, v := range value {
    68  			array[k] = Float32(v)
    69  		}
    70  	case []int32:
    71  		array = make([]float32, len(value))
    72  		for k, v := range value {
    73  			array[k] = Float32(v)
    74  		}
    75  	case []int64:
    76  		array = make([]float32, len(value))
    77  		for k, v := range value {
    78  			array[k] = Float32(v)
    79  		}
    80  	case []uint:
    81  		for _, v := range value {
    82  			array = append(array, Float32(v))
    83  		}
    84  	case []uint8:
    85  		if json.Valid(value) {
    86  			_ = json.UnmarshalUseNumber(value, &array)
    87  		} else {
    88  			array = make([]float32, len(value))
    89  			for k, v := range value {
    90  				array[k] = Float32(v)
    91  			}
    92  		}
    93  	case []uint16:
    94  		array = make([]float32, len(value))
    95  		for k, v := range value {
    96  			array[k] = Float32(v)
    97  		}
    98  	case []uint32:
    99  		array = make([]float32, len(value))
   100  		for k, v := range value {
   101  			array[k] = Float32(v)
   102  		}
   103  	case []uint64:
   104  		array = make([]float32, len(value))
   105  		for k, v := range value {
   106  			array[k] = Float32(v)
   107  		}
   108  	case []bool:
   109  		array = make([]float32, len(value))
   110  		for k, v := range value {
   111  			array[k] = Float32(v)
   112  		}
   113  	case []float32:
   114  		array = value
   115  	case []float64:
   116  		array = make([]float32, len(value))
   117  		for k, v := range value {
   118  			array[k] = Float32(v)
   119  		}
   120  	case []interface{}:
   121  		array = make([]float32, len(value))
   122  		for k, v := range value {
   123  			array[k] = Float32(v)
   124  		}
   125  	}
   126  	if array != nil {
   127  		return array
   128  	}
   129  	if v, ok := any.(iFloats); ok {
   130  		return Float32s(v.Floats())
   131  	}
   132  	if v, ok := any.(iInterfaces); ok {
   133  		return Float32s(v.Interfaces())
   134  	}
   135  	// JSON format string value converting.
   136  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   137  		return array
   138  	}
   139  	// Not a common type, it then uses reflection for conversion.
   140  	originValueAndKind := reflection.OriginValueAndKind(any)
   141  	switch originValueAndKind.OriginKind {
   142  	case reflect.Slice, reflect.Array:
   143  		var (
   144  			length = originValueAndKind.OriginValue.Len()
   145  			slice  = make([]float32, length)
   146  		)
   147  		for i := 0; i < length; i++ {
   148  			slice[i] = Float32(originValueAndKind.OriginValue.Index(i).Interface())
   149  		}
   150  		return slice
   151  
   152  	default:
   153  		if originValueAndKind.OriginValue.IsZero() {
   154  			return []float32{}
   155  		}
   156  		return []float32{Float32(any)}
   157  	}
   158  }
   159  
   160  // Float64s converts `any` to []float64.
   161  func Float64s(any interface{}) []float64 {
   162  	if any == nil {
   163  		return nil
   164  	}
   165  	var (
   166  		array []float64 = nil
   167  	)
   168  	switch value := any.(type) {
   169  	case string:
   170  		if value == "" {
   171  			return []float64{}
   172  		}
   173  		return []float64{Float64(value)}
   174  	case []string:
   175  		array = make([]float64, len(value))
   176  		for k, v := range value {
   177  			array[k] = Float64(v)
   178  		}
   179  	case []int:
   180  		array = make([]float64, len(value))
   181  		for k, v := range value {
   182  			array[k] = Float64(v)
   183  		}
   184  	case []int8:
   185  		array = make([]float64, len(value))
   186  		for k, v := range value {
   187  			array[k] = Float64(v)
   188  		}
   189  	case []int16:
   190  		array = make([]float64, len(value))
   191  		for k, v := range value {
   192  			array[k] = Float64(v)
   193  		}
   194  	case []int32:
   195  		array = make([]float64, len(value))
   196  		for k, v := range value {
   197  			array[k] = Float64(v)
   198  		}
   199  	case []int64:
   200  		array = make([]float64, len(value))
   201  		for k, v := range value {
   202  			array[k] = Float64(v)
   203  		}
   204  	case []uint:
   205  		for _, v := range value {
   206  			array = append(array, Float64(v))
   207  		}
   208  	case []uint8:
   209  		if json.Valid(value) {
   210  			_ = json.UnmarshalUseNumber(value, &array)
   211  		} else {
   212  			array = make([]float64, len(value))
   213  			for k, v := range value {
   214  				array[k] = Float64(v)
   215  			}
   216  		}
   217  	case []uint16:
   218  		array = make([]float64, len(value))
   219  		for k, v := range value {
   220  			array[k] = Float64(v)
   221  		}
   222  	case []uint32:
   223  		array = make([]float64, len(value))
   224  		for k, v := range value {
   225  			array[k] = Float64(v)
   226  		}
   227  	case []uint64:
   228  		array = make([]float64, len(value))
   229  		for k, v := range value {
   230  			array[k] = Float64(v)
   231  		}
   232  	case []bool:
   233  		array = make([]float64, len(value))
   234  		for k, v := range value {
   235  			array[k] = Float64(v)
   236  		}
   237  	case []float32:
   238  		array = make([]float64, len(value))
   239  		for k, v := range value {
   240  			array[k] = Float64(v)
   241  		}
   242  	case []float64:
   243  		array = value
   244  	case []interface{}:
   245  		array = make([]float64, len(value))
   246  		for k, v := range value {
   247  			array[k] = Float64(v)
   248  		}
   249  	}
   250  	if array != nil {
   251  		return array
   252  	}
   253  	if v, ok := any.(iFloats); ok {
   254  		return v.Floats()
   255  	}
   256  	if v, ok := any.(iInterfaces); ok {
   257  		return Floats(v.Interfaces())
   258  	}
   259  	// JSON format string value converting.
   260  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   261  		return array
   262  	}
   263  	// Not a common type, it then uses reflection for conversion.
   264  	originValueAndKind := reflection.OriginValueAndKind(any)
   265  	switch originValueAndKind.OriginKind {
   266  	case reflect.Slice, reflect.Array:
   267  		var (
   268  			length = originValueAndKind.OriginValue.Len()
   269  			slice  = make([]float64, length)
   270  		)
   271  		for i := 0; i < length; i++ {
   272  			slice[i] = Float64(originValueAndKind.OriginValue.Index(i).Interface())
   273  		}
   274  		return slice
   275  
   276  	default:
   277  		if originValueAndKind.OriginValue.IsZero() {
   278  			return []float64{}
   279  		}
   280  		return []float64{Float64(any)}
   281  	}
   282  }