Files
blog-server/internal/pkg/validator/null_int32.go
2026-07-22 17:50:33 +08:00

29 lines
452 B
Go

package validator
import "encoding/json"
// NullInt32 Valid(false) + Set(true) = null | Valid(false) + Set(false) = nil
type NullInt32 struct {
Value int32
Valid bool
Set bool
}
func (i *NullInt32) UnmarshalJSON(data []byte) error {
i.Set = true
if string(data) == "null" {
i.Valid = false
return nil
}
var temp int32
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
i.Value = temp
i.Valid = true
return nil
}