forked from volatiletech/null
-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
136 lines (116 loc) · 2.7 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package null
import (
"bytes"
"database/sql/driver"
"encoding/json"
"github.com/metricsglobal/null/convert"
"github.com/volatiletech/randomize"
)
// String is a nullable string. It supports SQL and JSON serialization.
type String struct {
String string
Valid bool
}
// 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)
}
// NewString creates a new String
func NewString(s string, valid bool) String {
return String{
String: s,
Valid: valid,
}
}
// UnmarshalJSON implements json.Unmarshaler.
func (s *String) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, NullBytes) {
s.String = ""
s.Valid = false
return nil
}
if err := json.Unmarshal(data, &s.String); err != nil {
return err
}
s.Valid = true
return nil
}
// MarshalJSON implements json.Marshaler.
func (s String) MarshalJSON() ([]byte, error) {
if !s.Valid {
return NullBytes, nil
}
return json.Marshal(s.String)
}
// MarshalText implements encoding.TextMarshaler.
func (s String) MarshalText() ([]byte, error) {
if !s.Valid {
return []byte{}, nil
}
return []byte(s.String), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (s *String) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
s.Valid = false
return nil
}
s.String = string(text)
s.Valid = true
return nil
}
// 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
}
// Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
func (s String) Ptr() *string {
if !s.Valid {
return nil
}
return &s.String
}
// IsZero returns true for null strings, for potential future omitempty support.
func (s String) IsZero() 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
}
// Randomize for sqlboiler
func (s *String) Randomize(nextInt func() int64, fieldType string, shouldBeNull bool) {
str, ok := randomize.FormattedString(nextInt, fieldType)
if ok {
s.String = str
s.Valid = true
return
}
if shouldBeNull {
s.String = ""
s.Valid = false
} else {
s.String = randomize.Str(nextInt, 1)
s.Valid = true
}
}