2014-08-31 10:36:30 +02:00
|
|
|
// Package null provides a convenient way of handling null values.
|
|
|
|
// Types in this package consider empty or zero input the same as null input.
|
|
|
|
// Types in this package will encode to their zero value, even if null.
|
2014-08-31 10:39:31 +02:00
|
|
|
// Use the nuller subpackage if you don't want this.
|
2014-08-28 17:11:18 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2014-08-29 03:29:17 +02:00
|
|
|
// String is a nullable string.
|
2014-08-28 17:11:18 +02:00
|
|
|
type String struct {
|
|
|
|
sql.NullString
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:29:17 +02:00
|
|
|
// NewString creates a new String
|
|
|
|
func NewString(s string, valid bool) String {
|
2014-08-28 17:11:18 +02:00
|
|
|
return String{
|
|
|
|
NullString: sql.NullString{
|
|
|
|
String: s,
|
2014-08-29 04:31:09 +02:00
|
|
|
Valid: valid,
|
2014-08-28 17:11:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 10:28:34 +02:00
|
|
|
// StringFrom creates a new String that will be null if s is blank.
|
|
|
|
func StringFrom(s string) String {
|
|
|
|
return NewString(s, s != "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringFrom creates a new String that be null if s is nil or blank.
|
|
|
|
// It will make s point to the String's value.
|
|
|
|
func StringFromPtr(s *string) String {
|
|
|
|
if s == nil {
|
|
|
|
return NewString("", false)
|
|
|
|
}
|
|
|
|
str := NewString(*s, *s != "")
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:29:17 +02:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
// It supports string and null input. Blank string input produces a null String.
|
|
|
|
// It also supports unmarshalling a sql.NullString.
|
2014-08-28 17:11:18 +02:00
|
|
|
func (s *String) UnmarshalJSON(data []byte) error {
|
|
|
|
var err error
|
|
|
|
var v interface{}
|
|
|
|
json.Unmarshal(data, &v)
|
2014-08-30 16:53:25 +02:00
|
|
|
switch x := v.(type) {
|
2014-08-28 17:11:18 +02:00
|
|
|
case string:
|
2014-08-30 16:53:25 +02:00
|
|
|
s.String = x
|
2014-08-28 17:11:18 +02:00
|
|
|
case map[string]interface{}:
|
|
|
|
err = json.Unmarshal(data, &s.NullString)
|
|
|
|
case nil:
|
|
|
|
s.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-29 03:29:17 +02:00
|
|
|
s.Valid = (err == nil) && (s.String != "")
|
2014-08-28 17:11:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:29:17 +02:00
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
|
|
// It will encode a blank string when this String is null.
|
|
|
|
func (s String) MarshalText() ([]byte, error) {
|
|
|
|
if !s.Valid {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return []byte(s.String), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
// It will unmarshal to a null String if the input is a blank string.
|
|
|
|
func (s *String) UnmarshalText(text []byte) error {
|
|
|
|
s.String = string(text)
|
|
|
|
s.Valid = s.String != ""
|
|
|
|
return nil
|
2014-08-28 17:11:18 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 03:29:17 +02:00
|
|
|
// Pointer returns a pointer to this String's value, or a nil pointer if this String is null.
|
2014-08-31 10:28:34 +02:00
|
|
|
func (s String) Ptr() *string {
|
2014-08-30 16:53:25 +02:00
|
|
|
if !s.Valid {
|
2014-08-28 17:11:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &s.String
|
|
|
|
}
|
2014-08-28 17:37:35 +02:00
|
|
|
|
2014-08-29 04:12:48 +02:00
|
|
|
// IsZero returns true for null or empty strings, for future omitempty support. (Go 1.4?)
|
2014-08-28 17:37:35 +02:00
|
|
|
func (s String) IsZero() bool {
|
2014-08-29 04:09:04 +02:00
|
|
|
return !s.Valid || s.String == ""
|
2014-08-28 17:37:35 +02:00
|
|
|
}
|