自定义null类型UnmarshalJSON应该抛出异常

This commit is contained in:
xh
2025-11-24 02:48:03 +08:00
parent ea67c0336b
commit 9cc1cdaf2a
3 changed files with 17 additions and 86 deletions

View File

@@ -16,24 +16,6 @@ type NullFloat struct {
func DecodeFloat(value any) (any, error) {
switch v := value.(type) {
// case float64:
// f := v
// return NullFloat{Float: &f, Valid: true}, nil
// case float32:
// f := float64(v)
// return NullFloat{Float: &f, Valid: true}, nil
// case int:
// f := float64(v)
// return NullFloat{Float: &f, Valid: true}, nil
// case int64:
// f := float64(v)
// return NullFloat{Float: &f, Valid: true}, nil
// case string:
// if v == "" {
// return NullFloat{Float: nil, Valid: false}, nil
// }
// f, err := strconv.ParseFloat(v, 64)
// return NullFloat{Float: &f, Valid: true}, err
case nil:
return NullFloat{Float: nil, Valid: false}, nil
case NullFloat:
@@ -57,35 +39,6 @@ func (f *NullFloat) Scan(value interface{}) error {
}
f.Float, f.Valid = &result, true
return nil
// switch v := value.(type) {
// case float64:
// f.Float, f.Valid = &v, true
// case float32:
// // 直接用float64(float32(v)), 会丢失精度
// val, _ := strconv.ParseFloat(fmt.Sprintf("%f", v), 64)
// f.Float, f.Valid = &val, true
// // 匹配所有int
// case uint, uint8, uint16, uint32, uint64, int8, int16, int, int32, int64:
// val := float64(v)
// f.Float, f.Valid = &val, true
// case string:
// if v == "" {
// f.Float, f.Valid = nil, false
// return nil
// }
// val, err := strconv.ParseFloat(v, 64)
// if err != nil {
// f.Float, f.Valid = nil, false
// return err
// }
// f.Float, f.Valid = &val, true
// return err
// case nil:
// f.Float, f.Valid = nil, false
// return nil
// }
// return nil
}
// gorm实现 Valuer
@@ -124,6 +77,11 @@ func (f *NullFloat) UnmarshalJSON(data []byte) error {
return err
}
switch v := x.(type) {
case int64:
f64 := float64(v)
f.Float = &f64
f.Valid = true
return nil
case float64:
f.Float = &v
f.Valid = true

View File

@@ -3,6 +3,8 @@ package core
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strconv"
"x_admin/util/convert_util"
)
@@ -14,45 +16,8 @@ type NullInt struct {
Valid bool
}
// func EncodeInt(value any) any {
// switch v := value.(type) {
// case NullInt:
// if v.Valid {
// return *v.Int
// } else {
// return nil
// }
// case map[string]any:
// if v["Int"] != nil {
// val := v["Int"]
// switch i := val.(type) {
// case *int:
// return *i
// case *int64:
// return *i
// case *string:
// return *i
// default:
// return nil
// }
// // return val
// }
// }
// return nil
// }
func DecodeInt(value any) (any, error) {
switch v := value.(type) {
// case int:
// i := int64(v)
// return NullInt{Int: &i, Valid: true}, nil
// case int64:
// return NullInt{Int: &v, Valid: true}, nil
// case string:
// if v == "" {
// return NullInt{Int: nil, Valid: false}, nil
// }
// i, err := strconv.ParseInt(v, 10, 64)
// return NullInt{Int: &i, Valid: true}, err
case nil:
return NullInt{Int: nil, Valid: false}, nil
case NullInt:
@@ -106,7 +71,7 @@ func (i NullInt) MarshalJSON() ([]byte, error) {
}
}
// 实现json反序列化接口
// 实现json反序列化接口,支持 int64, stringnull类型对于float64类型判断转换前后是否相等防止精度丢失
func (i *NullInt) UnmarshalJSON(data []byte) error {
var x any
if err := json.Unmarshal(data, &x); err != nil {
@@ -119,6 +84,11 @@ func (i *NullInt) UnmarshalJSON(data []byte) error {
return nil
case float64:
i64 := int64(v)
// 判断转换前后是否相等,防止精度丢失
if float64(i64) != v {
i.Valid = false
return errors.New("int64转换失败" + fmt.Sprintf("%f", v) + "精度丢失")
}
i.Int = &i64
i.Valid = true
return nil

View File

@@ -36,7 +36,10 @@ func (t *NullTime) UnmarshalJSON(bs []byte) error {
}
return nil
}
tt, _ := time.ParseInLocation(TimeFormat, date, time.Local)
tt, err := time.ParseInLocation(TimeFormat, date, time.Local)
if err != nil {
return err
}
*t = NullTime{
Time: &tt,
Valid: true,