Debian Codegraph @

string.go

raw symbols SCIP
1 // Package null contains SQL types that consider zero input and null input as separate values,
2 // with convenient support for JSON and text marshaling.
3 // Types in this package will always encode to their null value if null.
4 // Use the zero subpackage if you want zero values and null to be treated the same.
5 package null
6
7 import (
8 "database/sql"
9 "encoding/json"
10 "fmt"
11 "reflect"
12 )
13
14 // String is a nullable string. It supports SQL and JSON serialization.
15 // It will marshal to null if null. Blank string input will be considered null.
16 type String struct {
17 sql.NullString
18 }
19
20 // StringFrom creates a new String that will never be blank.
21 func StringFrom(s string) String {
22 return NewString(s, true)
23 }
24
25 // StringFromPtr creates a new String that be null if s is nil.
26 func StringFromPtr(s *string) String {
27 if s == nil {
28 return NewString("", false)
29 }
30 return NewString(*s, true)
31 }
32
33 // ValueOrZero returns the inner value if valid, otherwise zero.
34 func (s String) ValueOrZero() string {
35 if !s.Valid {
36 return ""
37 }
38 return s.String
39 }
40
41 // NewString creates a new String
42 func NewString(s string, valid bool) String {
43 return String{
44 NullString: sql.NullString{
45 String: s,
46 Valid: valid,
47 },
48 }
49 }
50
51 // UnmarshalJSON implements json.Unmarshaler.
52 // It supports string and null input. Blank string input does not produce a null String.
53 // It also supports unmarshalling a sql.NullString.
54 func (s *String) UnmarshalJSON(data []byte) error {
55 var err error
56 var v interface{}
57 if err = json.Unmarshal(data, &v); err != nil {
58 return err
59 }
60 switch x := v.(type) {
61 case string:
62 s.String = x
63 case map[string]interface{}:
64 err = json.Unmarshal(data, &s.NullString)
65 case nil:
66 s.Valid = false
67 return nil
68 default:
69 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name())
70 }
71 s.Valid = err == nil
72 return err
73 }
74
75 // MarshalJSON implements json.Marshaler.
76 // It will encode null if this String is null.
77 func (s String) MarshalJSON() ([]byte, error) {
78 if !s.Valid {
79 return []byte("null"), nil
80 }
81 return json.Marshal(s.String)
82 }
83
84 // MarshalText implements encoding.TextMarshaler.
85 // It will encode a blank string when this String is null.
86 func (s String) MarshalText() ([]byte, error) {
87 if !s.Valid {
88 return []byte{}, nil
89 }
90 return []byte(s.String), nil
91 }
92
93 // UnmarshalText implements encoding.TextUnmarshaler.
94 // It will unmarshal to a null String if the input is a blank string.
95 func (s *String) UnmarshalText(text []byte) error {
96 s.String = string(text)
97 s.Valid = s.String != ""
98 return nil
99 }
100
101 // SetValid changes this String's value and also sets it to be non-null.
102 func (s *String) SetValid(v string) {
103 s.String = v
104 s.Valid = true
105 }
106
107 // Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
108 func (s String) Ptr() *string {
109 if !s.Valid {
110 return nil
111 }
112 return &s.String
113 }
114
115 // IsZero returns true for null strings, for potential future omitempty support.
116 func (s String) IsZero() bool {
117 return !s.Valid
118 }
119
120 // Equal returns true if both strings have the same value or are both null.
121 func (s String) Equal(other String) bool {
122 return s.Valid == other.Valid && (!s.Valid || s.String == other.String)
123 }