forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
65 lines (57 loc) · 1.35 KB
/
int.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
package slices
import (
"database/sql/driver"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
// Int is a slice of int.
type Int []int
// Interface implements the nulls.nullable interface.
func (i Int) Interface() interface{} {
return []int(i)
}
// Scan implements the sql.Scanner interface.
// It allows to read the int slice from the database value.
func (i *Int) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not []byte")
}
str := string(b)
(*i) = strToInt(str)
return nil
}
// Value implements the driver.Valuer interface.
// It allows to convert the int slice to a driver.value.
func (i Int) Value() (driver.Value, error) {
sa := make([]string, len(i))
for x, v := range i {
sa[x] = strconv.Itoa(v)
}
return fmt.Sprintf("{%s}", strings.Join(sa, ",")), nil
}
// UnmarshalText will unmarshall text value into
// the int slice representation of this value.
func (i *Int) UnmarshalText(text []byte) error {
ss := []int{}
for _, x := range strings.Split(string(text), ",") {
f, err := strconv.Atoi(x)
if err != nil {
return errors.WithStack(err)
}
ss = append(ss, f)
}
(*i) = ss
return nil
}
func strToInt(s string) []int {
r := strings.Trim(s, "{}")
a := make([]int, 0, 10)
for _, t := range strings.Split(r, ",") {
i, _ := strconv.Atoi(t)
a = append(a, i)
}
return a
}