mirror of
https://github.com/Icinga/icingadb.git
synced 2026-06-09 08:56:54 -04:00
types.Int: Implement UnmarshalText()
This commit is contained in:
parent
96835ace04
commit
564d3d594e
1 changed files with 22 additions and 4 deletions
|
|
@ -4,7 +4,9 @@ import (
|
|||
"bytes"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Int adds JSON support to sql.NullInt64.
|
||||
|
|
@ -23,6 +25,21 @@ func (i Int) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
func (i *Int) UnmarshalText(text []byte) error {
|
||||
parsed, err := strconv.ParseInt(string(text), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*i = Int{sql.NullInt64{
|
||||
Int64: parsed,
|
||||
Valid: true,
|
||||
}}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||
// Supports JSON null.
|
||||
func (i *Int) UnmarshalJSON(data []byte) error {
|
||||
|
|
@ -41,8 +58,9 @@ func (i *Int) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// Assert interface compliance.
|
||||
var (
|
||||
_ json.Marshaler = Int{}
|
||||
_ json.Unmarshaler = (*Int)(nil)
|
||||
_ driver.Valuer = Int{}
|
||||
_ sql.Scanner = (*Int)(nil)
|
||||
_ json.Marshaler = Int{}
|
||||
_ json.Unmarshaler = (*Int)(nil)
|
||||
_ encoding.TextUnmarshaler = (*Int)(nil)
|
||||
_ driver.Valuer = Int{}
|
||||
_ sql.Scanner = (*Int)(nil)
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue