-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
54 lines (50 loc) · 1.01 KB
/
array.go
File metadata and controls
54 lines (50 loc) · 1.01 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
package pq_array
import (
"bytes"
"database/sql/driver"
"fmt"
"strconv"
"strings"
)
type IntArray []int
// Scan implements the Scanner interface.
func (a *IntArray) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok || len(b) < 2 || b[0] != '{' || b[len(b)-1] != '}' {
return fmt.Errorf("Invalid value: %v", value)
}
// empty array
if len(b) == 2 {
*a = make(IntArray, 0)
return nil
}
nums := strings.Split(string(b[1:len(b)-1]), ",")
*a = make(IntArray, len(nums))
for i, s := range nums {
var err error
if (*a)[i], err = strconv.Atoi(s); err != nil {
return err
}
}
return nil
}
// Value implements the driver Valuer interface.
// output format is like '{1,2,3,4}'
func (a IntArray) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
var buf bytes.Buffer
buf.WriteString("{")
for i, num := range a {
if i != 0 {
buf.WriteRune(',')
}
buf.WriteString(strconv.Itoa(num))
}
buf.WriteString("}")
return buf.Bytes(), nil
}