/
utils.go
198 lines (167 loc) · 4.93 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
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
package trinity
import (
"fmt"
"net"
"os"
"reflect"
"regexp"
"runtime"
"strings"
"time"
"github.com/bwmarrin/snowflake"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)
// RFC3339FullDate for rfc full date
const RFC3339FullDate = "2006-01-02"
// GetCurrentTime get current time
func GetCurrentTime() time.Time {
return time.Now()
}
// GetCurrentTimeString get current time with string
func GetCurrentTimeString(timeType string) string {
return GetCurrentTime().Format(timeType)
}
// GetCurrentTimeUnix get current time with unix time
func GetCurrentTimeUnix() int64 {
return GetCurrentTime().Unix()
}
// CheckFileIsExist : check file if exist ,exist -> true , not exist -> false ,
/**
* @param filename string ,the file name need to check
* @return boolean string
*/
func CheckFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}
// GetRequestType to get http request type with restful style
func GetRequestType(c *gin.Context) string {
if c.Request.Method == "GET" {
if len(c.Params) > 0 {
return "RETRIEVE"
}
}
return c.Request.Method
}
// HandleBackend handle filter backend
func HandleBackend(c *gin.Context, backend func(c *gin.Context, db *gorm.DB) *gorm.DB) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
// You can use reqUserID here to check user authorization
return backend(c, db)
}
}
func handleMultistructBySelect(c *gin.Context, resource interface{}) (int, interface{}, error) {
SelectByField := c.Query("SelectBy")
if SelectByField == "" {
return 200, resource, nil
}
return 200, resource, nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
//InSlice if value in stringlist
func InSlice(value string, stringSlice []string) bool {
for _, v := range stringSlice {
if v == value {
return true
}
}
return false
}
//SliceInSlice if slice in slice
func SliceInSlice(sliceToCheck []string, slice []string) bool {
for _, v := range sliceToCheck {
if !InSlice(v, slice) {
return false
}
}
return true
}
//RecordErrorLevelTwo login error and print line , func , and error to gin context
func RecordErrorLevelTwo() (uintptr, string, int) {
funcName, file, line, _ := runtime.Caller(2)
return funcName, file, line
}
// Getparentdirectory : get parent directory of the path ,
/*
* @param path string ,the path you want to get parent directory
* @return string , the parent directory you need
*/
func Getparentdirectory(path string, level int) string {
return strings.Join(strings.Split(path, "/")[0:len(strings.Split(path, "/"))-level], "/")
}
//DeleteExtraSpace remove extra space
func DeleteExtraSpace(s string) string {
s1 := strings.Replace(s, " ", " ", -1) //替换tab为空格
regstr := "\\s{2,}" //两个及两个以上空格的正则表达式
reg, _ := regexp.Compile(regstr) //编译正则表达式
s2 := make([]byte, len(s1)) //定义字符数组切片
copy(s2, s1) //将字符串复制到切片
spcIndex := reg.FindStringIndex(string(s2)) //在字符串中搜索
for len(spcIndex) > 0 { //找到适配项
s2 = append(s2[:spcIndex[0]+1], s2[spcIndex[1]:]...) //删除多余空格
spcIndex = reg.FindStringIndex(string(s2)) //继续在字符串中搜索
}
return string(s2)
}
// GetTypeName to get struct type name
func GetTypeName(myvar interface{}, isToLowerCase bool) string {
name := ""
t := reflect.TypeOf(myvar)
if t.Kind() == reflect.Ptr {
name = t.Elem().Name()
} else {
name = t.Name()
}
if isToLowerCase {
name = strings.ToLower(name)
}
return name
}
// GenerateSnowFlakeID generate snowflake id
func GenerateSnowFlakeID(nodenumber int64) int64 {
// Create a new Node with a Node number of 1
node, _ := snowflake.NewNode(nodenumber)
// Generate a snowflake ID.
id := node.Generate().Int64()
return id
}
// IsFuncInited : is function inited : true inited function , false not inited or not a function
func IsFuncInited(function MixinCallback) bool {
if function == nil {
return false
}
return true
}
// GetFreePort get one free port
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
// GetServiceName get service name which will register to service mesh
func GetServiceName(projectName string) string {
return fmt.Sprintf("grpc.health.v1.%v", projectName)
}
// GetServiceID get service name which will register to service mesh
func GetServiceID(projectName string, projectVersion string, ServiceIP string, ServicePort int) string {
ServiceName := GetServiceName(projectName)
return fmt.Sprintf("%v-%v-%v-%v", ServiceName, projectVersion, ServiceIP, ServicePort)
}