-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes-string.go
More file actions
56 lines (50 loc) · 935 Bytes
/
types-string.go
File metadata and controls
56 lines (50 loc) · 935 Bytes
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 sqljson
import (
"database/sql"
"encoding/json"
"reflect"
)
// NullString //
type NullString struct {
sql.NullString
}
// NullStringValidateValuer //
func NullStringValidateValuer(field reflect.Value) interface{} {
if nullString, ok := field.Interface().(NullString); ok {
if nullString.Valid {
return nullString.String
}
}
return nil
}
// StringPtrOrNil //
func (ns NullString) StringPtrOrNil() *string {
if ns.Valid {
s := ns.String
return &s
}
return nil
}
// MarshalJSON //
func (ns NullString) MarshalJSON() ([]byte, error) {
if ns.Valid {
return json.Marshal(ns.String)
}
return json.Marshal(nil)
}
// UnmarshalJSON //
func (ns *NullString) UnmarshalJSON(data []byte) error {
value := new(string)
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
if value != nil {
ns.String = *value
ns.Valid = true
} else {
ns.String = ""
ns.Valid = false
}
return nil
}