Merge pull request #1 from joelkek/master

UnmarshalJSON should return an error for invalid types
This commit is contained in:
Greg 2014-09-09 12:21:16 +09:00
commit b550c62e10
5 changed files with 16 additions and 0 deletions

View file

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

View file

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

BIN
gin-bin Normal file

Binary file not shown.

4
int.go
View file

@ -3,6 +3,8 @@ package null
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"reflect"
"strconv" "strconv"
) )
@ -52,6 +54,8 @@ func (i *Int) UnmarshalJSON(data []byte) error {
case nil: case nil:
i.Valid = false i.Valid = false
return nil 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 i.Valid = err == nil
return err return err

View file

@ -6,6 +6,8 @@ package null
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"reflect"
) )
// String is an even nuller nullable string. // String is an even nuller nullable string.
@ -51,6 +53,8 @@ func (s *String) UnmarshalJSON(data []byte) error {
case nil: case nil:
s.Valid = false s.Valid = false
return nil return nil
default:
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name())
} }
s.Valid = (err == nil) && (s.String != "") s.Valid = (err == nil) && (s.String != "")
return err return err