-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
68 lines (61 loc) · 1.29 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
package null
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
// String represents a string that may be null.
// String implements the Scanner interface so
// it can be used as a scan destination.
// String implements the Marshaller interface
// so it can be used to read and write null
// values.
type String struct {
String string
Valid bool // Valid is true if String is not ""
}
func (nt *String) Scan(value interface{}) error {
if value == nil {
nt.String = ""
nt.Valid = false
return nil
}
switch v := value.(type) {
case string:
nt.String = v
nt.Valid = nt.String != ""
case []byte:
nt.String = string(v)
nt.Valid = nt.String != ""
default:
return fmt.Errorf("unable to scan into null.String from type %T", value)
}
return nil
}
func (nt String) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.String, nil
}
func (nt String) MarshalJSON() ([]byte, error) {
if !nt.Valid {
return json.Marshal(nil)
}
return json.Marshal(nt.String)
}
func (nt *String) UnmarshalJSON(data []byte) error {
if data == nil {
nt.String = ""
} else if err := json.Unmarshal(data, &nt.String); err != nil {
return err
}
nt.Valid = nt.String != ""
return nil
}
func NewString(t string) String {
return String{
String: t,
Valid: t != "",
}
}