-
Notifications
You must be signed in to change notification settings - Fork 13
/
convert.go
289 lines (241 loc) · 4.98 KB
/
convert.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package helper
import (
"strconv"
"strings"
"time"
"github.com/ericlagergren/decimal"
"github.com/iancoleman/strcase"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/types"
)
type RemovedID struct {
ID uint
}
func RemovedIDsToUint(removedIds []RemovedID) []uint {
uintIDs := make([]uint, len(removedIds))
for index, id := range removedIds {
uintIDs[index] = id.ID
}
return uintIDs
}
func IntsToInterfaces(ints []int) []interface{} {
interfaces := make([]interface{}, len(ints))
for index, number := range ints {
interfaces[index] = number
}
return interfaces
}
func IDsToBoilerInterfaces(ids []string) []interface{} {
interfaces := make([]interface{}, len(ids))
for index, id := range ids {
interfaces[index] = IDToBoiler(id)
}
return interfaces
}
func IDsToBoiler(ids []string) []uint {
ints := make([]uint, len(ids))
for index, stringID := range ids {
ints[index] = IDToBoiler(stringID)
}
return ints
}
func IDToBoiler(ID string) uint {
splitted := strings.Split(ID, "-")
if len(splitted) > 1 {
// nolint: errcheck
i, _ := strconv.ParseUint(splitted[1], 10, 64)
return uint(i)
}
return 0
}
func IDToNullBoiler(ID string) null.Uint {
uintID := IDToBoiler(ID)
if uintID == 0 {
return null.NewUint(0, false)
}
return null.Uint{
Uint: uintID,
Valid: false,
}
}
func IDToGraphQL(id uint, tableName string) string {
return strcase.ToLowerCamel(tableName) + "-" + strconv.Itoa(int(id))
}
func IDsToGraphQL(ids []uint, tableName string) []string {
stringIDs := make([]string, len(ids))
for index, id := range ids {
stringIDs[index] = IDToGraphQL(id, tableName)
}
return stringIDs
}
func NullDotBoolToPointerBool(v null.Bool) *bool {
return v.Ptr()
}
func NullDotStringToPointerString(v null.String) *string {
return v.Ptr()
}
func NullDotTimeToPointerInt(v null.Time) *int {
pv := v.Ptr()
if pv == nil {
return nil
}
u := int(pv.Unix())
return &u
}
func TimeTimeToInt(v time.Time) int {
return int(v.Unix())
}
func IntToTimeTime(v int) time.Time {
return time.Unix(int64(v), 0)
}
func NullDotStringToString(v null.String) string {
if v.Ptr() == nil {
return ""
}
return *v.Ptr()
}
func NullDotUintToPointerInt(v null.Uint) *int {
pv := v.Ptr()
if pv == nil {
return nil
}
u := int(*pv)
return &u
}
func PointerStringToString(v *string) string {
if v == nil {
return ""
}
return *v
}
func PointerIntToNullDotTime(v *int) null.Time {
return null.TimeFrom(time.Unix(int64(*v), 0))
}
func StringToNullDotString(v string) null.String {
return null.StringFrom(v)
}
func PointerStringToNullDotString(v *string) null.String {
return null.StringFromPtr(v)
}
func PointerBoolToNullDotBool(v *bool) null.Bool {
return null.BoolFromPtr(v)
}
func TypesNullDecimalToFloat64(v types.NullDecimal) float64 {
f, _ := v.Float64()
return f
}
func Float64ToTypesNullDecimal(v float64) types.NullDecimal {
d := new(decimal.Big)
d.SetFloat64(v)
return types.NewNullDecimal(d)
}
func TypesNullDecimalToPointerString(v types.NullDecimal) *string {
s := v.String()
if s == "" {
return nil
}
return &s
}
func PointerStringToTypesNullDecimal(v *string) types.NullDecimal {
if v == nil {
return types.NewNullDecimal(nil)
}
d := new(decimal.Big)
if _, ok := d.SetString(*v); !ok {
nd := types.NewNullDecimal(nil)
if err := d.Context.Err(); err != nil {
return nd
}
// TODO: error handling maybe write log line here
// https://github.com/volatiletech/sqlboiler/blob/master/types/decimal.go#L156
return nd
}
return types.NewNullDecimal(d)
}
func PointerIntToInt(v *int) int {
if v == nil {
return 0
}
return *v
}
func PointerIntToNullDotInt(v *int) null.Int {
return null.IntFromPtr((v))
}
func PointerIntToNullDotUint(v *int) null.Uint {
if v == nil {
return null.UintFromPtr(nil)
}
uv := *v
return null.UintFrom(uint(uv))
}
func NullDotIntToPointerInt(v null.Int) *int {
return v.Ptr()
}
func IntToInt8(v int) int8 {
return int8(v)
}
func Int8ToInt(v int8) int {
return int(v)
}
func NullDotFloat64ToPointerFloat64(v null.Float64) *float64 {
return v.Ptr()
}
func PointerFloat64ToNullDotFloat64(v *float64) null.Float64 {
return null.Float64FromPtr(v)
}
func IntToUint(v int) uint {
return uint(v)
}
func UintToInt(v uint) int {
return int(v)
}
func BoolToInt(v bool) int {
if v {
return 1
}
return 0
}
func IntToBool(v int) bool {
return v == 1
}
func NullDotBoolToPointerInt(v null.Bool) *int {
pv := v.Ptr()
if pv == nil {
return nil
}
if *pv {
i := 1
return &i
}
i := 0
return &i
}
func PointerIntToNullDotBool(v *int) null.Bool {
if v == nil {
return null.Bool{
Valid: false,
}
}
return null.Bool{
Valid: v != nil,
Bool: *v == 1,
}
}
func NullDotIntToUint(v null.Int) uint {
return uint(v.Int)
}
func NullDotUintToUint(v null.Uint) uint {
return v.Uint
}
func NullDotIntIsFilled(v null.Int) bool {
return !v.IsZero()
}
func NullDotUintIsFilled(v null.Uint) bool {
return !v.IsZero()
}
func UintIsFilled(v uint) bool {
return v != 0
}
func IntIsFilled(v int) bool {
return v != 0
}