UnmarshalJSON in Int now returns an error message when asked to unmarshal from an invalid type. This is in line with the behavior of the built-in sql.NullInt64.

This commit is contained in:
Joel Kek 2014-09-09 00:52:08 +08:00
parent 46b452db55
commit 7cccb14361
3 changed files with 7 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"errors"
// "fmt"
)
// Bool is an even nuller nullable bool.
@ -52,6 +53,8 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
case nil:
b.Valid = false
return nil
// default:
// err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", v.(type).Name())
}
b.Valid = err == nil
return err

BIN
gin-bin Normal file

Binary file not shown.

4
int.go
View file

@ -3,6 +3,8 @@ package null
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"strconv"
)
@ -52,6 +54,8 @@ func (i *Int) UnmarshalJSON(data []byte) error {
case nil:
i.Valid = false
return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Int", reflect.TypeOf(v).Name())
}
i.Valid = err == nil
return err