UnmarshalJSON in null.Bool, null.Float, and null.String now return an error message when asked to unmarshal from an invalid type. This is in line with the behavior of the built-in sql library's null values.
This commit is contained in:
parent
7cccb14361
commit
f5cd2b14cc
3 changed files with 12 additions and 3 deletions
7
bool.go
7
bool.go
|
@ -4,7 +4,8 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
// "fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bool is an even nuller nullable bool.
|
// Bool is an even nuller nullable bool.
|
||||||
|
@ -53,8 +54,8 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
|
||||||
case nil:
|
case nil:
|
||||||
b.Valid = false
|
b.Valid = false
|
||||||
return nil
|
return nil
|
||||||
// default:
|
default:
|
||||||
// err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", v.(type).Name())
|
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
|
||||||
|
|
4
float.go
4
float.go
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue