Repository navigation
Expand file tree
/
Copy pathutilities.go
More file actions
137 lines (115 loc) · 3.13 KB
/
Copy pathutilities.go
File metadata and controls
137 lines (115 loc) · 3.13 KB
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
package uniform
import (
"crypto/sha512"
"encoding/base64"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"reflect"
"strings"
)
// ParseRequest Gets the index of a string in an array of strings
var ParseRequest = func(data []byte) (Request, error) {
var request Request
if err := bson.Unmarshal(data, &request); err != nil {
return request, err
}
switch value := request.Model.(type) {
case primitive.Binary:
request.Model = value.Data
break
}
return request, nil
}
// IndexOf Gets the index of a string in an array of strings
var IndexOf = func(haystack []string, needle string, caseSensitive bool) int {
if caseSensitive {
for i, item := range haystack {
if item == needle {
return i
}
}
} else {
lowNeedle := strings.ToLower(needle)
for i, item := range haystack {
if strings.ToLower(item) == lowNeedle {
return i
}
}
}
return -1
}
// Contains checks if a string array contains a given string
var Contains = func(haystack []string, needle string, caseSensitive bool) bool {
return IndexOf(haystack, needle, caseSensitive) != -1
}
// Filter trims the filterItems from the items array
var Filter = func(items, filters []string, caseSensitive bool) []string {
if filters == nil || len(filters) <= 0 {
return items
}
filteredItems := make([]string, 0)
for _, item := range items {
if Contains(filters, item, caseSensitive) {
continue
}
filteredItems = append(filteredItems, item)
}
return filteredItems
}
// private function required for circular reference
func isEmpty(object interface{}) bool {
objValue := reflect.ValueOf(object)
switch objValue.Kind() {
// collection types are empty when they have no element
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
return objValue.Len() == 0
// pointers are empty if nil or if the value they point to is empty
case reflect.Ptr:
if objValue.IsNil() {
return true
}
deref := objValue.Elem().Interface()
return isEmpty(deref)
// for all other types, compare against the zero value
default:
zero := reflect.Zero(objValue.Type())
return reflect.DeepEqual(object, zero.Interface())
}
}
// IsEmpty determines if a value is empty or not
var IsEmpty = func(value interface{}) bool {
if value == nil || value == "" {
return true
}
stringValue := strings.TrimSpace(fmt.Sprint(value))
if stringValue == "" || stringValue == "nil" || stringValue == "<nil>" {
return true
}
if strings.HasPrefix(stringValue, "0001-01-01") {
return true
}
return isEmpty(value)
}
// Hash generates a sha512 hash for the given value/salt combo
var Hash = func(value interface{}, salt string) string {
concatenatedData := []byte(fmt.Sprintf(`%s%v`, salt, value))
encoder := sha512.New()
_, err := encoder.Write(concatenatedData)
if err != nil {
panic(err)
}
hashedData := encoder.Sum(nil)
base64EncodedString := base64.StdEncoding.EncodeToString(hashedData)
return base64EncodedString
}
// MapKeysToSlice extract the keys of map as an array
func MapKeysToSlice[K comparable, V any](m map[K]V) []K {
keys := make([]K, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return keys
}