-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
93 lines (78 loc) · 1.87 KB
/
functions.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
package helpers
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/FianGumilar/email-otp-verification/models"
"github.com/labstack/echo"
)
func InArray(v interface{}, in interface{}) (ok bool, i int) {
val := reflect.Indirect(reflect.ValueOf(in))
switch val.Kind() {
case reflect.Slice, reflect.Array:
for ; i < val.Len(); i++ {
if ok = v == val.Index(i).Interface(); ok {
return
}
}
}
return
}
func BindValidateStruct(ctx echo.Context, i interface{}) error {
if err := ctx.Bind(i); err != nil {
return err
}
if err := ctx.Validate(i); err != nil {
return err
}
return nil
}
func ResponseJSON(success bool, code string, msg string, result interface{}) models.Response {
tm := time.Now()
response := models.Response{
Success: success,
StatusCode: code,
Result: result,
Message: msg,
ResponseDatetime: tm,
}
return response
}
func LOG(title string, i interface{}, end bool) {
log, _ := json.Marshal(i)
logString := string(log)
fmt.Println(title, logString)
if end {
fmt.Println(title, strings.Repeat("_", 50), "END LOG", strings.Repeat("_", 50))
}
}
// ReplaceSQL ...
func ReplaceSQL(old, searchPattern string) string {
tmpCount := strings.Count(old, searchPattern)
for m := 1; m <= tmpCount; m++ {
old = strings.Replace(old, searchPattern, "$"+strconv.Itoa(m), 1)
}
return old
}
func WeekRange(year, week int) (start, end time.Time) {
start = WeekStart(year, week)
end = start.AddDate(0, 0, 6)
return
}
func WeekStart(year, week int) time.Time {
// Start from the middle of the year:
t := time.Date(year, 7, 1, 0, 0, 0, 0, time.UTC)
// Roll back to Monday:
if wd := t.Weekday(); wd == time.Sunday {
t = t.AddDate(0, 0, -6)
} else {
t = t.AddDate(0, 0, -int(wd)+1)
}
// Difference in weeks:
_, w := t.ISOWeek()
t = t.AddDate(0, 0, (week-w)*7)
return t
}