-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
108 lines (93 loc) · 2.17 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
package nullable
// Do not modify. Generated by nullable-generate.
import (
"bytes"
"database/sql"
"database/sql/driver"
"encoding/json"
)
// String represents a string value that may be null.
// This type implements the Scanner interface so it
// can be used as a scan destination, similar to NullString.
// It also implements the necessary interfaces to serialize
// to and from JSON.
type String struct {
String string
Valid bool
}
// StringFromPtr returns a String whose value matches ptr.
func StringFromPtr(ptr *string) String {
var v String
return v.Assign(ptr)
}
// Assign the value of the pointer. If the pointer is nil,
// then then Valid is false, otherwise Valid is true.
func (s *String) Assign(ptr *string) String {
if ptr == nil {
s.Valid = false
s.String = ""
} else {
s.Valid = true
s.String = *ptr
}
return *s
}
// Ptr returns a pointer to string. If Valid is false
// then the pointer is nil, otherwise it is non-nil.
func (s String) Ptr() *string {
if s.Valid {
v := s.String
return &v
}
return nil
}
// Normalized returns a String that can be compared with
// another String for equality.
func (s String) Normalized() String {
if s.Valid {
return s
}
// If !Valid, then String could be any value.
// Normalized value can be compared for equality.
return String{}
}
// Scan implements the sql.Scanner interface.
func (s *String) Scan(value interface{}) error {
var nt sql.NullString
err := nt.Scan(value)
if err != nil {
return err
}
s.Valid = nt.Valid
s.String = nt.String
return nil
}
// Value implements the driver.Valuer interface.
func (s String) Value() (driver.Value, error) {
if !s.Valid {
return nil, nil
}
return s.String, nil
}
// MarshalJSON implements the json.Marshaler interface.
func (s String) MarshalJSON() ([]byte, error) {
if s.Valid {
return json.Marshal(s.String)
}
return []byte("null"), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *String) UnmarshalJSON(p []byte) error {
if bytes.Equal(p, jsonNull) {
s.String = ""
s.Valid = false
return nil
}
var v string
if err := json.Unmarshal(p, &v); err != nil {
return err
}
s.String = v
s.Valid = true
return nil
}