Introduce String

This commit is contained in:
Alexander A. Klimov 2021-03-12 15:11:50 +01:00
parent e4539d90fc
commit e91f2a7aa5

48
pkg/types/string.go Normal file
View file

@ -0,0 +1,48 @@
package types
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
)
// String adds JSON support to sql.NullString.
type String struct {
sql.NullString
}
// MarshalJSON implements the json.Marshaler interface.
// Supports JSON null.
func (s String) MarshalJSON() ([]byte, error) {
var v interface{}
if s.Valid {
v = s.String
}
return json.Marshal(v)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// Supports JSON null.
func (s *String) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if bytes.HasPrefix(data, []byte{'n'}) {
return nil
}
err := json.Unmarshal(data, &s.String)
if err == nil {
s.Valid = true
}
return err
}
// Assert interface compliance.
var (
_ json.Marshaler = String{}
_ json.Unmarshaler = (*String)(nil)
_ driver.Valuer = String{}
_ sql.Scanner = (*String)(nil)
)