Valid改为Exist更清晰作用

This commit is contained in:
xh
2025-12-05 12:16:06 +08:00
parent 5806e72497
commit ea0d138e88
7 changed files with 99 additions and 94 deletions

View File

@@ -69,7 +69,7 @@ func (service {{{ toCamelCase .EntityName }}}Service) GetUpdateMap(listReq schem
updateMap := make(map[string]interface{})
{{{- range .Columns }}}
{{{- if .IsEdit }}}
if editReq.{{{ toUpperCamelCase .ColumnName }}}.Valid {
if editReq.{{{ toUpperCamelCase .ColumnName }}}.IsExists() {
updateMap["{{{ .ColumnName }}}"] = editReq.{{{ toUpperCamelCase .ColumnName }}}.GetValue()
}
{{{- end }}}
@@ -178,7 +178,7 @@ func (service {{{ toCamelCase .EntityName }}}Service) Edit(editReq schema.{{{ to
return
}
service.CacheUtil.RemoveCache(obj.Id)
service.Detail(obj.Id)
// service.Detail(obj.Id)
return
}

View File

@@ -67,16 +67,16 @@ func (service userProtocolService) GetModel(listReq schema.UserProtocolListReq)
func (service userProtocolService) GetUpdateMap(editReq schema.UserProtocolEditReq) map[string]interface{} {
updateMap := make(map[string]interface{})
if editReq.Tag.Valid {
if editReq.Tag.IsExists() {
updateMap["tag"] = editReq.Tag.GetValue()
}
if editReq.Title.Valid {
if editReq.Title.IsExists() {
updateMap["title"] = editReq.Title.GetValue()
}
if editReq.Content.Valid {
if editReq.Content.IsExists() {
updateMap["content"] = editReq.Content.GetValue()
}
if editReq.Version.Valid {
if editReq.Version.IsExists() {
updateMap["version"] = editReq.Version.GetValue()
}
return updateMap
@@ -180,7 +180,7 @@ func (service userProtocolService) Edit(editReq schema.UserProtocolEditReq) (e e
return
}
service.CacheUtil.RemoveCache(obj.Id)
service.Detail(obj.Id)
// service.Detail(obj.Id)
return
}

View File

@@ -3,31 +3,32 @@ package core
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"x_admin/util/convert_util"
)
// 支持前端传递nullintfloatstring类型和不传值
// 前端传1“1”都可以都转换为float64类型: NullFloat{Float: 1.0, Valid: true}
// 前端null值: NullFloat{Float: nil, Valid: true}
// 前端没传值: NullFloat{Float: nil, Valid: false}
// 前端传1“1”都可以都转换为float64类型: NullFloat{Float: 1.0, Exist: true}
// 前端null值: NullFloat{Float: nil, Exist: true}
// 前端没传值: NullFloat{Float: nil, Exist: false}
type NullFloat struct {
Val *float64
Valid bool // 是否有值
Exist bool // 是否有值
}
func DecodeFloat(value any) (any, error) {
switch v := value.(type) {
case nil:
return NullFloat{Val: nil, Valid: false}, nil
return NullFloat{Val: nil, Exist: false}, nil
case NullFloat:
return v, nil
default:
result, err := convert_util.ToFloat64(value)
if err != nil {
return NullFloat{Val: nil, Valid: false}, err
return NullFloat{Val: nil, Exist: false}, err
}
return NullFloat{Val: &result, Valid: true}, nil
return NullFloat{Val: &result, Exist: true}, nil
}
}
@@ -38,13 +39,13 @@ func (f *NullFloat) Scan(value interface{}) error {
if err != nil {
return err
}
f.Val, f.Valid = &result, true
f.Val, f.Exist = &result, true
return nil
}
// gorm实现 Valuer
func (f NullFloat) Value() (driver.Value, error) {
if !f.Valid {
if !f.Exist {
return nil, nil
}
v := f.Val
@@ -55,7 +56,7 @@ func (f NullFloat) Value() (driver.Value, error) {
}
func (f NullFloat) String() string {
if f.Valid {
if f.Exist {
return strconv.FormatFloat(*f.Val, 'f', -1, 64)
} else {
return ""
@@ -73,7 +74,7 @@ func (i *NullFloat) UnmarshalParam(param string) error {
// 实现json序列化接口
func (f NullFloat) MarshalJSON() ([]byte, error) {
if f.Valid {
if f.Exist {
return json.Marshal(f.Val)
} else {
return json.Marshal(nil)
@@ -88,48 +89,47 @@ func (f *NullFloat) UnmarshalJSON(data []byte) error {
}
switch v := x.(type) {
case nil:
f.Valid = true
f.Exist = true
return nil
case int64:
f64 := float64(v)
f.Val = &f64
f.Valid = true
f.Exist = true
return nil
case float64:
f.Val = &v
f.Valid = true
f.Exist = true
return nil
case string:
if v == "" {
f.Val = nil
f.Valid = true
f.Exist = true
return nil
}
num, err := strconv.ParseFloat(v, 64)
if err == nil {
f.Val = &num
f.Valid = true
f.Exist = true
} else {
f.Valid = false
f.Exist = false
}
return err
default:
f.Valid = false
return fmt.Errorf("不能将类型 %T 转换为 float64, 值为 %v", v, v)
}
return nil
}
func (i *NullFloat) SetValue(value float64) {
i.Val = &value
i.Valid = true
i.Exist = true
}
func (i *NullFloat) SetNull() {
i.Val = nil
i.Valid = true
i.Exist = true
}
func (i *NullFloat) IsValid() bool {
return i.Valid
func (i *NullFloat) IsExists() bool {
return i.Exist
}
func (i *NullFloat) GetValue() *float64 {
return i.Val

View File

@@ -10,27 +10,27 @@ import (
)
// 支持前端传递nullintstring类型和不传值
// 前端传1“1”都可以都转换为int64类型: NullInt{Int: 1, Valid: true}
// 前端null值: NullInt{Int: nil, Valid: true}
// 前端没传值: NullInt{Int: nil, Valid: false}
// 前端传1“1”都可以都转换为int64类型: NullInt{Int: 1, Exist: true}
// 前端null值: NullInt{Int: nil, Exist: true}
// 前端没传值: NullInt{Int: nil, Exist: false}
type NullInt struct {
Val *int64 // 整数或者null
Valid bool // 是否有值
Exist bool // 是否有值
}
func DecodeInt(value any) (any, error) {
switch v := value.(type) {
case nil:
return NullInt{Val: nil, Valid: false}, nil
return NullInt{Val: nil, Exist: false}, nil
case NullInt:
return v, nil
default:
result, err := convert_util.ToInt64(value)
if err != nil {
return NullInt{Val: nil, Valid: false}, err
return NullInt{Val: nil, Exist: false}, err
}
return NullInt{Val: &result, Valid: true}, nil
return NullInt{Val: &result, Exist: true}, nil
}
}
@@ -40,18 +40,18 @@ func (i *NullInt) Scan(value interface{}) error {
// 判断int64、string类型
switch v := value.(type) {
case nil:
i.Valid = true
i.Exist = true
return nil
case int64:
i.Val, i.Valid = &v, true
i.Val, i.Exist = &v, true
return nil
case string:
num, err := strconv.ParseInt(v, 10, 64)
if err == nil {
i.Val = &num
i.Valid = true
i.Exist = true
} else {
i.Valid = false
i.Exist = false
}
return err
default:
@@ -61,7 +61,7 @@ func (i *NullInt) Scan(value interface{}) error {
// gorm实现 Valuer
func (i NullInt) Value() (driver.Value, error) {
if !i.Valid {
if !i.Exist {
return nil, nil
}
v := i.Val
@@ -71,7 +71,7 @@ func (i NullInt) Value() (driver.Value, error) {
return *v, nil
}
func (i NullInt) String() string {
if i.Valid {
if i.Exist {
return strconv.FormatInt(*i.Val, 10)
} else {
return ""
@@ -80,7 +80,7 @@ func (i NullInt) String() string {
// 实现json序列化接口
func (i NullInt) MarshalJSON() ([]byte, error) {
if i.Valid {
if i.Exist {
return json.Marshal(i.Val)
} else {
return json.Marshal(nil)
@@ -103,52 +103,52 @@ func (i *NullInt) UnmarshalJSON(data []byte) error {
}
switch v := x.(type) {
case nil:
i.Valid = true
i.Exist = true
return nil
case int64:
i.Val = &v
i.Valid = true
i.Exist = true
return nil
case float64:
i64 := int64(v)
// 判断转换前后是否相等,防止精度丢失
if float64(i64) != v {
i.Valid = false
i.Exist = false
return errors.New("int64转换失败" + fmt.Sprintf("%f", v) + "精度丢失")
}
i.Val = &i64
i.Valid = true
i.Exist = true
return nil
case string:
if v == "" {
i.Val = nil
i.Valid = true
i.Exist = true
return nil
}
num, err := strconv.ParseInt(v, 10, 64)
if err == nil {
i.Val = &num
i.Valid = true
i.Exist = true
} else {
i.Valid = false
i.Exist = false
}
return err
default:
return fmt.Errorf("不能将类型 %T 转换为 int64", v)
return fmt.Errorf("不能将类型 %T 转换为 int64, 值为 %v", v, v)
}
return nil
}
func (i *NullInt) SetValue(value int64) {
i.Val = &value
i.Valid = true
i.Exist = true
}
func (i *NullInt) SetNull() {
i.Val = nil
i.Valid = true
i.Exist = true
}
func (i *NullInt) IsValid() bool {
return i.Valid
func (i *NullInt) IsExists() bool {
return i.Exist
}
func (i *NullInt) GetValue() *int64 {
return i.Val

View File

@@ -7,24 +7,24 @@ import (
)
// 支持前端传递nullintstring类型和不传值
// 前端传1“1”都可以都转换为int64类型: NullString{Int: "1", Valid: true}
// 前端null值: NullString{Int: nil, Valid: true}
// 前端没传值: NullString{Int: nil, Valid: false}
// 前端传1“1”都可以都转换为int64类型: NullString{Int: "1", Exist: true}
// 前端null值: NullString{Int: nil, Exist: true}
// 前端没传值: NullString{Int: nil, Exist: false}
type NullString struct {
Val *string //解析行为默认""而不是nil
Valid bool
Exist bool
}
func DecodeString(value any) (any, error) {
switch v := value.(type) {
case nil:
var s string
return NullString{Val: &s, Valid: true}, nil
return NullString{Val: &s, Exist: true}, nil
case NullString:
return v, nil
default:
result := convert_util.ToString(v)
return NullString{Val: &result, Valid: true}, nil
return NullString{Val: &result, Exist: true}, nil
}
}
@@ -35,15 +35,15 @@ func (i *NullString) Scan(value any) error {
case nil:
var s string
i.Val = &s
i.Valid = true
i.Exist = true
return nil
case string:
i.Val, i.Valid = &v, true
i.Val, i.Exist = &v, true
return nil
default:
result := convert_util.ToString(v)
i.Val, i.Valid = &result, true
i.Val, i.Exist = &result, true
return nil
// return fmt.Errorf("类型转换失败期望string类型实际类型为%T值为%v", value, value)
}
@@ -51,7 +51,7 @@ func (i *NullString) Scan(value any) error {
// gorm实现 Valuer
func (i NullString) Value() (driver.Value, error) {
if !i.Valid {
if !i.Exist {
return nil, nil
}
v := i.Val
@@ -71,7 +71,7 @@ func (i *NullString) UnmarshalParam(param string) error {
// 实现json序列化接口
func (i NullString) MarshalJSON() ([]byte, error) {
if i.Valid {
if i.Exist {
return json.Marshal(i.Val)
} else {
return json.Marshal(nil)
@@ -88,14 +88,17 @@ func (i *NullString) UnmarshalJSON(data []byte) error {
case nil:
var s string
i.Val = &s
i.Valid = true
i.Exist = true
return nil
default:
result := convert_util.ToString(v)
i.Val = &result
i.Valid = true
i.Exist = true
return nil
}
return nil
}
func (i NullString) String() string {
@@ -107,15 +110,15 @@ func (i NullString) String() string {
}
func (i *NullString) SetValue(value string) {
i.Val = &value
i.Valid = true
i.Exist = true
}
func (i *NullString) SetNull() {
var s string
i.Val = &s
i.Valid = true
i.Exist = true
}
func (i *NullString) IsValid() bool {
return i.Valid
func (i *NullString) IsExists() bool {
return i.Exist
}
func (i *NullString) GetValue() *string {
return i.Val

View File

@@ -16,7 +16,7 @@ const TimeFormat = "2006-01-02 15:04:05"
// NullTime 自定义时间格式
type NullTime struct {
Val *time.Time
Valid bool
Exist bool
// Format string
}
@@ -38,7 +38,7 @@ func (t *NullTime) UnmarshalJSON(bs []byte) error {
if date == "" {
*t = NullTime{
Val: nil,
Valid: true,
Exist: true,
}
return nil
}
@@ -48,7 +48,7 @@ func (t *NullTime) UnmarshalJSON(bs []byte) error {
}
*t = NullTime{
Val: &tt,
Valid: true,
Exist: true,
}
return nil
}
@@ -56,7 +56,7 @@ func (t *NullTime) UnmarshalJSON(bs []byte) error {
// MarshalJSON 将NullTime类型的时间转化为JSON字符串格式
// 返回转化后的JSON字符串和错误信息
func (t NullTime) MarshalJSON() ([]byte, error) {
if t.Valid {
if t.Exist {
if t.Val == nil {
return json.Marshal(nil)
}
@@ -82,7 +82,7 @@ func (t *NullTime) Scan(v any) error {
switch val := v.(type) {
case nil:
t.Val = nil
t.Valid = true
t.Exist = true
return nil
case string:
tt, err := time.ParseInLocation(TimeFormat, val, time.Local)
@@ -90,24 +90,26 @@ func (t *NullTime) Scan(v any) error {
return err
}
t.Val = &tt
t.Valid = true
t.Exist = true
return nil
case time.Time:
tt := val.Format(TimeFormat)
if tt == "0001-01-01 00:00:00" {
t.Val = nil
t.Valid = true
t.Exist = true
} else {
t.Val = &val
t.Valid = true
t.Exist = true
}
return nil
default:
return fmt.Errorf("不能将类型 %T 转换为 time.Time, 值为 %v", v, v)
}
return fmt.Errorf("NullTime cant convert %s", v)
// return fmt.Errorf("NullTime cant convert %s", v)
}
func (t NullTime) String() string {
if !t.Valid {
if !t.Exist {
return ""
}
if t.Val == nil {
@@ -133,14 +135,14 @@ func (NullTime) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}
func (i *NullTime) SetValue(value time.Time) {
i.Val = &value
i.Valid = true
i.Exist = true
}
func (i *NullTime) SetNull() {
i.Val = nil
i.Valid = true
i.Exist = true
}
func (i *NullTime) IsValid() bool {
return i.Valid
func (i *NullTime) IsExists() bool {
return i.Exist
}
func (i *NullTime) GetValue() *time.Time {
return i.Val

View File

@@ -84,7 +84,7 @@ func (t nullTimeUtil) Parse(value interface{}) (core.NullTime, error) {
func (t nullTimeUtil) ParseTime(date time.Time) core.NullTime {
return core.NullTime{
Val: &date,
Valid: true,
Exist: true,
}
}
@@ -96,7 +96,7 @@ func (t nullTimeUtil) ParseString(date string) (core.NullTime, error) {
}
return core.NullTime{
Val: &tt,
Valid: true,
Exist: true,
}, nil
}
@@ -104,7 +104,7 @@ func (t nullTimeUtil) ParseString(date string) (core.NullTime, error) {
func (t nullTimeUtil) Null() core.NullTime {
return core.NullTime{
Val: nil,
Valid: true,
Exist: true,
}
}
@@ -113,6 +113,6 @@ func (t nullTimeUtil) Now() core.NullTime {
now := time.Now()
return core.NullTime{
Val: &now,
Valid: true,
Exist: true,
}
}