-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullable_ordered_array.go
87 lines (74 loc) · 2.38 KB
/
nullable_ordered_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
package gosql
import (
"database/sql/driver"
"sort"
)
// NullableOrderedNumberArray[T Number] type of field
type NullableOrderedNumberArray[T Number] []T
// Value implements the driver.Valuer interface, []int field
func (f NullableOrderedNumberArray[T]) Value() (driver.Value, error) {
if f == nil {
return nil, nil
}
return NullableNumberArray[T](f).Value()
}
// Scan implements the driver.Valuer interface, []int field
func (f *NullableOrderedNumberArray[T]) Scan(value any) error {
if value == nil {
*f = nil
return nil
}
return (*NullableNumberArray[T])(f).Scan(value)
}
// UnmarshalJSON implements the json.Unmarshaller
func (f *NullableOrderedNumberArray[T]) UnmarshalJSON(b []byte) error {
if b == nil {
return ErrNullValueNotAllowed
}
return (*NullableNumberArray[T])(f).UnmarshalJSON(b)
}
// MarshalJSON implements the json.Marshaler
func (f NullableOrderedNumberArray[T]) MarshalJSON() ([]byte, error) {
return (NullableNumberArray[T](f)).MarshalJSON()
}
// DecodeValue implements the gocast.Decoder
func (f *NullableOrderedNumberArray[T]) DecodeValue(v any) error {
return (*NullableNumberArray[T])(f).DecodeValue(v)
}
// Sort ints array
func (f NullableOrderedNumberArray[T]) Sort() NullableOrderedNumberArray[T] {
sort.Sort(f)
return f
}
// Len of array
func (s NullableOrderedNumberArray[T]) Len() int { return len(s) }
func (s NullableOrderedNumberArray[T]) Less(i, j int) bool { return s[i] < s[j] }
func (s NullableOrderedNumberArray[T]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// IndexOf array value
func (f NullableOrderedNumberArray[T]) IndexOf(v T) int {
i := sort.Search(f.Len(), func(i int) bool { return f[i] >= v })
if i >= 0 && i < f.Len() && f[i] == v {
return i
}
return -1
}
// OneOf value in array
func (f NullableOrderedNumberArray[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
}
// Filter current array and create filtered copy
func (f NullableOrderedNumberArray[T]) Filter(fn func(v T) bool) NullableOrderedNumberArray[T] {
return NullableOrderedNumberArray[T](NullableNumberArray[T](f).Filter(fn))
}
// Map transforms every value into the target
func (f NullableOrderedNumberArray[T]) Map(fn func(v T) (T, bool)) NullableOrderedNumberArray[T] {
return NullableOrderedNumberArray[T](NullableNumberArray[T](f).Map(fn))
}