-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
113 lines (97 loc) · 2.75 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
package Sex
import (
"reflect"
str "strings"
"encoding/json"
"os"
)
func fixPath(path string) string {
return "/" + str.Trim(str.Trim(path, " "), "/")
}
// Copy provides util to copy map or struct to another map or struct
// Required: Destine need to be a pointer
// Example:
// var m struct { Name string `json:"name"` }
// j := Dict{
// "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)
}
// Merge provides util to merge map or struct to another map or struct
// Required: Destine need to be a pointer
// Example:
// var m := struct { Name string `json:"name"` } {
// Name: "Joao",
// }
// j := Dict{
// "idade": "Joao",
// "name": nil,
// }
// f := Sex.Merge(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) (interface{}, error) {
final := Dict{}
if override != nil && override[0] {
if err := Copy(destine, &final); err != nil {
return nil, err
}
if err := Copy(source, &final); err != nil {
return nil, err
}
} else {
if err := Copy(source, &final); err != nil {
return nil, err
}
}
if err := Copy(final, destine); err != nil {
return nil, err
}
return destine, nil
}
// Jsonify decode JSON encoded byte array into variable
// Required: v needs to be a pointer
func Jsonify(encoded []byte, v interface{}) error {
return json.Unmarshal(encoded, v)
}
// Byteify encode variable into JSON encoded byte array
func Byteify(v interface{}) []byte {
res, _ := json.Marshal(v)
return res
}
// Stringify encode variable into JSON encoded string
func Stringify(v interface{}) string {
return string(Byteify(v))
}
// GetEnv 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
}
// IndexOf function to index first ocurrence of thing in thing slice
func IndexOf(i interface{}, l interface{}) int {
if typ := reflect.TypeOf(l).Kind(); typ == reflect.Slice || typ == reflect.Array || typ == reflect.String {
list := reflect.ValueOf(l)
item := reflect.ValueOf(i)
for i := 0; i < list.Len(); i++ {
value := list.Index(i)
if reflect.DeepEqual(value.Interface(), item.Interface()) {
return i
}
}
}
return -1
}