-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
237 lines (191 loc) · 5.42 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
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
package Sex
import (
str "strings"
re "regexp"
"mime/multipart"
"encoding/json"
"net/url"
"reflect"
"bytes"
"fmt"
"os"
)
func fixPath(path string) string {
path = str.Trim(path, " ")
if path == "/" || path == "" {
return path
}
end_path := len(path)-1
if path[end_path] == '/' {
path = path[:end_path]
}
return path
}
// Sex utility function to make copy of map or struct to another map or struct
// Required: Destine need to be a pointer
// Example:
// var m struct { Name string `json:"name"` }
// j := map[string]interface{}{
// "name": "Joao",
// }
// Sex.Copy(j, &m)
func Copy(source interface{}, destine interface{}) error {
encoded, err := json.Marshal(source)
if err != nil {
return err
}
return json.Unmarshal(encoded, destine)
}
// Sex utility function to make merge of map or struct and another map or struct
// Required: Destine need to be a pointer
// Example:
// var m := struct { Name string `json:"name"` } {
// Name: "Joao",
// }
// j := map[string]interface{}{
// "idade": "Joao",
// "name": nil,
// }
// Sex.Copy(m, &j)
//
// Merge rules:
// If the field on source dont exists on destine it will be created (just if destine are map)
// If the field on source exists on destine but are dont seted it will be seted
// If the field on source exists on destine but are seted it will not be seted
// If override are seted as true, the field on destine will be overrided by source
func Merge(source interface{}, destine interface{}, override ...bool) (map[string]interface{}, error) {
final := map[string]interface{}{}
dst := map[string]interface{}{}
src := map[string]interface{}{}
ok := false
if src, ok = source.(map[string]interface{}); !ok {
err := Copy(source, &src)
if err != nil {
return final, err
}
}
if dst, ok = destine.(map[string]interface{}); !ok {
err := Copy(destine, &dst)
if err != nil {
return final, err
}
}
for k, v := range src {
final[k] = v
}
for k, v := range dst {
if _, exists := final[k]; exists && (override != nil && override[0]) {
continue
}
final[k] = v
}
return final, nil
}
// Function thats parse a byte list on a json and write on a variable
// Required: v needs to be a pointer
func FromJson(encoded []byte, v interface{}) error {
return json.Unmarshal(encoded, v)
}
// Function thats parse a byte list on a json and write on a variable
func Jsonify(v interface{}) []byte {
res, _ := json.Marshal(v)
return res
}
// Function thats get a environment var or default value if var does not exist
func GetEnv(key string, def string) string {
val, ok := os.LookupEnv(key)
if !ok {
return def
}
return val
}
func TypeParse(t string) string {
t_re := re.MustCompile(`([A-Z][a-z_0-9]{1,})?`)
list_match := t_re.FindAllStringSubmatch(t, -1)
type_out := ""
for i, v := range list_match {
type_out += str.ToLower(v[0])
if i < len(list_match)-1 {
type_out += "_"
}
}
type_out += "s"
return type_out
}
// Debuging stdout display
func SuperPut (v...interface{}) {
fmt.Println("\n--------------------------------")
fmt.Println(v...)
fmt.Print("--------------------------------\n")
}
func isRawFunc(f interface{}) bool {
var gf rawRouteFunc
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isRawFuncNoStatus(f interface{}) bool {
var gf rawRouteFuncNoStatus
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isStrFunc(f interface{}) bool {
var gf strRouteFunc
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isStrFuncNoStatus(f interface{}) bool {
var gf strRouteFuncNoStatus
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isResFunc(f interface{}) bool {
var gf resRouteFunc
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isResFuncNoStatus(f interface{}) bool {
var gf resRouteFuncNoStatus
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isJsonFunc(f interface{}) bool {
var gf jsonRouteFunc
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func isJsonFuncNoStatus(f interface{}) bool {
var gf jsonRouteFuncNoStatus
return reflect.TypeOf(f).AssignableTo(reflect.TypeOf(gf))
}
func GenericInterface () reflect.Type {
var i interface{}
return reflect.TypeOf(i)
}
func GenericInt () reflect.Type {
var i int
return reflect.TypeOf(i)
}
func GenericString () reflect.Type {
var i string
return reflect.TypeOf(i)
}
func GenericJsonObj() reflect.Type {
return reflect.MapOf(GenericString(), GenericInterface())
}
func GenericJsonArray() reflect.Type {
return reflect.ArrayOf(-1, reflect.MapOf(GenericString(), GenericInterface().Elem()))
}
func GenericBuff() reflect.Type {
var i bytes.Buffer
return reflect.TypeOf(&i)
}
func GenericForm() reflect.Type {
var i url.Values
return reflect.TypeOf(&i)
}
func GenericMultipartForm() reflect.Type {
var i multipart.Form
return reflect.TypeOf(&i)
}
func ValidateData(data interface{}, t func()reflect.Type) bool {
if data == nil {
return false
}
if reflect.TypeOf(data).Kind() == t().Kind() {
return true
}
return false
}