2015-09-10 13:05:24 +02:00
|
|
|
// Package null contains SQL types that consider zero input and null input as separate values,
|
|
|
|
// with convenient support for JSON and text marshaling.
|
2014-09-01 20:22:17 +02:00
|
|
|
// Types in this package will always encode to their null value if null.
|
2015-09-10 13:05:24 +02:00
|
|
|
// Use the zero subpackage if you want zero values and null to be treated the same.
|
2014-08-28 17:11:18 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2014-09-08 18:55:40 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-08-28 17:11:18 +02:00
|
|
|
)
|
|
|
|
|
2015-01-21 03:33:38 +01:00
|
|
|
// String is a nullable string. It supports SQL and JSON serialization.
|
|
|
|
// It will marshal to null if null. Blank string input will be considered null.
|
2014-08-28 17:11:18 +02:00
|
|
|
type String struct {
|
|
|
|
sql.NullString
|
|
|
|
}
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
// StringFrom creates a new String that will never be blank.
|
|
|
|
func StringFrom(s string) String {
|
|
|
|
return NewString(s, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringFromPtr creates a new String that be null if s is nil.
|
|
|
|
func StringFromPtr(s *string) String {
|
|
|
|
if s == nil {
|
|
|
|
return NewString("", false)
|
|
|
|
}
|
|
|
|
return NewString(*s, true)
|
|
|
|
}
|
|
|
|
|
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-29 03:29:17 +02:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2015-10-02 07:42:03 +02:00
|
|
|
// It supports string and null input. Blank string input does not produce a null String.
|
2014-08-29 03:29:17 +02:00
|
|
|
// 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{}
|
2015-09-10 13:05:24 +02:00
|
|
|
if err = json.Unmarshal(data, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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-09-08 18:55:40 +02:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name())
|
2014-08-28 17:11:18 +02:00
|
|
|
}
|
2015-10-02 07:42:03 +02:00
|
|
|
s.Valid = err == nil
|
2014-08-28 17:11:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
// It will encode null if this String is null.
|
|
|
|
func (s String) MarshalJSON() ([]byte, error) {
|
2014-08-29 03:29:17 +02:00
|
|
|
if !s.Valid {
|
2014-09-01 20:22:17 +02:00
|
|
|
return []byte("null"), nil
|
2014-08-29 03:29:17 +02:00
|
|
|
}
|
2014-09-01 20:22:17 +02:00
|
|
|
return json.Marshal(s.String)
|
2014-08-29 03:29:17 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 01:53:16 +01: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
|
|
|
|
}
|
|
|
|
|
2014-08-29 03:29:17 +02:00
|
|
|
// 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-09-01 07:25:57 +02:00
|
|
|
// SetValid changes this String's value and also sets it to be non-null.
|
|
|
|
func (s *String) SetValid(v string) {
|
|
|
|
s.String = v
|
|
|
|
s.Valid = true
|
|
|
|
}
|
|
|
|
|
2014-08-31 10:58:34 +02:00
|
|
|
// Ptr 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
|
|
|
|
2016-02-28 01:32:36 +01:00
|
|
|
// IsZero returns true for null strings, for potential future omitempty support.
|
2014-08-28 17:37:35 +02:00
|
|
|
func (s String) IsZero() bool {
|
2014-09-01 20:22:17 +02:00
|
|
|
return !s.Valid
|
2014-08-28 17:37:35 +02:00
|
|
|
}
|