-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
143 lines (117 loc) · 2.67 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
137
138
139
140
141
142
143
/*
Copyright © Portalnesia <support@portalnesia.com>
*/
package nullable
import (
"bytes"
"database/sql"
"database/sql/driver"
"go.mongodb.org/mongo-driver/bson"
"reflect"
"encoding/json"
"gopkg.in/guregu/null.v4"
)
// String represents a string that may be null or not
// present in json at all.
type String struct {
Present bool // Present is true if key is present in json
Valid bool // Valid is true if value is not null and valid string
Data string
}
func NewString(data string, presentValid ...bool) String {
d := String{Present: true, Valid: true, Data: data}
if len(presentValid) > 0 {
d.Present = presentValid[0]
d.Valid = false
if len(presentValid) > 1 {
d.Valid = presentValid[1]
}
}
return d
}
func NewStringPtr(data string, presentValid ...bool) *String {
d := NewString(data, presentValid...)
return &d
}
func (s String) Null() null.String {
return null.NewString(s.Data, s.Present && s.Valid && s.Data != "")
}
func (s String) Ptr() *string {
if s.Valid {
return &s.Data
}
return nil
}
// sql.Value interface
func (s *String) Scan(value interface{}) error {
s.Present = true
var i sql.NullString
if err := i.Scan(value); err != nil {
return err
}
s.Valid = i.Valid
s.Data = i.String
return nil
}
// sql.Value interface
func (s String) Value() (driver.Value, error) {
if !s.Valid {
return nil, nil
}
return s.Data, nil
}
// MarshalJSON implements json.Marshaler interface.
func (s String) MarshalJSON() ([]byte, error) {
if !s.Present {
return []byte(`null`), nil
} else if !s.Valid {
return []byte("null"), nil
}
return json.Marshal(s.Data)
}
// UnmarshalJSON implements json.Marshaler interface.
func (s *String) UnmarshalJSON(data []byte) error {
s.Present = true
if bytes.Equal(data, []byte("null")) {
return nil
}
if bytes.Equal(data, []byte(`""`)) {
return nil
}
if err := json.Unmarshal(data, &s.Data); err != nil {
return nil
}
s.Valid = true
return nil
}
// MarshalBSON implements bson.Marshaler interface.
func (s String) MarshalBSON() (byt []byte, err error) {
var tmp *string
_, byt, err = bson.MarshalValue(tmp)
if !s.Present {
return byt, err
} else if !s.Valid {
return byt, err
}
_, byt, err = bson.MarshalValue(s.Data)
return byt, err
}
// UnmarshalBSON implements bson.Marshaler interface.
func (s *String) UnmarshalBSON(data []byte) error {
s.Present = true
if bytes.Equal(data, []byte("null")) {
return nil
}
if bytes.Equal(data, []byte(`""`)) {
return nil
}
if err := bson.Unmarshal(data, &s.Data); err != nil {
return nil
}
s.Valid = true
return nil
}
func (String) FiberConverter(value string) reflect.Value {
a := NewString(value, true, true)
return reflect.ValueOf(a)
}