This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
158 lines (136 loc) · 3.42 KB
/
utils.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
package utils
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// BatchInsertQuery to create raw batch insert query.
func BatchInsertQuery(tableName string, rowCount int, colCount int, cols ...[]string) string {
if rowCount <= 0 || colCount <= 0 {
return ""
}
// Create column format. Ex. (?, ?, ?)
colList := []string{}
for i := 0; i < colCount; i++ {
colList = append(colList, "?")
}
colFormat := fmt.Sprintf("(%s)", strings.Join(colList, ", "))
rowList := []string{}
for i := 0; i < rowCount; i++ {
rowList = append(rowList, colFormat)
}
colNames := ""
if len(cols) > 0 {
if len(cols[0]) == colCount {
colNames = fmt.Sprintf("(%s)", strings.Join(cols[0], ", "))
}
}
return fmt.Sprintf("INSERT INTO %s %s VALUES %s",
tableName,
colNames,
strings.Join(rowList, ", "),
)
}
// GetMapKey to get key from map.
func GetMapKey(m map[int]string, value string) int {
for i, v := range m {
if strings.EqualFold(v, value) {
return i
}
}
return 0
}
// GetDuration to get duration in seconds.
// Example: "3 hr. 30 min. 20 sec." => 12620.
func GetDuration(duration string) (seconds int) {
duration = strings.TrimSpace(duration)
if duration == "" {
return 0
}
// Count hour.
r := regexp.MustCompile(`\d+ hr.`)
match := r.FindStringSubmatch(duration)
if len(match) > 0 {
h := GetValueFromSplit(match[0], " ", 0)
seconds += StrToNum(h) * 3600
}
// Count minute.
r = regexp.MustCompile(`\d+ min.`)
match = r.FindStringSubmatch(duration)
if len(match) > 0 {
m := GetValueFromSplit(match[0], " ", 0)
seconds += StrToNum(m) * 60
}
// Count minute.
r = regexp.MustCompile(`\d+ sec.`)
match = r.FindStringSubmatch(duration)
if len(match) > 0 {
s := GetValueFromSplit(match[0], " ", 0)
seconds += StrToNum(s)
}
return seconds
}
// GetValueFromSplit to get value from splitted string.
// Will return empty string if out of index.
func GetValueFromSplit(str string, separator string, index int) string {
splitStr := strings.Split(str, separator)
if len(splitStr) <= index {
return ""
}
return strings.TrimSpace(splitStr[index])
}
// StrToNum to convert string number to integer including comma removal (1,234 -> 1234).
func StrToNum(strNum string) int {
strNum = strings.TrimSpace(strNum)
strNum = strings.Replace(strNum, ",", "", -1)
intNum, _ := strconv.Atoi(strNum)
return intNum
}
// SecondToString to convert int seconds to string duration.
// Example: 12620 => 03:30:20.
func SecondToString(seconds int) string {
d, _ := time.ParseDuration(fmt.Sprintf("%vs", seconds))
duration := d.String()
h, m, s := "00", "00", "00"
// Count hour.
r := regexp.MustCompile(`\d+h`)
match := r.FindStringSubmatch(duration)
if len(match) > 0 {
h = strings.Replace(match[0], "h", "", -1)
if len(h) == 1 {
h = "0" + h
}
}
// Count minute.
r = regexp.MustCompile(`\d+m`)
match = r.FindStringSubmatch(duration)
if len(match) > 0 {
m = strings.Replace(match[0], "m", "", -1)
if len(m) == 1 {
m = "0" + m
}
}
// Count minute.
r = regexp.MustCompile(`\d+s`)
match = r.FindStringSubmatch(duration)
if len(match) > 0 {
s = strings.Replace(match[0], "s", "", -1)
if len(s) == 1 {
s = "0" + s
}
}
return fmt.Sprintf("%v:%v:%v", h, m, s)
}
// UniqueInt to remove duplicate in slice.
func UniqueInt(list []int) (unique []int) {
keys := make(map[int]bool)
for _, v := range list {
if _, value := keys[v]; !value {
keys[v] = true
unique = append(unique, v)
}
}
return unique
}