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