forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
57 lines (49 loc) · 990 Bytes
/
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
package slices
import (
"database/sql/driver"
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
type Int []int
func (i Int) Interface() interface{} {
return []int(i)
}
func (s *Int) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not []byte")
}
str := string(b)
(*s) = strToInt(str)
return nil
}
func (s Int) Value() (driver.Value, error) {
sa := make([]string, len(s))
for x, i := range s {
sa[x] = strconv.Itoa(i)
}
return fmt.Sprintf("{%s}", strings.Join(sa, ",")), nil
}
func (s *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)
}
(*s) = 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
}