-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper-conv.go
467 lines (405 loc) · 12.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Package helper:
// common project provides commonly used helper utility functions, custom utility types, and third party package wrappers.
// common project helps code reuse, and faster composition of logic without having to delve into commonly recurring code logic and related testings.
// common project source directories and brief description:
// + /ascii = helper types and/or functions related to ascii manipulations.
// + /crypto = helper types and/or functions related to encryption, decryption, hashing, such as rsa, aes, sha, tls etc.
// + /csv = helper types and/or functions related to csv file manipulations.
// + /rest = helper types and/or functions related to http rest api GET, POST, PUT, DELETE actions invoked from client side.
// + /tcp = helper types providing wrapped tcp client and tcp server logic.
// - /wrapper = wrappers provides a simpler usage path to third party packages, as well as adding additional enhancements.
// - /aws = contains aws sdk helper types and functions.
// - /cloudmap = wrapper for aws cloudmap service discovery.
// - /dynamodb = wrapper for aws dynamodb data access and manipulations.
// - /gin = wrapper for gin-gonic web server, and important middleware, into a ready to use solution.
// - /hystrixgo = wrapper for circuit breaker logic.
// - /kms = wrapper for aws key management services.
// - /mysql = wrapper for mysql + sqlx data access and manipulation.
// - /ratelimit = wrapper for ratelimit logic.
// - /redis = wrapper for redis data access and manipulation, using go-redis.
// - /s3 = wrapper for aws s3 data access and manipulation.
// - /ses = wrapper for aws simple email services.
// - /sns = wrapper for aws simple notification services.
// - /sqlite = wrapper for sqlite + sqlx data access and manipulation.
// - /sqlserver = wrapper for sqlserver + sqlx data access and manipulation.
// - /sqs = wrapper for aws simple queue services.
// - /systemd = wrapper for kardianos service to support systemd, windows, launchd service creations.
// - /viper = wrapper for viper config.
// - /waf2 = wrapper for aws waf2 (web application firewall v2).
// - /xray = wrapper for aws xray distributed tracing.
// - /zap = wrapper for zap logging.
//
// /helper-conv.go = helpers for data conversion operations.
// /helper-db.go = helpers for database data type operations.
// /helper-emv.go = helpers for emv chip card related operations.
// /helper-io.go = helpers for io related operations.
// /helper-net.go = helpers for network related operations.
// /helper-num.go = helpers for numeric related operations.
// /helper-other.go = helpers for misc. uncategorized operations.
// /helper-reflect.go = helpers for reflection based operations.
// /helper-regex.go = helpers for regular express related operations.
// /helper-str.go = helpers for string operations.
// /helper-struct.go = helpers for struct related operations.
// /helper-time.go = helpers for time related operations.
// /helper-uuid.go = helpers for generating globally unique ids.
package helper
/*
* Copyright 2020-2023 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 (
"fmt"
"math"
"reflect"
"strconv"
"strings"
"time"
)
// Itoa converts integer into string.
func Itoa(i int) string {
return strconv.Itoa(i)
}
// ItoaZeroBlank converts integer into string, if integer is zero, blank is returned.
func ItoaZeroBlank(i int) string {
if i == 0 {
return ""
} else {
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, true = "true", false = "false".
func Btoa(b bool) string {
if b == true {
return "true"
}
return "false"
}
// Atob converts string to bool.
// string values with first char lower cased "t", "y", "1" treated as true, otherwise false.
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 int pointer from int value.
func IntPtr(i int) *int {
return &i
}
// IntVal gets int value from int pointer.
// if pointer is nil, zero is returned.
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)
}
// UintPtr gets uint pointer from uint value.
func UintPtr(i uint) *uint {
return &i
}
// UintVal gets uint value from uint pointer, if uint pointer is nil, 0 is returned.
func UintVal(i *uint) uint {
if i == nil {
return 0
} else {
return *i
}
}
// StrToUint converts from string to uint, if string is not a valid uint, 0 is returned.
func StrToUint(s string) uint {
if v, e := strconv.ParseUint(s, 10, 32); e != nil {
return 0
} else {
return uint(v)
}
}
// Int64Ptr gets int64 pointer from int64 value.
func Int64Ptr(i int64) *int64 {
return &i
}
// Int64Val gets int64 value from int64 pointer, if pointer is nil, 0 is returned.
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 pointer is nil, otherwise actual int64 value.
func Int64PtrToInt64(n *int64) int64 {
if n == nil {
return 0
} else {
return *n
}
}
// UInt64ToString converts uint64 value to string.
func UInt64ToString(n uint64) string {
return strconv.FormatUint(n, 10)
}
// StrToUint64 converts from string to uint64, if string is not a valid uint64, 0 is returned.
func StrToUint64(s string) uint64 {
if v, e := strconv.ParseUint(s, 10, 64); e != nil {
return 0
} else {
return v
}
}
// StrToInt64 converts from string to int64, if string is not a valid int64, 0 is returned.
func StrToInt64(s string) int64 {
if v, e := strconv.ParseInt(s, 10, 64); e != nil {
return 0
} else {
return v
}
}
// Float32Ptr gets float32 pointer from float32 value.
func Float32Ptr(f float32) *float32 {
return &f
}
// Float32ToString converts float32 value to string.
func Float32ToString(f float32) string {
return fmt.Sprintf("%f", f)
}
// Float32ToStringCents converts float32 value into string representing cent values, 2.12 returned as "212".
// Since float32 can not be precisely calculated in some cases, use math.Round returns the nearest integer
func Float32ToStringCents(f float32) string {
centsInt := int(math.Round(math.Abs(float64(f) * 100)))
if f >= 0.00 {
return strings.ReplaceAll(PadLeft(Itoa(centsInt), 3), " ", "0")
} else {
return "-" + strings.ReplaceAll(PadLeft(Itoa(centsInt), 3), " ", "0")
}
}
// Float64ToIntCents converts float64 value into int, representing cent values, 2.12 returned as 212.
// Since float64 can not be precisely calculated in some cases, use math.Round returns the nearest integer
func Float64ToIntCents(f float64) int {
return int(math.Round(f * 100))
}
// Float32PtrToFloat32 returns 0 if pointer is nil, otherwise actual float32 value.
func Float32PtrToFloat32(f *float32) float32 {
if f == nil {
return 0.00
} else {
return *f
}
}
// Float64Ptr gets float64 pointer from float64 value.
func Float64Ptr(f float64) *float64 {
return &f
}
// Float64Val gets float64 value from float64 pointer, if pointer is nil, 0 is returned.
func Float64Val(f *float64) float64 {
if f == nil {
return 0
} else {
return *f
}
}
// Float64ToInt converts from float64 into int, if conversion fails, 0 is returned.
func Float64ToInt(f float64) int {
if v, b := ParseInt32(FloatToString(f)); !b {
return 0
} else {
return v
}
}
// CentsToFloat64 converts int (cents) into float64 value with two decimal value.
func CentsToFloat64(i int) float64 {
f, _ := ParseFloat64(fmt.Sprintf("%.2f", float64(i)*0.01))
return f
}
// FloatToString converts float64 value into string value.
func FloatToString(f float64) string {
return fmt.Sprintf("%f", f)
}
// Float64ToString converts float64 value into string value.
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, true = 1, false = 0.
func BoolToInt(b bool) int {
if b {
return 1
} else {
return 0
}
}
// BoolToString converts bool to string value, true = "true", false = "false".
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 value to bool pointer.
func BoolPtr(b bool) *bool {
return &b
}
// BoolVal gets bool value from bool pointer, if pointer is nil, false is returned.
func BoolVal(b *bool) bool {
if b == nil {
return false
} else {
return *b
}
}
// DatePtrToString formats pointer time.Time to string date format yyyy-mm-dd.
func DatePtrToString(t *time.Time) string {
if t == nil {
return ""
}
return FormatDate(*t)
}
// DateTimePtrToString formats pointer time.Time to string date time format yyyy-mm-dd hh:mm:ss AM/PM.
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 struct to Time pointer.
func TimePtr(t time.Time) *time.Time {
return &t
}
// TimeVal gets Time struct from Time pointer, if pointer is nil, a time.Time{} is returned.
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
}
// DurationVal gets Duration value from Duration pointer, if pointer is nil, 0 is returned.
func DurationVal(d *time.Duration) time.Duration {
if d == nil {
return 0
} else {
return *d
}
}
// StringPtr gets string pointer from string value.
func StringPtr(s string) *string {
return &s
}
// StringVal gets string value from string pointer, if pointer is nil, blank string is returned.
func StringVal(s *string) string {
if s == nil {
return ""
} else {
return *s
}
}
// StringPtrToString gets string value from string pointer, if pointer is nil, blank string is returned.
func StringPtrToString(s *string) string {
if s == nil {
return ""
} else {
return *s
}
}
// SliceObjectsToSliceInterface converts slice of objects into slice of interfaces.
// objectsSlice is received via interface parameter, and is expected to be a Slice,
// the slice is enumerated to convert each object within the slice to interface{},
// the final converted slice of interface is returned, if operation failed, nil is returned.
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
}