-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullable_number_array.go
134 lines (120 loc) · 2.95 KB
/
nullable_number_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
package gosql
import (
"database/sql/driver"
"sort"
)
// NullableNumberArray type of field
type NullableNumberArray[T Number] []T
// Value implements the driver.Valuer interface, []int field
func (f NullableNumberArray[T]) Value() (driver.Value, error) {
if f == nil {
return nil, nil
}
return ArrayNumberEncode('{', '}', f).String(), nil
}
// Scan implements the driver.Valuer interface, []int field
func (f *NullableNumberArray[T]) Scan(value any) error {
if value == nil {
*f = nil
return nil
}
if res, err := ArrayNumberDecode[T](value, '{', '}'); err == nil {
*f = NullableNumberArray[T](res)
} else {
return err
}
return nil
}
// MarshalJSON implements the json.Marshaler
func (f NullableNumberArray[T]) MarshalJSON() ([]byte, error) {
if f == nil {
return []byte("null"), nil
}
return ArrayNumberEncode('[', ']', f).Bytes(), nil
}
// UnmarshalJSON implements the json.Unmarshaller
func (f *NullableNumberArray[T]) UnmarshalJSON(b []byte) error {
res, err := ArrayNumberDecode[T](b, '[', ']')
*f = NullableNumberArray[T](res)
return err
}
// DecodeValue implements the gocast.Decoder
func (f *NullableNumberArray[T]) DecodeValue(v any) error {
switch val := v.(type) {
case nil:
*f = nil
case []T:
*f = NullableNumberArray[T](val)
case NullableNumberArray[T]:
*f = val
case NumberArray[T]:
*f = NullableNumberArray[T](val)
case []byte, string:
list, err := ArrayNumberDecode[T](v, '[', ']')
if err != nil {
return err
}
*f = NullableNumberArray[T](list)
default:
return ErrInvalidDecodeValue
}
return nil
}
// Sort ints array
func (f NullableNumberArray[T]) Sort() NullableNumberArray[T] {
sort.Sort(f)
return f
}
// Len of array
func (s NullableNumberArray[T]) Len() int { return len(s) }
func (s NullableNumberArray[T]) Less(i, j int) bool { return s[i] < s[j] }
func (s NullableNumberArray[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// IndexOf array value
func (f NullableNumberArray[T]) IndexOf(v T) int {
if f == nil {
return -1
}
for i, vl := range f {
if vl == v {
return i
}
}
return -1
}
// OneOf value in array
func (f NullableNumberArray[T]) OneOf(vals []T) bool {
if len(f) < 1 || len(vals) < 1 {
return false
}
for _, v := range vals {
if f.IndexOf(v) != -1 {
return true
}
}
return false
}
// Ordered object
func (f NullableNumberArray[T]) Ordered() NullableOrderedNumberArray[T] {
f.Sort()
return NullableOrderedNumberArray[T](f)
}
// Filter current array and create filtered copy
func (f NullableNumberArray[T]) Filter(fn func(v T) bool) NullableNumberArray[T] {
resp := make(NullableNumberArray[T], 0, len(f))
for _, v := range f {
if fn(v) {
resp = append(resp, v)
}
}
return resp
}
// Map transforms every value into the target
func (f NullableNumberArray[T]) Map(fn func(v T) (T, bool)) NullableNumberArray[T] {
resp := make(NullableNumberArray[T], 0, len(f))
for _, v := range f {
if vl, ok := fn(v); ok {
resp = append(resp, vl)
}
}
return resp
}