lbry.go/null/string.go

118 lines
2.3 KiB
Go
Raw Normal View History

2014-08-28 17:11:18 +02:00
package null
import (
"bytes"
"database/sql/driver"
2014-08-28 17:11:18 +02:00
"encoding/json"
"gopkg.in/nullbio/null.v6/convert"
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.
2014-08-28 17:11:18 +02:00
type String struct {
String string
Valid bool
2014-08-28 17:11:18 +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{
String: s,
Valid: valid,
2014-08-28 17:11:18 +02:00
}
}
2014-08-29 03:29:17 +02:00
// UnmarshalJSON implements json.Unmarshaler.
2014-08-28 17:11:18 +02:00
func (s *String) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, NullBytes) {
s.String = ""
2014-08-28 17:11:18 +02:00
s.Valid = false
return nil
}
if err := json.Unmarshal(data, &s.String); err != nil {
return err
}
s.Valid = true
return nil
2014-08-28 17:11:18 +02:00
}
// MarshalJSON implements json.Marshaler.
func (s String) MarshalJSON() ([]byte, error) {
2014-08-29 03:29:17 +02:00
if !s.Valid {
return NullBytes, nil
2014-08-29 03:29:17 +02:00
}
return json.Marshal(s.String)
2014-08-29 03:29:17 +02:00
}
// MarshalText implements encoding.TextMarshaler.
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.
func (s *String) UnmarshalText(text []byte) error {
2016-11-11 08:15:01 +01:00
if text == nil || len(text) == 0 {
s.Valid = false
return nil
}
2014-08-29 03:29:17 +02:00
s.String = string(text)
2016-11-11 08:15:01 +01:00
s.Valid = true
2014-08-29 03:29:17 +02:00
return nil
2014-08-28 17:11:18 +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
}
// IsNull returns true for null strings, for potential future omitempty support.
func (s String) IsNull() bool {
return !s.Valid
}
// Scan implements the Scanner interface.
func (s *String) Scan(value interface{}) error {
if value == nil {
s.String, s.Valid = "", false
return nil
}
s.Valid = true
return convert.ConvertAssign(&s.String, value)
}
// Value implements the driver Valuer interface.
func (s String) Value() (driver.Value, error) {
if !s.Valid {
return nil, nil
}
return s.String, nil
}