-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathint8_array.go
82 lines (62 loc) · 1.36 KB
/
int8_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
package pqextended
import (
"database/sql/driver"
"fmt"
"strconv"
"strings"
)
// Int8Array implements the pq Scan and Value interface for int8 slice type
type Int8Array []int8
// Scan implements the sql.Scanner interface
func (a *Int8Array) Scan(src interface{}) 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("PQ Extended: Cannot convert %T to Int8Array", src)
}
func (a *Int8Array) scanBytes(src []byte) error {
str := string(src)
str = strings.TrimSuffix(str, "}")
str = strings.TrimPrefix(str, "{")
if str == "" {
*a = make([]int8, 0)
return nil
}
strSlice := strings.Split(str, ",")
b := make([]int8, 0, len(strSlice))
for i := 0; i < len(strSlice); i++ {
number, err := strconv.Atoi(strSlice[i])
if err != nil {
return err
}
b = append(b, int8(number))
}
*a = b
return nil
}
// Value implements the driver.Valuer interface
func (a Int8Array) Value() (driver.Value, error) {
if a == nil {
return nil, nil
}
if n := len(a); n > 0 {
var b strings.Builder
b.WriteString("{")
for i := 0; i < n; i++ {
if i != n-1 {
b.WriteString(fmt.Sprintf("%d,", a[i]))
} else {
b.WriteString(fmt.Sprintf("%d", a[i]))
}
}
b.WriteString("}")
return b.String(), nil
}
return "{}", nil
}