/
helper-conv.go
378 lines (318 loc) · 6.57 KB
/
helper-conv.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package helper
/*
* Copyright 2020 Aldelo, LP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"reflect"
"strconv"
"strings"
"time"
"fmt"
)
// Itoa converts integer into string
func Itoa(i int) string {
return strconv.Itoa(i)
}
// Atoi converts string to integer
func Atoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
return 0
}
return i
}
// Btoa converts bool to string
func Btoa(b bool) string {
if b == true {
return "true"
}
return "false"
}
// Atob converts string to bool
func Atob(s string) bool {
if LenTrim(s) == 0 {
return false
}
var b bool
b = false
switch strings.ToLower(s[:1]) {
case "t":
b = true
case "y":
b = true
case "1":
b = true
case "f":
b = false
case "n":
b=false
case "0":
b=false
}
return b
}
// IntPtr casts int to int pointer
func IntPtr(i int) *int {
return &i
}
func IntVal(i *int) int {
if i == nil {
return 0
} else {
return *i
}
}
// Int32PtrToInt returns 0 if nil, otherwise actual int value
func Int32PtrToInt(n *int) int {
if n == nil {
return 0
} else {
return *n
}
}
// UintToStr converts from uint to string
func UintToStr(i uint) string {
return fmt.Sprintf("%d", i)
}
func UintPtr(i uint) *uint {
return &i
}
func UintVal(i *uint) uint {
if i == nil {
return 0
} else {
return *i
}
}
// StrToUint converts from string to uint
func StrToUint(s string) uint {
if v, e := strconv.ParseUint(s, 10, 32); e != nil {
return 0
} else {
return uint(v)
}
}
// Int64Ptr casts int64 to int64 pointer
func Int64Ptr(i int64) *int64 {
return &i
}
func Int64Val(i *int64) int64 {
if i == nil {
return 0
} else {
return *i
}
}
// Int64ToString converts int64 into string value
func Int64ToString(n int64) string {
return strconv.FormatInt(n, 10)
}
// Int64PtrToInt64 returns 0 if nil, otherwise actual int64 value
func Int64PtrToInt64(n *int64) int64 {
if n == nil {
return 0
} else {
return *n
}
}
// UInt64ToString converts uint64 into string value
func UInt64ToString(n uint64) string {
return strconv.FormatUint(n, 10)
}
// StrToUint64 converts from string to uint64
func StrToUint64(s string) uint64 {
if v, e := strconv.ParseUint(s, 10, 64); e != nil {
return 0
} else {
return uint64(v)
}
}
// Float32Ptr casts float32 to float32 pointer
func Float32Ptr(f float32) *float32 {
return &f
}
// Float32ToString converts float32 into string value
func Float32ToString(f float32) string {
return fmt.Sprintf("%f", f)
}
// Float32ToStringCents converts float32 into string representing cent values
func Float32ToStringCents(f float32) string {
if f >= 0.00 {
return strings.ReplaceAll(PadLeft(Itoa(int(f * 100)), 3), " ", "0")
} else {
return "-" + strings.ReplaceAll(PadLeft(Itoa(int(f * 100 * -1)), 3), " ", "0")
}
}
// Float32PtrToFloat32 returns 0 if nil, otherwise actual float32 value
func Float32PtrToFloat32(f *float32) float32 {
if f == nil {
return 0.00
} else {
return *f
}
}
// Float64Ptr casts float64 to float64 pointer
func Float64Ptr(f float64) *float64 {
return &f
}
func Float64Val(f *float64) float64 {
if f == nil {
return 0
} else {
return *f
}
}
// Float64ToInt converts from float64 into int
func Float64ToInt(f float64) int {
if v, b := ParseInt32(FloatToString(f)); !b {
return 0
} else {
return v
}
}
// FloatToString converts float64 into string value
func FloatToString(f float64) string {
return fmt.Sprintf("%f", f)
}
// Float64ToString is same as FloatToString
func Float64ToString(f float64) string {
return fmt.Sprintf("%f", f)
}
// Float64PtrToFloat64 returns 0 if nil, otherwise actual float64 value
func Float64PtrToFloat64(f *float64) float64 {
if f == nil {
return 0.00
} else {
return *f
}
}
// BoolToInt converts bool to int value
func BoolToInt(b bool) int {
if b {
return 1
} else {
return 0
}
}
// BoolToString converts bool to string value
func BoolToString(b bool) string {
if b {
return "true"
} else {
return "false"
}
}
// BoolPtrToBool converts bool pointer to bool value
func BoolPtrToBool(b *bool) bool {
if b == nil {
return false
} else {
return *b
}
}
// BoolPtr converts bool to *bool
func BoolPtr(b bool) *bool {
return &b
}
func BoolVal(b *bool) bool {
if b == nil {
return false
} else {
return *b
}
}
// DatePtrToString formats pointer time.Time to string date format
func DatePtrToString(t *time.Time) string {
if t == nil {
return ""
}
return FormatDate(*t)
}
// DateTimePtrToString formats pointer time.Time to string date time format
func DateTimePtrToString(t *time.Time) string {
if t == nil {
return ""
}
return FormatDateTime(*t)
}
// DateTimePtrToDateTime formats pointer time.Time to time.Time struct
func DateTimePtrToDateTime(t *time.Time) time.Time {
if t == nil {
return time.Time{}
} else {
return *t
}
}
// TimePtr casts Time to Time pointer
func TimePtr(t time.Time) *time.Time {
return &t
}
func TimeVal(t *time.Time) time.Time {
if t == nil {
return time.Time{}
} else {
return *t
}
}
// DurationPtr casts Duration to Duration pointer
func DurationPtr(d time.Duration) *time.Duration {
return &d
}
func DurationVal(d *time.Duration) time.Duration {
if d == nil {
return 0
} else {
return *d
}
}
// StringPtr casts string to string pointer
func StringPtr(s string) *string {
return &s
}
func StringVal(s *string) string {
if s == nil {
return ""
} else {
return *s
}
}
// StringPtrToString gets string value from string pointer
func StringPtrToString(s *string) string {
if s == nil {
return ""
} else {
return *s
}
}
// SliceObjectsToSliceInterfaces will convert slice of objects into slice of interfaces
func SliceObjectsToSliceInterface(objectsSlice interface{}) (output []interface{}) {
if reflect.TypeOf(objectsSlice).Kind() == reflect.Slice {
s := reflect.ValueOf(objectsSlice)
for i := 0; i < s.Len(); i++ {
output = append(output, s.Index(i).Interface())
}
return output
} else {
return nil
}
}
// IntToHex returns HEX string representation of i, in 2 digit blocks
func IntToHex(i int) string {
buf := StringToHex(Itoa(i))
if len(buf) % 2 != 0 {
buf = "0" + buf
}
return buf
}