2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-12-11 21:22:41 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
2021-11-16 00:41:47 +08:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2022-03-17 20:27:59 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2022-03-11 10:24:42 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/reflection"
|
2021-11-16 00:41:47 +08:00
|
|
|
)
|
2020-10-30 22:26:26 +08:00
|
|
|
|
2019-12-11 21:22:41 +08:00
|
|
|
// SliceFloat is alias of Floats.
|
2021-02-05 14:44:20 +08:00
|
|
|
func SliceFloat(any interface{}) []float64 {
|
|
|
|
return Floats(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SliceFloat32 is alias of Float32s.
|
2021-02-05 14:44:20 +08:00
|
|
|
func SliceFloat32(any interface{}) []float32 {
|
|
|
|
return Float32s(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SliceFloat64 is alias of Float64s.
|
2021-02-05 14:44:20 +08:00
|
|
|
func SliceFloat64(any interface{}) []float64 {
|
|
|
|
return Floats(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Floats converts `any` to []float64.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Floats(any interface{}) []float64 {
|
|
|
|
return Float64s(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Float32s converts `any` to []float32.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Float32s(any interface{}) []float32 {
|
|
|
|
if any == nil {
|
2019-12-11 21:22:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-16 00:41:47 +08:00
|
|
|
var (
|
|
|
|
array []float32 = nil
|
|
|
|
)
|
2021-02-05 14:44:20 +08:00
|
|
|
switch value := any.(type) {
|
2020-12-09 13:39:09 +08:00
|
|
|
case string:
|
|
|
|
if value == "" {
|
|
|
|
return []float32{}
|
|
|
|
}
|
|
|
|
return []float32{Float32(value)}
|
2020-04-07 21:25:52 +08:00
|
|
|
case []string:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []int:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []int8:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []int16:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []int32:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []int64:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []uint:
|
|
|
|
for _, v := range value {
|
|
|
|
array = append(array, Float32(v))
|
|
|
|
}
|
|
|
|
case []uint8:
|
2022-03-17 20:27:59 +08:00
|
|
|
if json.Valid(value) {
|
|
|
|
_ = json.UnmarshalUseNumber(value, &array)
|
|
|
|
} else {
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
}
|
|
|
|
case []uint16:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []uint32:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []uint64:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []bool:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []float32:
|
|
|
|
array = value
|
|
|
|
case []float64:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
array = make([]float32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float32(v)
|
|
|
|
}
|
2021-11-16 00:41:47 +08:00
|
|
|
}
|
|
|
|
if array != nil {
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
if v, ok := any.(iFloats); ok {
|
|
|
|
return Float32s(v.Floats())
|
|
|
|
}
|
|
|
|
if v, ok := any.(iInterfaces); ok {
|
|
|
|
return Float32s(v.Interfaces())
|
|
|
|
}
|
|
|
|
// JSON format string value converting.
|
|
|
|
if checkJsonAndUnmarshalUseNumber(any, &array) {
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
// Not a common type, it then uses reflection for conversion.
|
2022-03-11 10:24:42 +08:00
|
|
|
originValueAndKind := reflection.OriginValueAndKind(any)
|
2021-11-16 00:41:47 +08:00
|
|
|
switch originValueAndKind.OriginKind {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
var (
|
|
|
|
length = originValueAndKind.OriginValue.Len()
|
|
|
|
slice = make([]float32, length)
|
|
|
|
)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
slice[i] = Float32(originValueAndKind.OriginValue.Index(i).Interface())
|
|
|
|
}
|
|
|
|
return slice
|
2021-02-05 14:44:20 +08:00
|
|
|
|
2021-11-16 00:41:47 +08:00
|
|
|
default:
|
|
|
|
if originValueAndKind.OriginValue.IsZero() {
|
|
|
|
return []float32{}
|
2020-10-30 22:26:26 +08:00
|
|
|
}
|
2021-11-16 00:41:47 +08:00
|
|
|
return []float32{Float32(any)}
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Float64s converts `any` to []float64.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Float64s(any interface{}) []float64 {
|
|
|
|
if any == nil {
|
2019-12-11 21:22:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-16 00:41:47 +08:00
|
|
|
var (
|
|
|
|
array []float64 = nil
|
|
|
|
)
|
2021-02-05 14:44:20 +08:00
|
|
|
switch value := any.(type) {
|
2020-12-09 13:39:09 +08:00
|
|
|
case string:
|
|
|
|
if value == "" {
|
|
|
|
return []float64{}
|
|
|
|
}
|
|
|
|
return []float64{Float64(value)}
|
2020-04-07 21:25:52 +08:00
|
|
|
case []string:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []int:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []int8:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []int16:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []int32:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []int64:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []uint:
|
|
|
|
for _, v := range value {
|
|
|
|
array = append(array, Float64(v))
|
|
|
|
}
|
|
|
|
case []uint8:
|
2022-03-17 20:27:59 +08:00
|
|
|
if json.Valid(value) {
|
|
|
|
_ = json.UnmarshalUseNumber(value, &array)
|
|
|
|
} else {
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
}
|
|
|
|
case []uint16:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []uint32:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []uint64:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []bool:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []float32:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
|
|
|
case []float64:
|
|
|
|
array = value
|
|
|
|
case []interface{}:
|
|
|
|
array = make([]float64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Float64(v)
|
|
|
|
}
|
2021-11-16 00:41:47 +08:00
|
|
|
}
|
|
|
|
if array != nil {
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
if v, ok := any.(iFloats); ok {
|
|
|
|
return v.Floats()
|
|
|
|
}
|
|
|
|
if v, ok := any.(iInterfaces); ok {
|
|
|
|
return Floats(v.Interfaces())
|
|
|
|
}
|
|
|
|
// JSON format string value converting.
|
|
|
|
if checkJsonAndUnmarshalUseNumber(any, &array) {
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
// Not a common type, it then uses reflection for conversion.
|
2022-03-11 10:24:42 +08:00
|
|
|
originValueAndKind := reflection.OriginValueAndKind(any)
|
2021-11-16 00:41:47 +08:00
|
|
|
switch originValueAndKind.OriginKind {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
var (
|
|
|
|
length = originValueAndKind.OriginValue.Len()
|
|
|
|
slice = make([]float64, length)
|
|
|
|
)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
slice[i] = Float64(originValueAndKind.OriginValue.Index(i).Interface())
|
|
|
|
}
|
|
|
|
return slice
|
2021-02-05 14:44:20 +08:00
|
|
|
|
2021-11-16 00:41:47 +08:00
|
|
|
default:
|
|
|
|
if originValueAndKind.OriginValue.IsZero() {
|
|
|
|
return []float64{}
|
2020-10-30 22:26:26 +08:00
|
|
|
}
|
2021-11-16 00:41:47 +08:00
|
|
|
return []float64{Float64(any)}
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
}
|