mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-05 16:17:00 +08:00
ToInt64
This commit is contained in:
@@ -16,30 +16,35 @@ type NullFloat struct {
|
|||||||
|
|
||||||
func DecodeFloat(value any) (any, error) {
|
func DecodeFloat(value any) (any, error) {
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case float64:
|
// case float64:
|
||||||
f := v
|
// f := v
|
||||||
return NullFloat{Float: &f, Valid: true}, nil
|
// return NullFloat{Float: &f, Valid: true}, nil
|
||||||
case float32:
|
// case float32:
|
||||||
f := float64(v)
|
// f := float64(v)
|
||||||
return NullFloat{Float: &f, Valid: true}, nil
|
// return NullFloat{Float: &f, Valid: true}, nil
|
||||||
case int:
|
// case int:
|
||||||
f := float64(v)
|
// f := float64(v)
|
||||||
return NullFloat{Float: &f, Valid: true}, nil
|
// return NullFloat{Float: &f, Valid: true}, nil
|
||||||
case int64:
|
// case int64:
|
||||||
f := float64(v)
|
// f := float64(v)
|
||||||
return NullFloat{Float: &f, Valid: true}, nil
|
// return NullFloat{Float: &f, Valid: true}, nil
|
||||||
case string:
|
// case string:
|
||||||
if v == "" {
|
// if v == "" {
|
||||||
return NullFloat{Float: nil, Valid: false}, nil
|
// return NullFloat{Float: nil, Valid: false}, nil
|
||||||
}
|
// }
|
||||||
f, err := strconv.ParseFloat(v, 64)
|
// f, err := strconv.ParseFloat(v, 64)
|
||||||
return NullFloat{Float: &f, Valid: true}, err
|
// return NullFloat{Float: &f, Valid: true}, err
|
||||||
case nil:
|
case nil:
|
||||||
return NullFloat{Float: nil, Valid: false}, nil
|
return NullFloat{Float: nil, Valid: false}, nil
|
||||||
case NullFloat:
|
case NullFloat:
|
||||||
return v, nil
|
return v, nil
|
||||||
|
default:
|
||||||
|
result, err := convert_util.ToFloat64(value)
|
||||||
|
if err != nil {
|
||||||
|
return NullFloat{Float: nil, Valid: false}, err
|
||||||
|
}
|
||||||
|
return NullFloat{Float: &result, Valid: true}, nil
|
||||||
}
|
}
|
||||||
return NullFloat{Float: nil, Valid: false}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// gorm实现Scanner
|
// gorm实现Scanner
|
||||||
|
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"x_admin/util/convert_util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// int类型别名,支持前端传递null,int,string类型
|
// int类型别名,支持前端传递null,int,string类型
|
||||||
@@ -41,23 +42,29 @@ type NullInt struct {
|
|||||||
// }
|
// }
|
||||||
func DecodeInt(value any) (any, error) {
|
func DecodeInt(value any) (any, error) {
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case int:
|
// case int:
|
||||||
i := int64(v)
|
// i := int64(v)
|
||||||
return NullInt{Int: &i, Valid: true}, nil
|
// return NullInt{Int: &i, Valid: true}, nil
|
||||||
case int64:
|
// case int64:
|
||||||
return NullInt{Int: &v, Valid: true}, nil
|
// return NullInt{Int: &v, Valid: true}, nil
|
||||||
case string:
|
// case string:
|
||||||
if v == "" {
|
// if v == "" {
|
||||||
return NullInt{Int: nil, Valid: false}, nil
|
// return NullInt{Int: nil, Valid: false}, nil
|
||||||
}
|
// }
|
||||||
i, err := strconv.ParseInt(v, 10, 64)
|
// i, err := strconv.ParseInt(v, 10, 64)
|
||||||
return NullInt{Int: &i, Valid: true}, err
|
// return NullInt{Int: &i, Valid: true}, err
|
||||||
case nil:
|
case nil:
|
||||||
return NullInt{Int: nil, Valid: false}, nil
|
return NullInt{Int: nil, Valid: false}, nil
|
||||||
case NullInt:
|
case NullInt:
|
||||||
return v, nil
|
return v, nil
|
||||||
|
default:
|
||||||
|
result, err := convert_util.ToInt64(value)
|
||||||
|
if err != nil {
|
||||||
|
return NullInt{Int: nil, Valid: false}, err
|
||||||
}
|
}
|
||||||
return NullInt{Int: nil, Valid: false}, nil
|
return NullInt{Int: &result, Valid: true}, nil
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// gorm实现Scanner
|
// gorm实现Scanner
|
||||||
|
@@ -7,12 +7,3 @@ type PageResp struct {
|
|||||||
PageSize int `json:"pageSize"` // 每页Size
|
PageSize int `json:"pageSize"` // 每页Size
|
||||||
Lists interface{} `json:"lists"` // 数据
|
Lists interface{} `json:"lists"` // 数据
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy 拷贝结构体
|
|
||||||
// func Copy(toValue interface{}, fromValue interface{}) interface{} {
|
|
||||||
// if err := copier.Copy(toValue, fromValue); err != nil {
|
|
||||||
// core.Logger.Errorf("Copy err: err=[%+v]", err)
|
|
||||||
// panic(SystemError)
|
|
||||||
// }
|
|
||||||
// return toValue
|
|
||||||
// }
|
|
||||||
|
@@ -15,13 +15,14 @@ import (
|
|||||||
|
|
||||||
func ToFloat64(value interface{}) (float64, error) {
|
func ToFloat64(value interface{}) (float64, error) {
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case float64, float32:
|
case float32:
|
||||||
return strconv.ParseFloat(fmt.Sprintf("%f", v), 64)
|
return strconv.ParseFloat(fmt.Sprintf("%f", v), 64)
|
||||||
default:
|
default:
|
||||||
return convertor.ToFloat(value)
|
return convertor.ToFloat(value)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// return 0, nil
|
func ToInt64(value interface{}) (int64, error) {
|
||||||
|
return convertor.ToInt(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StructToMap 结构体转换成map,深度转换
|
// StructToMap 结构体转换成map,深度转换
|
||||||
|
Reference in New Issue
Block a user