-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_array.go
235 lines (206 loc) · 5.09 KB
/
string_array.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// @project GeniusRabbit
// @author Dmitry Ponomarev <[email protected]> 2016, 2020 – 2021
//
package gosql
import (
"bytes"
"database/sql/driver"
"encoding/json"
"strings"
)
// NullableStringArray implementation
type NullableStringArray []string
// Join array to string
func (f NullableStringArray) Join(sep string) string {
if f == nil {
return ""
}
return strings.Join(f, sep)
}
// SetArray value
func (f *NullableStringArray) SetArray(arr []string) *NullableStringArray {
*f = arr
return f
}
// Value implements the driver.Valuer interface, []string field
func (f NullableStringArray) Value() (driver.Value, error) {
if f == nil {
return nil, nil
}
return encodeNullableStringArray('{', '}', '"', `""`, f).String(), nil
}
// Scan implements the driver.Valuer interface, []string field
func (f *NullableStringArray) Scan(value any) error {
switch val := value.(type) {
case []byte:
*f = decodeNullableStringArray(string(val), '{', '}', '"', `""`)
case string:
*f = decodeNullableStringArray(val, '{', '}', '"', `""`)
case nil:
*f = nil
}
return nil
}
// MarshalJSON implements the json.Marshaler
func (f NullableStringArray) MarshalJSON() ([]byte, error) {
if f == nil {
return []byte("null"), nil
}
return json.Marshal([]string(f))
}
// UnmarshalJSON implements the json.Unmarshaller
func (f *NullableStringArray) UnmarshalJSON(b []byte) error {
var list []string
if err := json.Unmarshal(b, &list); err != nil {
return err
}
*f = list
return nil
}
// DecodeValue implements the gocast.Decoder
func (f *NullableStringArray) DecodeValue(v any) error {
switch val := v.(type) {
case []string:
*f = NullableStringArray(val)
case NullableStringArray:
*f = val
case StringArray:
*f = NullableStringArray(val)
case []byte:
*f = NullableStringArray(strings.Split(string(val), ","))
case string:
*f = NullableStringArray(strings.Split(val, ","))
default:
return ErrInvalidDecodeValue
}
return nil
}
// Len of array
func (f NullableStringArray) Len() int {
return len(f)
}
// IndexOf array value
func (f NullableStringArray) IndexOf(v string) int {
if f == nil {
return -1
}
for i, vl := range f {
if vl == v {
return i
}
}
return -1
}
// OneOf value in array
func (f NullableStringArray) OneOf(vals []string) bool {
if len(f) < 1 || len(vals) < 1 {
return false
}
for _, v := range vals {
if f.IndexOf(v) != -1 {
return true
}
}
return false
}
///////////////////////////////////////////////////////////////////////////////
// StringArray implementation
type StringArray NullableStringArray
// Join array to string
func (f StringArray) Join(sep string) string {
return NullableStringArray(f).Join(sep)
}
// SetArray value
func (f *StringArray) SetArray(arr []string) *StringArray {
if arr == nil {
arr = []string{}
}
*f = arr
return f
}
// Value implements the driver.Valuer interface, []string field
func (f StringArray) Value() (driver.Value, error) {
if f == nil {
return "{}", ErrNullValueNotAllowed
}
return NullableStringArray(f).Value()
}
// Scan implements the driver.Valuer interface, []string field
func (f *StringArray) Scan(value any) error {
if value == nil {
return ErrNullValueNotAllowed
}
return (*NullableStringArray)(f).Scan(value)
}
// UnmarshalJSON implements the json.Unmarshaller
func (f *StringArray) UnmarshalJSON(b []byte) error {
if b == nil {
return ErrNullValueNotAllowed
}
return (*NullableStringArray)(f).UnmarshalJSON(b)
}
// DecodeValue implements the gocast.Decoder
func (f *StringArray) DecodeValue(v any) error {
if v == nil {
return ErrNullValueNotAllowed
}
return (*NullableStringArray)(f).DecodeValue(v)
}
// Len of array
func (f StringArray) Len() int {
return len(f)
}
// IndexOf array value
func (f StringArray) IndexOf(v string) int {
return (NullableStringArray)(f).IndexOf(v)
}
// OneOf value in array
func (f StringArray) OneOf(vals []string) bool {
return (NullableStringArray)(f).OneOf(vals)
}
///////////////////////////////////////////////////////////////////////////////
/// Helpers
///////////////////////////////////////////////////////////////////////////////
func decodeNullableStringArray(arrSrc string, begin, end, border byte, escape string) []string {
if strings.EqualFold(arrSrc, "null") {
return nil
}
if arrSrc == string([]byte{begin, end}) {
return []string{}
}
arr := strings.Split(strings.Trim(arrSrc, "{}[]"), ",")
for i, val := range arr {
val = strings.TrimSpace(val)
if val == string([]byte{border, border}) {
arr[i] = ""
} else {
if strings.HasPrefix(val, `"`) && strings.HasSuffix(val, `"`) {
val = val[1 : len(val)-1]
}
arr[i] = strings.ReplaceAll(val, escape, string(border))
}
}
return arr
}
func encodeNullableStringArray(begin, end, border byte, escape string, arr []string) *bytes.Buffer {
var buff bytes.Buffer
buff.WriteByte(begin)
for i, v := range arr {
if i > 0 {
buff.WriteByte(',')
}
if byte(0) != border {
buff.WriteByte(border)
if escape != "" {
v = strings.ReplaceAll(v, string(border), escape)
}
}
buff.WriteString(v)
if byte(0) != border {
buff.WriteByte(border)
}
}
buff.WriteByte(end)
return &buff
}