You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devops/util/dts/null_uint.go

48 lines
755 B

3 months ago
package dts
import (
"database/sql"
"database/sql/driver"
"encoding/json"
)
type NullUint struct {
V uint
Valid bool
}
func (nu *NullUint) Scan(value any) error {
sn := new(sql.Null[uint])
err := sn.Scan(value)
nu.V = sn.V
nu.Valid = sn.Valid
return err
}
func (nu NullUint) Value() (driver.Value, error) {
if !nu.Valid {
return nil, nil
}
// 注意:driver.Value 不支持 uint
return int64(nu.V), nil
}
func (nu NullUint) MarshalJSON() ([]byte, error) {
if nu.Valid {
return json.Marshal(nu.V)
}
return json.Marshal(nil)
}
func (nu *NullUint) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
nu.Valid = false
return nil
}
err := json.Unmarshal(b, &nu.V)
if err == nil {
nu.Valid = true
}
return err
}