-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.go
56 lines (45 loc) · 1.14 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
package sqle
import (
"database/sql/driver"
"encoding/json"
)
type String struct {
Null[string]
}
func NewString(s string) String {
return String{Null: NewNull(s, true)}
}
// Scan implements the [sql.Scanner] interface.
func (t *String) Scan(value any) error { // skipcq: GO-W1029
return t.Null.Scan(value)
}
// Value implements the [driver.Valuer] interface.
func (t String) Value() (driver.Value, error) { // skipcq: GO-W1029
return t.Null.Value()
}
// Time returns the underlying time.Time value of the Time struct.
func (t *String) String() string { // skipcq: GO-W1029
return t.TValue()
}
// MarshalJSON implements the json.Marshaler interface
func (t String) MarshalJSON() ([]byte, error) { // skipcq: GO-W1029
if t.Valid {
return json.Marshal(t.TValue())
}
return nullJsonBytes, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *String) UnmarshalJSON(data []byte) error { // skipcq: GO-W1029
if len(data) == 0 || string(data) == nullJson {
t.Null.Valid = false
return nil
}
var v string
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
t.Null.V = v
t.Null.Valid = true
return nil
}