lbry.go/string.go

48 lines
741 B
Go
Raw Normal View History

2014-08-28 17:11:18 +02:00
package null
import (
"database/sql"
"encoding/json"
)
type String struct {
sql.NullString
}
func StringFrom(s string) String {
return String{
NullString: sql.NullString{
String: s,
Valid: s != "",
},
}
}
func (s *String) UnmarshalJSON(data []byte) error {
var err error
var v interface{}
json.Unmarshal(data, &v)
switch v.(type) {
case string:
err = json.Unmarshal(data, &s.String)
case map[string]interface{}:
err = json.Unmarshal(data, &s.NullString)
case nil:
s.Valid = false
return nil
}
s.Valid = err == nil
return err
}
func (s String) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String)
}
func (s String) Pointer() *string {
if s.String == "" {
return nil
}
return &s.String
}