29 lines
452 B
Go
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
|
|
}
|