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"
"encoding/json"
"errors"
"fmt"
"reflect"
)
// Bool is an even nuller nullable bool.
@ -52,6 +54,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", reflect.TypeOf(v).Name())
}
b.Valid = err == nil
return err

View file

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

View file

@ -6,6 +6,8 @@ package null
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
)
// String is an even nuller nullable string.
@ -51,6 +53,8 @@ func (s *String) UnmarshalJSON(data []byte) error {
case nil:
s.Valid = false
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 != "")
return err