代码拉取完成,页面将自动刷新
package pandas
import (
"fmt"
"gitee.com/quant1x/gox/exception"
"gitee.com/quant1x/pandas/stat"
"reflect"
"strconv"
"strings"
)
const (
//MAX_FLOAT32_PRICE = float32(9999.9999) // float32的价最大阀值触发扩展到float64
MAX_FLOAT32_PRICE = float32(0) // float32的价最大阀值触发扩展到float64
)
var (
ErrUnsupportedType = exception.New(0, "Unsupported type")
)
func mustFloat64(f float32) bool {
if f > MAX_FLOAT32_PRICE {
return true
}
return false
}
func findTypeByString(arr []string) (stat.Type, error) {
var hasFloats, hasInts, hasBools, hasStrings bool
var useFloat32, useFloat64 bool
var stringLengthEqual = -1
var stringLenth = -1
for _, str := range arr {
if str == "" || str == "NaN" {
continue
}
tLen := len(str)
if strings.HasPrefix(str, "0") {
stringLengthEqual = 0
}
if stringLenth < 1 {
if stringLengthEqual <= 0 {
stringLenth = tLen
}
} else if stringLengthEqual >= 0 && tLen != stringLenth {
stringLengthEqual += 1
}
if _, err := strconv.Atoi(str); err == nil {
hasInts = true
continue
}
if f, err := strconv.ParseFloat(str, 64); err == nil {
hasFloats = true
if float32(f) < stat.MaxFloat32 {
if mustFloat64(float32(f)) {
useFloat64 = true
} else {
useFloat32 = true
}
}
continue
}
if str == "true" || str == "false" {
hasBools = true
continue
}
hasStrings = true
}
if !hasFloats && stringLengthEqual == 0 {
hasStrings = true
}
// 类型优先级, string > bool > float > int, string 为默认类型
switch {
case hasStrings:
return stat.SERIES_TYPE_STRING, nil
case hasBools:
return stat.SERIES_TYPE_BOOL, nil
case useFloat32 && !useFloat64:
return stat.SERIES_TYPE_FLOAT32, nil
case hasFloats:
return stat.SERIES_TYPE_FLOAT64, nil
case hasInts:
return stat.SERIES_TYPE_INT64, nil
default:
return stat.SERIES_TYPE_STRING, fmt.Errorf("couldn't detect type")
}
}
func parseType(s string) (stat.Type, error) {
switch s {
case "float", "float32":
return stat.SERIES_TYPE_FLOAT32, nil
case "float64":
return stat.SERIES_TYPE_FLOAT64, nil
case "int", "int64", "int32", "int16", "int8":
return stat.SERIES_TYPE_INT64, nil
case "uint", "uint64", "uint32", "uint16", "uint8", "byte":
return stat.SERIES_TYPE_INT64, nil
case "string":
return stat.SERIES_TYPE_STRING, nil
case "bool":
return stat.SERIES_TYPE_BOOL, nil
}
return stat.SERIES_TYPE_INVAILD, fmt.Errorf("type (%s) is not supported", s)
}
func detectTypes[T stat.GenericType](v T) (stat.Type, any) {
var _type = stat.SERIES_TYPE_STRING
vv := reflect.ValueOf(v)
vk := vv.Kind()
switch vk {
case reflect.Invalid:
_type = stat.SERIES_TYPE_INVAILD
case reflect.Bool:
_type = stat.SERIES_TYPE_BOOL
case reflect.Int64:
_type = stat.SERIES_TYPE_INT64
case reflect.Float32:
_type = stat.SERIES_TYPE_FLOAT32
case reflect.Float64:
_type = stat.SERIES_TYPE_FLOAT64
case reflect.String:
_type = stat.SERIES_TYPE_STRING
default:
panic(fmt.Errorf("unknown type, %+v", v))
}
return _type, vv.Interface()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。