-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
199 lines (176 loc) · 4.64 KB
/
util.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
package sprocket
import (
"database/sql"
"embed"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/go-zoo/bone"
"github.com/jmoiron/sqlx"
"gopkg.in/yaml.v2"
)
// GetTemplates ...
func GetTemplates(viewFS *embed.FS, patterns []string, filenames []string) (*template.Template, error) {
templateFS, err := template.ParseFS(viewFS, patterns...)
if err != nil {
return nil, err
}
return templateFS.New("base").ParseFiles(filenames...)
}
// TransactionQuery ...
func TransactionQuery(db *sqlx.DB, query string, args ...interface{}) (sql.Result, error) {
err := db.Ping()
if err == nil {
tx, err := db.Beginx()
if err == nil {
sqlResult, err := tx.Exec(query, args...)
if err != nil {
tx.Rollback()
return nil, err
}
tx.Commit()
return sqlResult, nil
}
}
return nil, err
}
// TxTransactionQuery ...
func TxTransactionQuery(tx *sqlx.Tx, query string, args ...interface{}) (sql.Result, error) {
sqlResult, err := tx.Exec(query, args...)
if err != nil {
tx.Rollback()
return nil, err
}
return sqlResult, nil
}
// RowScanWrap ...
func RowScanWrap(r *sql.Row, dest ...interface{}) error {
return r.Scan(dest...)
}
// RowsScanWrap ...
func RowsScanWrap(r *sql.Rows, dest ...interface{}) error {
return r.Scan(dest...)
}
// JustJSONMarshal ...
func JustJSONMarshal(v interface{}) string {
result, err := json.Marshal(v)
if err != nil {
log.Panic(err)
}
return string(result)
}
// InterfaceToString ...
func InterfaceToString(v interface{}) string {
if v != nil {
return v.(string)
}
return ""
}
// InterfaceArrayToStringArray ...
func InterfaceArrayToStringArray(t []interface{}) []string {
s := make([]string, len(t))
for i, v := range t {
s[i] = fmt.Sprint(v)
}
return s
}
// ParseToDatabaseDate a helper method to parse string to time
// layout example `02/01/2006`
func ParseToDatabaseDate(layout string, v string) interface{} {
parsedDate, err := time.Parse(layout, strings.TrimSpace(v))
if err != nil {
return nil
}
return parsedDate
}
// ExtractQueryParamAsInt ...
func ExtractQueryParamAsInt(r *http.Request, name string, defaultValue int) int {
s := bone.GetQuery(r, name)
if len(s) > 0 {
if tmp, err := strconv.Atoi(s[0]); err == nil {
return tmp
}
}
return defaultValue
}
// ExtractQueryParamAsString ...
func ExtractQueryParamAsString(r *http.Request, name string, defaultValue string) string {
s := bone.GetQuery(r, name)
if len(s) > 0 {
return s[0]
}
return defaultValue
}
// MetaValueStringExtractor ...
func MetaValueStringExtractor(v interface{}) string {
if v != nil {
return v.(string)
}
return ""
}
// LoadYAML used to load config.yml into the program
func LoadYAML(filename string, configuration interface{}) error {
file, err := os.Open(filename)
if err != nil {
return err
}
decoder := yaml.NewDecoder(file)
err = decoder.Decode(configuration)
if err != nil {
return err
}
return nil
}
// RespondOkayJSON ...
func RespondOkayJSON(w http.ResponseWriter, payload interface{}) {
RespondStatusCodeWithJSON(w, http.StatusOK, payload)
}
// RespondInternalServerError ...
func RespondInternalServerError(w http.ResponseWriter, payload interface{}) {
RespondStatusCodeWithJSON(w, http.StatusInternalServerError, payload)
}
// RespondNotImplementedJSON ...
func RespondNotImplementedJSON(w http.ResponseWriter, payload interface{}) {
RespondStatusCodeWithJSON(w, http.StatusNotImplemented, payload)
}
// RespondNotFoundJSON ...
func RespondNotFoundJSON(w http.ResponseWriter, payload interface{}) {
RespondStatusCodeWithJSON(w, http.StatusNotFound, payload)
}
// RespondBadRequestJSON ...
func RespondBadRequestJSON(w http.ResponseWriter, payload interface{}) {
RespondStatusCodeWithJSON(w, http.StatusBadRequest, payload)
}
// RespondStatusCodeWithJSON ...
func RespondStatusCodeWithJSON(w http.ResponseWriter, statusCode int, payload interface{}) {
response, err := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
crashPayload := []byte(`{
"success": false,
"result": null,
"errors": ["` + fmt.Sprintf("%s", err.Error()) + `"]
}`)
w.Write(crashPayload)
return
}
w.WriteHeader(statusCode)
w.Write(response)
}
// GenerateMySQLTotalCount this is used to generate total records base on the given SQL query for pagination
func GenerateMySQLTotalCount(sql string) string {
upperSQL := strings.ToUpper(sql)
upperSQL = strings.ReplaceAll(upperSQL, "\n", " ")
indexOfFrom := strings.Index(upperSQL, " FROM ")
if indexOfFrom != -1 {
return "SELECT COUNT(*) FROM " + strings.ReplaceAll(sql[indexOfFrom+6:], "\n", " ")
}
return ""
}