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