-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
146 lines (128 loc) · 2.74 KB
/
types.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package dbutil
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"
)
type JsonStrList []string
func (list *JsonStrList) Scan(value interface{}) error {
if value == nil {
*list = nil
}
switch v := value.(type) {
case string:
if err := json.Unmarshal([]byte(v), list); err != nil {
return fmt.Errorf("invalid JsonStrList: %v", err)
}
case []byte:
if err := json.Unmarshal(v, list); err != nil {
return fmt.Errorf("invalid JsonStrList: %v", err)
}
default:
return fmt.Errorf("invalid JsonStrList: %#v", value)
}
return nil
}
func (list JsonStrList) Value() (driver.Value, error) {
if list == nil {
return nil, nil
}
if data, err := json.Marshal(list); err == nil {
return string(data), nil
} else {
return nil, err
}
}
type NameList []string
func (sl *NameList) Scan(value interface{}) error {
if value == nil {
*sl = nil
return nil
}
switch v := value.(type) {
case string:
*sl = strings.Split(v, ",")
case []byte:
*sl = strings.Split(string(v), ",")
default:
return fmt.Errorf("invalid string list value: %#v", value)
}
return nil
}
func (sl NameList) Value() (driver.Value, error) {
if sl == nil {
return nil, nil
}
return strings.Join(sl, ","), nil
}
type JsonNumList []int64
func (list *JsonNumList) Scan(value interface{}) error {
if value == nil {
*list = nil
}
switch v := value.(type) {
case string:
if err := json.Unmarshal([]byte(v), list); err != nil {
return fmt.Errorf("invalid JsonNumList: %v", err)
}
case []byte:
if err := json.Unmarshal(v, list); err != nil {
return fmt.Errorf("invalid JsonNumList: %v", err)
}
default:
return fmt.Errorf("invalid JsonNumList: %#v", value)
}
return nil
}
func (list JsonNumList) Value() (driver.Value, error) {
if list == nil {
return nil, nil
}
if data, err := json.Marshal(list); err == nil {
return string(data), nil
} else {
return nil, err
}
}
type NumList []int64
func (sl *NumList) Scan(value interface{}) error {
if value == nil {
*sl = nil
return nil
}
var ns []string
switch v := value.(type) {
case string:
ns = strings.Split(v, ",")
case []byte:
ns = strings.Split(string(v), ",")
default:
return fmt.Errorf("invalid string list value: %#v", value)
}
nums := make([]int64, 0, len(ns))
for _, numstr := range ns {
numstr = strings.TrimSpace(numstr)
if numstr == "" {
continue
}
if n, err := strconv.ParseInt(numstr, 10, 64); err == nil {
nums = append(nums, n)
} else {
return fmt.Errorf("invalid number: %s", numstr)
}
}
*sl = nums
return nil
}
func (sl NumList) Value() (driver.Value, error) {
if sl == nil {
return nil, nil
}
ns := make([]string, len(sl))
for i, n := range sl {
ns[i] = strconv.FormatInt(n, 10)
}
return strings.Join(ns, ","), nil
}