-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathint64_array.go
More file actions
99 lines (82 loc) · 1.92 KB
/
int64_array.go
File metadata and controls
99 lines (82 loc) · 1.92 KB
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
package pq_types
import (
"database/sql"
"database/sql/driver"
"fmt"
"sort"
"strconv"
"strings"
)
// Int64Array is a slice of int64 values, compatible with PostgreSQL's bigint[].
type Int64Array []int64
func (a Int64Array) Len() int { return len(a) }
func (a Int64Array) Less(i, j int) bool { return a[i] < a[j] }
func (a Int64Array) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Value implements database/sql/driver Valuer interface.
func (a Int64Array) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
s := make([]string, len(a))
for i, v := range a {
s[i] = strconv.Itoa(int(v))
}
return []byte("{" + strings.Join(s, ",") + "}"), nil
}
// Scan implements database/sql Scanner interface.
func (a *Int64Array) Scan(value interface{}) error {
if value == nil {
*a = nil
return nil
}
var b []byte
switch v := value.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
return fmt.Errorf("Int64Array.Scan: expected []byte or string, got %T (%q)", value, value)
}
if len(b) < 2 || b[0] != '{' || b[len(b)-1] != '}' {
return fmt.Errorf("Int64Array.Scan: unexpected data %q", b)
}
p := strings.Split(string(b[1:len(b)-1]), ",")
// reuse underlying array if present
if *a == nil {
*a = make(Int64Array, 0, len(p))
}
*a = (*a)[:0]
for _, s := range p {
if s == "" {
continue
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*a = append(*a, int64(i))
}
return nil
}
// EqualWithoutOrder returns true if two int64 arrays are equal without order, false otherwise.
// It may sort both arrays in-place to do so.
func (a Int64Array) EqualWithoutOrder(b Int64Array) bool {
if len(a) != len(b) {
return false
}
sort.Sort(a)
sort.Sort(b)
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// check interfaces
var (
_ sort.Interface = Int64Array{}
_ driver.Valuer = Int64Array{}
_ sql.Scanner = &Int64Array{}
)