Add NullBool type
This commit is contained in:
parent
d413226d4d
commit
e2ba99c511
1 changed files with 30 additions and 0 deletions
30
types.go
30
types.go
|
@ -12,6 +12,36 @@ import (
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type NullBool struct {
|
||||||
|
sql.NullBool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *NullBool) MarshalJSON() ([]byte, error) {
|
||||||
|
if !b.Valid {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
return json.Marshal(b.Bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NullBool) UnmarshalJSON(b []byte) error {
|
||||||
|
if bytes.Equal(b, []byte("null")) {
|
||||||
|
n.Bool = false
|
||||||
|
n.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var x interface{}
|
||||||
|
var err error
|
||||||
|
json.Unmarshal(b, &x)
|
||||||
|
switch x.(type) {
|
||||||
|
case bool:
|
||||||
|
err = json.Unmarshal(b, &n.Bool)
|
||||||
|
case map[string]interface{}:
|
||||||
|
err = json.Unmarshal(b, &n.NullBool)
|
||||||
|
}
|
||||||
|
n.Valid = true
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
type NullString struct {
|
type NullString struct {
|
||||||
sql.NullString
|
sql.NullString
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue