-
Notifications
You must be signed in to change notification settings - Fork 1
/
intarray.go
114 lines (99 loc) · 2.75 KB
/
intarray.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
package notnull
import (
"database/sql/driver"
"encoding/json"
"fmt"
"slices"
"strconv"
"strings"
)
// IntArray implements the sql.Scanner and driver.Valuer interfaces
// for a slice of int64.
// The nil default value of the slice is returned as an empty (non null) array
// for SQL and JSON.
// Use nullable.IntArray if the nil value should be treated as SQL and JSON null.
type IntArray []int64
// String implements the fmt.Stringer interface.
func (a IntArray) String() string {
var b strings.Builder
b.WriteByte('[')
for i := range a {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(strconv.FormatInt(a[i], 10))
}
b.WriteByte(']')
return b.String()
}
// Contains reports if the passed value is present in a.
func (a IntArray) Contains(value int64) bool {
return slices.Contains(a, value)
}
// Value implements the database/sql/driver.Valuer interface
func (a IntArray) Value() (driver.Value, error) {
var b strings.Builder
b.WriteByte('{')
for i := range a {
if i > 0 {
b.WriteByte(',')
}
b.WriteString(strconv.FormatInt(a[i], 10))
}
b.WriteByte('}')
return b.String(), nil
}
// Scan implements the sql.Scanner interface.
func (a *IntArray) Scan(src any) error {
switch src := src.(type) {
case []byte:
return a.scanBytes(src)
case string:
return a.scanBytes([]byte(src))
case nil:
*a = nil
return nil
}
return fmt.Errorf("can't convert %T to notnull.IntArray", src)
}
func (a *IntArray) scanBytes(src []byte) (err error) {
if len(src) == 0 {
*a = nil
return nil
}
if src[0] != '{' || src[len(src)-1] != '}' {
return fmt.Errorf("can't parse %q as notnull.IntArray", string(src))
}
if len(src) == 2 { // src == "{}"
*a = nil
return nil
}
elements := strings.Split(string(src[1:len(src)-1]), ",")
newArray := make(IntArray, len(elements))
for i, elem := range elements {
newArray[i], err = strconv.ParseInt(elem, 10, 64)
if err != nil {
return fmt.Errorf("can't parse %q as notnull.IntArray because of: %w", string(src), err)
}
}
*a = newArray
return nil
}
// MarshalJSON returns a as the JSON encoding of a.
// MarshalJSON implements encoding/json.Marshaler.
func (a IntArray) MarshalJSON() ([]byte, error) {
if len(a) == 0 {
return []byte("[]"), nil
}
return json.Marshal([]int64(a))
}
// Len is the number of elements in the collection.
// One of the methods to implement sort.Interface.
func (a IntArray) Len() int { return len(a) }
// Less reports whether the element with
// index i should sort before the element with index j.
// One of the methods to implement sort.Interface.
func (a IntArray) Less(i, j int) bool { return a[i] < a[j] }
// Swap swaps the elements with indexes i and j.
// One of the methods to implement sort.Interface.
func (a IntArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }